当前位置:网站首页>Image quality evaluation indicators: SNR, PSNR, MSE and SSIM

Image quality evaluation indicators: SNR, PSNR, MSE and SSIM

2022-07-19 02:43:00 lucky-wz

Image quality evaluation index

Generally, there are four methods to evaluate image noise , Namely :

  • Signal-to-noise ratio (Signal to Noise Ratio,SNR)
  • Peak signal to noise ratio (Peak Signal to Noise Ratio, PSNR)
  • Mean square error (Mean Square Error, MSE)
  • Structural similarity (Structural SIMilarity, SSIM).

These four evaluation indicators are introduced below .

Mean square error (MSE)

Mean square difference is used to compare two images K K K, I I I The mean square difference of
M S E = 1 m n ∑ i = 0 n − 1 ∑ j = 0 m − 1 ∥ K ( i , j ) − I ( i , j ) ∥ 2 M S E=\frac{1}{m n} \sum_{i=0}^{n-1} \sum_{j=0}^{m-1}\|K(i, j)-I(i, j)\|^{2} MSE=mn1i=0n1j=0m1K(i,j)I(i,j)2

Signal-to-noise ratio (SNR)

SNR Used to describe the ratio of signal to noise
S N R ( d B ) = 10 log ⁡ 10 [ ∑ x = 0 m − 1 ∑ y = 0 n − 1 ( f ( x , y ) ) 2 ∑ x = 0 m − 1 ∑ y = 0 n − 1 ( f ( x , y ) − f ^ ( x , y ) ) 2 ] S N R(d B)=10 \log _{10}\left[\frac{\sum_{x=0}^{m-1} \sum_{y=0}^{n-1}(f(x, y))^{2}}{\sum_{x=0}^{m-1} \sum_{y=0}^{n-1}(f(x, y)-\hat{f}(x, y))^{2}}\right] SNR(dB)=10log10[x=0m1y=0n1(f(x,y)f^(x,y))2x=0m1y=0n1(f(x,y))2]

SNR Code implementation

The following code is to add that the signal-to-noise ratio is not leveldB Noise code of :

def add_noise(y, level):
        with torch.no_grad():
            sigma = 10 ** (- (1 / 20) * level)
            y = y + sigma * torch.randn(y.size()).to(self.device)
        return y

Peak signal to noise ratio (PSNR)

grayscale PSNR Calculation

Given a size of m × n m\times n m×n Clean image of I I I And noise images K K K, Mean square error ( M S E MSE MSE) Defined as :
M S E = 1 m n ∑ i = 0 m − 1 ∑ j = 0 n − 1 [ I ( i , j ) − K ( i , j ) ] 2 M S E=\frac{1}{m n} \sum_{i=0}^{m-1} \sum_{j=0}^{n-1}[I(i, j)-K(i, j)]^{2} MSE=mn1i=0m1j=0n1[I(i,j)K(i,j)]2
On this basis ,PSNR(dB) Is defined as :
P S N R = 10 ⋅ log ⁡ 10 ( M A X I 2 M S E ) P S N R=10 \cdot \log _{10}\left(\frac{M A X_{I}^{2}}{M S E}\right) PSNR=10log10(MSEMAXI2)
among M A X I 2 MAX_I^2 MAXI2 Is the maximum possible pixel value of the image . If every pixel is made up of 8 Bit binary to represent , So it's 255. Usually , If the pixel value is determined by B B B Bit binary to represent , that M A X I = 2 B − 1 MAX_I=2^B-1 MAXI=2B1.

In a general way , in the light of uint8 data , The maximum pixel value is 255; For floating point data , The maximum pixel value is 1.

RGB Images PSNR Calculation

If it's a color image , There are usually three ways to calculate .

  • Separate calculation RGB Three channels PSNR, Then take the average .
  • Calculation RGB Three channel MSE , Then divide by 3 .
  • Convert picture to YCbCr Format , Then just calculate Y Component, that is, the brightness component PSNR.

among , The second and third methods are more common .

Peak signal to noise ratio PSNR An objective measure of image distortion or noise level .2 Between two images PSNR The bigger the value is. , The more similar . The general benchmark is 30dB,30dB The following image degradation is more obvious .

PSNR Code implementation

Method 1 : utilize skimage Modular compare_psnr Function calculation .

# method 1
diff = im1 - im2
mse = np.mean(np.square(diff))
psnr = 10 * np.log10(255 * 255 / mse)

