在FindContours操作后提取单个轮廓
我现在正在使用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
现在终于有办法只获取一个轮廓了。只需要使用另一个需要输入cvseq的函数,比如ConvexHull。这样输出的结果就只会是序列中的第一个轮廓。