how to find the correlation between two images

Question:

I need some help in trying to figure out something. I currently a python script which generates two images using the imshow method in matplotlib. My task is to find the correlation between these two images, or in other words the similarity between the two images. Both images are the same size and both use the jet colormap.
Let me know if this is clear enough or if i need to explain in more detail. It would be helpful if someone could provide an example code of how to do this.

Asked By: user1750948

||

Answers:

Have you looked at the scipy signal processing kit?

from scipy import signal
cor = signal.correlate2d (im1, im2)

will calculate the 2D correlation for you.

Answered By: lxop

Another way to find the correlation of 2 images is to use filter2D from opencv. In the filter2D function, you can pass one of the images as the InputArray (or "src") and the other as the kernel. This will give you the correlation, and it is fast. Using the signal.correlate2d from scipy took about 18 seconds for a 256×256 image. Using filter2D took about 0.008 seconds for the same image.

import cv2

corr = cv2.filter2D(image1, ddepth=-1, kernel=image2)

I would also recommend passing in float images instead of uint8 images, since using uint8 images can lead to inconvenient round off errors.

# convert to float32
image1_norm = cv2.normalize(image1, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
Answered By: Isaac Berrios
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.