gpt4 book ai didi

python - 如何正确使用 cv2.pointPolygonTest() 和 cv2.findcontours()?

转载 作者:太空宇宙 更新时间:2023-11-03 21:34:47 28 4
gpt4 key购买 nike

我想使用 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)

在这个阈值图像上它工作正常:

test1

(x,y) 在对象内部时 x1 被正确计算

np.shape(contours) 等于 (1, 241, 1, 2)

但是在这张图片上:

enter image description here

所有点都按外面计算

np.shape(contours) 等于 (11,)

我想我没有以正确的方式使用轮廓,但我不知道应该将哪个维度的轮廓传递给 cv2.pointPolygonTest()

最佳答案

不知你测试重点的代码是不是只有这样:

 x1 = cv2.pointPolygonTest(contours[0], (x, y), False)

在这段代码中,函数只测试第一个轮廓。也许在第二张图片中有多个轮廓,请尝试使用循环。

关于python - 如何正确使用 cv2.pointPolygonTest() 和 cv2.findcontours()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36482165/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com