gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:45:37 24 4
gpt4 key购买 nike

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

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

timeit 使用 SciPy 方法

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com