Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | Peak Signal-to-Noise Ratio (PSNR)

  • Last Updated : 06 Feb, 2020

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: 

     \[PSNR = 10log_{10}(\frac{(L - 1)^2}{MSE})= 20log_{10}(\frac{L - 1}{RMSE})\]

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

 
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: 

     \[MSE = \frac{1}{mn}\sum_{i=0}^{m-1}\sum_{j=0}^{n-1}\left ( O(i, j) - D(i, j)\right )^{2}\]

 
 

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :