epattaro
epattaro

Reputation: 2428

cv2.findContours issues, opencv

I am trying to run some code from Kaggle, but I am unable to.

The code is here.

The error message is:

---------------------------------------------------------------------------
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

It might be Python or package version related, as other people haven't experienced the code.

I tried opening some outputs but I am new to cv2.

To my understanding:

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

What I do get:

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)

Hence, countours is empty, which might be the problem. thresh_mask is an all zero matrix for that case, which might be the cause. Unsure of it though.

Any hints/advice?

Upvotes: 2

Views: 1021

Answers (1)

Shahrukh khan
Shahrukh khan

Reputation: 382

It happens because your input image (thresh_mask) is empty i.e complete black. No contours are detected. Please check contents of thresh_mask or display it using cv2.imshow.

Upvotes: 2

Related Questions