13

I'm using OpenCV (Canny + findCountours) to find external contours of objects. The curve drawn is typically almost, but not entirely, closed. I'd like to close it - to find the region it bounds.

How do I do this?

Things considered:

  • Dilation - the examples I've seen show this after Canny, although it would seem to me it makes more sense to do this after findContours
  • Convex hull - might work, though I'm really trying to complete a curve
  • Shape simplification - related, but not exactly what I want
4
  • 5
    approxPoly offers this feature on top of findContours() Commented Jan 30, 2014 at 23:25
  • 7
    Just duplicate the first element of the contour list at the end? Commented Jan 31, 2014 at 0:08
  • MartinBeckett, berak, I think both comments should have been answers, +1 for both Commented Jan 31, 2014 at 8:20
  • 1
    SRobertJames Can you put input and desired output images ? Commented Jan 20, 2017 at 15:21

2 Answers 2

1

Using PolyLine method to draw contours

cv2.PolyLine(img, points, is_closed=True, 255, thickness=1, lineType=8, shift=0)

Read the docs for further details: http://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html

Mark answered if it resolved your problem. If not then let me know.

Sign up to request clarification or add additional context in comments.

Comments

0

A little late, but I just had to deal with this myself. If you perform an opening operation (erosion followed by dilation) prior to the Canny edge detection, the edges you detect should be more continuous, resulting in better contours. This method appears to be fairly robust. I mentioned this in another post and posted a code sample as part of a recent question.

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.