Two evaluation indicators for image enhancement: peak signal-to-noise ratio PSNR and structural similarity SSIM

Two image enhancement evaluation indicators: PSNR and SSIM


The evaluation indicators of image enhancement usually include mean absolute error (MAE), mean square error (MSE), peak signal-to-noise ratio (PSNR) and structural similarity (SSIM) at the pixel level. Currently, the more authoritative objective evaluation standards in the field of image enhancement are peak signal-to-noise ratio (PSNR) and structural similarity (SSIM).
Note: These two indicators need to be referenced by the standard chart (not the original chart), that is, the full reference indicator

Peak Signal to Noise Ratio PSNR

PSNR (Peak Signal to Noise Ratio) is expressed as the ratio of peak signal energy to average noise energy. It is generally taken as 10lg in dB (decibel). The average energy of noise can be expressed as the mean squared error MSE (Mean Squared Error) between the real image and the noisy image. The calculation method of PSNR is as shown in the formula, MAXI is the peak signal energy, bits is the single-channel pixel value bit depth, M and N are the image width and height, x(i,j) and x~(i,j) respectively represent the enhanced The image and the pixel value of the original image at pixel point (i,j).
Insert image description here

Structural similarity SSIM

Natural images have extremely high structure, which is reflected in the strong correlation between the pixels of the image, especially when the space is similar. These correlations carry important information about the structure of objects in the visual scene. By analyzing whether the structural information changes, the distortion of the image can be judged. Most image quality evaluation indicators based on error sensitivity, such as MSE and PSNR, use linear transformation to decompose image signals and do not involve the correlation between two images. SSIM (Structural Similarity) gives a more direct method to compare distortion. The structural difference between an image and a reference image is an indicator of the similarity between two images. It defines the structural information of an image from the perspective of image combination as an attribute that is independent of brightness and contrast and reflects the structure of objects in the scene. The distortion is modeled as a combination of three different factors: brightness, contrast and structure. Image quality is comprehensively evaluated from three aspects: brightness similarity, contrast similarity and structural similarity. The mean is used as an estimate of brightness and the standard deviation is used as an estimate of contrast. , covariance as a measure of structural similarity.
Given two images and , their structural similarity can be given by the formula xx, where μ and σ represent the mean and variance of the image respectively, and σxy represents the difference between image x and image y The covariance of C1 and C2 are constants, which can generally be 0.
Insert image description here

The SSIM value is [-1, 1]. When the two images are exactly the same, the SSIM value is 1. In practical applications, Gaussian functions are generally used to calculate the mean, variance and covariance of images. Since the statistical characteristics of an image are usually unevenly distributed in space, the SSIM is usually calculated locally on the image, and then average pooling is performed to obtain the SSIM of the entire image.

python implementation

Found it online.

SSIM code

#相关操作
#由于使用的高斯函数圆对称,因此相关操作和卷积操作结果相同
def correlation(img,kernal):
    kernal_heigh = kernal.shape[0]
    kernal_width = kernal.shape[1]
    cor_heigh = img.shape[0] - kernal_heigh + 1
    cor_width = img.shape[1] - kernal_width + 1
    result = np.zeros((cor_heigh, cor_width), dtype=np.float64)
    for i in range(cor_heigh):
        for j in range(cor_width):
            result[i][j] = (img[i:i + kernal_heigh, j:j + kernal_width] * kernal).sum()
    return result

#产生二维高斯核函数
#这个函数参考自:https://blog.csdn.net/qq_16013649/article/details/78784791
def gaussian_2d_kernel(kernel_size=11, sigma=1.5):
    kernel = np.zeros([kernel_size, kernel_size])
    center = kernel_size // 2

    if sigma == 0:
        sigma = ((kernel_size - 1) * 0.5 - 1) * 0.3 + 0.8

    s = 2 * (sigma ** 2)
    sum_val = 0
    for i in range(0, kernel_size):
        for j in range(0, kernel_size):
            x = i - center
            y = j - center
            kernel[i, j] = np.exp(-(x ** 2 + y ** 2) / s)
            sum_val += kernel[i, j]
    sum_val = 1 / sum_val
    return kernel * sum_val


#ssim模型
def ssim(distorted_image,original_image,window_size=11,gaussian_sigma=1.5,K1=0.01,K2=0.03,alfa=1,beta=1,gama=1):
    distorted_image=np.array(distorted_image,dtype=np.float64)
    original_image=np.array(original_image,dtype=np.float64)
    if not distorted_image.shape == original_image.shape:
        raise ValueError("Input Imagees must has the same size")
    if len(distorted_image.shape) > 2:
        raise ValueError("Please input the images with 1 channel")
    kernal=gaussian_2d_kernel(window_size,gaussian_sigma)

    #求ux uy ux*uy ux^2 uy^2 sigma_x^2 sigma_y^2 sigma_xy等中间变量
    ux=correlation(distorted_image,kernal)
    uy=correlation(original_image,kernal)
    distorted_image_sqr=distorted_image**2
    original_image_sqr=original_image**2
    dis_mult_ori=distorted_image*original_image
    uxx=correlation(distorted_image_sqr,kernal)
    uyy=correlation(original_image_sqr,kernal)
    uxy=correlation(dis_mult_ori,kernal)
    ux_sqr=ux**2
    uy_sqr=uy**2
    uxuy=ux*uy
    sx_sqr=uxx-ux_sqr
    sy_sqr=uyy-uy_sqr
    sxy=uxy-uxuy
    C1=(K1*255)**2
    C2=(K2*255)**2
    #常用情况的SSIM
    if(alfa==1 and beta==1 and gama==1):
        ssim=(2*uxuy+C1)*(2*sxy+C2)/(ux_sqr+uy_sqr+C1)/(sx_sqr+sy_sqr+C2)
        return np.mean(ssim)
    #计算亮度相似性
    l=(2*uxuy+C1)/(ux_sqr+uy_sqr+C1)
    l=l**alfa
    #计算对比度相似性
    sxsy=np.sqrt(sx_sqr)*np.sqrt(sy_sqr)
    c=(2*sxsy+C2)/(sx_sqr+sy_sqr+C2)
    c=c**beta
    #计算结构相似性
    C3=0.5*C2
    s=(sxy+C3)/(sxsy+C3)
    s=s**gama
    ssim=l*c*s
    return np.mean(ssim)

PSNR code

Two methods are provided, which can be found online.

def psnr_A(img1, img2):
    mse = np.mean((img1 / 1.0 - img2 / 1.0) ** 2)
    if mse < 1e-10:
        return 100
    psnr1 = 20 * math.log10(255 / math.sqrt(mse))
    return psnr1


def psnr_B(img1, img2):  # 第二种法:归一化
    mse = np.mean((img1 / 255.0 - img2 / 255.0) ** 2)
    if mse < 1e-10:
        return 100
    PIXEL_MAX = 1
    psnr2 = 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
    return psnr2

Guess you like

Origin blog.csdn.net/weixin_45246566/article/details/130570797