I have drawn a contour around extreme points. Inside polygon figure I have others points. How to check if they are inside contour?
You can use the cv2.pointPolygonTest() function available in OpenCV.
For example:
dist = cv2.pointPolygonTest(cnt,(50,50),True)
In this example we are checking whether the coordinate (50, 50)
is present withing the contour cnt
dist
returns one of the following three:
Within the function cv2.pointPolygonTest()
the third parameter decides whether you want one of the following two :
dist
returns either the positive or negative distance of the point, if it is either inside or outside the contour respectively.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)
Output:
('dist1 : ', -45.17742799230607)
('dist2 : ', 49.9799959983992)
('dist3 : ', -0.0)