首页 文章

openCV 3中与contourArea的兼容性问题

提问于
浏览
14

我试图对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结果与contourArea的类型相匹配

谢谢 :)

编辑:结果我需要采取findContours的第二个参数

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

1 回答

  • 26

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

    • 图片

    • 轮廓

    • 层次结构

    所以你需要重写你的陈述:

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

相关问题