How do I calculate PNSR of an Image

12 次查看(过去 30 天)
Chidiebere Ike
Chidiebere Ike 2018-11-7
评论: DGM 2024-1-16
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 个评论
Rik
Rik 2024-1-15
Have a read here and here. It will greatly improve your chances of getting an answer.
Did you Google how to read an image? And how to calculate the PSNR?

请先登录,再进行评论。

回答(2 个)

Shreeya
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
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
P1 = 16.9947
% compare
P2 = psnr(J,I)
P2 = 16.9947
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().

请先登录,再进行评论。


Image Analyst
Image Analyst 2024-1-16
There is a psnr function in the Image Processing Toolbox.
help psnr
PSNR Peak Signal-To-Noise Ratio. PEAKSNR = PSNR(A, REF) calculates the peak signal-to-noise ratio for the image in array A, with the image in array REF as the reference. A and REF can be N-D arrays, and must be of the same size and class. PEAKSNR = PSNR(A, REF, PEAKVAL) uses PEAKVAL as the peak signal value for calculating the peak signal-to-noise ratio. [PEAKSNR, SNR] = PSNR(A, REF, __) also returns the simple signal-to-noise in SNR, in addition to the peak signal-to-noise ratio. [___] = PSNR(___,Name,Value) accepts name value pairs to control aspects of computation. Supported options include: 'DataFormat' Dimension labels of the input data A and REF specified as a string scalar or character vector. The format options 'S','C', and 'B' are supported. The options 'S', 'C' and 'B' correspond to spatial, channel, and batch dimensions, respectively. For data with a batch or 'B' dimension, the output PEAKSNR and SNR will contain a separate result for each index along the batch dimension. As an example, input RGB data with two spatial dimensions and one channel dimension would have a 'SSC' DataFormat. Default: All input dimensions in A treated as spatial dimensions. Notes ----- 1. When dlarray input is labeled and contains a batch dimension, psnr yields a separate result for each element along the batch dimension. Class Support ------------- Input arrays A and REF must be one of the following classes: uint8, int16, uint16, single, or double. Both A and REF must be of the same class. They must be nonsparse. PEAKVAL is a scalar of any numeric class. PEAKSNR and SNR are scalars of class double, unless A and REF are of class single in which case PEAKSNR and SNR are scalars of class single. Example --------- % This example shows how to compute PSNR for noisy image given the % original reference image. ref = imread('pout.tif'); A = imnoise(ref,'salt & pepper', 0.02); [peaksnr, snr] = psnr(A, ref); fprintf('\n The Peak-SNR value is %0.4f', peaksnr); fprintf('\n The SNR value is %0.4f \n', snr); See also IMMSE, MEAN, MEDIAN, SSIM, SUM, VAR. Documentation for psnr doc psnr Other uses of psnr dlarray/psnr

标签

Community Treasure Hunt

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

Start Hunting!

Translated by