【问题标题】:finding distance between a given point and a contour edge in an image python在图像python中查找给定点和轮廓边缘之间的距离
【发布时间】:2020-10-15 19:42:38
【问题描述】:

我正在尝试找到离图像中给定点最近的轮廓边缘。使用精明边缘检测确定边缘。

上图是显示边缘的图像,红点是用户给定的点(右上角)。

我遍历所有可能的轮廓边缘,以找到边缘与坐标为 (t1,x1) 的给定点之间的最短距离。然后求该点到所有边的距离,然后找到与该点距离最短的边。

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

现在我们知道与给定点最近的边缘对应的索引,然后我使用 下面的代码来确定最近的轮廓边缘的索引来绘制它。我将最近的轮廓边缘的坐标保存在“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]

使用上述逻辑,代码找到了不正确的边缘,即位于右下角的红线,而不是下图中给定点所在的右上角。

【问题讨论】:

  • cv2.pointPolygonTest(cnt,(t1,x1),True) 此行返回有符号距离,您正在存储abs 距离值。你觉得这是个好主意吗??
  • 我需要从边缘到点的最短距离。这就是为什么有距离的大小来找到轮廓的索引。

标签: python image shortest-path edge-detection


【解决方案1】:

使用 pointInPolygonTest 似乎有点矫枉过正,有人想知道开放轮廓的有符号距离的含义是什么。也不需要存储所有距离值。

我会用双循环来解决这个问题,首先在边缘,然后在单个像素上,并通过计算欧几里得距离(平方以避免无用的平方根)来保持最近的像素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多