Why do I have different results?

1 view (last 30 days)
Chidiebere Ike
Chidiebere Ike on 17 Nov 2018
Commented: Image Analyst on 17 Nov 2018
Hello.
I tried to estimate MSE and RMSE error between orignal image and my reconstructed images using the two codes below. I do observe I have different results of MSE and RMSE values for both codes when run separately. I don't know why. Both codes uses the same Original and reconstructed images (Gray scale images). Can anyone explain this to me ?? Also find attached function file used with code 1. Thanks
%% Code 1 (Obtained MSE = 4.635 and RMSE = 2.153 )
% Read original and reconstructed image
data = imread('baby_GT[1-Original].bmp');
subplot(2, 2, 1);
imshow(data, []);
title('Original Gray Image');
estimate = imread('baby_GT[8-Our Method].bmp');
subplot(2, 2, 2);
imshow(estimate, []);
title('Reconstructed Gray Image');
% To compute MSE and RMSE
[mse, rmse] = RMSE(data,estimate);
% Alert user of the answer.
message = sprintf('The MSE = %.3f.\nThe RMSE = %.3f.', mse, rmse);
msgbox(message);%
%% Code 2 (Obtained MSE = 10.03 and RMSE = 3.17 )
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('baby_GT[1-Original].bmp');
[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.
% Read in a reconstructed gray scale demo image.
reconImage = imread('baby_GT[8-Our Method].bmp');
[rows columns] = size(reconImage);
% Display the second image.
subplot(2, 2, 2);
imshow(reconImage, []);
title('Reconstructed 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(reconImage)) .^ 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);
% To get the Root Mean Squared Error. It will be a floating numbers.
rmse = sqrt(mse);
% 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 MSE = %.2f.\nThe RMSE = %.2f.\nThe PSNR = %.2f', mse, rmse, PSNR);
msgbox(message);

Accepted Answer

Miriam
Miriam on 17 Nov 2018
Edited: Miriam on 17 Nov 2018
You should convert data and estimate using double() as well. The RMSE function also contains a squared error calculation where any negative differences will be clipped to zero. Try:
[mse, rmse] = RMSE(double(data),double(estimate));
for code 1.

More Answers (1)

Image Analyst
Image Analyst on 17 Nov 2018
The R in RMSE means root, or square root. Check out this line of code
rmse = sqrt(mse);
so, now, you say "I do observe I have different results in MSE and RMSE values. I don't know why." Did you overlook the line of code above?
Attach 'baby_GT[1-Original].bmp' and 'baby_GT[8-Our Method].bmp' and the RMSE() function, if you want more help so we can run your code.
  4 Comments
Chidiebere Ike
Chidiebere Ike on 17 Nov 2018
Noted, Thanks. But does the same code (code you wrote) applicable to color image as well ? I used the code only on Grayscale images
Image Analyst
Image Analyst on 17 Nov 2018
It looks like you'd need to change this
mse = sum(sum(squaredErrorImage)) / (rows * columns);
to this
mse = sum(sum(squaredErrorImage)) / numel(squaredErrorImage);
but I'd really recommend you use the built-in functions psnr() and immse().

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!