擅长:python、mysql、java
<p>根据OpenCV版本的不同,<code>cv2.findContours()</code>具有不同的返回签名。</p>
<p>在OpenCV 3.4.X中,<a href="https://docs.opencv.org/3.4/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a" rel="nofollow noreferrer">^{<cd1>}</a>返回3个项</p>
<pre><code>image, contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
</code></pre>
<p>在OpenCV 2.X和4.1.X中,<a href="https://docs.opencv.org/4.1.0/d3/dc0/group__imgproc__shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0" rel="nofollow noreferrer">^{<cd1>}</a>返回2个项</p>
<pre><code>contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
</code></pre>
<p>无论是哪种版本,您都可以轻松获得轮廓:</p>
<pre><code>cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
</code></pre>