The image quality evaluation index PSNR (peak signal-to-noise ratio) of image processing

1. Basic definition of PSNR

The full name of PSNR is "Peak Signal-to-Noise Ratio", which means peak signal-to-noise ratio in Chinese, which is one of the indicators to measure image quality. PSNR is defined based on MSE (mean square error). For a given original image I of size m*n and noise image K after adding noise to it, its MSE can be defined as: Then PSNR can be defined as: where
insert image description here
MAXI
insert image description here
is The maximum pixel value of the image, the unit of PSNR is dB. If each pixel is represented by 8-bit binary, its value is 2^8-1=255 . But note that this is a calculation method for grayscale images. If it is a color image, it can usually be calculated by the following method:

Method 1 : Calculate the MSE value of each channel of the three channels of the RGB image and then calculate the average value, and then calculate the PSNR

Method 2 : Use matlab's built-in function psnr() directly ( note that this function treats all images as grayscale images ).

Method 3 : Convert the image to YCbCr format, and only calculate the PSNR of the Y component, that is, the brightness component.

2. PSNR Evaluation Criteria

The larger the PSNR value, the better the image quality, generally speaking:

(1) Higher than 40dB: indicating that the image quality is excellent (that is, very close to the original image)
(2) 30-40dB: usually indicates that the image quality is good (that is, the distortion can be detected but acceptable)
(3) 20-30dB: Description Poor image quality
(4) below 20dB: image quality is unacceptable

3. Matlab implements PSNR

1. Method 1: rgbPSNR.m

function psnrvalue = rgbPSNR(image1,image2)
% image1和image2大小相等
row=size(image1,1); % 图像的长
col=size(image1,2); % 图像的宽
% 注意不加下面两行代码,得出的最终PSNR值将比加上偏大
image1=double(image1);
image2=double(image2);

MSE_R=double(zeros(row,col));
MSE_G=double(zeros(row,col));
MSE_B=double(zeros(row,col));
image1_R=image1(:,:,1);  % R通道
image1_G=image1(:,:,2);  % G通道
image1_B=image1(:,:,3);  % B通道
image2_R=image2(:,:,1);
image2_G=image2(:,:,2);
image2_B=image2(:,:,3);
% 计算RGB图像三个通道每个通道的MSE值再求平均值
for i=1:row
    for j=1:col
        MSE_R(i,j)=(image1_R(i,j)-image2_R(i,j))^2;
        MSE_G(i,j)=(image1_G(i,j)-image2_G(i,j))^2;
        MSE_B(i,j)=(image1_B(i,j)-image2_B(i,j))^2;
    end
end
MSE_RGB=sum(MSE_R(:))+sum(MSE_G(:))+sum(MSE_B(:)); % 将RGB三个通道计算的MSE值相加,注意(:)的用法
MSE=MSE_RGB/(row*col);
B=8;         % 编码一个像素所用二进制位数
MAX=2^B-1;   % 图像的灰度级数        
psnrvalue=20*log10(MAX/sqrt(MSE)); % 两个图像的峰值信噪比                     
end

2. Method 2: grayPSNR.m

function psnrvalue = grayPSNR(image1,image2)
% image1和image2大小相等
row=size(image1,1); % 图像的长
col=size(image1,2); % 图像的宽
image1=double(image1);
image2=double(image2);
B=8;         % 编码一个像素所用二进制位数
MAX=2^B-1;   % 图像的灰度级数       
MSE=sum(sum((image1-image2).^2))/(row*col);     % 均方差  
psnrvalue=20*log10(MAX/sqrt(MSE));                        
end

3. Method 3: ycbcrPSNR.m

function psnrvalue = ycbcrPSNR(image1,image2)
% image1和image2大小相等
row=size(image1,1); % 图像的长
col=size(image1,2); % 图像的宽
% rgb2ycbcr函数将 RGB 颜色值转换为 YCbCr 颜色空间
ycbcrimage1=rgb2ycbcr(image1);
ycbcrimage2=rgb2ycbcr(image2);
% 取出Y通道
ycbcrimage1_y=ycbcrimage1(:,:,1);
ycbcrimage2_y=ycbcrimage2(:,:,1);

ycbcrimage1_y=double(ycbcrimage1_y);
ycbcrimage2_y=double(ycbcrimage2_y);

B=8;         % 编码一个像素所用二进制位数
MAX=2^B-1;   % 图像的灰度级数       
MSE=sum(sum((ycbcrimage1_y-ycbcrimage2_y).^2))/(row*col);     % 均方差  
psnrvalue=20*log10(MAX/sqrt(MSE)); 
end

4. Main function main.m

clc;clear;close all;
rgbimage=imread('boy.jpg');
attack_rgbimage=imnoise(rgbimage,'gaussian',0,0.001);
figure(1),
subplot(121),imshow(rgbimage);
title('原始图像');
subplot(122),imshow(attack_rgbimage);
title('噪声攻击图像');

grayimage=rgb2gray(imread('boy.jpg'));
attack_grayimage=imnoise(grayimage,'gaussian',0,0.001);
figure(2),
subplot(121),imshow(grayimage);
title('原始图像');
subplot(122),imshow(attack_grayimage);
title('噪声攻击图像');


% =====================PSNR Test===================== %
% 高于40dB:说明图像质量极好(即非常接近原始图像)
% 3040dB:通常表示图像质量是好的(即失真可以察觉但可以接受)
% 2030dB:说明图像质量差
% 低于20dB:图像质量不可接受

% 注意每次运行产生的PSNR值都会一点点差别
 psnrvalue = rgbPSNR(rgbimage,attack_rgbimage);% 方法一
 disp('RGB图像的峰值信噪比:');
 disp(psnrvalue);
 
 psnrvalue1 = psnr(rgbimage,attack_rgbimage);% 方法二(psnr函数为matlab内置函数,但其将所有图像当成灰度图像处理,得出的PSNR值偏大)
 disp('matlab函数的峰值信噪比:');
 disp(psnrvalue1);
 
 psnrvalue2 = grayPSNR(grayimage,attack_grayimage);% 方法二
 disp('灰度图像的峰值信噪比:');
 disp(psnrvalue2);
 
 psnrvalue3 = ycbcrPSNR(rgbimage,attack_rgbimage);% 方法三
 disp('YCbCr图像Y通道的峰值信噪比:');
 disp(psnrvalue3);

4. Realize the result analysis

1. Output result

RGB image:

insert image description here
Corresponding grayscale image:
insert image description here
PSNR value output by various methods:
insert image description here
2. Results analysis

1. The PSNR value calculated for the RGB image is the smallest, the PSNR value of the grayscale image calculated by the two methods is basically the same, and the PSNR value of the Y channel of the YCbCr image is the largest.

2. Note that every time you run the main function main.m file, the output PSNR value will be slightly different, you can compare the upper and lower pictures.
insert image description here
3. For the three methods proposed above, it does not matter which method is used to calculate PSNR, as long as the experimental part uses the same method. However, methods 1 and 3 are generally used more often.

4. Only the parameters of Gaussian noise are discussed. We change the variance of Gaussian noise in the main function main.m file to 0.1 , which can be compared with the PSNR result obtained above with a variance of 0.001. It can be seen that the obtained PSNR is much smaller .
insert image description here

Guess you like

Origin blog.csdn.net/qq_44111805/article/details/127676377