您的位置:首页 > 编程语言 > Python开发

OpenCV-Python教程(6)(7)(8): Sobel算子 Laplacian算子 Canny边缘检测

2018-01-05 20:16 609 查看
<div class="article_content tracking-ad" id="article_content" data-mod="popu_307" data-dsm="post">
<h1><a name="t0"></a><span style="font-size:32px">OpenCV-Python教程(6、Sobel算子)</span></h1>

<p></p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

本篇文章介绍如何用OpenCV-<a title="Python知识库" class="replace_word" style="color:rgb(223,52,52); text-decoration:none; font-weight:bold" href="http://lib.csdn.net/base/11" target="_blank">Python</a>来使用Sobel算子。<strong><br>

</strong></p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<strong>提示:</strong><br>

</p>

<ul style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<li>转载请详细注明原作者及出处,谢谢!<br>

</li><li>本文介绍使用OpenCV-Python实现基本的滤波处理</li><li>本文不介详细的理论知识,读者可从其他资料中获取相应的背景知识。笔者推荐清华大学出版社的《<a style="color:rgb(51,102,153); text-decoration:none" href="http://www.amazon.cn/%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86%E4%B8%8E%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89%E7%AE%97%E6%B3%95%E5%8F%8A%E5%BA%94%E7%94%A8-%E5%B8%95%E7%A7%91%E5%B0%94/dp/B0083PY050/ref=sr_1_1?ie=UTF8&qid=1372255286&sr=8-1&keywords=%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86%E4%B8%8E%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89%E7%AE%97%E6%B3%95%E5%8F%8A%E5%BA%94%E7%94%A8"
target="_blank"><span class="lrg"></span></a><span class="med reg"></span>图像处理与计算机视觉算法及应用(第2版)

 》。</li></ul>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t1"></a>

<a name="t0" style="color:rgb(51,102,153)" target="_blank"></a>Sobel算子</h2>

<h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t2"></a>

<a name="t1" style="color:rgb(51,102,153)" target="_blank"></a>原型<br>

</h3>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

Sobel算子依然是一种过滤器,只是其是带有方向的。在OpenCV-Python中,使用Sobel的算子的函数原型如下:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9170013#" target="_blank">view

 plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9170013#" target="_blank">copy</a><div style="left: 487px; top: 808px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_1" width="18" height="18" align="middle"
id="ZeroClipboardMovie_1" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=1&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:861px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">dst = cv2.Sobel(src, ddepth, dx, dy[, dst[, ksize[, scale[, delta[, borderType]]]]])  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">函数返回其处理结果。</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

前四个是必须的参数:<br>

</p>

<ul style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<li>第一个参数是需要处理的图像;</li><li>第二个参数是图像的深度,-1表示采用的是与原图像相同的深度。目标图像的深度必须大于等于原图像的深度;</li><li>dx和dy表示的是求导的阶数,0表示这个方向上没有求导,一般为0、1、2。</li></ul>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

其后是可选的参数:</p>

<ul style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<li>dst不用解释了;</li><li>ksize是Sobel算子的大小,必须为1、3、5、7。</li><li>scale是缩放导数的比例常数,默认情况下没有伸缩系数;</li><li>delta是一个可选的增量,将会加到最终的dst中,同样,默认情况下没有额外的值加到dst中;</li><li>borderType是判断图像边界的模式。这个参数默认值为<span class="n">cv2</span><span class="o">.</span><span class="n">BORDER_DEFAULT</span>。</li></ul>

<h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t3"></a>

<a name="t2" style="color:rgb(51,102,153)" target="_blank"></a>使用</h3>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

在OpenCV-Python中,Sobel函数的使用如下:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9170013#" target="_blank">view

 plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9170013#" target="_blank">copy</a><div style="left: 487px; top: 1289px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_2" width="18" height="18" align="middle"
