import Polygon, numpy
# Here I extracted and combined some contours and created a convex hull from it.
# Now I wanna check whether a contour acquired differently intersects with this hull or not.
for contour in contours: # The result of cv2.findContours is a list of contours
contour1 = contour.flatten()
contour1 = numpy.reshape(contour1, (int(contour1.shape[0]/2),-1))
poly1 = Polygon.Polygon(contour1)
hull = hull.flatten() # This is the hull is previously constructued
hull = numpy.reshape(hull, (int(hull.shape[0]/2),-1))
poly2 = Polygon.Polygon(hull)
if (poly1 & poly2).area()<= some_max_val:
some_operations
您可以使用http://pypi.python.org/pypi/Polygon/2.0.4,下面是一个示例:
要将cv2.findContours的结果转换为多边形点格式,可以:
这将把形状从(N,1,2)转换为(N,2)
下面是一个完整的示例:
输出:
可以将数组视图用作intersect1d函数的一维,如下所示:
这将创建每个数组的视图,将每一行更改为一个值元组。然后执行交集,并将结果更改回原始格式。下面是一个使用它的示例:
这张照片:
所以我就是这么做的:
我不得不使用for循环,这看起来有点乏味,尽管它给了我预期的结果。如果有更好的方法,我们将不胜感激!
相关问题 更多 >
编程相关推荐