Peak signal-to-noise ratio (PSNR) is the ratio between the maximum possible power of an image and the power of corrupting noise that affects the quality of its representation. To estimate the PSNR of an image, it is necessary to compare that image to an ideal clean image with the maximum possible power.
PSNR is defined as follows:
Here, L is the number of maximum possible intensity levels (minimum intensity level suppose to be 0) in an image.
MSE is the mean squared error & it is defined as:
Where, O represents the matrix data of original image. D represents the matrix data of degraded image. m represents the numbers of rows of pixels and i represents the index of that row of the image. n represents the number of columns of pixels and j represents the index of that column of the image.
RMSE is the root mean squared error.
Here, we have an original image and it’s compressed version, let’s see the PSNR value for these images,
Original Image :
Compressed Image :
Below is the Python implementation –
from math import log10, sqrt import cv2 import numpy as np def PSNR(original, compressed): mse = np.mean((original - compressed) * * 2 ) if (mse = = 0 ): # MSE is zero means no noise is present in the signal . # Therefore PSNR have no importance. return 100 max_pixel = 255.0 psnr = 20 * log10(max_pixel / sqrt(mse)) return psnr def main(): original = cv2.imread( "original_image.png" ) compressed = cv2.imread( "compressed_image.png" , 1 ) value = PSNR(original, compressed) print (f "PSNR value is {value} dB" ) if __name__ = = "__main__" : main() |
Output:
PSNR value is 43.862955653517126 dB
PSNR is most commonly used to estimate the efficiency of compressors, filters, etc. The larger the value of PSNR, the more efficient is a corresponding compression or filter method.