如何在 opencv 4.4.0 版上正确使用 cv2.findContours()? [英] How to properly use cv2.findContours() on opencv version 4.4.0.?
问题描述
我正在尝试在 opencv 4.4.0 版上使用 cv2.findContours().(我使用的是 Python 3.8.5 版)但它抛出了一个错误,我无法弄清楚.我不确定代码有什么问题.这是一些背景:
Im trying to use cv2.findContours() on opencv version 4.4.0. (Im using Python version 3.8.5) but it throws an error, I cant figure out. Im not sure whats wrong with the code. Here's some background:
根据 OpenCV,cv2.findContours() 的语法如下:Python:轮廓,层次= cv.findContours(图像,模式,方法[,轮廓[,层次[,偏移]]])
According to OpenCV the syntax for cv2.findContours() is as follows: Python: contours, hierarchy = cv.findContours( image, mode, method[, contours[, hierarchy[, offset]]] )
我找了一些例子来确保如何正确实现它,这是我发现的:示例 1_, 轮廓, _ = cv2.findContours(binary_image,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
I looked for some examples to make sure how to properly implement it, heres what I found: example 1 _, contours, _ = cv2.findContours(binary_image,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
示例 2(_, cnts, _) = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
example 2 (_, cnts, _) = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
那些是我在网上找到的工作项目,有很多这样的例子.所以,我试图实现我从视频中获得的一些代码以对该主题有一些了解,但它似乎对我不起作用,我找不到原因.代码如下:
Those are from working projects I found online, there are plenty examples like those. So, Im trying to implement some code I got from a video to gain some understanding on the topic but it does not seem to work for me and I cant find why. Heres the code:
import cv2
import numpy as np
imagen =cv2.imread('lettuce.jpg')
gray = cv2.cvtColor(imagen,cv2.COLOR_BGR2GRAY)
_,binary = cv2.threshold(gray,100,255,cv2.THRESH_BINARY)
image,contours,hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image,contours, -1, (0,255,0),3)
错误:回溯(最近一次调用最后一次):第 8 行,在图像,轮廓,层次结构 = cv2.findContours(二进制,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)值错误:没有足够的值来解包(预期为 3,得到 2)
Error: Traceback (most recent call last): line 8, in image,contours,hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) ValueError: not enough values to unpack (expected 3, got 2)
推荐答案
在 Python/OpenCV 4.4.0 中,findContours 仅返回 2 个值,您列出了 3 个.
In Python/OpenCV 4.4.0, findContours returns only 2 values, you list 3.
你展示:
image,contours,hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
OpenCV 4.4.0,列表:
OpenCV 4.4.0, lists:
contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
请务必查看文档.见
https://docs.opencv.org/4.4.0/d3/dc0/group__imgproc__shape.html#gadf1ad6a0b82947fa1fe3c3d497f260e0
一种以独立于版本的方式处理这个问题的方法,如果你想要的只是轮廓,是(归功于@nathancy):
One way to handle this in a version independent way, if all you want are the contours, is (credit to @nathancy):
contours = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
如果您不想要所有嵌套轮廓,则使用 RETR_EXTERNAL 而不是 RETR_LIST.
If you do not want all nested contours, then use RETR_EXTERNAL and not RETR_LIST.
这篇关于如何在 opencv 4.4.0 版上正确使用 cv2.findContours()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!