Is this Function to Calculate the MSE and the SNR Correct?
14 views (last 30 days)
Show older comments
Hello.
I've been asked to create a Matlab function that calculates the MSE and the SNR for an assignment. I think I have the code right, but would like an expert to take a look.
The equation for MSE that I have been given is:
The equation for SNR that I have been given is:
This is my code:
%Creating the Matlab Function for MSE
function [mse snrdB] = snr2d(noisy_image, reference_image);
%Converting Reference and Noisy Images into Doubles
noisy_image_double = double(noisy_image);
reference_image_double = double(reference_image);
[rows, columns] = size(reference_image);
%Incorporating the Formula
SquaredError = (noisy_image_double - reference_image_double) .^2;
mse = sum(sum(squaredError)) / (rows * columns);
snrdB = 10*log10((sum(sum(reference_image_double .^2))) / (sum(sum(squaredError))));
message = sprintf('The mean square error is ',mse,' and the SNR is ',snrdB);
msgbox(message);
end
Is this correct? Also, I am a little confused on how to "call" this function when needed and if there is a special way to save it - we will apparently call this function for future assignments. My apologies for any basic errors - I am learning Matlab using Google, the MatLab Help documentation, and other online resources.
Thanks. Steve.
0 Comments
Answers (1)
Uttiya Ghosh
on 19 Jun 2020
Hi Whatchamacallit,
From my understanding you want to know the correctness of the function you have written. You also want to know the procedure to call a function and save it.
Refer to the below code to understand the procedure to call a function.
reference_image=imread("cameraman.tif");
noisy_image=imnoise(reference_image,'gaussian');
[mse, snrdB] = snr2d(noisy_image, reference_image);
message = sprintf('The mean square error is %0.2f and the SNR is %0.2f',mse,snrdB);
msgbox(message);
You can save the function in a snr2d.m file to be used in the future. MATLAB offers built in function to calculate the mse of an image. You can compare your answers with it to know the correctness of your output. Refer to the link below.
0 Comments
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!