pointPolygonTest使用opencv对边界无法正常工作?

时间:2015-04-08 08:04:09

标签: python opencv geometry contour np

我正在尝试使用pointPolygontest的功能,我想检查内外的边界和点。

形成功能的自由裁量权 double pointPolygonTest(InputArray contour,Point2f pt,bool measureDist)

参数:

contour – Input contour.

pt – Point tested against the contour.

measureDist – If true, the function estimates the signed distance from the point to the nearest contour edge. Otherwise, the function only checks if the point is inside a contour or not. 

当measureDist = false时,返回值为+ 1,-1和0,t相应地返回正(内部),负(外部)或零(边缘)值。

现在我测试了我的多边形边缘的下面的代码,并没有得到ecpexted结果。

代码如下:

r = 100
src = np.zeros((4*r,4*r),np.uint8)
rows,cols = src.shape
points = [[0.0*r, 0.0*r], [0.0*r,2.0*r], [2.0*r,2.0*r], [2.0*r,0.0*r] ]
print points
points = np.array(points,np.int0)
cv2.polylines(src,[points],True,255,3)
contours,hierarchy = cv2.findContours(src,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
res = np.zeros(src.shape,np.float32) # array to store distances
drawing = np.zeros((rows,cols,3),np.uint8) # image to draw the distance
cnt = contours[0] # We take only one contour for testing
# Calculate distance from each point

print cv2.pointPolygonTest(cnt,(0.0*r,0.0*r),False)
print cv2.pointPolygonTest(cnt,(0.0*r,2.0*r),False)
print cv2.pointPolygonTest(cnt,(2.0*r,0.0*r),False)
print cv2.pointPolygonTest(cnt,(2.0*r,2.0*r),False)

输出

[[0.0, 0.0], [0.0, 200.0], [200.0, 200.0], [200.0, 0.0]]
-1.0
-1.0
-1.0
 1.0

这意味着3个boubdries在外面,最后一个在内部,但据我所知,所有这些都应该是0.0

那么,什么是错误?

1 个答案:

答案 0 :(得分:0)

我厌倦了重现。您可以检查轮廓值以及-1.0和1.0的原因。

[199, 2; 199, 198; 198, 199; 2, 199; 2, 201; 200, 201; 201, 200; 201, 2]

尝试

cv2.fillPoly
points = [[1.0, 1.0], [1.0,2.0*r], [2.0*r,2.0*r], [2.0*r,1.0] ]

而不是

points = [[0.0*r, 0.0*r], [0.0*r,2.0*r], [2.0*r,2.0*r], [2.0*r,0.0*r] ]

pointPolygon中通常省略边界(实例0,0或400,400)不会按预期返回0而是返回-1