0

I am currently using FindContours and DrawContours function in order to segment an image.

I only extract external contours, and want to save only the contour which contains a given point. I use h_next to move through the cv_seq structure and test if the point is contained using PointPolygonTest

I actually can find the contour that interests me, but my problem is to extract it.

Here is the python code :

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

At the end, I got contour. But this structure still contains all the contours that have not been tested yet. How can I do to keep only the first contour of my output sequence?

Thanks by advance !

4

1 回答 1

0

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

于 2011-11-24T12:01:03.087 回答