Skip to content

Instantly share code, notes, and snippets.

@YoshiRi
Last active April 18, 2018 11:01
Show Gist options
  • Save YoshiRi/7d97a80e1169ba5c41dc37a867dc622c to your computer and use it in GitHub Desktop.
Save YoshiRi/7d97a80e1169ba5c41dc37a867dc622c to your computer and use it in GitHub Desktop.
Evaluation function for Image Matching SSD, NCC, ZNCC,
%% SSD %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function e = SSD(img1,img2)
if(~(size(img1)==size(img2)))
error('IMG size is differnt!');
end
[height,width] = size(img1);
Total = height * width;
%% Substractg image one by one
MX = double(img1) - double(img2);
%% Adding up squared errors with matrix form
VX = reshape(MX,[1 numberofelements(MX)]);
VY = VX.';
e = VX * VY ;
end
% Normalized correlation
%% NCC %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function e = NCC(img1,img2)
img1 = double(img1);
img2 = double(img2);
if(~(size(img1)==size(img2)))
error('IMG size is differnt!');
end
%% Take correlation
V1 = reshape(img1,[1 numel(img1)]) ;
V2 = reshape(img2,[1 numel(img2)]) ;
% Normalization
Nolm =sqrt( V1* V1.' + V2 * V2.' );
e = (V1 * V2.') / Nolm;
end
%% ZNCC %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function e = ZNCC(img1,img2)
img1 = double(img1);
img2 = double(img2);
if(~(size(img1)==size(img2)))
error('IMG size is differnt!');
end
%% Get Correlation
% Get mean intensity
avg1 = mean2(img1);
avg2 = mean2(img2);
% Calculate correlation
V1 = reshape(img1,[1 numel(img1)]) - avg1;
V2 = reshape(img2,[1 numel(img2)]) - avg2;
Nolm =sqrt( V1* V1.' + V2 * V2.' );
e = (V1 * V2.') / Nolm;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment