user1750948
user1750948

Reputation: 759

how to find the correlation between two images

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.

Upvotes: 8

Views: 36975

Answers (2)

Isaac Berrios
Isaac Berrios

Reputation: 165

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 256x256 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)

Upvotes: 3

lxop
lxop

Reputation: 8595

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.

Upvotes: 11

Related Questions