目录
python点到多边形距离 负数表示在多边形外,正数代表多边形内。
matplotlib判断是否在多边形内
import matplotlib.path as mplPath
import numpy as np
# 定义多边形顶点
polygon_vertices = np.array([[0, 0], [0, 5], [5, 5], [5, 0]])
# 创建一个 Path 对象
polygon = mplPath.Path(polygon_vertices)
# 要检测的点
point = np.array([3, 3])
# 检查点是否在多边形内
is_inside = polygon.contains_point(point)
print(is_inside) # 输出:True
python点到多边形距离 负数表示在多边形外,正数代表多边形内。
最简单例子:
要求:polygon的数据必须是int类型,不能是float类型。
import cv2
import numpy as np
# 创建一个空白图像
image = np.zeros((400, 400, 3), dtype="uint8")
# 定义多边形的顶点
polygon = np.array([[50, 300], [200, 300], [300, 250], [200, 100], [100, 150]], np.int32)
polygon = polygon.reshape((-1, 1, 2)) # OpenCV需要这种结构的数据
# 定义点
point = (150, 150)
# 绘制多边形
cv2.polylines(image, [polygon], True, (0, 255, 0), 2)
# 绘制点
cv2.circle(image, point, 5, (0, 0, 255), -1)
# 计算点到多边形的距离
distance = cv2.pointPolygonTest(polygon, point, True)
# 在图像上显示距离
text = f"Distance: {distance:.2f}"
cv2.putText(image, text, (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
# 显示图像
cv2.imshow("Point to Polygon Distance", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
缺点:
不能批量计算法,只能一个点一个点的计算。
感谢博客:
使用cv2.pointPolygonTest()和cv2.polylines()的问题-python黑洞网
def gray_res():
import cv2
import numpy as np
# create background
img = np.zeros((400 ,400) ,dtype=np.uint8)
# define shape
pts = np.array([[18 ,306] ,[50 ,268] ,[79 ,294] ,[165 ,328] ,[253 ,294] ,[281 ,268] ,[313 ,306] ,[281 ,334] ,[270 ,341]
,[251 ,351] ,[230 ,360] ,[200 ,368] ,[165 ,371] ,[130 ,368] ,[100 ,360] ,[79 ,351] ,[50 ,334]
,[35 ,323]], np.int32)
pts = pts.reshape((-1 ,1 ,2))
# draw shape
cv2.polylines(img ,[pts] ,True ,(255), 2)
# draw point of interest
cv2.circle(img ,(52 ,288) ,1 ,(127) ,3)
# perform pointPolygonTest
dist = cv2.pointPolygonTest(pts, (52 ,288), False)
print(dist)
# show image
cv2.imshow('test', img)
cv2.waitKey()
cv2.destroyAllWindows()
def color_res():
import cv2
import numpy as np
point=(52, 208)
# create background
img = np.zeros((400, 400,3), dtype=np.uint8)
# define shape
pts = np.array(
[[18, 306], [50, 268], [79, 294], [165, 328], [253, 294], [281, 268], [313, 306], [281, 334], [270, 341],
[251, 351], [230, 360], [200, 368], [165, 371], [130, 368], [100, 360], [79, 351], [50, 334], [35, 323]],
np.int32)
pts = pts.reshape((-1, 1, 2))
# draw shape
cv2.polylines(img, [pts], True, (0,255,0), 2)
# draw point of interest
cv2.circle(img, point, 1, (0,0,255), 3)
# perform pointPolygonTest
dist = cv2.pointPolygonTest(pts, point, True)
print(dist)
# show image
cv2.imshow('test', img)
cv2.waitKey()
cv2.destroyAllWindows()
if __name__ == '__main__':
color_res()
感谢博客:
(Python)从零开始,简单快速学机器仿人视觉Opencv---第十九节:关于轮廓的函数 - 古月居
检测凸缺陷
opencv凸缺陷的基础知识_究极目标的博客-CSDN博客_opencv凸缺陷
凸包与轮廓之间的部分,称为凸缺陷。在opencv中凸缺陷的语法格式为:
convexityDefects =cv2.convexityDefects (contour,convexhull)
import cv2
import numpy as np
img =cv2.imread("contours2.png")#读图
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#转化为灰度图像
ret,binary =cv2.threshold(gray,32,255,0)#阈值处理
contours,h =cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)#查找轮廓
hull =cv2.convexHull(contours[0],returnPoints=False)#获取凸包
defects =cv2.convexityDefects(contours[0],hull)
for i in range(defects.shape[0]):
s,e,f,d=defects[i,0]
start = tuple(contours[0][s][0])
end = tuple(contours[0][e][0])
far = tuple(contours[0][f][0])
cv2.line(img,start,end,(255,255,0))#线条绘制
cv2.circle(img,far,5,(0,255,0))
print(defects)
cv2.imshow("img",img)#展示凸包
cv2.waitKey(0)