How to calculate MSE and PSNR in two different size image (block matching)?

3 views (last 30 days)
% This code is for template matching
clc; clear all; close all;
[filename, pathname, filterindex] = uigetfile( ...
{ '*.jpg','JPEG (*.jpg)'; ...
'*.bmp','Windows Bitmap (*.bmp)'; ...
'*.tif','TIFF (*.tif)';...
'*.fig','Figures (*.fig)'; ...
'*.*', 'All Files (*.*)'}, ...
'Choose image(s) to be processed', ...
'MultiSelect', 'off');
if filterindex==0, end
filename=cellstr(filename);
J= imread(horzcat(pathname,char(filename))); % read complete image
I= rgb2gray(J); % colour to gray conversion/ 3D to 2D
imshow(I)
[Ir Ic]=size(I); %size check
subplot(2, 2, 1);
imshow(I)
title('Complete Grayscale Image', 'FontSize', 10);
T= imread('Template1.jpg'); % read template of the image
[Tr Tc]= size(T);
subplot(2, 2, 2);
imshow(T)
title('Template Image', 'FontSize', 10);
R= normxcorr2(T,I) % Find normalize cross correlation
S = xcorr(T)
U=imcrop(R,[Tc Tr Ic Ir]); % crop the extra pixels
[r c y]= find(U==(max(max(R)))) %find the coordinates where maximum correlation exists
RGB = insertShape(I, 'rectangle', [c r Tc Tr], 'LineWidth', 3); % create a box around maximum match
subplot(2, 2, 3);
imshow(RGB)
title('Matching Image', 'FontSize', 10);
subplot(2, 2, 4);
surf(U), shading flat
title('Cross correlation in surface', 'FontSize', 10); % graphics processing unit (GPU)'
[row col] = size(I);
err = 0;
for i = 1:row
for j = 1:col
err = err + (I(i,j) - T(i,j))^2;
end
end
mse = err / (row*col);
psnr = 10*log10(n*n/mse);
I GOT THIS ERROR
Index in position 2 exceeds array bounds (must not exceed 112).
Error in Template_matching (line 75)
err = err + (I(i,j) - T(i,j))^2;
  4 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 23 Dec 2018
You don't want to do it that way since your template does not have as many rows as your larger image. What you want to do is to use your template as a kernel in a 2-D convolution. Untested code:
[rowsI, columnsI, numColorsI] = size(I);
[rowsT, columnsT, numColorsT] = size(T);
err = 0;
for j = 1 : (columnsI - columnsT)
for i = 1 : (rowsI - rowsT)
for c = 1 : columnsT
for r = 1 : rowsT
err = err + (I(i + r, j + c) - T(r, c))^2;
end
end
end
end
If you want, you could handle the bottom and right edge effects where you have a shrinking window. I just went up until the template bumped up against the edge of the window and quit.
  5 Comments
Image Analyst
Image Analyst on 25 Dec 2018
I wish you had just said that in the first place because I have a demo already made up for that. See Attached.
0000 Screenshot.png
LFM
LFM on 25 Dec 2018
Thank you but i see the demo is about normalized cross correlation. Now what i need to find is the error between the the template and the image when the template goes around to find the perfect match. In this case i thought MSE could be use. Am I wrong?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!