如何检查点是否放置在轮廓内? [英] How to check if point is placed inside contour?
问题描述
我已在极端点周围绘制了轮廓.在多边形图中,我还有其他要点. 如何检查它们是否在轮廓内?
I have drawn a contour around extreme points. Inside polygon figure I have others points. How to check if they are inside contour?
推荐答案
您可以使用OpenCV中可用的cv2.pointPolygonTest()函数.
You can use the cv2.pointPolygonTest() function available in OpenCV.
例如:
dist = cv2.pointPolygonTest(cnt,(50,50),True)
在此示例中,我们正在检查是否在轮廓cnt
中出现坐标(50, 50)
In this example we are checking whether the coordinate (50, 50)
is present withing the contour cnt
-
dist
返回以下三个之一:
-
如果点在轮廓的内部内,则为
- 正值值 如果点在轮廓的外部之外,则
- 负值
- 归零(如果该点位于轮廓上 上 )
- Positive value if the point is inside the contour
- Negative value if the point is outside the contour
- Zero if the point is on the contour
在函数cv2.pointPolygonTest()
中,第三个参数决定是否要以下两个之一:
Within the function cv2.pointPolygonTest()
the third parameter decides whether you want one of the following two :
- 如果它是 True ,则
dist
返回该点的正或负距离(如果该点分别在轮廓的内部或外部). - 另一方面,如果将其设置为 False ,则根据分别位于内部,外部或轮廓上的点,它会返回+ 1,-1或0.
- If it is True,
dist
returns either the positive or negative distance of the point, if it is either inside or outside the contour respectively. - On the other hand, if it is set to False, it returns +1, -1 or 0 depending on the point lying inside, outside or on the contour respectively
有关更多详细信息,请参见 DOCS
See THE DOCS for more details
我添加了一个示例来演示其工作原理.我考虑了以下获得轮廓的图像:
I added an example to show how it works. I considered the following image for which a contour was obtained:
我假设要使用以下几点进行说明:
I assumed the following points to be used as illustration:
(50, 70), (170, 152), (152, 48)
dist1 = cv2.pointPolygonTest(contours[0], (50, 70), True)
dist2 = cv2.pointPolygonTest(contours[0], (170, 152), True)
dist3 = cv2.pointPolygonTest(contours[0], (152, 48), True)
print('dist1 : ', dist1)
print('dist2 : ', dist2)
print('dist3 : ', dist3)
输出:
('dist1 : ', -45.17742799230607)
('dist2 : ', 49.9799959983992)
('dist3 : ', -0.0)
这篇关于如何检查点是否放置在轮廓内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!