[英]Find closest 8-connected chessboard distance between multiple pairs of points: shortest m-path
I am working on binary images in Python using OpenCV. 我正在使用OpenCV在Python中处理二进制图像。 I have two sets of points: PNodes and FNodes.
我有两套观点:PNodes和FNodes。 I want to find the closest PNode to each of the FNodes (shortest m-path);
我想找到最接近每个FNode的PNode(最短的m路径); closest in terms of 8-connected chessboard distance.
在八连棋盘距离方面最接近。
In the example below, suppose PNodes (donated by *) are: (6,1), (6,5) and (5,8). 在下面的示例中,假设PNode(由*捐赠)是:(6,1),(6,5)和(5,8)。 (indexing starts from 0, first element is row number).
(索引从0开始,第一个元素是行号)。 FNodes (denoted by #) are: (0,1), (0,9), (1,6), (2,5) and (4,3).
FNode(用#表示)是:(0,1),(0,9),(1,6),(2,5)和(4,3)。
import numpy as np
In = np.array ((
[ 0, 1#, 0, 0, 0, 0, 0, 0, 0, 1#, 0],
[ 1, 0, 0, 0, 0, 0, 1#, 0, 1, 0, 0],
[ 0, 1, 0, 0, 0, 1#, 0, 0, 1, 0, 0],
[ 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0],
[ 0, 1, 1, 1#, 0, 1, 0, 0, 1, 0, 0],
[ 0, 1, 0, 0, 0, 1, 0, 0, 1*, 0, 0],
[ 0, 1*, 0, 0, 0, 1*, 0, 0, 1, 0, 0],
[ 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0],
[ 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0]), dtype = "uint8")
Distance_Matrix = np.array ((
[ 0, 6#, 0, 0, 0, 0, 0, 0, 0, 5#, 0],
[ 5, 0, 0, 0, 0, 0, 5#, 0, 4, 0, 0],
[ 0, 4, 0, 0, 0, 4#, 0, 0, 3, 0, 0],
[ 0, 0, 3, 0, 0, 0, 3, 0, 0, 2, 0],
[ 0, 2, 2, 3#, 0, 2, 0, 0, 1, 0, 0],
[ 0, 1, 0, 0, 0, 1, 0, 0, **, 0, 0],
[ 0, **, 0, 0, 0, **, 0, 0, 1, 0, 0],
[ 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0],
[ 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0]), dtype = "uint8")
I am not concerned with the exact value of distance, I just want to find out the closest pair. 我不关心距离的确切值,我只想找出最接近的一对。 Something like this: FNode at (0,1) is closest to PNode at (6,1).
像这样:(0,1)的FNode最接近(6,1)的PNode。 FNode at (4,3) is closest to PNode at (6,1).
(4,3)处的FNode最靠近(6,1)处的PNode。 All distances are in terms of 8-connected chessboard distance.
所有距离均以八连棋盘距离表示。
Ultimate requirement from this entire process : Basically, I just want to make sure all PNodes have atleast 1 FNode which lie within a given distance range (along the path of 1s). 这是整个过程的最终要求 :基本上,我只想确保所有PNode都具有至少1个FNode,它们位于给定的距离范围内(沿1s的路径)。
Suppose PNode (PN_1) has a FNode (FN_1) which lies within the required distance range, I also make sure that PN_1 is closest to FN_1, and not any other PNode. 假设PNode(PN_1)的FNode(FN_1)在所需的距离范围内,则我还要确保PN_1最接近FN_1,而不是其他任何PNode。
For a better understanding, I have attached an image below; 为了更好的理解,我在下面附加了一张图片; FNodes are rectangular and PNodes are circular.
FNode是矩形的,PNode是圆形的。
I don't care about other elements in this matrix, apart from those of PNodes and FNodes, as depicted. 如图所示,除了PNodes和FNodes的元素外,我不在乎此矩阵中的其他元素。
I'd use Dijkstra's algorithm for this. 我会为此使用Dijkstra的算法 。 It finds the shortest distance between a source node and every other node by exploring nearer nodes before farther nodes.
它通过在更远的节点之前探索更近的节点来找到源节点与每个其他节点之间的最短距离。
Seed a Dijkstra search at each of your P-nodes and halt the search when you either find the first F-node or exceed your distance constraint. 在每个P节点处播种Dijkstra搜索,并在找到第一个F节点或超出距离约束时停止搜索。
Since you don't have a graph, per se, you can generate neighbours using offsets. 由于您本身没有图,因此可以使用偏移量生成邻居。 For instance if you have a cell (x,y) you can define some offsets
例如,如果您有一个像元(x,y) ,则可以定义一些偏移量
dx = [-1,-1, 0, 1,1,1,0,-1]
dy = [ 0,-1,-1,-1,0,1,1, 1]
and then take (x+dx[i], y+dy[i]) for i∈[0,7] to generate the neighbours of a given cell. 然后取i∈ [0,7]的 (x + dx [i],y + dy [i])生成给定像元的邻居。
You can define a separate 2D array/matrix for tracking whether cells have been visited. 您可以定义一个单独的2D数组/矩阵来跟踪是否已访问单元格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.