psnr
Peak signal-to-noise ratio (PSNR)
Syntax
Description
also specifies the dimension labels, peaksnr
= psnr(___,'DataFormat',dataFormat
)dataFormat
, of unformatted
image data. Use this syntax to return a separate PSNR for each element along a batch
dimension.
Examples
Calculate PSNR for Noisy Image Given Original Image as Reference
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
Calculate PSNR for dlarray
Input
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
Calculate PSNR of Images in Image Sequence
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])
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
A
— Image to be analyzed
numeric array | dlarray
object
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
peakval
— Peak signal level
nonnegative number
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
orsingle
, thenpsnr
assumes that image data is in the range [0, 1]. The default value ofpeakval
is1
.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. Foruint8
data, the default value ofpeakval
is255
. Foruint16
orint16
, the default is65535
.
dataFormat
— Dimension labels
string scalar | character vector
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
— SpatialC
— ChannelB
— 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
peaksnr
— PSNR
numeric scalar | numeric array | dlarray
object
PSNR in decibels, returned as one of these values.
Input Image Type | PSNR Value |
---|---|
| Numeric scalar with a single PSNR measurement. |
| 1-by-1 dlarray object with a single
PSNR measurement. |
| 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. |
| 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
.
snr
— Signal-to-noise ratio
numeric scalar | numeric array | dlarray
object
Signal-to-noise ratio in decibels, returned as one of these values.
Input Image Type | PSNR Value |
---|---|
| Numeric scalar with a single SNR measurement. |
| 1-by-1 dlarray object with a single
SNR measurement. |
| 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. |
| 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:
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
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
psnr
supports the generation of C
code (requires MATLAB®
Coder™). For more information, see Code Generation for Image Processing.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Image Processing on a GPU.
Version History
Introduced in R2014aR2021a: Calculate metrics of deep learning arrays and specify dimensions of computation
The psnr
function now accepts dlarray
input
for deep learning applications.
This function also supports formatted data with dimension labels of
'S'
(spatial), 'C'
(channel), and
'B'
(batch). For data with a batch dimension, the function
returns a separate result for each index along the batch dimension.
R2021a: Support for GPU acceleration
psnr
now supports GPU acceleration
(requires Parallel Computing Toolbox™).
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)