Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image comparison using Matlab

Tags:

matlab

I want to compare two images in Matlab (I learned that Matlab has more features for comparing images and processing them). Can anyone suggest a good and simple method for doing the same? And the image needs to be exactly same. So the brightness and position of the image need not be considered.

I need to complete my project in two months so I would be glad if any one helps me with a good algorithm or method.

like image 231
Aravind Avatar asked May 21 '26 09:05

Aravind


1 Answers

You can use the EMD algorithm. It works on histograms. Here is some code that may help:

function[d] = hcompare_EMD(h1,h2)
% This function calculates Earth Movers Distance between two normalized
% histograms h1 and h2. Normalized histogram is histogram h, that has at
% each i place in it, value:
% (number of picture pixels with gray level i-1) / 
% (total num of pixels in picture).


% ALternative fast way:
d = sum(abs(cumsum(h1) - cumsum(h2)));
end

and the histograms for the two images are calculated this way:

function[h] = histImage(img)
% This function calculates normalized histogram of image.
% Normalized histogram is histogram h, that has at
% each i place in it, value:
% (number of picture pixels with gray level i-1) / 
% (total num of pixels in picture).
sum = 0;
[y,x] = size(img); % getting sizes of image  
h = zeros(1,256);    % creating output histogram array
for i = 1:1: y     % runing on rows
    for j = 1:1: x % running on colomns
        % gray level is addtess to cell in output histogram array
        % we add there 1 (bacause of current pixel (y,x) has this gray level
        h(img(i,j)) = h(img(i,j)) + 1;
        % pay attention to fact, that we use here pixel value as index!
    end
end

h = h./(y*x);
end

to calculate the distance between the histograms of the two images (histArray, histPattern):

function[dmap] = patDistMAp(histArray, histPattern)
% Given histograms of an image and pattern returns an array (image) 
% of distance values between
% img windows and pattern. Distance values are computed between the histograms
% of the windows and the pattern using the histogram distance function

[y,x,z] = size(histArray);
dmap = zeros(y,x);                  % output array

for i = 1:1: y       % runing on rows
    for j = 1:1: x   % running on colomns
        hist = histArray(i,j,:);
%         for k = 1:1:256             % making array 1x256 from 1x1x256
%             h1(k) = hist(1,1,k);    % there is a permute function,
%         end                         % but we will use it next time)
        h1 = permute(squeeze(hist),[2,1]);
        % Using temp variable, as MATLAB7 wants it:
        temp = hcompare_EMD(histPattern,h1);
        dmap(i,j) = temp;
    end
end

end
like image 133
Mr.Queries Avatar answered May 23 '26 00:05

Mr.Queries



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!