Sunday, January 26, 2014

PSNR of two gray scale images in Matlab

This post is about finding the PSNR of two greyscale images in matlab. The program will show the two images and will display result in an alert box.
inImage = imread('rose_in.bmp');
[rows columns] = size(inImage) ;
subplot(2, 2, 1) ;
imshow(inImage, []);
title('Input image');
set(gcf, 'Position', get(0,'Screensize'));

outImage = imread('rose_out.bmp') ;
[rowsO columnO] = size(outImage) ;
subplot(2, 2, 2);
imshow(outImage, []);
title('Output Image');


squaredErrorImage = (double(inImage) - double(outImage)) .^ 2;
%subplot(2, 2, 3);
%imshow(squaredErrorImage, []);
%title('Squared Error Image');

mse = sum(sum(squaredErrorImage)) / (rows * columns);

PSNR = 10 * log10( 256^2 / mse);

message = sprintf('The mean square error is %.2f.\nThe PSNR = %.2f', mse, PSNR);
msgbox(message);