python - 在Python中计算矩阵中值的距离的有效方法

标签 python numpy distance

我有一个图像,想要计算每个非零值像素到最近的零值像素的距离。 我尝试这样做的方法如下:

import numpy as np
from scipy.spatial.distance import cdist
from skimage import io

im=io.imread('imagepath')
#getting array where elements are 0
a,b = np.where(im == 0) 
# create a list with (row,column)
x = list(zip(a, b)) 
#getting array where elements are non zero
r, c =np.where(im!=0)
#create a list with (row, column) of all non 0 values
#note coordinates are in y, x format
obj = list(zip(r,c))
dist_dict={}
#calculating for each pixel of the object
for o in obj:    
    d = (cdist(np.array([o]), x, metric='euclidean')).min() 
    dist_dict.update({o:d})

我相信这应该可行,但速度很慢。对于单个像素,计算距离大约需要 0.2 秒。因此,对于大小约为 50,000 像素的对象,每张图像将花费大约三个小时的计算时间,这根本不可行。 我在这里看到的一个问题是我只是迭代所有非零像素。 有没有办法不从数组的开头开始搜索,而是从当前坐标开始搜索,直到找到零值?或者还有其他建议如何加快此过程?

最佳答案

您可以使用scipy.ndimage.morphology.distance_transform_edt它找到与输入像素具有最小欧氏距离的最近背景点(值 0)。

from scipy import ndimage
import pprint

def nearest_zero(image):
    " Finds closest background (zero) element for each element in image "

    # Find closest zero elements in the inverted image (same as closest non-zero for image)
    edt = ndimage.distance_transform_edt(image, return_indices=False)

    # Create dictionary of indexes
    return {(r,c):edt[r][c] for r in range(image.shape[0]) for c in range(image.shape[1]) if image[r][c]}

使用示例

image = np.array(([0,50,75,15,0],
                  [2,0,111,10,15],
                  [0,112,25,110,115],
                  [0,10,110,115,0],
                  [15,12,115,0,0]))


d = nearest_zero(image)
pp = pprint.PrettyPrinter(indent=4)

print('Original Image')
print(image)

print('\nDictionary of Distances to closest background pixel for each non-background pixel')
pp.pprint(sorted(d.items(), key=lambda x: x[0]))

输出

Original Image
[[  0  50  75  15   0]
 [  2   0 111  10  15]
 [  0 112  25 110 115]
 [  0  10 110 115   0]
 [ 15  12 115   0   0]]

Dictionary of Distances to closest background pixel for each non-background pixel
[   ((0, 1), 1.0),
    ((0, 2), 1.4142135623730951),
    ((0, 3), 1.0),
    ((1, 0), 1.0),
    ((1, 2), 1.0),
    ((1, 3), 1.4142135623730951),
    ((1, 4), 1.0),
    ((2, 1), 1.0),
    ((2, 2), 1.4142135623730951),
    ((2, 3), 1.4142135623730951),
    ((2, 4), 1.0),
    ((3, 1), 1.0),
    ((3, 2), 1.4142135623730951),
    ((3, 3), 1.0),
    ((4, 0), 1.0),
    ((4, 1), 1.4142135623730951),
    ((4, 2), 1.0)]

性能测试

结果:SciPy 快约 100 倍

测试数据生成 - 随机图像(大小 250x250 = 62、500 像素)

import random
size = 250
z = [random.randrange(0, 255) for r in range(size) for c in range(size)]
image = np.array(z).reshape(size, size)

测试图像中的零数

print(np.count_nonzero(image==0))  # 62262

timeit 使用原始帖子中的方法

11.6 s ± 89.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
which is an average of 1.8e-04 seconds per non-zero point

使用 SciPy 方法进行 timeit

119 ms ± 17.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
Which is an average of 1.9e-06

因此 SciPy 快约 100 倍

关于python - 在Python中计算矩阵中值的距离的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59126316/

相关文章:

python - 将整数数组转换为 "vector"

php - PHP 中 2 点之间的相对位置(纬度/经度)

javascript - 测量两个 HTML 元素中心之间的距离

python - 3D 张量输入到 keras 或 tensorflow 中的嵌入层?

python - Flask-如何处理URL中的中文字符

python - numpy 点积但保留为矢量(不添加元素)

python - python中的有界圆插值

python - 在开发过程中如何使用 Keras 获得可重现的结果?

python - 无法从线程显示窗口

c++ - 输出迭代器适配器计数但不复制