FindContours操作后仅提取一个轮廓

时间:2011-11-18 16:10:48

标签: python opencv image-segmentation

我目前正在使用FindContours和DrawContours功能来分割图像。

我只提取外部轮廓,并且只想保存包含给定点的轮廓。 我使用h_next移动cv_seq结构并测试是否使用PointPolygonTest包含该点

我实际上可以找到感兴趣的轮廓,但我的问题是提取它。

这是python代码:

def contour_from_point(contours, point, in_img):
"""
Extract the contour from a sequence of contours which contains the point.
For this to work in the eagle road case, the sequence has to be performed using the 
FindContours function with CV_RETR_EXTERNAL
"""
if contours:
    # We got at least one contour. Search for the one which contains point
    contour = contours # first contour of the list
    distance = cv.PointPolygonTest(contour, point, 0)
    while distance < 0: # 0 means on eadge of contour
        contour = contour.h_next()       
        if contour: # avoid end of contours
            distance = cv.PointPolygonTest(contour, point, 0)
        else : 
            contour = None

else:# 
    contour = None  

return contour

最后,我得到轮廓。但是这个结构仍然包含尚未测试的所有轮廓。 如何仅保留输出序列的第一个轮廓?

先谢谢!

1 个答案:

答案 0 :(得分:0)

终于有办法只获得一个轮廓。 Juste使用另一个需要输入cvseq的函数,例如ConvexHull。输出将只是序列的第一个轮廓。