我试图检查的相似性n图像我保存在一个数组(由1797个样本组成我的目标是将每对图像的L1距离保存在大小为1797x1797的数组中(注意计算L1的方法写在下面). t此刻我正在使用下面的方法来比较它们,但它似乎有点慢和“非Python”,有没有一种方法可以对numpy或类似的库做同样的事情,这可能最终会在性能方面升级?
import numpy as np
def L1(x1, x2):
x1 = x1.astype(np.int64)
x2 = x2.astype(np.int64)
distanceL1 = np.sum(np.abs(x2 - x1))
return distanceL1
def L1_final(dataset):
images = dataset
images = np.array(images).reshape([1797,64]) #this is due to the fact that the 64 bits are a bi-dimensional array of size 8x8 at the start
d = np.zeros([len(images), len(images)])
length = len(images)
for x1 in range(length):
img = images[x1]
for x2 in range(length):
d[x1,x2] = L1(img, images[x2])
return d
print(L1_final(digits.images))
1条答案
按热度按时间0g0grzrc1#
通过在Google上搜索“pairwise distance numpy”,我可以找到很多方法。
例如,this one使用NumPy广播:
其中
a
是图像数据集数组还有其他使用外部库的方法,如: