Python openCV 3中contourArea的兼容性问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39475125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
compatibility issue with contourArea in openCV 3
提问by YNWA
I am trying to do a simple area calculation of contours I get from findContours. My openCv version is 3.1.0
我正在尝试对从 findContours 获得的轮廓进行简单的面积计算。我的 openCv 版本是 3.1.0
My code is:
我的代码是:
cc = cv2.findContours(im_bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.contourArea(cc[0])
error: 'C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\imgproc\src\shapedescr.cp...: error: (-215) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function cv::contourArea\n'
Cant seem to solve it, I have a feeling its just typecasting altough I expect the findContours result to match the type of contourArea
似乎无法解决它,我有一种感觉它只是类型转换,尽管我希望 findContours 结果与 contourArea 的类型相匹配
Thanks :)
谢谢 :)
EDIT: turns out I need to take the 2nd argument of findContours
编辑:原来我需要采用 findContours 的第二个参数
im2, cc, hierarchy = cv2.findContours(im_bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
回答by ZdaR
回答by Qin Heyang
This problem is caused by the different return value of cv2.findContours in different OpenCV versions.
这个问题是由于不同OpenCV版本中cv2.findContours的返回值不同造成的。
In OpenCV 4.0.0, this error may looks like cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\convhull.cpp:137: error: (-215:Assertion failed) total >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::convexHull'
在 OpenCV 4.0.0 中,这个错误可能看起来像 cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\convhull.cpp:137: error: (-215:Assertion failed) total >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::convexHull'
You can find a detailed explanation and solutions here: How to use `cv2.findContours` in different OpenCV versions?
你可以在这里找到详细的解释和解决方案:如何在不同的 OpenCV 版本中使用 `cv2.findContours`?
回答by nathancy
Depending on the OpenCV version, cv2.findContours()
has varying return signatures.
根据 OpenCV 版本,cv2.findContours()
具有不同的返回签名。
In OpenCV 3.4.X, cv2.findContours()
returns 3 items
在 OpenCV 3.4.X 中,cv2.findContours()
返回 3 个项目
image, contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
In OpenCV 2.X and 4.1.X, cv2.findContours()
returns 2 items
在 OpenCV 2.X 和 4.1.X 中,cv2.findContours()
返回 2 个项目
contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
You can easily obtain the contours regardless of the version like this:
无论版本如何,您都可以轻松获得轮廓:
cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
回答by Hosein Basafa
Thanks for @ZdaR; By the way, you can do the following in OpenCV 4.1:
感谢@ZdaR;顺便说一句,您可以在 OpenCV 4.1 中执行以下操作:
contours, hierarchy = cv2.findContours(im_bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)