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

 採用された回答

Image Analyst
Image Analyst 2013 年 7 月 3 日

2 投票

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 件のコメント

Soum
Soum 2013 年 7 月 4 日
編集済み: Soum 2013 年 7 月 4 日
Thanks Image Analyst for your answer
Just a question in my case I've a noisy Image 'already noisy' without adding noise like in input and in the out put I've a De-noised image not like your case Image---->Noisy Image
anyway my Question is :if I calculate the SNR and PSNR between the noisy image and De-noised image
squaredErrorImage = (double(NoisyImage) - double(De-noisedImage)) .^ 2;
Thanks in advanced :)
rashed
rashed 2014 年 1 月 14 日
Sir, Please see the attached image. I copy and paste the code and found the result. I do not know what they are meaning. I am developing a steganography apps and for this analysis part i have to calculate the MSE and PSNR of the stego image and original image. Please help me out of this confussion sir. Thanks
Image Analyst
Image Analyst 2014 年 1 月 14 日
That message box could not have been generated by my code if you look at my code. I only state the values once, not 3 times. Why don't you start a new discussion and post both your m-file and your image and we'll see how we can fix this?
rashed
rashed 2014 年 1 月 15 日
Sir I have told you that i only copy and paste this code but my result shows me like this. That's why i upload the message box why it show 3 times. Thanks
Image Analyst
Image Analyst 2014 年 1 月 18 日
Somehow your cameraman.tif must have turned into a color image. The version of it that ships with MATLAB is definitely a grayscale image. What does this say:
[rows, columns, numberOfColorChannels] = size(grayImage)
It should say 256, 256, 1. If the third number is 3 then either you changed my demo to use a color image (most likely) or else somehow your cameraman.tif image is not the original one.
Rasheed Khankan
Rasheed Khankan 2016 年 3 月 14 日
I think that the maximum value is 255 not 256.
JDC
JDC 2017 年 10 月 2 日
That may well be. But he's referring to the dimensions of the image, not the pixel value.
hayat ali
hayat ali 2019 年 2 月 21 日
編集済み: DGM 2023 年 2 月 12 日
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
I = imread('eight.tif');
J = imnoise(I,'salt & pepper',D);
K = imnoise(I,'gaussian ',M,V);
DGM
DGM 2023 年 2 月 12 日
編集済み: DGM 2023 年 2 月 12 日
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.

サインインしてコメントする。

その他の回答 (2 件)

ashkan abbasi
ashkan abbasi 2014 年 4 月 11 日

2 投票

% 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 件のコメント

ameena begam
ameena begam 2015 年 6 月 10 日
hai,,ashkan my question is why u r using mean in MSE ,,why not using SUM function,,,thankyou,,please tell me
Image Analyst
Image Analyst 2015 年 6 月 10 日
The M in MSE means "Mean". He should use immse() and psnr(), the built in functions, though, if he has a recent enough version of MATLAB.
Rasheed Khankan
Rasheed Khankan 2016 年 3 月 14 日
Great, concise, and operative code... Thanks a lot.

サインインしてコメントする。

Desmond Michael
Desmond Michael 2016 年 2 月 10 日
編集済み: Rik 2022 年 2 月 2 日

0 投票

Hello everyone, I've found a website regarding the above and its very helpful. http://vaaiibhav.me/calculating-the-psnr-and-mse-code-matlab/
Edit @Rik:
The link seems to have gone down. Here is a capture of that page from 2019.

6 件のコメント

Image Analyst
Image Analyst 2016 年 2 月 10 日
I don't see anything there at that web site - no zip file or download link like it says. Anyway, since my answer above, MATLAB has added built-in functions immse() and psnr() to make it easy for you.
Shaveta Arora
Shaveta Arora 2016 年 2 月 13 日
編集済み: Image Analyst 2016 年 2 月 13 日
Why is my Matlab is displaying
immse not found
and also psnr() is not there. Which versions support these built in functions?
Image Analyst
Image Analyst 2016 年 2 月 13 日
編集済み: Image Analyst 2016 年 2 月 13 日
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
indrani dalui 2019 年 3 月 26 日
This code are also calculate the PSNR for color image ?
Image Analyst
Image Analyst 2019 年 3 月 27 日
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.
DGM
DGM 2023 年 2 月 12 日
Note that if you have a version newer than R2014x and you don't have psnr() or immse(), bear in mind that both are still part of the Image Processing Toolbox, so you'll also need that.

サインインしてコメントする。

タグ

質問済み:

2013 年 7 月 3 日

編集済み:

DGM
2023 年 2 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by