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

1. Basic definition of MSE

The full name of MSE is "Mean Square Error", which means mean square error in Chinese, which is one of the indicators to measure image quality. The calculation principle is to square the difference between the real value and the predicted value, then sum and then average . The formula is as follows:
insert image description here
where, M is the total number of pixels in image I, and N is the total number of pixels in image K. The smaller the MSE value, the more similar the images are . There are four ways to calculate MSE:

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

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

Method 3 : Determine the dimension of the image. If it is three-dimensional, it is an RGB image to find its MSE. If it is two-dimensional, it is a grayscale image to find its MSE.

Method 4 : Same as method 3, normalize MSE

Two, matlab realizes MSE

1. Method 1: rgbMSE.m

function msevalue = rgbMSE(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值相加,注意(:)的用法
msevalue=MSE_RGB/(row*col); % 求出三个通道的平均MSE值
end 

2. Method 2: grayMSE.m

function msevalue = grayMSE(image1,image2)
% image1和image2大小相等
row=size(image1,1); % 图像的长
col=size(image1,2); % 图像的宽
image1=double(image1);
image2=double(image2);
msevalue=sum(sum((image1-image2).^2))/(row*col);
end

3. Method 3: rgbgrayMSE.m

function msevalue = rgbgrayMSE(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
msevalue=sum_mse/(row*col);
end

4. Method 4: NMSE.m

function nmsevalue = NMSE(image1,image2)
% 在归一化MSE时,使用不到图像的长和宽,因为约分相消
% 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
nmsevalue=sum_mse1/sum_mse2;
end

5. The main function main.m

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

grayimage=rgb2gray(imread('boy.jpg'));
attack_grayimage=imnoise(grayimage,'salt & pepper',0.01);
figure(2),
subplot(121),imshow(grayimage);
title('原始图像');
subplot(122),imshow(attack_grayimage);
title('噪声攻击图像');
% =============rgbMSE.m============= %
msevalue1 = rgbMSE(rgbimage,attack_rgbimage);
disp('RGB图像的均方误差:');
disp(msevalue1);
% =============immse============= %
msevalue2 = immse(rgbimage,attack_rgbimage);% immse函数为matlab内置函数,err = immse(X,Y)计算数组 XY 之间的均方误差 (MSE).其将所有图像当成灰度图像处理
disp('matlab函数的均方误差:');
disp(msevalue2);
% =============grayMSE.m============= %
msevalue3 = grayMSE(grayimage,attack_grayimage);
disp('灰度图像的均方误差:');
disp(msevalue3);
% =============rgbgrayMSE.m============= %
msevalue4 = rgbMSE(rgbimage,attack_rgbimage);
disp('RGB图像的均方误差:');
disp(msevalue4);

msevalue5 = grayMSE(grayimage,attack_grayimage);
disp('灰度图像的均方误差:');
disp(msevalue5);
% =============NMSE.m============= %
nmsevalue1 = NMSE(rgbimage,attack_rgbimage);
disp('RGB图像的归一化均方误差:');
disp(nmsevalue1);

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

3. Realize the result analysis

1. Output result

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

1. Note that every time you run the main function main.m file, the output MSE value will be slightly different, you can compare the upper and lower graphs.
insert image description here
2. Only discussing the parameters of salt and pepper noise, we changed the variance of the salt and pepper noise of the main function main.m file to 0.001 , which can be compared with the MSE result obtained above with a variance of 0.01. It can be seen that the obtained MSE is much smaller. Indicates better image quality.

insert image description here
3. The grayscale image MSE calculated by using the built-in function immse of matlab is larger than the grayscale image MSE calculated by our own method.

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

PS: MSE is the L2 loss commonly used in our machine learning

Guess you like

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