Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How to calculate PSNR of compressed images, and how to compare PSNR of images compressed by two different techniques.

Asked by akanksha sharma on 4 Oct 2012
Latest activity Commented on by Image Analyst on 12 Oct 2013

I have to compare image compression techniques like VQ, JPEG, WAVELET, and fractal. For this, the parameter to be compared is PSNR. Please tell me how I can calculate PSNR OF AN IMAGE which is COMPRESSED by different compression techniques. plz explain with example.


akanksha sharma

1 Answer

Answer by Image Analyst on 4 Oct 2012
Edited by Image Analyst on 4 Oct 2012

See my demo:

% Demo to calculate PSNR of a gray scale image.
% Clean up.
close all; 
clear all; 
%------ GET DEMO IMAGES ----------------------------------------------------------
% Read in a standard MATLAB gray scale demo image.
grayImage = imread('cameraman.tif');
[rows columns] = size(grayImage);
% Display the first image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Gray Scale Image');
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Get a second image by adding noise to the first image.
noisyImage = imnoise(grayImage, 'gaussian', 0, 0.003);
% Display the second image.
subplot(2, 2, 2);
imshow(noisyImage, []);
title('Noisy Image');
%------ PSNR CALCULATION ----------------------------------------------------------
% Now we have our two images and we can calculate the PSNR.
% First, calculate the "square error" image.
% Make sure they're cast to floating point so that we can get negative differences.
% Otherwise two uint8's that should subtract to give a negative number
% would get clipped to zero and not be negative.
squaredErrorImage = (double(grayImage) - double(noisyImage)) .^ 2;
% Display the squared error image.
subplot(2, 2, 3);
imshow(squaredErrorImage, []);
title('Squared Error Image');
% Sum the Squared Image and divide by the number of elements
% to get the Mean Squared Error.  It will be a scalar (a single number).
mse = sum(sum(squaredErrorImage)) / (rows * columns);
% Calculate PSNR (Peak Signal to Noise Ratio) from the MSE according to the formula.
PSNR = 10 * log10( 256^2 / mse);
% Alert user of the answer.
message = sprintf('The mean square error is %.2f.\nThe PSNR = %.2f', mse, PSNR);


rashmi on 6 Oct 2013

PSNR = 10 * log10( 256^2 / mse); WHY YOU USE 256^2 //// IF OUR IMAGE SIZE IS 225X225 .... THAN ALSO WE USE 256^2?

Image Analyst on 6 Oct 2013

See the definition. It's 256 because that's the maximum gray level. It doesn't have anything to do with what size your image is. Why do you think it does? The image size comes in when you calculate MSE because you have to get the mean, but after that it's not used.

Image Analyst on 12 Oct 2013

Akanksha, please read Cris's blog on PSNR: If we're done here, please mark my answer as Accepted.

Image Analyst

Contact us