我试图在python中找到一个简单的方法,其中对于2dim掩码中的每个像素,我可以得到最近的非零邻居的索引.在Matlab中有一个bwdist,它正好返回.例如:如果我的输入是:
array [[0 0 0 0 0 0 0]
[0 1 0 0 0 0 0]
[0 0 0 0 0 1 0]
[0 0 0 0 0 0 0]]
Run Code Online (Sandbox Code Playgroud)
我的输出应该是:
array [[(1,1) (1,1) (1,1) (1,1) (2,5) (2,5) (2,5)]
[(1,1) (1,1) (1,1) (1,1) (2,5) (2,5) (2,5)]
[(1,1) (1,1) (1,1) (2,5) (2,5) (2,5) (2,5)]
[(1,1) (1,1) (1,1) (2,5) (2,5) (2,5) (2,5)]]
Run Code Online (Sandbox Code Playgroud)
该函数还可以返回绝对索引(对于1dim数组),如Matlab中的bwdist.
谢谢!
编辑:到目前为止,我已经尝试了一些与scipy相关的潜在解决方案,例如distance_transform_edt,但它只找到距离最近的像素而不是像素本身的距离.如果相关,我也会在我的代码中的其他地方使用OpenCV和VLfeat.
python opencv image-processing nearest-neighbor computer-vision