2011-11-18 26 views
0

我目前使用FindContours和DrawContours函數來分割圖像。在FindContours操作後僅提取一個輪廓

我只提取外部輪廓,並且只想保存包含給定點的輪廓。 我使用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 

最後,我得到了輪廓。但是這個結構仍然包含了所有尚未測試的輪廓。 如何才能只保留輸出序列的第一個輪廓?

感謝提前!

+0

我不熟悉python語法,但爲什麼不只是做一些像訪問輪廓結構的第一個元素並將其設置爲等於單獨的數組? – scord

+0

嗨。感謝你的回答。唯一發現的是用var = contour [:]檢索輪廓的所有點。但之後內部Opencv Draw函數不能使用此輸出。我想實際上是一個只包含我想要的輪廓的CvSeq。 :秒。 – jlengrand

+0

終於有辦法只得到一個輪廓。 Juste使用另一個需要輸入cvseq的函數,例如ConvexHull。輸出將只是序列的第一個輪廓。 – jlengrand

回答

0

終於有辦法得到只有一個輪廓。 Juste使用另一個需要輸入cvseq的函數,例如ConvexHull。輸出將只是序列的第一個輪廓。

+0

乾草可以提供代碼示例來解決該問題嗎? –