我希望使用cv2.pointPolygonTest来检查点是否位于轮廓内或外,但我无法弄清楚为什么它不起作用。
这是我获取轮廓的方式:
在这个阈值图像上它工作得很好:
这是我获取轮廓的方式:
img_name = a295121c-f893-43f5-8d00-6bfddbc19658.jpg"
im = cv2.imread(img_name)
im_gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
ret, thresh = cv2.threshold(im_gray, 10, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_TC89_L1)
这就是我检查点是否在检测到的对象内部或外部的方法。
x1 = cv2.pointPolygonTest(contours[0], (x, y), False)
在这个阈值图像上它工作得很好:
当(x,y)
在物体内部时,x1会被正确计算。
np.shape(contours)
等于(1, 241, 1, 2)
然而,在这张图片上:
所有点都被计算为外部。
np.shape(contours)
等于(11,)
我想我没有正确使用轮廓,但我无法确定应该传递哪个轮廓维度给cv2.pointPolygonTest()
。
drawContours
),然后每个点都具有值0(轮廓外)或255(轮廓内)。 - Miki