opencv foundation 53-image contour 06-judging the relationship between the pixel point and the contour (inside the contour, on the contour, outside the contour) cv2.pointPolygonTest()

point-to-contour distance

In OpenCV, the function cv2.pointPolygonTest() is used to calculate the shortest distance (that is, the
vertical line distance) from the point to the polygon (contour), this calculation process is also called the relationship test between the point and the polygon. The syntax format of this function is:
retval = cv2.pointPolygonTest( contour, pt, measureDist )
The return value in the formula is retval, which is related to the value of the parameter measureDist.
The parameters in the formula are as follows:

  • contour is the contour.
  • pt is the point to be determined.
  • measureDist is a Boolean value, indicating the way to determine the distance.
  • When the value is True, it means to calculate the distance from the point to the contour. If the point is outside the contour, the return value is negative; if the point is on the contour, the return value is 0; if the point is inside the contour, the return value is positive.
  • When the value is False, the distance is not calculated, and only a value among "-1", "0" and "1" is returned, indicating the positional relationship of the point relative to the contour. If the point is outside the contour, the return value is "-1"; if the point is on the contour, the return value is "0"; if the point is inside the contour, the return value is "1".

Example: Use the function cv2.pointPolygonTest() to calculate the shortest distance from a point to a contour.

Use the function cv2.pointPolygonTest() to calculate the shortest distance from the point to the contour,需要将参数 measureDist 的值设置为 True。

code show as below:

import cv2
#----------------原始图像-------------------------
o = cv2.imread('cs.bmp')
cv2.imshow("original",o)
#----------------获取凸包------------------------
gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary,
 cv2.RETR_LIST,
 cv2.CHAIN_APPROX_SIMPLE)
hull = cv2.convexHull(contours[0])

cv2.polylines(o, [hull], True, (0, 255, 0), 2)
#----------------内部点 A 到轮廓的距离-------------------------
distA = cv2.pointPolygonTest(hull, (300, 150), True)
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(o,'A',(300,150), font, 1,(0,255,0),2)
print("distA=",distA)
#----------------外部点 B 到轮廓的距离-------------------------
distB = cv2.pointPolygonTest(hull, (300, 250), True)
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(o,'B',(300,250), font, 1,(0,255,0),2)
print("distB=",distB)
#------------正好处于轮廓上的点 C 到轮廓的距离-----------------
distC = cv2.pointPolygonTest(hull, (423, 112), True)
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(o,'C',(423,112), font, 1,(0,255,0),2)
print("distC=",distC)

#----------------显示-------------------------
cv2.imshow("result1",o)
cv2.waitKey()
cv2.destroyAllWindows()

operation result:

insert image description here
At the same time, the program will display the following results:

distA= 16.891650862259112
distB= -81.17585848021565
distC= -0.0

From the above results it can be seen that,

  • The distance calculated from point A is "16.891650862259112", which is a positive number, indicating that point A is inside the contour.
  • The distance calculated from point B is "-81.17585848021565", which is a negative number, indicating that point B is outside the contour.
  • The distance calculated from point C is "-0.0", indicating that point C is on the contour.

In actual use, if you want to obtain the points on the contour, you can obtain it by printing the contour point set. For example, in this example, the point on the contour can be obtained by the statement "print(hull)". After obtaining the point on the contour, it can be used as a parameter of the function cv2.pointPolygonTest() to test whether the return value of the function is zero.

Example 2: Use the function cv2.pointPolygonTest() to judge the relationship between points and contours.

code show as below:

import cv2
#----------------原始图像-------------------------
o = cv2.imread('cs.bmp')
cv2.imshow("original",o)
#----------------获取凸包------------------------
gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary,
 cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)
hull = cv2.convexHull(contours[0])
image = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)
cv2.polylines(image, [hull], True, (0, 255, 0), 2)
#----------------内部点 A 与轮廓的关系-------------------------
distA = cv2.pointPolygonTest(hull, (300, 150),False)
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image,'A',(300,150), font, 1,(0,255,0),3)
print("distA=",distA)
#----------------外部点 B 与轮廓的关系-------------------------
distB = cv2.pointPolygonTest(hull, (300, 250), False)
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image,'B',(300,250), font, 1,(0,255,0),3)
print("distB=",distB)
#----------------边缘线上的点 C 与轮廓的关系----------------------
distC = cv2.pointPolygonTest(hull, (423, 112),False)
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image,'C',(423,112), font, 1,(0,255,0),3)

print("distC=",distC)
#----------------显示-------------------------
cv2.imshow("result",image)
cv2.waitKey()
cv2.destroyAllWindows()

operation result:
insert image description here

At the same time, the program will also display the following running results:

distA= 1.0
distB= -1.0
distC= 0.0

From the above results it can be seen that,

  • The calculated relationship value of point A is "1", indicating that the point is inside the contour.
  • The calculated relationship value of point B is "-1", indicating that the point is outside the contour.
  • The calculated relationship value of point C is zero, indicating that the point is on the contour.

In practical applications, we can use this method to judge whether the pixel points detected by the template are within a specified ROI area. We will explain the specific application examples and code examples in the actual combat chapter.

Guess you like

Origin blog.csdn.net/hai411741962/article/details/132202254