完整搜尋頁面 →

MSE、PSNR 與資料範圍

選讀・IP 02・數位影像基礎 的補充資料(第 5/5 則)。主線不依賴這則補充;引用編號對應本頁底部的參考文獻。

導覽:本頁與本系列: 導覽:本頁與本系列

處理後的影像離原圖有多遠?最簡單的答案是把逐像素差的平方取平均。這個數字單獨看很難判讀(100 算大嗎?),所以我們拿它與像素能取到的最大值相比,並用對數尺度報告比值,就像聲音的分貝。

設 ff 與 f^\hat f 是兩張各有 MNMN 個像素的影像。均方誤差(mean squared error,MSE)與峰值訊噪比(peak signal-to-noise ratio,PSNR)為

MSE=1MN∑x,y(f^(x,y)−f(x,y))2,PSNR=10log⁡10MAX2MSE dB,\text{MSE} = \frac{1}{MN}\sum_{x,y}\big(\hat f(x,y) - f(x,y)\big)^2, \qquad \text{PSNR} = 10\log_{10}\frac{\text{MAX}^2}{\text{MSE}}\ \text{dB},

其中 f(x,y)f(x,y) 與 f^(x,y)\hat f(x,y) 是 (x,y)(x,y) 處的像素值,MAX\text{MAX} 是資料範圍(data range):像素格式允許的最大值(8 位元 uint8 影像為 255,縮放到 [0,1][0, 1] 的浮點影像為 1.0)。在 scikit-image 中,它就是 skimage.metrics.peak_signal_noise_ratio 的 data_range 參數。因為 erms=MSEe_{\text{rms}} = \sqrt{\text{MSE}},它與 IP 08 使用的 20log⁡10(Lmax⁡/erms)20\log_{10}(L_{\max}/e_{\text{rms}}) 是同一個量。

為什麼用分貝?誤差可以跨越很多個數量級,取對數後,相同的比值就是相同的步長。MSE 減半會增加 10log⁡102≈3.0110\log_{10} 2 \approx 3.01 dB;雜訊標準差減半(MSE 變成四分之一)則增加約 6.02 dB。PSNR 越高,誤差越小。由於 MAX\text{MAX} 在公式裡面,同樣的像素誤差在不同範圍下會得到不同的 PSNR,所以 PSNR 必須連同它的範圍一起才有意義。

程式碼 · Python · 21 行
import numpy as np
from skimage import color, data
from skimage.metrics import mean_squared_error, peak_signal_noise_ratio

ref = data.camera()                              # uint8, values 0..255
rng = np.random.default_rng(0)
for sigma in (10, 20):
    noisy = np.clip(ref + rng.normal(0, sigma, ref.shape), 0, 255).astype(np.uint8)
    mse = mean_squared_error(ref, noisy)
    print(f"sigma={sigma}: MSE={mse:.1f}, by hand {10 * np.log10(255**2 / mse):.2f} dB, "
          f"uint8 {peak_signal_noise_ratio(ref, noisy):.2f} dB")
    ref_f, noisy_f = ref / 255.0, noisy / 255.0  # the same pair as floats in [0, 1]
    print(f"   float, data_range=1.0: {peak_signal_noise_ratio(ref_f, noisy_f, data_range=1.0):.2f} dB")
    print(f"   float, data_range=255: {peak_signal_noise_ratio(ref_f, noisy_f, data_range=255):.2f} dB  (wrong)")

# Pitfall: the same noise on RGB and on luma Y gives different PSNR
rgb = data.astronaut()
noisy_rgb = np.clip(rgb + rng.normal(0, 10, rgb.shape), 0, 255).astype(np.uint8)
y = lambda im: color.rgb2gray(im) * 255          # luma in [0, 255]
print(f"RGB: {peak_signal_noise_ratio(rgb, noisy_rgb):.2f} dB, "
      f"Y: {peak_signal_noise_ratio(y(rgb), y(noisy_rgb), data_range=255):.2f} dB")

執行後印出:

程式碼 · 7 行
sigma=10: MSE=97.8, by hand 28.23 dB, uint8 28.23 dB
   float, data_range=1.0: 28.23 dB
   float, data_range=255: 76.36 dB  (wrong)
sigma=20: MSE=374.0, by hand 22.40 dB, uint8 22.40 dB
   float, data_range=1.0: 22.40 dB
   float, data_range=255: 70.53 dB  (wrong)
RGB: 28.61 dB, Y: 31.03 dB

