<p>对于那些想要OpenCV解决方案的人,这里是:</p>
<pre><code>ret,thresh = cv2.threshold(image,245,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
tam = 0
for contorno in contours:
if len(contorno) > tam:
contornoGrande = contorno
tam = len(contorno)
cv2.drawContours(image,contornoGrande.astype('int'),-1,(0,255,0),2)
cv2.imshow('My image',image)
cv2.waitKey()
cv2.destroyAllWindows()
</code></pre>
<p>在这个例子中,我只画了最大的轮廓。请记住,“image”必须是单通道数组。</p>
<p>您应该更改threshold函数、findContours函数和drawcours函数的参数以获得所需的结果。</p>
<ul>
<li><a href="http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=threshold#cv2.threshold" rel="noreferrer">threshold Documentation</a></li>
<li><a href="http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#cv2.findContours" rel="noreferrer">findContours Documentation</a></li>
<li><a href="http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=drawcontours#cv2.drawContours" rel="noreferrer">drawContours Documentation</a></li>
</ul>
<p>我在drawContours函数中执行到“in t”的转换,因为Open CV 2.4.3版本中有一个bug,如果不执行此转换,程序将中断。
<a href="http://code.opencv.org/issues/2505" rel="noreferrer">This is the bug</a>。</p>