日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

图像去噪评估—PSNR和SSIM

發(fā)布時間:2023/12/19 综合教程 51 生活家
生活随笔 收集整理的這篇文章主要介紹了 图像去噪评估—PSNR和SSIM 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

PSNR簡介

PSNR的全稱為“Peak Signal-to-Noise Ratio”,直譯為中文就是峰值信噪比。是一種衡量圖像質量的指標。在很多領域都會需要這個指標,比如在超分辨率重建圖像的時候,PSNR就是很重要的指標了。

WIKI解釋

峰值信噪比(英語:Peak signal-to-noise ratio,常縮寫為PSNR)是一個表示信號最大可能功率和影響它的表示精度的破壞性噪聲功率的比值的工程術語。由于許多信號都有非常寬的動態(tài)范圍,峰值信噪比常用對數分貝單位來表示。

計算PSNR要先知道MSE(均方誤差)的計算。兩個m×n單色圖像I和K,如果一個為另外一個的噪聲近似,那么它們的的均方誤差定義為:

MSE的概念應該是比較熟悉的,這也是常見的損失函數。而PSNR就是通過MSE得出來的,公式如下:

其中,MAXI是表示圖像點顏色的最大數值,如果每個采樣點用 8 位表示,那么就是 255。

所以MSE越小,則PSNR越大;所以PSNR越大,代表著圖像質量越好。一般來說,

PSNR高于40dB說明圖像質量極好(即非常接近原始圖像),
在30—40dB通常表示圖像質量是好的(即失真可以察覺但可以接受),
在20—30dB說明圖像質量差;
最后,PSNR低于20dB圖像不可接受

python代碼實現PSNR計算

# PSNR.py
 
import numpy as np
import math

def psnr(target, ref, scale):
    # target:目標圖像  ref:參考圖像  scale:尺寸大小
    # assume RGB image
    target_data = np.array(target)
    target_data = target_data[scale:-scale,scale:-scale]
 
    ref_data = np.array(ref)
    ref_data = ref_data[scale:-scale,scale:-scale]
 
    diff = ref_data - target_data
    diff = diff.flatten('C')
    rmse = math.sqrt( np.mean(diff ** 2.) )
    return 20*math.log10(1.0/rmse)

以上代碼僅作參考之用,tensorflow框架里有直接關于psnr計算的函數,直接調用就行了:(更推薦)

注意:計算PSNR的時候必須滿足兩張圖像的size要完全一樣!

import tensorflow as tf
 
def read_img(path):
	return tf.image.decode_image(tf.read_file(path))
 
def psnr(tf_img1, tf_img2):
	return tf.image.psnr(tf_img1, tf_img2, max_val=255)
 
def _main():
	t1 = read_img('t1.jpg')
	t2 = read_img('t2.jpg')
	with tf.Session() as sess:
		sess.run(tf.global_variables_initializer())
		y = sess.run(psnr(t1, t2))
		print(y)
 
if __name__ == '__main__':
    _main()

SSIM簡介

SSIM是一種衡量兩幅圖片相似度的指標。
出處來自于2004年的一篇TIP,標題為:Image Quality Assessment: From Error Visibility to Structural Similarity 論文地址,與PSNR一樣,SSIM也經常用作圖像質量的評價。

先了解SSIM的輸入
SSIM的輸入就是兩張圖像,我們要得到其相似性的兩張圖像。其中一張是未經壓縮的無失真圖像(即ground truth),另一張就是你恢復出的圖像。所以,SSIM可以作為super-resolution質量的指標。假設我們輸入的兩張圖像分別是x和y,那么

α>0, β>0β>0,and γ>0γ>0. 式1是SSIM的數學定義,其中:

其中l(wèi)(x, y)是亮度比較,c(x,y)是對比度比較,s(x,y)是結構比較μ**xμx和μ**yμy分別代表x,y的平均值,σ**xσx和σ**yσy分別代表x,y的標準差。σxyσxy代表x和y的協方差。而c1c1,c2c2,c3c3分別為常數,避免分母為0帶來的系統錯誤。
在實際工程計算中,我們一般設定α=β=γ=1α=β=γ=1,以及c3=c2/2c3=c2/2,可以將SSIM簡化為下:

總結

SSIM具有對稱性,即SSIM(x,y)=SSIM(y,x)
SSIM是一個0到1之間的數,越大表示輸出圖像和無失真圖像的差距越小,即圖像質量越好。當兩幅圖像一模一樣時,SSIM=1;

如PSNR一樣,SSIM這種常用計算函數也被tensorflow收編了,我們只需在tf中調用ssim就可以了tf.image.ssim(x, y, 255)

源代碼如下:

def ssim(img1, img2, max_val):
  """Computes SSIM index between img1 and img2.

  This function is based on the standard SSIM implementation from:
  Wang, Z., Bovik, A. C., Sheikh, H. R., & Simoncelli, E. P. (2004). Image
  quality assessment: from error visibility to structural similarity. IEEE
  transactions on image processing.

  Note: The true SSIM is only defined on grayscale.  This function does not
  perform any colorspace transform.  (If input is already YUV, then it will
  compute YUV SSIM average.)

  Details:
    - 11x11 Gaussian filter of width 1.5 is used.
    - k1 = 0.01, k2 = 0.03 as in the original paper.
  The image sizes must be at least 11x11 because of the filter size.
  Example:
  # Read images from file.
      im1 = tf.decode_png('path/to/im1.png')
      im2 = tf.decode_png('path/to/im2.png')
      # Compute SSIM over tf.uint8 Tensors.
      ssim1 = tf.image.ssim(im1, im2, max_val=255)

      # Compute SSIM over tf.float32 Tensors.
      im1 = tf.image.convert_image_dtype(im1, tf.float32)
      im2 = tf.image.convert_image_dtype(im2, tf.float32)
      ssim2 = tf.image.ssim(im1, im2, max_val=1.0)
      # ssim1 and ssim2 both have type tf.float32 and are almost equal.
    img1: First image batch.
    img2: Second image batch.
    max_val: The dynamic range of the images (i.e., the difference between the
      maximum the and minimum allowed values).
  Returns:
    A tensor containing an SSIM value for each image in batch.  Returned SSIM
    values are in range (-1, 1], when pixel values are non-negative. Returns
    a tensor with shape: broadcast(img1.shape[:-3], img2.shape[:-3]).
  """
   _, _, checks = _verify_compatible_image_shapes(img1, img2)
  with ops.control_dependencies(checks):
    img1 = array_ops.identity(img1)
  # Need to convert the images to float32.  Scale max_val accordingly so that
  # SSIM is computed correctly.
  max_val = math_ops.cast(max_val, img1.dtype)
  max_val = convert_image_dtype(max_val, dtypes.float32)
  img1 = convert_image_dtype(img1, dtypes.float32)
  img2 = convert_image_dtype(img2, dtypes.float32)
  ssim_per_channel, _ = _ssim_per_channel(img1, img2, max_val)
  # Compute average over color channels.
  return math_ops.reduce_mean(ssim_per_channel, [-1])

總結

以上是生活随笔為你收集整理的图像去噪评估—PSNR和SSIM的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。