使用opencv查找图像上一组6个点的最短路径
Using opencv to find the shortest path for a set of 6 points on an image
我已经设法在二维图像上找到了六个兴趣点。现在,我需要画一条最短距离的路径来连接这些点。
路径从点 0 开始,不会循环回到它。
我将从点 0 到下一个最近点画一条线,然后从该点到下一个最近点画一条线,依此类推,直到我到达最后一个点连接的。
将六个兴趣点保存为Point2f类型的数组。该数组的索引 0 是起点。其余索引的剩余点没有特定顺序存储。
如有任何帮助,我们将不胜感激
cv::norm 可用于计算两点之间的距离。我在 200 x 200 的棋盘上随机取了 6 个点。
现在我只是循环遍历其余点以使用 cv::norm 找到最小距离,然后将其索引与下一个点交换。我的结果是:
抱歉,密码在 python:
import cv2
import numpy as np
def find_nn(point, neighborhood):
"""
Finds the nearest neighborhood of a vector.
Args:
point (float array): The initial point.
neighborhood (numpy float matrix): The points that are around the initial point.
Returns:
float array: The point that is the nearest neighbor of the initial point.
integer: Index of the nearest neighbor inside the neighborhood list
"""
min_dist = float('inf')
nn = neighborhood[0]
nn_idx = 0
for i in range(len(neighborhood)):
neighbor = neighborhood[i]
dist = cv2.norm(point, neighbor, cv2.NORM_L2)
if dist < min_dist:
min_dist = dist
nn = neighbor
nn_idx = i
nn_idx = nn_idx + j + 1
return nn, nn_idx
#taking 6 random points on a board of 200 x 200
points = [(10, 10), (115, 42), (36, 98), (78, 154), (167, 141), (189, 4)]
board = np.ones((200, 200, 3), dtype = np.uint8) * 255
for i in range(6):
cv2.circle(board, points[i], 5, (0, 255, 255), -1)
for j in range(5):
nn, nn_idx = find_nn(points[j], points[j+1:])
points[j+1], points[nn_idx] = points[nn_idx], points[j+1]
for i in range(5):
cv2.arrowedLine(board, points[i], points[i+1], (255, 0, 0), 1, tipLength = 0.07)
cv2.imshow('image', board)
cv2.waitKey(0)
cv2.destroyAllWindows()
我已经设法在二维图像上找到了六个兴趣点。现在,我需要画一条最短距离的路径来连接这些点。
路径从点 0 开始,不会循环回到它。
我将从点 0 到下一个最近点画一条线,然后从该点到下一个最近点画一条线,依此类推,直到我到达最后一个点连接的。
将六个兴趣点保存为Point2f类型的数组。该数组的索引 0 是起点。其余索引的剩余点没有特定顺序存储。
如有任何帮助,我们将不胜感激
cv::norm 可用于计算两点之间的距离。我在 200 x 200 的棋盘上随机取了 6 个点。
现在我只是循环遍历其余点以使用 cv::norm 找到最小距离,然后将其索引与下一个点交换。我的结果是:
抱歉,密码在 python:
import cv2
import numpy as np
def find_nn(point, neighborhood):
"""
Finds the nearest neighborhood of a vector.
Args:
point (float array): The initial point.
neighborhood (numpy float matrix): The points that are around the initial point.
Returns:
float array: The point that is the nearest neighbor of the initial point.
integer: Index of the nearest neighbor inside the neighborhood list
"""
min_dist = float('inf')
nn = neighborhood[0]
nn_idx = 0
for i in range(len(neighborhood)):
neighbor = neighborhood[i]
dist = cv2.norm(point, neighbor, cv2.NORM_L2)
if dist < min_dist:
min_dist = dist
nn = neighbor
nn_idx = i
nn_idx = nn_idx + j + 1
return nn, nn_idx
#taking 6 random points on a board of 200 x 200
points = [(10, 10), (115, 42), (36, 98), (78, 154), (167, 141), (189, 4)]
board = np.ones((200, 200, 3), dtype = np.uint8) * 255
for i in range(6):
cv2.circle(board, points[i], 5, (0, 255, 255), -1)
for j in range(5):
nn, nn_idx = find_nn(points[j], points[j+1:])
points[j+1], points[nn_idx] = points[nn_idx], points[j+1]
for i in range(5):
cv2.arrowedLine(board, points[i], points[i+1], (255, 0, 0), 1, tipLength = 0.07)
cv2.imshow('image', board)
cv2.waitKey(0)
cv2.destroyAllWindows()