Ask Your Question
0

Can't get contours index array

asked 2018-04-02 19:06:34 -0600

mmt gravatar image

Hello,

I am trying to find the 2 biggest contours, but I am facing some problems.

im2, contours, hierarchy = cv.findContours(roi, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
largest_area = 0
second_area = 0
l_index = 0
s_index = 0
for i, c in enumerate(contours):
    area = cv.contourArea(c)
    if (area > largest_area):
        largest_area = area
        l_index = i
    elif (area > second_area):
        second_area = area
        s_index = i

cv.drawContours(frame, contours[l_index], -1, (0, 255, 0), 2)
cv.imshow('frame',frame)

I have tried to get the index of the 2 biggest contours' array, but it says:

cv.drawContours(frame, contours[l_index], -1, (0, 255, 0), 2)
IndexError: list index out of range

And, I would like to know, how could I draw (drawContours) both after get them? .

Thanks!

edit retag flag offensive close merge delete

Comments

even 0 as an index is only valid, IF it found contours AT ALL ! (you didn't check)

berak gravatar imageberak ( 2018-10-19 07:38:17 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-10-19 07:10:08 -0600

supra56 gravatar image

Try this:

cv.drawContours(frame, [contours[l_index]], -1, (0, 255, 0), 2)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-04-02 19:06:34 -0600

Seen: 737 times

Last updated: Oct 19 '18