2

I have written this code to draw rectangular boxes around contours drawn in an image, however on running, I get everything right except the boxes, which I don't see at all. What's the error?

for cnt,heir in zip(contours, hierarchy):
    (x,y,w,h) = cv2.boundingRect(cnt);
    cv2.rectangle(im2,(x,y),(x+w,y+h),(0,255,0),2)

cv2.drawContours(im2, contours, -1, (255,255,255), 2);   
cv2.imshow("Contours",im2);

PS. I use OpenCV 3.1.0 and Python 2.7

EDIT: I tried iterating through each contour and in order to check it I modified the code as follows:

for cnt,heir in zip(contours, hierarchy):
    print ('Contour Area:',cv2.contourArea(cnt));
    (x,y,w,h) = cv2.boundingRect(cnt);
    print (x,y,h,w)
    cv2.putText(im2,'worm',(x+w,y+h), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2, cv2.LINE_AA);
    cv2.rectangle(im2,(x,y),(x+w,y+h),(255,0,0),2);

I printed the contour area for each, the values of (x,y,w,h) for each and putting text "worm" for each contour and drawing the rectangular boxes around each contour. However I get just 1 output:

Output for edited code

for an image like:

Source Image

I need to display the text "worm" at each of the worm-like creature. However I just get it once. What are the issues?

10
  • 1
    use print() to see what you have in variables (ie. print(contours)) and which part of code is executed (ie. print("I'm inside for-loop")). Maybe OpenCV returns empty list with contours BTW: OpenCV if can't do something doesn't raise error but returns None and you don't know that there is a problem. Commented Jan 9, 2017 at 7:26
  • stackoverflow.com/questions/22137511/… - they use different color because (0,255,0) is invisible on their image. Commented Jan 9, 2017 at 7:30
  • @furas This is what I get : i.imgur.com/dSS3YRy.png - A very large list of nested arrays with some numbers. Also, I changed the colour, doesn't work ! Commented Jan 9, 2017 at 10:15
  • did you check what you have in (x,y,w,h) ? maybe you have w=0, h=0 Commented Jan 9, 2017 at 10:52
  • can you upload the image you are working with? and what are you trying to detect? Commented Jan 9, 2017 at 11:09

2 Answers 2

3

I used to use below code to draw rectangles on the contours detected. Hope it helps.

for contour in contours:
    # get rectangle bounding contour
    [x,y,w,h] = cv2.boundingRect(contour)

    # draw rectangle around contour on original image
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,255),2)
Sign up to request clarification or add additional context in comments.

1 Comment

coincidentally I just tried by removing the hierarchy part - an it does work! Thank you for your help :)
0

Simply write:

for c in contours:
    (x,y,w,h) = cv2.boundingRect(c);
    cv2.putText(im2,'worm',(x+w,y+h), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2, cv2.LINE_AA);
    cv2.rectangle(im2,(x,y),(x+w,y+h),(255,0,0),2);

cv2.drawContours(im2, contours, -1, (255,255,255), 2);   
cv2.imshow("Contours",im2);

As suggested by furas, zip(contours,hierarchy) will return only one pair if hierarchy has only one. In this case a simple loop over contours list works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.