python点到多边形距离

目录

matplotlib判断是否在多边形内

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)

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值