Main Content

psnr

Peak signal-to-noise ratio (PSNR)

Description

peaksnr = psnr(A,ref) calculates the peak signal-to-noise ratio (PSNR) for the image A, with the image ref as the reference. A greater PSNR value indicates better image quality.

example

peaksnr = psnr(A,ref,peakval) calculates the PSNR of image A using the peak signal value peakval.

peaksnr = psnr(___,'DataFormat',dataFormat) also specifies the dimension labels, dataFormat, of unformatted image data. Use this syntax to return a separate PSNR for each element along a batch dimension.

example

[peaksnr,snr] = psnr(___) also returns the simple signal-to-noise ratio, snr.

example

Examples

collapse all

Read image and create a copy with added noise. The original image is the reference image.

ref = imread('pout.tif');
A = imnoise(ref,'salt & pepper', 0.02);

Calculate the PSNR.

[peaksnr, snr] = psnr(A, ref);
  
fprintf('\n The Peak-SNR value is %0.4f', peaksnr);
 The Peak-SNR value is 22.6437
fprintf('\n The SNR value is %0.4f \n', snr);
 The SNR value is 15.5524 

Read an image into the workspace, then create an unformatted dlarray object with the image data.

ref = imread("strawberries.jpg");
ref = im2single(ref);
dlref = dlarray(ref);

Add salt and pepper noise to the image, then create an unformatted dlarray object with the noisy image data.

noisy = imnoise(ref,'salt & pepper');
dlnoisy = dlarray(noisy);

Calculate the peak SNR and SNR of the noisy data with respect to the original data.

[peaksnr,snr] = psnr(dlnoisy,dlref)
peaksnr = 
  1x1 single dlarray

   17.5941

snr = 
  1x1 single dlarray

   11.1265

Read a reference image into the workspace.

ref = imread("office_1.jpg");

Preallocate two arrays that store a sequence of six images of the size of the reference image.

numFrames = 6;
imsOriginal = zeros([size(ref) numFrames],class(ref));
imsNoisy = zeros([size(ref) numFrames],class(ref));

Read and add images to the preallocated arrays. One array stores the original image data. The second array stores the image data with added salt and pepper noise.

for p = 1:numFrames
    filename = strcat("office_",num2str(p),".jpg");
    im = imread(filename);
    imsOriginal(:,:,:,p) = im;
    imsNoisy(:,:,:,p) = imnoise(im,"salt & pepper");
end

Display the image sequences in a montage. The first row shows the sequence with original image data. The second row shows the sequence with noisy image data.

montage(cat(4,imsOriginal,imsNoisy),"Size",[2 numFrames])

Figure contains an axes object. The hidden axes object contains an object of type image.

Calculate the PSNR of each noisy image with respect to the corresponding pristine image by specifying the data format of the input arrays as "SSCB" (spatial, spatial, channel, batch).

peak_psnrs = psnr(imsNoisy,imsOriginal,"DataFormat","SSCB");
peak_psnrs = squeeze(peak_psnrs)
peak_psnrs = 6×1

   16.3560
   16.9698
   17.8079
   18.1843
   18.0656
   17.1682

Input Arguments

collapse all

Image to be analyzed, specified as a numeric array of any dimension or a dlarray (Deep Learning Toolbox) object.

Data Types: single | double | int16 | uint8 | uint16

Reference image, specified as a numeric array or a dlarray (Deep Learning Toolbox) object. The reference image has the same size and data type as image A.

Data Types: single | double | int16 | uint8 | uint16

Peak signal level, specified as a nonnegative number. If not specified, the default value for peakval depends on the class of A and ref.

  • If the images are of data type double or single, then psnr assumes that image data is in the range [0, 1]. The default value of peakval is 1.

  • If the images are of integer data types, then the default value of peakval is the largest value allowed by the range of the class. For uint8 data, the default value of peakval is 255. For uint16 or int16, the default is 65535.

Dimension labels of the input images A and ref, specified as a string scalar or character vector. Each character in dataFormat must be one of these labels:

  • S — Spatial

  • C — Channel

  • B — Batch observations

The format cannot include more than one channel label or batch label. Do not specify the 'dataFormat' argument when the input images are formatted dlarray objects.

Example: 'SSC' indicates that the array has two spatial dimensions and one channel dimension, appropriate for 2-D RGB image data.

Example: 'SSCB' indicates that the array has two spatial dimensions, one channel dimension, and one batch dimension, appropriate for a sequence of 2-D RGB image data.

Output Arguments

collapse all

PSNR in decibels, returned as one of these values.

Input Image TypePSNR Value

  • Unformatted numeric arrays

  • Formatted numeric arrays without a batch ('B') dimension

Numeric scalar with a single PSNR measurement.
  • Unformatted dlarray (Deep Learning Toolbox) objects

1-by-1 dlarray object with a single PSNR measurement.
  • Numeric arrays with a batch dimension specified using the dataFormat argument

Numeric array of the same dimensionality as the input images. The spatial and channel dimensions of peaksnr are singleton dimensions. There is one PSNR measurement for each element along the batch dimension.
  • Formatted dlarray objects with a batch dimension

  • Unformatted dlarray objects with a batch dimension specified using the dataFormat argument

dlarray object of the same dimensionality as the input images. The spatial and channel dimensions of peaksnr are singleton dimensions. There is one PSNR measurement for each element along the batch dimension.

If A and ref have data type single, then peaksnr has data type single. Otherwise, peaksnr has data type double.

Signal-to-noise ratio in decibels, returned as one of these values.

Input Image TypePSNR Value

  • Unformatted numeric arrays

  • Formatted numeric arrays without a batch ('B') dimension

Numeric scalar with a single SNR measurement.
  • Unformatted dlarray (Deep Learning Toolbox) objects

1-by-1 dlarray object with a single SNR measurement.
  • Numeric arrays with a batch dimension specified using the dataFormat argument

Numeric array of the same dimensionality as the input images. The spatial and channel dimensions of snr are singleton dimensions. There is one SNR measurement for each element along the batch dimension.
  • Formatted dlarray objects with a batch dimension

  • Unformatted dlarray objects with a batch dimension specified using the dataFormat argument

dlarray object of the same dimensionality as the input images. The spatial and channel dimensions of peaksnr are singleton dimensions. There is one SNR measurement for each element along the batch dimension.

If A and ref have data type single, then snr has data type single. Otherwise, snr has data type double.

Algorithms

The psnr function implements this equation to calculate PSNR:

PSNR=10log10(peakval2/MSE)

peakval is either specified by the user or taken from the range of the image data type. For example, for an image of data type uint8, the peakval is 255. MSE is the mean square error between A and ref.

Extended Capabilities

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced in R2014a

expand all