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

pointPolygonTest is not working correctly for the boundries using opencv?

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

形成函数的自由裁量权 双点多边形测试(InputArray 轮廓,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 时,return 值为 +1、-1 和 0,t returns 正(内部)、负(外部)或零(在边缘上) 值,相应地。

现在我针对多边形的边缘测试了以下代码,但没有得到预期的结果。

代码如下:

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 和 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)中的边界通常被省略,不会像预期的那样 return 0 而是 return -1