How do I calculate PNSR of an Image
Show older comments
Hello,
I have an image named "Foreman". I wish to estimate it's PSNR initial value in dB.
How do I achieve this ?
Thank you
1 Comment
Answers (2)
Shreeya
on 16 Jan 2024
For an m*n image "I" and it's noisy approximation "J" of 8-bit unsigned integer data type, the PSNR can be calculated using the following code implementation:
R = 255;
mse = mean((I-J).^2, "all");
if (mse ~= 0)
psnr = 20*log10(R/sqrt(mse));
end
Apart from the editor, these computations can also be performed in Simulink through the PSNR block. You can refer to the link below to understand more about it:
1 Comment
Even for uint8, this will be wrong unless both inputs were already mis-cast into a wider numeric class; otherwise truncation occurs in taking the MSE.
To avoid the whole problem, don't presume the input class. Expect only that it is properly-scaled for whatever class it has. Cast both images to floating point to take the MSE. Calculate the peak value based on whatever the class of the incoming images is.
% some test images
I = imread('cameraman.tif');
I = int16(I); % it's not uint8 anymore
J = imnoise(I,'gaussian',0.1);
% test it
R = diff(getrangefromclass(I));
mse = mean((double(I)-double(J)).^2, "all");
if (mse ~= 0)
P1 = 20*log10(R/sqrt(mse))
end
% compare
P2 = psnr(J,I)
This makes the same assumptions that IPT psnr() makes. I and J must both have the same class and must be properly-scaled for their class. The supported classes are only those handled by IPT getrangefromclass(), so it's still not totally class-agnostic, but neither is psnr().
help psnr
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!