# MSE Mean Square Error

285 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

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.

grayImage = imread('cameraman.tif');

[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);

% Alert user of the answer.

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

msgbox(message);

##### 9 Comments

DGM
on 12 Feb 2023

Edited: DGM
on 12 Feb 2023

From the imnoise() synopsis:

J = imnoise(I,'salt & pepper',D) adds "salt and pepper" noise to the

image I, where D is the noise density. This affects approximately

D*numel(I) pixels. The default for D is 0.05.

So D can obviously be anything from 0 to 1. Values outside that range will result in an error message.

Also from the synopsis:

The mean and variance parameters for 'gaussian', 'localvar', and

'speckle' noise types are always specified as if for a double image

in the range [0, 1].

So M can be anything real, and V can be anything real and positive. Given the fact that we're working in unit-scale though, it wouldn't make sense for V to be very large or for the magnitude of M to be far from zero.

### More Answers (2)

ashkan abbasi
on 11 Apr 2014

% MSE & PSNR for a grayscale image (cameraman.tif) & its filtered

% version

clear

clc

im=imread('cameraman.tif');

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);

##### 3 Comments

Desmond Michael
on 10 Feb 2016

Edited: Rik
on 2 Feb 2022

##### 6 Comments

DGM
on 12 Feb 2023

### See Also

### Categories

### Products

### Community Treasure Hunt

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

Start Hunting!