How do I calculate PNSR of an Image
12 次查看(过去 30 天)
显示 更早的评论
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
回答(2 个)
Shreeya
2024-1-16
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 个评论
DGM
2024-1-16
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().
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!