I'm trying to compare if two pictures are similar or close to similar. The picture is available as numpy.ndarray and contains of 28x28 pixels. It might look like the one below:

enter image description here

When I get the image as numpy.ndarray and calculate the corrcoef

image = data['test_dataset'][0]
matrix = np.corrcoef(image, image)

I was expecting a matrix full of 1's. Instead I get as print(matrix) something like:

[[ 1.          0.88450496  0.71112943 ...,  0.38349944  0.86398242
 [ 0.88450496  1.          0.85582039 ...,  0.60284577  0.99192916

Afterwards I wanted to create the .mean() of the matrix and if the value is higher than for example 0.98 the images are similar.

Can someone help me getting a matrix full of 1's instead of my strange looking output? Thanks in advance.


You cannot have a matrix full of 1's, only the diagonal will be (except if you have an image with identical rows).

From the numpy documentation of the first passed argument x : array_like A 1-D or 2-D array containing multiple variables and observations. Each row of x represents a variable, and each column a single observation of all those variables.

So in your result matrix, the coefficient appearing in position [0, 0] is the correlation of the first row of the image with itself (should be equal to 1, which is the case) The coefficient appearing in position [0, 1] is the correlation between the first row and the second row of the image and cannot be equal to 1 as these rows are clearly not identical. It should however be equal to the coefficient in position [1, 0] which is the case.

So the function seems to work well. You cannot calculate the correlation of one pixel with respect to one other pixel because it doesn't make sense. You need a time-series to have variations and correlations.

  • $\begingroup$ Thanks for the explanation! For the idea of comparing how similar are two pictures, do you have a hint, which technique I should use instead? Maybe a tutorial or something similar, because time-series is a wide field. $\endgroup$
    – So S
    Nov 22 '16 at 15:16
  • $\begingroup$ Mutual information maybe? It seems that the medpy library has a full module called metric.image. Sklearn.metric has also some metrics implemented. Good luck with this :) $\endgroup$
    – Eskapp
    Nov 22 '16 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.