python点到多边形距离,点到轮廓距离

python点到多边形距离,点到轮廓距离,第1张

python点到多边形距离,点到轮廓距离

python点到多边形距离

最简单例子:

负数表示在多边形外,正数代表多边形内。

    import numpy as np
    point=(7,7)
    hull=[[1,1],[1,2],[2,2],[2,1]]
    dist = cv2.pointPolygonTest(np.array(hull), point, True)
    print('dist',dist)

缺点:

不能批量计算法,只能一个点一个点的计算。

感谢博客:

使用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)

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5579757.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存