简体   繁体   English

在图像python中找到给定点和轮廓边缘之间的距离

[英]finding distance between a given point and a contour edge in an image python

I am trying to find the nearest contour edge to a given point in an image.我试图找到图像中给定点的最近轮廓边缘。 The edge was determined using the canny edge detection.使用canny边缘检测确定边缘。 在此处输入图片说明

The above figure is the image showing the edges and the red dot is the user given point (top right corner).上图是显示边缘的图像,红点是用户给定的点(右上角)。

I go over all the possible contour edges to find the shortest distance between the edge and a given point with the coordinates (t1,x1).我遍历所有可能的轮廓边缘以找到边缘与坐标(t1,x1)的给定点之间的最短距离。 Then find the distance of the point from all the edges and then find the edge that has the shortest distance from the point.然后找到该点与所有边的距离,然后找到与该点距离最短的边。

for i in range(0, num_contours):
    cnt=contours[i]
    # find the distance of the chosen point from all the contours
    dist= cv2.pointPolygonTest(cnt,(t1,x1),True)
    dist_abs[i]=abs(dist)

# find the minimum value and its index from a list of values 
val, idx = min((val, idx) for (idx, val) in enumerate(dist_abs))

Now we know the index corresponding to the nearest edge to a given point and then I use the following code to determine the indices of the nearest contour edge to plot it.现在我们知道与给定点最近的边缘对应的索引,然后我使用以下代码来确定最近的轮廓边缘的索引来绘制它。 I save the coordinates of the nearest contour edge in "jj" and "ii" vectors.我将最近的轮廓边缘的坐标保存在“jj”和“ii”向量中。

contour = contours[idx]
contour_lens = []
contour_len = contour.shape[0]
contour_lens.append(contour_len)

jj = [0] * contour_len
ii = [0] * contour_len 

for ilen in list(range(0, contour_len)):
    jj[ilen]=contour[ilen,0,1]
    ii[ilen]=contour[ilen,0,0]

Using the above logic, the code finds an incorrect edge ie the red line located in the bottom right corner instead of top right where the given point is located in the image below.使用上述逻辑,代码找到了不正确的边缘,即位于右下角的红线,而不是下图中给定点所在的右上角。

![edge_image2

Using the pointInPolygonTest seems overkill, and one wonders what is the meaning of a signed distance for an open contour.使用 pointInPolygonTest 似乎有点矫枉过正,有人想知道开放轮廓的带符号距离的含义是什么。 There is also no need to store all distance values.也不需要存储所有距离值。

I would solve this with a double loop, on the edges first, then on the individual pixels, and keep the closest pixel by computing the Euclidean distance (squared to avoid a useless square root).我会用双循环解决这个问题,首先在边缘上,然后在单个像素上,并通过计算欧几里德距离(平方以避免无用的平方根)来保持最近的像素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 和 opencv 在图像上绘制两个轮廓,它们之间的轮廓距离为 100 像素 - How can I draw two contours on an image given a contour distance of 100 pixels between them using python and opencv 使用 python 查找 2 个阈值图像之间的汉明距离 - Finding Hamming distance between 2 threshold image with python 如何填充连接边缘的轮廓并在 python 中裁剪图像? - How to fill contour of connected edge and crop the image out in python? Python中的图像处理(计算节点之间的图形边缘距离(区域质心) - Image manipulation in Python (compute graph edge distance between nodes (region centroids) 如何计算从正方形表面上的给定点到其任意方向的边缘的距离? - How to calculate the distance from a given point on the surface of a square to its edge with any given direction? 在Python中查找两个gps点之间的距离 - Finding distance between two gps points in Python 在Python中的给定点查找未知函数的梯度 - Finding gradient of an unknown function at a given point in Python 找到两点之间的最短距离 Python - Finding the shortest distance between two points Python 点和Python中的MultiPolygon Geoseries对象之间的距离 - Distance between a point and a MultiPolygon Geoseries object in Python 找到一个点和一个曲线python之间的距离 - find the distance between a point and a curve python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM