Python FindContours操作后仅提取一个轮廓

Python FindContours操作后仅提取一个轮廓,python,opencv,image-segmentation,Python,Opencv,Image Segmentation,我目前正在使用FindContours和DrawContours函数来分割图像 我只提取外部轮廓,只想保存包含给定点的轮廓。 我使用h_next遍历cv_seq结构,并使用PointPolyContest测试该点是否包含 实际上我可以找到我感兴趣的轮廓,但我的问题是提取它 以下是python代码: def contour_from_point(contours, point, in_img): """ Extract the contour from a sequence of contours

我目前正在使用FindContours和DrawContours函数来分割图像

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

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

以下是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
def contour_from_point(轮廓、点、in_img):
"""
从包含该点的轮廓序列中提取轮廓。
为了在eagle road的情况下工作,必须使用
具有CV_RETR_外部的FindContours函数
"""
如果轮廓:
#我们至少得到了一个轮廓。搜索包含点的点
轮廓=轮廓#列表的第一个轮廓
距离=等高线点多边形测试(等高线,点,0)
当距离<0时:#0表示在轮廓的顶部
轮廓=轮廓。h_next()
如果轮廓:#避免轮廓结束
距离=等高线点多边形测试(等高线,点,0)
其他:
轮廓=无
其他:#
轮廓=无
回程曲线
最后,我得到了轮廓。但这个结构仍然包含所有尚未测试的轮廓。 如何仅保留输出序列的第一个轮廓


提前谢谢

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

我不熟悉python语法,但为什么不访问轮廓结构的第一个元素并将其设置为单独的数组?您好。谢谢你的回答。找到的唯一方法是使用var=contour[:]检索等高线的所有点。但该输出不能由内部Opencv Draw函数使用。事实上,我想要的是一个只包含我想要的轮廓的CvSeq:s、 最后有一种方法可以只获得一个轮廓。只需使用另一个需要输入cvseq的函数,例如ConvexHull。输出将仅为序列的第一个轮廓。