解讀這些數字:當範圍與數值尺度一致時,uint8 與浮點兩種形式給出相同的 28.23 dB;但若宣告錯誤的範圍(對 [0,1][0, 1] 的浮點影像用 255),會多出 20log⁡10255≈48.1320\log_{10} 255 \approx 48.13 dB,同一張影像就被報告成驚人的 76.36 dB。把 σ\sigma 加倍,名義上 MSE 變成四倍(下降 6.02 dB);實測下降 28.23−22.40=5.8328.23 - 22.40 = 5.83 dB,略少一些,因為把數值截斷(clipping)到 [0,255][0, 255] 會在飽和的像素上移除一部分加入的誤差。最後一行顯示第三個陷阱:同樣的雜訊,在 RGB 上是 28.61 dB,在亮度通道 Y 上卻是 31.03 dB,因為 Y 是三個獨立含雜訊通道的平均。比較 PSNR 時,影像、範圍與通道(RGB 或 Y)都必須相同;整數影像也要先轉成浮點再相減,否則 uint8 運算會溢位繞回(scikit-image 內部已處理這點)。

最後,PSNR 衡量的是像素誤差,而不是外觀。PSNR 相同的兩張影像看起來可能差很多,而考慮結構的指標如 SSIM [13],或學習得到的感知距離 [15],與人的判斷較為吻合;見上面的「現代觀點」。Huynh-Thu 與 Ghanbari [19] 討論了 PSNR 作為品質指標在什麼範圍內有效。

參考文獻

  1. R. C. Gonzalez and R. E. Woods, Digital Image Processing, 4th ed., Pearson, 2018, Ch. 2. publisher page
  2. M. Unser, “Sampling—50 years after Shannon,” Proceedings of the IEEE, vol. 88, no. 4, pp. 569–587, 2000. doi
  3. R. Keys, “Cubic convolution interpolation for digital image processing,” IEEE Transactions on Acoustics, Speech, and Signal Processing, vol. 29, no. 6, pp. 1153–1160, 1981. doi
  4. P. Thévenaz, T. Blu, and M. Unser, “Interpolation revisited,” IEEE Transactions on Medical Imaging, vol. 19, no. 7, pp. 739–758, 2000. doi
  5. A. Rosenfeld and J. L. Pfaltz, “Distance functions on digital pictures,” Pattern Recognition, vol. 1, pp. 33–61, 1968. doi
  6. B. Zitová and J. Flusser, “Image registration methods: a survey,” Image and Vision Computing, vol. 21, no. 11, pp. 977–1000, 2003. doi
  7. M. Delbracio, D. Kelly, M. S. Brown, and P. Milanfar, “Mobile computational photography: A tour,” arXiv:2102.09000, 2021. arXiv
  8. R. Ramanath, W. E. Snyder, Y. Yoo, and M. S. Drew, “Color image processing pipeline,” IEEE Signal Processing Magazine, vol. 22, no. 1, pp. 34–43, 2005. doi
  9. Z. Wang, J. Chen, and S. C. H. Hoi, “Deep learning for image super-resolution: A survey,” IEEE Transactions on Pattern Analysis and Machine Intelligence, 2020. arXiv
  10. B. Moser, F. Raue, S. Frolov, J. Hees, S. Palacio, and A. Dengel, “Hitchhiker’s guide to super-resolution: Introduction and recent advances,” IEEE Transactions on Pattern Analysis and Machine Intelligence, 2023. arXiv
  11. Y. Chen, S. Liu, and X. Wang, “Learning continuous image representation with local implicit image function,” CVPR, 2021. arXiv
  12. X. Wang, L. Xie, C. Dong, and Y. Shan, “Real-ESRGAN: Training real-world blind super-resolution with pure synthetic data,” arXiv:2107.10833, 2021. arXiv
  13. Z. Wang, A. C. Bovik, H. R. Sheikh, and E. P. Simoncelli, “Image quality assessment: From error visibility to structural similarity,” IEEE Transactions on Image Processing, vol. 13, no. 4, pp. 600–612, 2004. project page
  14. G. Zhai and X. Min, “Perceptual image quality assessment: A survey,” Science China Information Sciences, vol. 63, no. 11, 2020. doi
  15. R. Zhang, P. Isola, A. A. Efros, E. Shechtman, and O. Wang, “The unreasonable effectiveness of deep features as a perceptual metric,” CVPR, 2018. arXiv
  16. K. Ding, K. Ma, S. Wang, and E. P. Simoncelli, “Comparison of full-reference image quality models for optimization of image processing systems,” International Journal of Computer Vision, 2021. arXiv
  17. C. Chen, Q. Chen, J. Xu, and V. Koltun, “Learning to see in the dark,” CVPR, 2018. arXiv
  18. A. Ignatov, L. Van Gool, and R. Timofte, “Replacing mobile camera ISP with a single deep learning model,” arXiv:2002.05509, 2020. arXiv
  19. Q. Huynh-Thu and M. Ghanbari, “Scope of validity of PSNR in image/video quality assessment,” Electronics Letters, vol. 44, no. 13, pp. 800–801, 2008. doi
  20. R. Hartley and A. Zisserman, Multiple View Geometry in Computer Vision, 2nd ed., Cambridge University Press, 2004. doi
  21. scikit-image contributors, “ProjectiveTransform.estimate,” scikit-image 0.25.2 documentation, accessed 5 November 2026. API 文件.