Finding the Peak Signal-to-Noise Ratio (PSNR) using Python


Peak Signal-to-Noise Ratio (PSNR) is a generally utilized statistic to compute the quality of digital signals, for images as well as video. It computes the difference between the original as well as distorted versions of the signal and gauges the amount of noise appended via compression, transmission, as well as for processing. PSNR is a utilized as tool in an assortment of applications, that has multimedia, video compression, as well as image processing. In this article, we'll monitor the two techniques for computing PSNR in Python. This method will generate algorithms, code implementations, as well as explanations that will make it possible for anyone to generate PSNR efficiently as well as precisely.

Peak Signal-to-Noise Ratio

It is a general metric for mentioning the quality of an image or any video stream. To compare the quality of the compressed output to the original input, PSNR is frequently utilized in image and video compression methods. It aids in determining the degree of information loss or degradation brought on by compression. The apparent quality of the compressed image or video increases as PSNR increases because distortion becomes less noticeable.

PSNR is determined employing the mean squared error (MSE) between the original and warped pictures, and it is expressed in decibels (dB). The following is the PSNR formula −

PSNR = 20*log10(MAX)-10*log10(MSE)

where −

MAX denotes the highest pixel value that may be represented by the image (for an 8-bit image, this would be 255).

The average of the squared deviations between corresponding pixels is employed to determine MSE or mean squared error, between the original and warped images.

Approaches

For finding the Peak Signal-to-Noise Ratio(PSNR) using Python, we can follow the two methods −

  • Calculating PSNR using Mean Squared Error(MSE).

  • Calculating PSNR using skimage library.

Let us look into both approaches −

Calculating PSNR Using Mean Squared Error(MSE)

The first method employs the Mean Squared Error (MSE) among the original as well as distorted pictures to determine the PSNR. This approach offers a simple technique for determining the PSNR score and gauging how comparable the two images are. One may find the PSNR employing the MSE technique by adhering to the instructions provided in the algorithm and code.

Algorithm

The algorithm for finding the Peak Signal-to-Noise Ratio(PSNR) using Python, is given below −

  • Step 1 − Import the cv2 as well as numpy module.

  • Step 2 − Build a function that takes two parameters as images path.

  • Step 3 − Compute the difference between the images in terms of pixels.

  • Step 4 − Get the square of the difference. And find the mean squared error.

  • Step 5 − Compute the psnr value and return it.

  • Step 6 − Get the two images, resize the images, and call the function “calculate_psr” to compute psnr.

  • Step 7 − Display the result.

Example

# import the cv2 as well as numpy library
import cv2
import numpy as np
# Create a function that takes two images’ paths as a parameter
def calculate_psnr(firstImage, secondImage):
   # Compute the difference between corresponding pixels
   diff = np.subtract(firstImage, secondImage)
   # Get the square of the difference
   squared_diff = np.square(diff)

   # Compute the mean squared error
   mse = np.mean(squared_diff)

   # Compute the PSNR
   max_pixel = 255
   psnr = 20 * np.log10(max_pixel) - 10 * np.log10(mse)
    
   return psnr

# Resize images to a common size
rHeight = 256
rWidth = 256

# Read the original and distorted images
firstI = cv2.imread('image1.jpg')
secondI = cv2.imread('image2.jpg')

# Check if images are loaded successfully
if firstI is None or secondI is None:
   print("Failed to load one or both images.")
else:
   # Resize images for first image
   firstI = cv2.resize(firstI, (rWidth, rHeight))
   # Resize the details for second image
   secondI = cv2.resize(secondI, (rWidth, rHeight))
    
   # Call the above function and perform the calculation
   psnr_score = calculate_psnr(firstI, secondI)
   # Display the result
   print("PSNR:", psnr_score)

Output

PSNR: 36.74928217740045

Calculating the PSNR Using Skimage Library

The second method makes use of the features of the scikit-image (skimage) package, which has a simple function to instantly calculate PSNR. The above technique enables one to employ a specific function supplied by the skimage library instead of performing the explicit computation of the MSE. The algorithm as well as the code described for this approach can be employed to calculate the PSNR rapidly and precisely.

Algorithm

The algorithm for finding the Peak Signal-to-Noise Ratio(PSNR) using Python, is given below −

  • Step 1 − Import the necessary module.

  • Step 2 − Create the function that takes two values as a parameter.

  • Step 3 − Read the two images.

  • Step 4 − Resize the images and compute the psnr with the help of skimage. Return the value of psnr.

  • Step 5 − Call the above function and pass the two image paths.

  • Step 6 − Display the psnr value.

Example

# import the necessary module
from skimage import io, metrics
import cv2
#Create the function having image paths as a parameter
def psnr_calculate(image1, image2):
   # Read the original as well as distorted images
   fimage1 = cv2.imread(image1)
   fimage2 = cv2.imread(image2)

   # Check if images are loaded successfully
   if fimage1 is None or fimage2 is None:
      print("Failed to load one or both images.")
      return None
    
   # Resize images to a common size
   fimage1 = cv2.resize(fimage1, (resized_width, resized_height))
   fimage2 = cv2.resize(fimage2, (resized_width, resized_height))

   # Compute the PSNR using skimage
   psnr = metrics.peak_signal_noise_ratio(fimage1, fimage2)
    
   return psnr

# Resize images to a common size
resized_width = 256
resized_height = 256

# Call the above function and pass the two image paths
psnr_score = psnr_calculate(image1.jpg', 'image2.jpg')
if psnr_score is not None:
   print("PSNR:", psnr_score)

Output

PSNR: 16.045797513039695

Conclusion

In this article, we investigated two methods for computing the Python PSNR (Peak Signal-to-Noise Ratio). In the context of image and video processing, PSNR is a critical statistic for evaluating the quality of digital data. One can determine precise PSNR scores and assess the quality of your digital signals using the Mean Squared Error (MSE) method or the skimage library.

Updated on: 18-Oct-2023

547 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements