要判断一个点是否在轮廓内,可以使用OpenCV中的函数pointPolygonTest。该函数可以计算一个点到轮廓的最短距离,如果这个距离为正,则表示点在轮廓内部,如果为负,则表示点在轮廓外部,如果为0,则表示点在轮廓上。
以下是一个简单的示例代码:
import cv2# 读取轮廓contour = cv2.imread('contour.jpg', 0)# 创建一个点point = (50, 50)# 判断点是否在轮廓内distance = cv2.pointPolygonTest(contour, point, False)if distance > 0: print("Point is inside the contour")elif distance < 0: print("Point is outside the contour")else: print("Point is on the contour")在上面的示例中,首先读取了一个轮廓图像,然后创建了一个点(50, 50),最后使用pointPolygonTest函数计算这个点到轮廓的最短距禮,并根据计算结果进行判断。


