How to remove a contour inside contour in Python OpenCV?

后端 未结 2 902
长发绾君心
长发绾君心 2020-12-28 19:55

OpenCV in Python provides the following code:

regions, hierarchy = cv2.findContours(binary_image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)


for region in reg         


        
2条回答
  •  心在旅途
    2020-12-28 20:44

    For that, you should take a look at this tutorial on how to use the hierarchy object returned by the method findContours .

    The main point is that you should use cv2.RETR_TREE instead of cv2.RETR_LIST to get parent/child relationships between your clusters:

    regions, hierarchy = cv2.findContours(binary_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    

    Then you can check whether a contour with index i is inside another by checking if hierarchy[0,i,3] equals -1 or not. If it is different from -1, then your contour is inside another.

提交回复
热议问题