Analyze image similarity

Question:

I am trying to check the similarity of n images I saved in an array (composed of 1797 samples, each of which is represented by 64 bits). My goal is to save the L1 distance of each pair of images in an array of size 1797×1797 (note that the method to compute L1 is written below). t the moment I am using the following method to compare them but it seems a bit slow and "non-pythonic", is there a way to do the same with numpy or similar libraries, which may end up in an upgrade in terms of performances?

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))
Asked By: Shark44

||

Answers:

By searching "pairwise distance numpy" on Google, I can find quite a few ways.

For example, this one uses NumPy broadcasting:

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

where a is the image dataset array

There are also other methods that use external libraries like:

sklearn.metrics.pairwise_distances
scipy.spatial.distance.pdist + squareform
...
Answered By: seermer
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.