# method 2
psnr = skimage.measure.compare_psnr(im1, im2, 255)

Method 2 : From the big guy LeCun Of PSNR function :

#  Calculation PSNR
def PSNR(target, pred, R=1, dummy=1e-4, reduction='mean'):
    with torch.no_grad():
        dims = (1, 2, 3) if len(target.shape) == 4 else 1
        mean_sq_err = ((target - pred) ** 2).mean(dims)
        mean_sq_err = mean_sq_err + (mean_sq_err == 0).float() * dummy  # if 0, fill with dummy -> PSNR of 40 by default
        output = 10 * torch.log10(R ** 2 / mean_sq_err)
        if reduction == 'mean':
            return output.mean()
        elif reduction == 'none':
            return output

Structural similarity (SSIM)

SSIM Describe the similarity between two images , The formula is based on samples x x x and y y y Three comparisons between : brightness (luminance)、 Contrast (contrast) And structure (structure).
l ( x , y ) = 2 μ x μ y + c 1 μ x 2 + μ y 2 + c 1 c ( x , y ) = 2 σ x σ y + c 2 σ x 2 + σ y 2 + c 2 s ( x , y ) = σ x y + c 3 σ x σ y + c 3 l(x, y)=\frac{2 \mu_{x} \mu_{y}+c_{1}}{\mu_{x}^{2}+\mu_{y}^{2}+c_{1}} c(x, y)=\frac{2 \sigma_{x} \sigma_{y}+c_{2}}{\sigma_{x}^{2}+\sigma_{y}^{2}+c_{2}} s(x, y)=\frac{\sigma_{x y}+c_{3}}{\sigma_{x} \sigma_{y}+c_{3}} l(x,y)=μx2+μy2+c12μxμy+c1c(x,y)=σx2+σy2+c22σxσy+c2s(x,y)=σxσy+c3σxy+c3
Usually take c 3 = c 2 / 2 c_3=c_2/2 c3=c2/2.

  • μ x \mu_x μx by x x x The average of , μ y \mu_y μy by x x x The average of ; σ x 2 \sigma_x^2 σx2 by x x x The variance of , σ y 2 \sigma_y^2 σy2 by y y y The variance of ; σ x y 2 \sigma_{xy}^2 σxy2 by x y xy xy The variance of ;
  • c 1 = ( k 1 L ) 2 , c 2 = ( k 2 L ) 2 c_{1}=\left(k_{1} L\right)^{2}, c_{2}=\left(k_{2} L\right)^{2} c1=(k1L)2,c2=(k2L)2 Is two constants , Avoid division by zero , L L L Is the range of pixel values ;
  • k 1 = 0.01 , k 2 = 0.03 k_{1}=0.01, k_{2}=0.03 k1=0.01,k2=0.03 As the default value .

that :
SSIM ⁡ ( x , y ) = [ l ( x , y ) α ⋅ c ( x , y ) β ⋅ s ( x , y ) γ ] \operatorname{SSIM}(x, y)=\left[l(x, y)^{\alpha} \cdot c(x, y)^{\beta} \cdot s(x, y)^{\gamma}\right] SSIM(x,y)=[l(x,y)αc(x,y)βs(x,y)γ]
take α , β , γ \alpha,\beta,\gamma α,β,γ Set to 1, You can get
SSIM ⁡ ( x , y ) = ( 2 μ x μ y + c 1 ) ( 2 σ x y + c 2 ) ( μ x 2 + μ y 2 + c 1 ) ( σ x 2 + σ y 2 + c 2 ) \operatorname{SSIM}(x, y)=\frac{\left(2 \mu_{x} \mu_{y}+c_{1}\right)\left(2 \sigma_{x y}+c_{2}\right)}{\left(\mu_{x}^{2}+\mu_{y}^{2}+c_{1}\right)\left(\sigma_{x}^{2}+\sigma_{y}^{2}+c_{2}\right)} SSIM(x,y)=(μx2+μy2+c1)(σx2+σy2+c2)(2μxμy+c1)(2σxy+c2)

Take one from the picture every time you calculate N × M N\times M N×M The window of , And then slide the window continuously to calculate , Finally, take the average value as the global SSIM.

SSIM Return the image of MSSIM. It's also a floating-point number between zero and one ( The higher, the better )

SSIM Code implementation

# im1  and  im2  All gray images ,uint8  type 
ssim = skimage.measure.compare_ssim(im1, im2, data_range=255)
原网站

版权声明
本文为[lucky-wz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170007316937.html