OpenCV Python – 如何找到图像中的点和轮廓之间的最短距离?
我们可以使用 cv2.pointPolygonTest() 计算图像上点和轮廓之间的最短距离,将轮廓点坐标和点坐标作为参数传递。在应用 cv2.pointPolygonTest() 之前,我们需要计算图像中的轮廓。我们可以按照以下给定的步骤来找到给定点与图像中物体轮廓之间的最短距离-
-
导入所需的库。在以下所有 Python 示例中,所需的 Python 库是 OpenCV。确保您已经安装了它。
-
使用 cv2.imread() 读取输入图像。使用此方法读取的 RGB 图像采用 BGR 格式。(可选)将读取的 BGR 图像分配给 img。
-
现在使用 cv2.cvtColor() 函数将此 BGR 图像转换为灰度图像,如下所示。(可选)将转换后的灰度图像指定为灰度。
-
对灰度图像应用阈值以将其转换为二进制图像。调整第二个参数(threshValue)以获得更好的二进制图像。
-
在二进制图像中找到轮廓。
-
选取第一个轮廓作为 cnt 或遍历所有轮廓。
-
使用 cv2.pointPolygonTest() 函数计算点与所选等值线之间的最短距离。将所需的参数传递给此函数。为了计算点 (250,250) 和等值线 cnt 之间的最短距离,我们使用以下代码片段:
dist = cv2.pointPolygonTest(cnt,(250,250),True)
-
打印输入图像中点和对象轮廓之间计算出的最短距离。
-
在图像中绘制点和检测到的轮廓,以获得更好的可视化效果。
让我们了解如何借助一些 Python 示例找到图像中给定点和对象轮廓之间的最短距离。
例
在这个Python程序中,我们在输入图像上取了两个点(250,250)和(350,250)。我们计算输入图像中点和轮廓之间的最短距离。输入图像只有一个对象轮廓。我们还在图像上绘制轮廓和点,以便清晰理解。
# import required libraries import cv2 # load the input image img = cv2.imread('four-point-star.png') # convert the input image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # apply thresholding to convert grayscale to binary image ret,thresh = cv2.threshold(gray,150,255,0) # find the contours contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) print("Number of contours detected:", len(contours)) # select the first contour cnt = contours[0] # find the shortest distance from point[250,250] dist1 = cv2.pointPolygonTest(cnt,(250,250),True) # print the shortest distance between the point 1 and contour detected. print('Shortest distance of Point 1 from contour:', dist1) dist2 = cv2.pointPolygonTest(cnt,(350,250),True) # print the shortest distance between the point 2 and contour detected. print('Shortest distance of Point 2 from contour:', dist2) # draw the point [250,250] on the image cv2.circle(img, (250,250), 4, (0, 0, 255), -1) cv2.putText(img, "Point 1", (255,255), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) cv2.circle(img, (350,250), 4, (0, 0, 255), -1) cv2.putText(img, "Point 2", (355,255), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) # draw contour on the input image cv2.drawContours(img, [cnt], -1, (0,255,255), 3) # display the image with drawn extreme points while True: cv2.imshow("Extreme Points", img) if cv2.waitKey(1) & 0xFF == 27: break cv2.destroyAllWindows()
我们将使用此图像作为该程序的输入文件 -
输出
当你运行上面的程序时,它将产生以下输出 -
Number of contours detected: 1 Shortest distance of Point 1 from contour: -17.72004514666935 Shortest distance of Point 2 from contour: 31.622776601683793
我们得到以下窗口,显示轮廓和两个点 -
在上面的输出图像中,轮廓以黄色绘制,两个点以芦苇色绘制。点 1 与等值线之间的最短距离为负数,表示该点位于等值线之外。点 2 和等值线之间的最短距离为正,表明该点位于等值线内。如果最短距离为零,则表示该点位于等值线边界上
例
在这个 Python 程序中,我们在输入图像上取了一个点 (350,250)。我们检测输入图像中的所有对象轮廓。图像有三个轮廓。我们计算输入图像中点和轮廓之间的最短距离。我们还在图像上绘制轮廓和点,以便清晰理解。
# import required libraries import cv2 # load the input image img = cv2.imread('convexhull.png') # convert the input image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # apply thresholding to convert grayscale to binary image ret,thresh = cv2.threshold(gray,100,255,0) # find the contours contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) print("Number of contours detected:", len(contours)) # draw contour and shape number for i, cnt in enumerate(contours): M = cv2.moments(cnt) x1, y1 = cnt[0,0] img1 = cv2.drawContours(img, [cnt], -1, (0,255,255), 3) cv2.putText(img, f'Contour:{i+1}', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) # compute shortest distance of point (350,250) from the contour dist = cv2.pointPolygonTest(cnt,(350,250),True) print(f'Shortest distance of Point (350,250) from contour {i+1}:', dist) cv2.circle(img, (350,250), 4, (0, 0, 255), -1) cv2.imshow("Shapes", img) cv2.waitKey(0) cv2.destroyAllWindows()
我们将使用此图像作为该程序的输入文件 -
输出
当你运行上面的程序时,它将产生以下输出 -
Number of contours detected: 3 Shortest distance of Point (350,250) from contour 1: -83.73768566183328 Shortest distance of Point (350,250) from contour 2: -62.81719509815764 Shortest distance of Point (350,250) from contour 3: -57.27564927611035
我们得到以下窗口,显示轮廓和点 -
在上面的输出图像中,轮廓以黄色绘制,点以芦苇色绘制。点和等值线之间的每个最短距离均为负数,表示该点位于每个等值线之外。与其他距离相比,点与等值线 3 之间的绝对距离最小。因此,该点最接近等高线 3。
相关文章
- Python中python-nmap模块的使用
- python - 如何使用 Docker 运行多个 Python 脚本和一个可执行文件?
- Python任意参数的数量/预习课python基础
- Python初学者如何系统的学习python————Python入门学习指南--内附学习路径
- 零基础自学Python需要多长时间从入门到精通?学python能兼职挣钱吗?怎么挣钱?
- Python-OpenCV下的窗口鼠标事件交互操作(实现鼠标移动轨迹的绘制)
- MATLAB、C++、OpenCV、Python、Python-Numpy近似值(取整)求取函数总结【round()、ceil()、floor()、fix()】
- 基于Python+MySQL实现物联网引论课程一个火警报警及应急处理系统【100010366】
- Python个人主页项目-1.项目分析与产品设计
- OpenCV Python – 如何将彩色图像转换为二进制图像?
- 如何使用OpenCV Python比较两个图像的直方图?
- Python教程之正则表达式实现拼音与中文提取并存储excel(教程含源码)
- Jenkins持续集成实战之Jenkins构建Python项目提示:'python' 不是内部或外部命令,也不是可运行的程序。
- 《Python硬件编程实战》——1.3 Python的特点
- 第一章 opencv与python介绍及环境搭建
- Python 基础 之 python 进程知识点整理,实现一个简单使用进程池的多进程文件夹文件copy器
- 【Python注意事项】如何理解python中间generator functions和yield表情
- (15)Python时间
- 【图像处理】——图像滤波(Python+opencv实现三种方法:均值滤波、中值滤波、高斯滤波等)
- Java 调用Python+Opencv实现图片定位
- python实现opencv(清华镜像)
- Python opencv-python 简单测试
- opencv python数据增强