【图像处理】——图像质量评价指标信噪比(PSNR)和结构相似性(SSIM)(含原理和Python代码)
目錄
一、信噪比(PSNR)
1、信噪比的原理與計(jì)算公式
2、Python常規(guī)代碼實(shí)現(xiàn)PSNR計(jì)算
3、TensorFlow實(shí)現(xiàn)PSNR計(jì)算
4、skimage實(shí)現(xiàn)PSNR計(jì)算
5、三種方法計(jì)算的結(jié)果比較
二、結(jié)構(gòu)相似性(SSIM)
1、結(jié)構(gòu)相似性(SSIM)的原理與計(jì)算公式
2、Python常規(guī)代碼實(shí)現(xiàn)SSIM計(jì)算
3、TensorFlow實(shí)現(xiàn)SSIM計(jì)算
(1)調(diào)用函數(shù)計(jì)算SSIM
(2)TensorFlow中ssim完整源代碼
(2)實(shí)例
三、知識(shí)補(bǔ)充
1、tf.io.read_file()
2、tf.image.decode_png()
3、tf.numpy()
一、信噪比(PSNR)
在不同的噪聲比例下分別計(jì)算PSNR,就能夠得到一條信噪比曲線,比較在相同噪聲比例下不同圖像的信噪比曲線,可以判斷出圖像質(zhì)量的優(yōu)劣
1、信噪比的原理與計(jì)算公式
信噪比是衡量去噪后或者加噪后圖片的質(zhì)量,值越大說(shuō)明濾波效果越好。在圖像中,用來(lái)評(píng)價(jià)兩幅圖像相比質(zhì)量的好壞,即失真情況,PSNR越高,圖像失真越小。
2、Python常規(guī)代碼實(shí)現(xiàn)PSNR計(jì)算
import cv2 import tensorflow as tf from image_gray.image_gray_methods import gray_mean_rgb import mathdef psnr(img1,img2):img1_gray = gray_mean_rgb(img1)img2_gray = gray_mean_rgb(img2)#灰度化h = img1_gray.shape[0]w = img1_gray.shape[1]#獲得灰度化后的圖像矩陣的高寬mes = 0#逐行計(jì)算原圖像和噪聲圖像對(duì)應(yīng)像素值的均差,再進(jìn)行累加for i in range(h):for j in range(w):mes += (img1_gray[i,j]-img2_gray[i,j])**2mes = mes/(h*w)psnr = abs(10*math.log10(255/mes))return psnr3、TensorFlow實(shí)現(xiàn)PSNR計(jì)算
import tensorflow as tf def tf_psnr(img1,img2,max_value=255):'''利用TensorFlow求解圖像的信噪比:param img1: 帶有噪聲的原始圖像:param img2: 去噪濾波后的圖像:param max_value: 最大的灰度值,一般圖像為8位的話,則max_value=2**8-1=255:return: 返回信噪比常數(shù)'''image1 = tf.io.read_file(img1) # 讀取圖片變成的是二進(jìn)制的張量tensorimage2 = tf.io.read_file(img2) # 考驗(yàn)通過(guò)變量.numpy()將其變?yōu)閚umpy矩陣im1 = tf.image.decode_png(image1)im2 = tf.image.decode_png(image2) # 對(duì)讀取的二進(jìn)制圖片信息進(jìn)行解碼,解碼成tensor量的矩陣# im1 = tf.io.decode_png(image1)# im2 = tf.io.decode_png(image2)# io模塊一樣的psnr_tensor = tf.image.psnr(im1,im2,max_value)#得到一個(gè)tensor常數(shù)psnr_numpy = psnr_tensor.numpy()#將tensor轉(zhuǎn)換為numpy數(shù)據(jù),是一個(gè)實(shí)常數(shù)return psnr_numpy4、skimage實(shí)現(xiàn)PSNR計(jì)算
from skimage.metrics.simple_metrics import peak_signal_noise_ratio from skimage import io def skimage_psnr(img1,img2,max_val=255):image1 = io.imread(img1) # 讀取圖片變成的是二進(jìn)制的張量tensorimage2 = io.imread(img2) # 考驗(yàn)通過(guò)變量.numpy()將其變?yōu)閚umpy矩陣psnr_skimage = peak_signal_noise_ratio(image1,image2)return psnr_skimage5、三種方法計(jì)算的結(jié)果比較
右邊是含有椒鹽噪點(diǎn)的圖像,左邊是經(jīng)過(guò)中值濾波后的圖像
if __name__ == '__main__':img1 = 'sp_noise.jpg'img2 = 'medium_denoise.jpg'psnr_value1 = def_psnr(img1,img2)psnr_value2 = tf_psnr(img1,img2)psnr_value3 = skimage_psnr(img1,img2)print("自定義psnr=",psnr_value1)print("tensorflow_psnr=",psnr_value2)print("skimage_psnr=",psnr_value3) 自定義psnr= 20.220593641797024 tensorflow_psnr= 18.701542 skimage_psnr= 18.72291245011262二、結(jié)構(gòu)相似性(SSIM)
SSIM(結(jié)構(gòu)相似性)-數(shù)學(xué)公式及Python實(shí)現(xiàn)_yuhongbei的博客-CSDN博客
1、結(jié)構(gòu)相似性(SSIM)的原理與計(jì)算公式
SSIM的輸入就是兩張圖像,我們要得到其相似性的兩張圖像。其中一張是未經(jīng)壓縮的無(wú)失真圖像(即ground truth),另一張就是你恢復(fù)出的圖像。所以,SSIM可以作為super-resolution質(zhì)量的指標(biāo)。假設(shè)我們輸入的兩張圖像分別是x和y,那么:
2、Python常規(guī)代碼實(shí)現(xiàn)SSIM計(jì)算
不再贅述,可以自行編程
3、TensorFlow實(shí)現(xiàn)SSIM計(jì)算
(1)調(diào)用函數(shù)計(jì)算SSIM
tf.image.ssim(x, y, 255) import tensorflow as tf def tf_ssim(img1,img2,max_value=255):'''利用TensorFlow求解圖像的結(jié)構(gòu)相似性ssim:param img1: 帶有噪聲的原始圖像:param img2: 去噪濾波后的圖像:param max_value: 最大的灰度值,一般圖像為8位的話,則max_value=2**8-1=255:return: 返回ssim常數(shù)'''image1 = tf.io.read_file(img1) # 讀取圖片變成的是二進(jìn)制的張量tensorimage2 = tf.io.read_file(img2) # 考驗(yàn)通過(guò)變量.numpy()將其變?yōu)閚umpy矩陣im1 = tf.image.decode_png(image1)im2 = tf.image.decode_png(image2) # 對(duì)讀取的二進(jìn)制圖片信息進(jìn)行解碼,解碼成tensor量的矩陣# im1 = tf.io.decode_png(image1)# im2 = tf.io.decode_png(image2)# io模塊一樣的ssim_tensor = tf.image.ssim(im1,im2,max_value)#得到一個(gè)tensor常數(shù)ssim_numpy = ssim_tensor.numpy()#將tensor轉(zhuǎn)換為numpy數(shù)據(jù),是一個(gè)實(shí)常數(shù)return ssim_numpy(2)TensorFlow中ssim完整源代碼
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). Imagequality assessment: from error visibility to structural similarity. IEEEtransactions on image processing.Note: The true SSIM is only defined on grayscale. This function does notperform any colorspace transform. (If input is already YUV, then it willcompute 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 themaximum the and minimum allowed values).Returns:A tensor containing an SSIM value for each image in batch. Returned SSIMvalues are in range (-1, 1], when pixel values are non-negative. Returnsa 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])(2)實(shí)例
右邊是含有椒鹽噪點(diǎn)的圖像,左邊是經(jīng)過(guò)中值濾波后的圖像
if __name__ == '__main__':img1 = 'sp_noise.jpg'img2 = 'medium_denoise.jpg'ssim_value = tf_ssim(img1,img2)print("TensorFlow_ssim=",ssim_value) TensorFlow_ssim= 0.47128722三、知識(shí)補(bǔ)充
1、tf.io.read_file()
tf.io.read_file()函數(shù)用于讀取文件,相當(dāng)于python的open()函數(shù),常與 tf.io.decode_jpeg() 搭配使用讀取圖片
tf.io.read_file()讀取出來(lái)的是二進(jìn)制數(shù)據(jù),要想使用數(shù)據(jù)就得用 tf.io.decode_jpeg()去解碼
參考:https://www.malaoshi.top/show_1EF4VxYA51W2.html
2、tf.image.decode_png()
用于對(duì)二進(jìn)制數(shù)據(jù)的圖片進(jìn)行解碼
參考:
Tensorflow tf.image.decode_image圖片解碼_菜的真真實(shí)實(shí)的博客-CSDN博客_decode image
tf.read_file和tf.image.decode_jpeg處理圖片(轉(zhuǎn))_monk1992的博客-CSDN博客
3、tf.numpy()
將tensor變量轉(zhuǎn)換為numpy數(shù)據(jù)
更多TensorFlow用法可見(jiàn):《tensorflow》
總結(jié)
以上是生活随笔為你收集整理的【图像处理】——图像质量评价指标信噪比(PSNR)和结构相似性(SSIM)(含原理和Python代码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java-appium-527进阶-1
- 下一篇: 阿里云怎么进入mysql_阿里云进入my