Closing a contour curve in OpenCV

余生长醉 提交于 2019-11-29 13:30:00

问题


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

回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/21469409/closing-a-contour-curve-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!