MATLAB corr2() Function

Ammar Ali Jun 27, 2022
MATLAB corr2() Function

This tutorial will discuss finding the correlation between two images using the corr2() function in MATLAB.

MATLAB corr2() Function

The corr2() function of MATLAB is used to find the correlation between two images or arrays. The correlation gives information about the similarity between two images.

If the correlation is 1, the two images are identical, and if the value is less than or equal to zero, the two images are opposite. Mostly the corr2() function will return a floating-point value between 0 to 1, and this value shows the similarity between the two images.

If the corr2() function returns 0.5, the two images have 50 percent similarity.

The basic syntax of the corr2() function is given below:

correlation_value = corr2(Image_1, Image_2)

The above syntax will return a numeric value or the correlation coefficient, showing the correlation between the two input images or arrays. Inputs of the corr2() function should be 2D arrays or matrices and have the same size.

For example, let’s find the correlation between two images using the corr2() function in MATLAB. See the code below.

clc
clear

Image = imread('pout.tif');
Filtered_image = medfilt2(Image);
Correlation = corr2(Image,Filtered_image)

Output:

Correlation =

    0.9959

In the above code, we used the imread() function to read an image, and then we used the medfilt2() function to apply the median filter to the image. We used the corr2() function to find the correlation between the input image and its filtered version, and the result is shown in the output.

The above code shows that the two images are 99 percent similar. If we find the correlation between two same images, the corr2() function will return 1, meaning the two images are 100 percent similar.

Now, let’s talk about the algorithm used to find the correlation between two images. The algorithm compares the intensity of the pixels present in the two images to find the correlation.

The algorithm will start from the first pixel in both images and find their difference, continuing until the final pixel. Then it takes the mean of all the differences in the pixel intensities to find the correlation.

The pixels being compared have the same position in the image, which means the first pixel of the first image will only be compared to the first pixel of the second image and so on.

The below image shows the formula that is used to find the correlation between the two images:

Correlation Formula

In the above formula, m and n represents the rows and columns; because an image is like a 2D matrix, we have to use the rows and columns to get each pixel. The characters with bars represent the mean of the image matrix.

In MATLAB, we can write this formula using the sum() and mean2() functions. See the code below.

Image_a = Image_a - mean2(Image_a);
Image_b = Image_b - mean2(Image_b);
correlation = sum(sum(Image_a.*Image_b))/sqrt(sum(sum(Image_a.*Image_a))*sum(sum(Image_b.*Image_b)))

The mean2() function is used to find the mean of the whole image matrix along with columns and rows. The mean2() function is equal to mean(mean()) function.

If we use a single mean() function, it will find the mean of each column and will return a vector containing the mean values of each column present in a matrix. So, we have to use the mean() function again to find a single mean value for the whole image matrix.

The sum() function is used to find the summation. It is also used twice because a single sum() function only calculates the sum in each column, but we want to find the sum of the entire matrix.

The sqrt() function is used to find the square root of the given value.

We can also use the above formula instead of the corr2() function to find the correlation between two images or arrays. For example, let’s use the above formula to find the correlation between two images.

See the code below.

clc
clear

Image_a = imread('pout.tif');
Image_b = medfilt2(Image_a);
Image_a = Image_a - mean2(Image_a);
Image_b = Image_b - mean2(Image_b);
correlation = sum(sum(Image_a.*Image_b))/sqrt(sum(sum(Image_a.*Image_a))*sum(sum(Image_b.*Image_b)))

Output:

correlation =

    0.9983

We can see in the output that the correlation is approximately equal to the correlation returned by the corr2() function. We should use the corr2() function because this checks input file formats, size, and other errors.

If one image is rotated or translated, we can not use the corr2() function to compare two images because the pixel location in the rotated image will be changed. As discussed above, the corr2() function compares pixels at the same location in both images; we cannot use it to find the correlation between two images whose pixel locations have been changed.

For example, let’s find the correlation between an image and its rotated version using the corr2() function. See the code below.

clc
clear

Image = imread('pout.tif');
flipped_image = flip(Image);
Correlation = corr2(Image,flipped_image)

Output:

Correlation =

   -0.1047

In the above code, we used the flip() function to flip the given image. We can see in the above output that the correlation shows that the two input images do not have any similarities. Still, we know that both images are the same; the only difference is that the second image is flipped.

So, the corr2() function cannot be used to compare two images in which the pixel locations have been changed. In this case, other methods exist to compare two images, like using artificial intelligence.

Check this link for more details about the corr2() function.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Image