2017-04-16 114 views
2

我想从Kaggle运行一些代码,但我无法。cv2.findContours问题,opencv

该代码是here

的错误信息是:

--------------------------------------------------------------------------- 
IndexError        Traceback (most recent call last) 
<ipython-input-6-2a3e36c2605f> in <module>() 
    59   _, contours_mask, _ = cv2.findContours(thresh_mask.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) 
    60 
---> 61   main_contour = sorted(contours_mask, key = cv2.contourArea, reverse = True)[0] 
    62 
    63   x,y,w,h = cv2.boundingRect(main_contour) 

IndexError: list index out of range 

这可能是Python或相关的软件包的版本,因为其他人都没有经历过的代码。

我试过打开一些输出,但我是新的cv2。

我的理解是:

cv2.findContours() -> image, contours, hierarchy 

我能得到什么:

cv2.findContours(thresh_mask.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) -> (array([[0, 0, 0, ..., 0, 0, 0], 
    [0, 0, 0, ..., 0, 0, 0], 
    [0, 0, 0, ..., 0, 0, 0], 
    ..., 
    [0, 0, 0, ..., 0, 0, 0], 
    [0, 0, 0, ..., 0, 0, 0], 
    [0, 0, 0, ..., 0, 0, 0]], dtype=uint8), [], None) 

因此,countours是空的,这可能是问题。 thresh_mask对于这种情况是一个全零矩阵,这可能是原因。虽然不确定。

任何提示/建议?

+0

是的,你是正确的'计数器是空的,这可能是问题',你可能需要调整生成二进制图像的方法的参数 – ZdaR

+0

文章中的评论读取:*发布在Kernels部分的代码可以帮助你开始你的项目,你可以把它们作为一个起点,但它们不是最终的解决方案。祝你好运!* – ZdaR

+0

我可以看到'len(sorted(contours_mask,key = cv2.contourArea,reverse = True))' – stovfl

回答

2

这是因为你的输入图像(thresh_mask)是空的,即全黑。没有检测到轮廓。 请检查thresh_mask的内容或使用cv2.imshow显示它。

+0

如果'thresh_mask'完全是白色的,我认为'cv2.findContours()'会至少找到** 1轮廓**。 –

+1

感谢卢克,你是对的。我编辑了我的答案。 –