Is this Function to Calculate the MSE and the SNR Correct?

16 vues (au cours des 30 derniers jours)
whatchamacallit
whatchamacallit le 26 Sep 2018
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.

Réponses (1)

Uttiya Ghosh
Uttiya Ghosh le 19 Juin 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.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by