# MSE Mean Square Error

269 views (last 30 days)
Soum on 3 Jul 2013
Commented: Image Analyst on 27 Mar 2019
I De-noise some images and I want to evaluate them so I calculate SNR but I want to use another like Mean Square Error (MSE) I saw some people use it but I don't know what is express in my case I have a noisy image like input and De-noised one in the out put Or maybe PSNR please help me

#### 1 Comment

Kithma Gamage on 12 Apr 2018
I want to calculate the mean square error of the desired(ideal) QMF filter and the designed QMF filter using matlab in order to optimize the filter using ABC and PSO algorithms.Can someone help me with the code please???

Image Analyst on 3 Jul 2013
See my demo:
% Demo to calculate PSNR of a gray scale image.
% http://en.wikipedia.org/wiki/PSNR
% Clean up.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%------ GET DEMO IMAGES ----------------------------------------------------------
% Read in a standard MATLAB gray scale demo image.
[rows columns] = size(grayImage);
% Display the first image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Gray Scale Image', 'FontSize', fontSize);
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', 'FontSize', fontSize);
%------ 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', 'FontSize', fontSize);
% 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);
message = sprintf('The mean square error is %.2f.\nThe PSNR = %.2f', mse, PSNR);
msgbox(message);

Rasheed Khankan on 14 Mar 2016
I think that the maximum value is 255 not 256.
JDC on 2 Oct 2017
That may well be. But he's referring to the dimensions of the image, not the pixel value.
hayat ali on 21 Feb 2019
thank yuo for your cooperation. but I have question
what are the range values that can be used in D, M,and V in both salt & pepper and gaussian
J = imnoise(I,'salt & pepper', D);
figure, imshow(I), figure, imshow(J)
J = imnoise(I,'gaussian ',M,V)

ashkan abbasi on 11 Apr 2014
% MSE & PSNR for a grayscale image (cameraman.tif) & its filtered
% version
clear
clc
im=im2double(im);
h1=1/9*ones(3,3);
imf1=imfilter(im,h1,'replicate');
h2=1/25*ones(5,5);
imf2=imfilter(im,h2,'replicate');
%
MSE1=mean(mean((im-imf1).^2));
MSE2=mean(mean((im-imf2).^2));
MaxI=1;% the maximum possible pixel value of the images.
PSNR1=10*log10((MaxI^2)/MSE1);
PSNR2=10*log10((MaxI^2)/MSE2);

Rasheed Khankan on 14 Mar 2016
Great, concise, and operative code... Thanks a lot.
no ur on 30 Dec 2018
PSNR and MSE according to bitrate????
Image Analyst on 30 Dec 2018
no - I have no idea what you're intending. Is this a question, or are you're just surprised at something unexpected that was said? Please ask a fully grammatically complete question if you have one. Otherwise I'm not sure people will respond to you in a way that is useful to you and give you exactly what you want.

jensi asir on 18 Jan 2014
im also getting the same message which show 3 times psnr values ? whats the wrong in it.can you please help me

#### 1 Comment

Image Analyst on 18 Jan 2014
You probably changed my demo to use a color image of your own.

iza on 20 Jan 2015
Can anyone check mine? Is it correct?...
% addition of graininess (i.e. noise) I_noise = imnoise(I, 'gaussian', 0.09);
% the average of 3^2, or 9 values(filters the multidimensional array A with the multidimensional filter h) h = ones(3,3) / 3^2; I2 = imfilter(I_noise,h);
% Measure signal-to-noise ratio img=I; img=double(img(:)); ima=max(img(:)); imi=min(img(:)); mse=std(img(:)); snr=20*log10((ima-imi)./mse)
% Measure Peak SNR [peaksnr, snr] = psnr(I_noise, I); fprintf('\n The Peak-SNR value is %0.4f', peaksnr); fprintf('\n The SNR value is %0.4f \n', snr); fprintf('\n The MSE value is %0.4f \n', mse);
%Plot original & filtered figure
subplot(1,2,1), imshow(I_noise), title('Original image')
subplot(1,2,2), imshow(I2), title('Filtered image')
text(size(I,2),size(I,1)+15, ...
'Gaussian = 0.09', ...
'FontSize',10,'HorizontalAlignment','right');

Desmond Michael on 10 Feb 2016
Hello everyone, I've found a website regarding the above and its very helpful. http://vaaiibhav.me/calculating-the-psnr-and-mse-code-matlab/

Image Analyst on 13 Feb 2016
immse() was introduced in R2014b and psnr() was introduced in R2014a. See my attached demo where I do it without toolbox functions, and as given in my Answer way up at the top.
indrani dalui on 26 Mar 2019
This code are also calculate the PSNR for color image ?
Image Analyst on 27 Mar 2019
You have to decide what you want when you think of PSNR for a color image. Maybe you want the average PSNR of each color channel.