Quality metrics of signal or image approximation
Measure Approximation Quality in RGB Image
Approximate an RGB image and compute the quality metrics.
Load an RGB image. Return the image dimensions and minimum and maximum values.
X = imread('africasculpt.jpg'); size(X)
ans = 1×3 512 512 3
ans = 1x2 uint8 row vector 0 236
Define the image approximation by setting equal to 1 all RGB values less than or equal to 100.
Xapp = X; Xapp(X<=100) = 1;
Display the image and its approximation.
subplot(1,2,1) image(X) title('Original Image') subplot(1,2,2) image(Xapp) title('Approximation')
Compute the quality metrics of the image approximation.
[psnr,mse,maxerr,L2rat] = measerr(X,Xapp)
psnr = 17.5287
mse = 1.1487e+03
maxerr = 99
L2rat = 0.9398
Measure Approximation Quality in Grayscale Image
Approximate a grayscale image and calculate approximation quality metrics.
Create a 256-by-256 grayscale image with intensities between and .
val = 0:2^16-1; X = reshape(val,256,256);
There are 16 bits per sample. Define the image approximation by setting equal to 1 all grayscale values less than or equal to 1000. Display the image and its approximation.
Xapp = X; Xapp(X<=1000) = 1; colormap(gray(2^16)) subplot(1,2,1) image(X) title('Original Image') subplot(1,2,2) image(Xapp) title('Approximation')
There are 16 bits per sample. Compute the quality metrics of the grayscale approximation.
bps = 16; [psnr,mse,maxerr,L2rat] = measerr(X,Xapp)
psnr = 11.0733
mse = 5.0786e+03
maxerr = 999
L2rat = 1.0000
X — Input signal or image
Input signal or image, specified as a real-valued array.
XAPP — Approximation of signal or image
Approximation of signal or image
X, specified as a real-valued
XAPP is the same size as
BPS — Bits per sample
8 (default) | positive integer
Bits per sample of the input data, specified as a positive integer. The default
8, so the maximum possible pixel value of an image (MAXI) is
255. More generally, when samples are represented using linear Pulse Code Modulation
with B bits per sample, MAXI is 2B−1.
PSNR — Peak signal-to-noise ratio
positive real number
Peak signal-to-noise ratio (PSNR) in decibels, returned as a positive real number. The PSNR is only meaningful for data encoded in terms of bits per sample or bits per pixel. For example, an image with 8 bits per pixel contains integers from 0 to 255.
Peak Signal to Noise Ratio
The peak signal-to-noise ratio (PSNR) in decibels between a signal and its approximation is
where MSE represents the mean square error, and B represents the bits per sample.
Mean Square Error
The mean square error (MSE) between a signal or image, X, and an approximation, Y, is
where N is the number of elements in the signal.
 Huynh-Thu, Q. and M. Ghanbari. "Scope of Validity of PSNR in Image/Video Quality Assessment." Electronics Letters. Vol. 44, Issue 13, 2008, pp. 800–801.
Introduced in R2010b