Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python openCV 3中与contourArea的兼容性问题_Python_Opencv_Opencv3.0 - Fatal编程技术网

Python openCV 3中与contourArea的兼容性问题

Python openCV 3中与contourArea的兼容性问题,python,opencv,opencv3.0,Python,Opencv,Opencv3.0,我试图对从findContours得到的轮廓进行简单的面积计算。 我的openCv版本是3.1.0 我的代码是: 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\\s

我试图对从findContours得到的轮廓进行简单的面积计算。 我的openCv版本是3.1.0

我的代码是:

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'
似乎无法解决这个问题,我有一种感觉,这只是类型转换,尽管我希望找到的轮廓线结果与轮廓线区域的类型匹配

谢谢:)

编辑:结果我需要接受findContours的第二个参数

 im2, cc, hierarchy = cv2.findContours(im_bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

在Opencv 3 API版本中,
cv2.findContours()
返回3

  • 形象
  • 轮廓
  • 等级制度
因此,您需要将您的陈述改写为:

image, contours, hierarchy = cv2.findContours(im_bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

此问题是由不同OpenCV版本中cv2.findContours的不同返回值引起的

在OpenCV 4.0.0中,此错误可能类似于
cv2。错误:函数“CV::convxhull”中的OpenCV(4.0.0)C:\projects\OpenCV python\OpenCV\modules\imgproc\src\convhull.cpp:137:error:(-215:Assertion failed)total>=0&(depth==cv32f | depth==cv32s)


您可以在这里找到详细的解释和解决方案:

根据OpenCV版本,
cv2.findContours()
具有不同的返回签名

在OpenCV 3.4.X中,返回3项

image, contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
在OpenCV 2.X和4.1.X中,返回2项

contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
无论版本如何,都可以轻松获得轮廓,如下所示:

cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
感谢@ZdaR; 顺便说一下,您可以在OpenCV 4.1中执行以下操作:

contours, hierarchy = cv2.findContours(im_bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

你检查过cc[0]是否为空了吗?在进一步挖掘后找到了它,结果我需要得到findcontours的第二个参数,tks!