Image quality evaluation index RMSE (root mean square error) for image processing

1. Basic definition of RMSE

The full name of MSE is "Root Mean Square Error", which means root mean square error in Chinese and is one of the indicators for measuring image quality. The calculation principle is to square the difference between the true value and the predicted value, then sum and average, and finally take the root . The formula is as follows:
Insert image description here
The smaller the RMSE value, the more similar the images are. There are four ways to calculate RMSE:

Method 1 : Calculate the MSE value of each channel of the three channels of the RGB image, average it, and finally take the root sign.

Method 2 : Directly use matlab's built-in function immse() (note that this function treats all images as grayscale images), and then open the root sign.

Method 3 : Determine the dimension of the image. If it is a three-dimensional image, it is an RGB image and its RMSE is found. If it is two-dimensional, it is a grayscale image and its RMSE is calculated.

Method 4 : Same as method 3, normalize RMSE

2. Matlab implements RMSE

1. Method 1: rgbRMSE.m

function rmsevalue= rgbRMSE(image1,image2)
% image1和image2大小相等
row=size(image1,1); % 图像的长
col=size(image1,2); % 图像的宽
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值相加,注意(:)的用法
rmsevalue=sqrt(MSE_RGB/(row*col)); % 在MSE的基础上开根号
end

2. Method 2: grayRMSE.m

function rmsevalue = grayRMSE(image1,image2)
% image1和image2大小相等
row=size(image1,1); % 图像的长
col=size(image1,2); % 图像的宽
image1=double(image1);
image2=double(image2);
rmsevalue=sqrt(sum(sum((image1-image2).^2))/(row*col)); % 在MSE的基础上开根号
end

3. Method three: rgbgrayRMSE.m

function rmsevalue = rgbgrayRMSE(image1,image2)
% image1和image2大小相等
row=size(image1,1); % 图像的长
col=size(image1,2); % 图像的宽

% 一定要注意转为double类型
image1=double(image1);
image2=double(image2);

dim=length(size(image1));% 图像的维度
if dim==2    % 灰度图像只有二维,彩色图像有三维
    sum_mse=sum(sum((image1-image2).^2));% 两次使用sum()函数,第一次使用sum将每行值相加,第二次使用sum将每行值的和再次相加
else
    sum_mse=sum(sum(sum((image1-image2).^2)));% 三次使用sum()函数,第一次使用sum将每行值相加,第二次使用sum将每行值的和再次相加,第三次使用sum将每个通道值的和再次相加
end
rmsevalue=sqrt(sum_mse/(row*col));
end

4. Method 4: NRMSE.m

function nrmsevalue = NRMSE(image1,image2)

% image1和image2大小相等
row=size(image1,1); % 图像的长
col=size(image1,2); % 图像的宽

% 一定要注意转为double类型
image1=double(image1);
image2=double(image2);

dim=length(size(image1));% 图像的维度

if dim==2    % 灰度图像只有二维,彩色图像有三维
    sum_mse1=sum(sum((image1-image2).^2));% 两次使用sum()函数,第一次使用sum将每行值相加,第二次使用sum将每行值的和再次相加
    sum_mse2=sum(sum(image1.^2));
else
    sum_mse1=sum(sum(sum((image1-image2).^2)));% 三次使用sum()函数,第一次使用sum将每行值相加,第二次使用sum将每行值的和再次相加,第三次使用sum将每个通道值的和再次相加
    sum_mse2=sum(sum(sum(image1.^2)));
end
nrmsevalue=sqrt(sum_mse1/(row*col))/sqrt(sum_mse2/(row*col));
end

5. Main function main.m

clc;clear;close all;
rgbimage=imread('ultraman.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('ultraman.jpg'));
attack_grayimage=imnoise(grayimage,'gaussian',0,0.01);
figure(2),
subplot(121),imshow(grayimage);
title('原始图像');
subplot(122),imshow(attack_grayimage);
title('噪声攻击图像');
% =============rgbRMSE.m============= %
rmsevalue1 = rgbRMSE(rgbimage,attack_rgbimage);
disp('RGB图像的均方根误差:');
disp(rmsevalue1);
% =============immse============= %
rmsevalue2 = sqrt(immse(rgbimage,attack_rgbimage)); % 在使用matlab内置函数immse的基础上开根号
disp('matlab函数的均方误差:');
disp(rmsevalue2);
% =============grayRMSE.m============= %
rmsevalue3 = grayRMSE(grayimage,attack_grayimage);
disp('灰度图像的均方根误差:');
disp(rmsevalue3);
% =============rgbgrayRMSE.m============= %
rmsevalue4 = rgbgrayRMSE(rgbimage,attack_rgbimage);
disp('RGB图像的均方根误差:');
disp(rmsevalue4);

rmsevalue5 = rgbgrayRMSE(grayimage,attack_grayimage);
disp('灰度图像的均方根误差:');
disp(rmsevalue5);
% =============NRMSE.m============= %
rmsevalue6 = NRMSE(rgbimage,attack_rgbimage);
disp('RGB图像的归一化均方根误差:');
disp(rmsevalue6);

rmsevalue7 = NRMSE(grayimage,attack_grayimage);
disp('灰度图像的归一化均方根误差:');
disp(rmsevalue7);

3. Realization result analysis

1. Output results

RGB image:
Insert image description here
corresponding grayscale image:
Insert image description here
RMSE value output by various methods:
Insert image description here
2. Result analysis

1. Note that every time you run the main function main.m file, the output RMSE value will be slightly different. You can compare the upper and lower pictures.
Insert image description here
2. Taking only the parameters of Gaussian noise as a discussion, we change the variance of the salt-and-pepper noise in the main function main.m file to 0.001 . We can compare it with the RMSE result with a variance of 0.01 obtained above. It can be seen that the obtained RMSE is much smaller. Indicates better image quality.

Insert image description here
3. The RMSE of the grayscale image calculated using matlab's built-in function immse is larger than the RMSE of the grayscale image calculated by the method we wrote ourselves.

4. In essence, method three is a combination of method one and method two.

Guess you like

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