使用Python查詢峰值信噪比 (PSNR)
峰值信噪比 (PSNR) 是一種常用的統計量,用於計算數字訊號(影像和影片)的質量。它計算原始訊號和失真訊號之間的差異,並衡量透過壓縮、傳輸和處理新增的噪聲量。PSNR 廣泛應用於多媒體、影片壓縮和影像處理等各種應用中。在本文中,我們將學習兩種在Python中計算PSNR的方法。本方法將提供演算法、程式碼實現和解釋,使任何人都能夠高效準確地計算PSNR。
峰值信噪比
它是一個常用的指標,用於表示影像或任何影片流的質量。在影像和影片壓縮技術中,經常使用PSNR來比較壓縮輸出與原始輸入的質量。它有助於確定壓縮造成的信 息丟失或退化的程度。隨著PSNR值的增加,壓縮影像或影片的感知質量也會提高,因為失真變得不那麼明顯。
PSNR 使用原始影像和失真影像之間的均方誤差 (MSE) 計算,並以分貝 (dB) 為單位表示。PSNR公式如下:
PSNR = 20*log10(MAX)-10*log10(MSE)
其中:
MAX 表示影像可以表示的最高畫素值(對於 8 點陣圖像,此值為 255)。
均方誤差 (MSE) 使用原始影像和失真影像之間對應畫素的平方偏差的平均值來計算。
方法
要使用Python查詢峰值信噪比 (PSNR),我們可以遵循以下兩種方法:
使用均方誤差 (MSE) 計算PSNR。
使用skimage庫計算PSNR。
讓我們來看看這兩種方法:
使用均方誤差 (MSE) 計算PSNR
第一種方法使用原始影像和失真影像之間的均方誤差 (MSE) 來計算PSNR。這種方法提供了一種簡單的方法來計算PSNR分數,並衡量兩幅影像的相似程度。按照演算法和程式碼中的說明,可以使用MSE方法找到PSNR。
演算法
使用Python查詢峰值信噪比 (PSNR) 的演算法如下:
步驟1 - 匯入cv2和numpy模組。
步驟2 - 建立一個函式,該函式以兩張影像路徑作為引數。
步驟3 - 計算影像之間畫素的差異。
步驟4 - 獲取差異的平方。並找到均方誤差。
步驟5 - 計算psnr值並返回。
步驟6 - 獲取兩張影像,調整影像大小,並呼叫函式“calculate_psr”來計算psnr。
步驟7 - 顯示結果。
示例
# 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)
輸出
PSNR: 36.74928217740045
使用Skimage庫計算PSNR
第二種方法利用scikit-image (skimage)庫的功能,該庫具有一個簡單的函式可以直接計算PSNR。上述方法允許使用skimage庫提供的專用函式,而不是執行MSE的顯式計算。此方法中描述的演算法和程式碼可用於快速準確地計算PSNR。
演算法
使用Python查詢峰值信噪比 (PSNR) 的演算法如下:
步驟1 - 匯入必要的模組。
步驟2 - 建立一個函式,該函式以兩個值作為引數。
步驟3 - 讀取兩張影像。
步驟4 - 調整影像大小,並使用skimage計算psnr。返回psnr的值。
步驟5 - 呼叫上述函式並傳遞兩張影像路徑。
步驟6 - 顯示psnr值。
示例
# 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)
輸出
PSNR: 16.045797513039695
結論
在本文中,我們研究了兩種計算Python PSNR(峰值信噪比)的方法。在影像和影片處理的背景下,PSNR是評估數字資料質量的關鍵統計量。使用均方誤差 (MSE) 方法或skimage庫,可以確定精確的PSNR分數並評估數字訊號的質量。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP