Check if two contours intersect?

前端 未结 2 1165
忘掉有多难
忘掉有多难 2020-12-21 03:40

I have 2 contours (cont1 and cont2) received from cv2.findContours(). How do I know if they intersect or not? I don\'t need coordinate

2条回答
  •  臣服心动
    2020-12-21 04:25

    The answer by nathancy works, but suffers on the performance side where as in the example creates 3 copies of the image to draw the contours thus, is sluggish when it comes to execution time.

    My alternative answer is as below;

    def contour_intersect(cnt_ref,cnt_query, edges_only = True):
    
        intersecting_pts = []
    
        ## Loop through all points in the contour
        for pt in cnt_query:
            x,y = pt[0]
    
            ## find point that intersect the ref contour
            ## edges_only flag check if the intersection to detect is only at the edges of the contour
    
            if edges_only and (cv2.pointPolygonTest(cnt_ref,(x,y),True) == 0):
                intersecting_pts.append(pt[0])
            elif not(edges_only) and (cv2.pointPolygonTest(cnt_ref,(x,y),True) >= 0):
                intersecting_pts.append(pt[0])
    
        if len(intersecting_pts) > 0:
            return True
        else:
            return False
    

提交回复
热议问题