【发布时间】: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