numpy 分析图像相似度

mcdcgff0  于 2023-04-12  发布在  其他
关注(0)|答案(1)|浏览(140)

我试图检查的相似性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))
0g0grzrc

0g0grzrc1#

通过在Google上搜索“pairwise distance numpy”,我可以找到很多方法。
例如,this one使用NumPy广播:

dist = a[:, None, :] - a[None, ...]
dist = np.abs(dist).sum(axis=-1)

其中a是图像数据集数组
还有其他使用外部库的方法,如:

sklearn.metrics.pairwise_distances
scipy.spatial.distance.pdist + squareform
...

相关问题