SKM
SKM

Reputation: 989

Matlab : How to check if two images are similar to each other

I have coloured images of size 20 by 20. The objective is that :

based on a query, I need to check which recalled image is the closest match to the query. For example, let 10 images be the recalled ones. Out of the 10 recalled images, I need to find the closest match to the query.

I was thinking of using the correlation between the images. One can use the Correlation coefficient - higher the value, more is the correlation between pixel values. (Please correct me where wrong).

R = corr2(A,B1) will compute the correlation coefficient between A and B where A is the query, B1 is the first recalled image image of the same size. I used the above command for colored images but I got the result R = NaN. How do I solve this problem for colored and gray scale. Thank you.

This is the QUERY IMAGE Query Image

The other image(recalled / retrieved B1) Recal

UPDATE : Code for testing correlation of an image with itself from a databasae called patches.mat (patches is the database. It consists of 59500 20x20 patches, reshaped into 400-dimensional column vectors, taken from a bunch of motorcycle images; I am using the 50 th example as the query image)

img_query = imagesc(reshape(patches(:,50),20,20));
colormap gray;axis image;

R = corr2(img_query,img_query)

Answer = NaN

Upvotes: 0

Views: 968

Answers (2)

Brahim Hamadicharef
Brahim Hamadicharef

Reputation: 36

I would try also the Universal Quality Index by Wang which gives you a 1.0 if both images are equal, and less in other cases with spatial indication where they differ, think of one image as reference, the other as a degraded one relative to the first one. See http://www.cns.nyu.edu/~zwang/files/research/quality_index/demo.html

Upvotes: 0

NKN
NKN

Reputation: 6424

That is because one of the images is black or includes a single color, meaning that all the values of the matrix are similar. Check the following examples:

I = imread('pout.tif');
J = I*0;                     % create a black image
R = corr2(I,J);

R = 
   NaN


I = imread('pout.tif');
J = 255*ones(size(I));         % create a white image
R = corr2(I,J);

R = 
   NaN

Update

It should work in your case, as you can see in the following example, it works perfectly:

I1 = abs(255*(rand(10,10));
I2 = abs(255*(rand(10,10));
corr2(I1,I2)

ans =

   0.0713

enter image description here

Even with the images you have shared, it is working for me. To find out your problem, you have to either share a part of your code, or post images as they are, not saved images (with a size 420x560x3).

Note: you cannot have images including more than 1 layer.


Your code shows that you are using the handle of the image instead of the image itself.


Test this:

I = reshape(patches(:,50),20,20);
corr2(I,I)

Upvotes: 3

Related Questions