id="ZeroClipboardMovie_2" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars=
1ebcd
"id=2&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:1396px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(0,130,0); background-color:inherit">#coding=utf-8</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> cv2  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> numpy as np    </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">img = cv2.imread(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"D:/lion.jpg"</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">x = cv2.Sobel(img,cv2.CV_16S,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">,</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">y = cv2.Sobel(img,cv2.CV_16S,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">,</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li
class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">absX = cv2.convertScaleAbs(x)   <span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(0,130,0); background-color:inherit"># 转回uint8</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">absY = cv2.convertScaleAbs(y)  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">dst = cv2.addWeighted(absX,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0.5</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">,absY,</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0.5</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">,</span><span class="number"
style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid;
border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"absX"</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">, absX)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px
3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"absY"</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">, absY)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Result"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, dst)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.waitKey(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li
class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.destroyAllWindows()   </span></li></ol>

</div>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t4"></a>

<a name="t3" style="color:rgb(51,102,153)" target="_blank"></a>解释</h3>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

在Sobel函数的第二个参数这里使用了cv2.CV_16S。因为OpenCV文档中对Sobel算子的介绍中有这么一句:“in the case of 8-bit input images it will result in truncated derivatives”。即Sobel函数求完导数后会有负值,还有会大于255的值。而原图像是uint8,即8位无符号数,所以Sobel建立的图像位数不够,会有截断。因此要使用16位有符号的数据类型,即cv2.CV_16S。</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

在经过处理后,别忘了用convertScaleAbs()函数将其转回原来的uint8形式。否则将无法显示图像,而只是一副灰色的窗口。convertScaleAbs()的原型为:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9170013#" target="_blank">view

 plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9170013#" target="_blank">copy</a><div style="left: 487px; top: 1892px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_3" width="18" height="18" align="middle"
id="ZeroClipboardMovie_3" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=3&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:2053px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">dst = cv2.convertScaleAbs(src[, dst[, alpha[, beta]]])  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">其中可选参数alpha是伸缩系数,beta是加到结果上的一个值。结果返回uint8类型的图片。</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

由于Sobel算子是在两个方向计算的,最后还需要用cv2.addWeighted(...)函数将其组合起来。其函数原型为:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9170013#" target="_blank">view

 plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9170013#" target="_blank">copy</a><div style="left: 487px; top: 2031px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_4" width="18" height="18" align="middle"
id="ZeroClipboardMovie_4" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=4&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:2206px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">dst = cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]])  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">其中alpha是第一幅图片中元素的权重,beta是第二个的权重,gamma是加到最后结果上的一个值。</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<h3 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t5"></a>

<a name="t4" style="color:rgb(51,102,153)" target="_blank"></a>结果</h3>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

原图像为:</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<img style="border:none; max-width:100%" alt="" src="https://img-blog.csdn.net/20130627154241703?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VubnkyMDM4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"></p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

结果为:</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<img style="border:none; max-width:100%" alt="" src="https://img-blog.csdn.net/20130627154403390?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VubnkyMDM4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"><br>

</p>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t6"></a>

<a name="t5" style="color:rgb(51,102,153)" target="_blank"></a>参考资料:</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

1、《Opencv2 Computer Vision Application Programming Cookbook》</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

2、《OpenCV References Manule》</p>

<br>

<p><br>

</p>

<p><br>

</p>

<h1><a name="t7"></a><span style="font-size:32px">OpenCV-Python教程(7、Laplacian算子)</span></h1>

<p></p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

本篇文章介绍如何用OpenCV-<a title="Python知识库" class="replace_word" style="color:rgb(223,52,52); text-decoration:none; font-weight:bold" href="http://lib.csdn.net/base/11" target="_blank">Python</a>来使用Laplacian算子。<strong><br>

</strong></p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<strong>提示:</strong><br>

</p>

<ul style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<li>转载请详细注明原作者及出处,谢谢!<br>

</li><li>本文介绍使用在OpenCV-Python中使用Laplacian函数</li><li>本文不介详细的理论知识,读者可从其他资料中获取相应的背景知识。笔者推荐清华大学出版社的《<a style="color:rgb(51,102,153); text-decoration:none" href="http://www.amazon.cn/%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86%E4%B8%8E%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89%E7%AE%97%E6%B3%95%E5%8F%8A%E5%BA%94%E7%94%A8-%E5%B8%95%E7%A7%91%E5%B0%94/dp/B0083PY050/ref=sr_1_1?ie=UTF8&qid=1372255286&sr=8-1&keywords=%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86%E4%B8%8E%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89%E7%AE%97%E6%B3%95%E5%8F%8A%E5%BA%94%E7%94%A8"
target="_blank"><span class="lrg"></span></a><span class="med reg"></span>图像处理与计算机视觉算法及应用(第2版)

 》。</li></ul>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t8"></a>

<a name="t0" style="color:rgb(51,102,153)" target="_blank"></a>Laplacian算子</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

图像中的边缘区域,像素值会发生“跳跃”,对这些像素求导,在其一阶导数在边缘位置为极值,这就是Sobel算子使用的原理——极值处就是边缘。如下图(<strong>下图来自OpenCV官方文档</strong>):</p>

<p align="center" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<img style="border:none; max-width:100%" alt="" src="https://img-blog.csdn.net/20130628171706750?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VubnkyMDM4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"><br>

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

如果对像素值求二阶导数,会发现边缘处的导数值为0。如下(<strong>下图来自OpenCV官方文档</strong>):</p>

<p align="center" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<img style="border:none; max-width:100%" alt="" src="https://img-blog.csdn.net/20130628171743593?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VubnkyMDM4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"><br>

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

Laplace函数实现的方法是先用Sobel 算子计算二阶x和y导数,再求和:(CSDN,你打水印,让我的公式怎么办?)</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<img style="border:none; max-width:100%" alt="" src="https://img-blog.csdn.net/20130628165100984?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VubnkyMDM4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"></p>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t9"></a>

<a name="t1" style="color:rgb(51,102,153)" target="_blank"></a>函数原型</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

在OpenCV-Python中,Laplace算子的函数原型如下:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9188441#" target="_blank">view

 plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9188441#" target="_blank">copy</a><div style="left: 487px; top: 4499px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_5" width="18" height="18" align="middle"
id="ZeroClipboardMovie_5" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=5&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:1522px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">dst = cv2.Laplacian(src, ddepth[, dst[, ksize[, scale[, delta[, borderType]]]]])  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">如果看了上一篇Sobel算子的介绍,这里的参数应该不难理解。</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

前两个是必须的参数:<br>

</p>

<ul style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<li>第一个参数是需要处理的图像;</li><li>第二个参数是图像的深度,-1表示采用的是与原图像相同的深度。目标图像的深度必须大于等于原图像的深度;</li></ul>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

其后是可选的参数:</p>

<ul style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<li>dst不用解释了;</li><li>ksize是算子的大小,必须为1、3、5、7。默认为1。</li><li>scale是缩放导数的比例常数,默认情况下没有伸缩系数;</li><li>delta是一个可选的增量,将会加到最终的dst中,同样,默认情况下没有额外的值加到dst中;</li><li>borderType是判断图像边界的模式。这个参数默认值为<span class="n">cv2</span><span class="o">.</span><span class="n">BORDER_DEFAULT</span>。</li></ul>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t10"></a>

<a name="t2" style="color:rgb(51,102,153)" target="_blank"></a>使用</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

这里还是以Sobel一文中的石狮作为测试图像,下面是测试代码:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9188441#" target="_blank">view

 plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9188441#" target="_blank">copy</a><div style="left: 487px; top: 4954px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_6" width="18" height="18" align="middle"
id="ZeroClipboardMovie_6" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=6&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:2031px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(0,130,0); background-color:inherit">#coding=utf-8</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> cv2  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> numpy as np    </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">img = cv2.imread(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"D:/lion.jpg"</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">gray_lap = cv2.Laplacian(img,cv2.CV_16S,ksize = <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">dst = cv2.convertScaleAbs(gray_lap)  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">'laplacian'</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">,dst)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.waitKey(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.destroyAllWindows()  </span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">为了让结果更清晰,这里的ksize设为3,效果图如下:</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<img style="border:none; max-width:100%" alt="" src="https://img-blog.csdn.net/20130628170607187?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VubnkyMDM4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"></p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

有点像粉笔画,是吧。这是因为原图像未经过去噪就直接处理了。可以通过<a style="color:rgb(51,102,153); text-decoration:none" href="http://blog.csdn.net/sunny2038/article/details/9155893" target="_blank">滤波一文</a>中,使用低通滤波一节中高斯模糊来先处理一下再用拉普拉斯函数。<br>

</p>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t11"></a>

<a name="t3" style="color:rgb(51,102,153)" target="_blank"></a>参考资料:</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

1、《Opencv2 Computer Vision Application Programming Cookbook》</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

2、《OpenCV References Manule》</p>

<br>

<p><br>

</p>

<p><br>

</p>

<h1><a name="t12"></a><span style="font-size:32px">OpenCV-Python教程(8、Canny边缘检测Sobel算子)</span></h1>

<p></p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

本篇文章介绍如何用OpenCV-<a title="Python知识库" class="replace_word" style="color:rgb(223,52,52); text-decoration:none; font-weight:bold" href="http://lib.csdn.net/base/11" target="_blank">Python</a>来使用Canny算子。<strong><br>

</strong></p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<strong>提示:</strong><br>

</p>

<ul style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<li>转载请详细注明原作者及出处,谢谢!<br>

</li><li>本文介绍使用OpenCV-Python实现基本的滤波处理</li><li>本文不介详细的理论知识,读者可从其他资料中获取相应的背景知识。笔者推荐清华大学出版社的《<a style="color:rgb(51,102,153); text-decoration:none" href="http://www.amazon.cn/%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86%E4%B8%8E%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89%E7%AE%97%E6%B3%95%E5%8F%8A%E5%BA%94%E7%94%A8-%E5%B8%95%E7%A7%91%E5%B0%94/dp/B0083PY050/ref=sr_1_1?ie=UTF8&qid=1372255286&sr=8-1&keywords=%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86%E4%B8%8E%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89%E7%AE%97%E6%B3%95%E5%8F%8A%E5%BA%94%E7%94%A8"
target="_blank"><span class="lrg"></span></a><span class="med reg"></span>图像处理与计算机视觉算法及应用(第2版)

 》。</li></ul>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t13"></a>

<a name="t0" style="color:rgb(51,102,153)" target="_blank"></a>原型<br>

</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

OpenCV-Python中Canny函数的原型为:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9202641#" target="_blank">view

 plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9202641#" target="_blank">copy</a><div style="left: 487px; top: 6413px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_7" width="18" height="18" align="middle"
id="ZeroClipboardMovie_7" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=7&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:835px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">edge = cv2.Canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient ]]])   </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">必要参数:</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<ul style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<li>第一个参数是需要处理的原图像,该图像必须为单通道的灰度图;</li><li>第二个参数是阈值1;</li><li>第三个参数是阈值2。</li></ul>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

其中较大的阈值2用于检测图像中明显的边缘,但一般情况下检测的效果不会那么完美,边缘检测出来是断断续续的。所以这时候用较小的第一个阈值用于将这些间断的边缘连接起来。<br>

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

可选参数中apertureSize就是Sobel算子的大小。而L2gradient参数是一个布尔值,如果为真,则使用更精确的L2范数进行计算(即两个方向的倒数的平方和再开放),否则使用L1范数(直接将两个方向导数的绝对值相加)。</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

具体的算法可参见清华大学出版社的《<a style="color:rgb(51,102,153); text-decoration:none" href="http://www.amazon.cn/%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86%E4%B8%8E%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89%E7%AE%97%E6%B3%95%E5%8F%8A%E5%BA%94%E7%94%A8-%E5%B8%95%E7%A7%91%E5%B0%94/dp/B0083PY050/ref=sr_1_1?ie=UTF8&qid=1372255286&sr=8-1&keywords=%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86%E4%B8%8E%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89%E7%AE%97%E6%B3%95%E5%8F%8A%E5%BA%94%E7%94%A8"
target="_blank"><span class="lrg"></span></a><span class="med reg"></span>图像处理与计算机视觉算法及应用(第2版)

 》第二章,其中有Canny算法的详细描述及实现。</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

函数返回一副二值图,其中包含检测出的边缘。</p>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t14"></a>

<a name="t1" style="color:rgb(51,102,153)" target="_blank"></a>使用</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

Canny函数的使用很简单,只需指定最大和最小阈值即可。如下:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9202641#" target="_blank">view

 plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9202641#" target="_blank">copy</a><div style="left: 487px; top: 6840px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_8" width="18" height="18" align="middle"
id="ZeroClipboardMovie_8" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=8&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:1358px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(0,130,0); background-color:inherit">#coding=utf-8</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> cv2  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> numpy as np    </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">img = cv2.imread(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"D:/lion.jpg"</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">img = cv2.GaussianBlur(img,(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">,</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">),</span><span class="number" style="margin:0px;
padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">canny = cv2.Canny(img, <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">50</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">150</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li
class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">'Canny'</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, canny)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.waitKey(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.destroyAllWindows()  </span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">首先,由于Canny只能处理灰度图,所以将读取的图像转成灰度图。</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

用高斯平滑处理原图像降噪。</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

调用Canny函数,指定最大和最小阈值,其中apertureSize默认为3。</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

处理结果如下:</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<img style="border:none; max-width:100%" alt="" src="https://img-blog.csdn.net/20130630174008515?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3VubnkyMDM4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"></p>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t15"></a>

<a name="t2" style="color:rgb(51,102,153)" target="_blank"></a>更多</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

这个程序只是静态的,在github上有一个可以在运行时调整阈值大小的程序。其代码如下:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9202641#" target="_blank">view

 plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9202641#" target="_blank">copy</a><div style="left: 487px; top: 7923px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_9" width="18" height="18" align="middle"
id="ZeroClipboardMovie_9" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=9&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:2369px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> cv2  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> numpy as np  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">def</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> CannyThreshold(lowThreshold):  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    detected_edges = cv2.GaussianBlur(gray,(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">,</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">),</span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li style="border-left-width:3px; border-style:none none
none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    detected_edges = cv2.Canny(detected_edges,lowThreshold,lowThreshold*ratio,apertureSize = kernel_size)  </span></li><li
class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    dst = cv2.bitwise_and(img,img,mask = detected_edges)  <span class="comment" style="margin:0px; padding:0px; border:none;
color:rgb(0,130,0); background-color:inherit"># just add some colours to edges from original image.</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">'canny demo'</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">,dst)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">lowThreshold = <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">max_lowThreshold = <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">100</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">ratio = <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">kernel_size = <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px
0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">img = cv2.imread(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">'D:/lion.jpg'</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.namedWindow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">'canny demo'</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.createTrackbar(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">'Min threshold'</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">,</span><span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">'canny demo'</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">,lowThreshold, max_lowThreshold, CannyThreshold)  </span></span></li><li
class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">CannyThreshold(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span><span
class="comment" style="margin:0px; padding:0px; border:none; color:rgb(0,130,0); background-color:inherit"># initialization</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li class="alt"
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">if</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> cv2.waitKey(</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">) == </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">27</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">:  </span></span></li><li style="border-left-width:3px; border-style:none
none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    cv2.destroyAllWindows()  </span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">原地址</span><a style="color:rgb(51,102,153); text-decoration:none; font-family:Arial; font-size:14px; line-height:26px" href="https://github.com/abidrahmank/OpenCV2-Python/blob/master/Official_Tutorial_Python_Codes/3_imgproc/canny.py"
target="_blank">在此</a><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,其中还有其他的初级图像处理的代码,大伙可以去看看。后续文章将介绍更多的OpenCV的函数使用,以及视频的处理。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t16"></a>

<a name="t3" style="color:rgb(51,102,153)" target="_blank"></a>参考资料:</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

1、《Opencv2 Computer Vision Application Programming Cookbook》</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

2、《OpenCV References Manule》</p>

<br>

<p><br>

</p>

<p><br>

</p>

<p><br>

</p>

<p>from: http://blog.csdn.net/sunny2038/article/category/904451<br>
</p>

  

</div>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