Image restoration for image processing [inverse filtering, Wiener filtering, constrained least squares, Lucy-Richardson and blind deconvolution restoration]

1. The difference between image restoration and image enhancement

The purpose of image enhancement is to remove noise, reveal details that have been blurred or simply highlight features in an image that are of interest to the reader, regardless of the cause of image degradation. Image restoration is to use some prior knowledge of the degradation phenomenon to establish a mathematical model of the degradation phenomenon, and then perform reverse deduction operations based on the model to restore the original scene image . Therefore, image restoration can be understood as the reverse process of image degradation process. Establishing the mathematical model of the reverse process of image restoration is the main task of image restoration.

2. Inverse filter restoration

1. Basic principles

f(x,y) represents the input image, that is, an ideal image without degradation, g(x,y) is the observed image after degradation, and n(x,y) is additive noise. After Fourier transform to the frequency domain, it is:
insert image description here

The purpose of image restoration is to obtain the estimated image F'(u,v) of the original image F(u,v) given G(u,v) and the degradation function H(u,v), as well as relevant knowledge about additive noise. ), making the image as close as possible to the original image F(u,v). The simplest method for restoring an image is to construct the following formula:
insert image description here
Then obtain the estimated value of the image through the inverse Fourier transform of F'(u,v), which is called inverse filtering. Inverse filtering is an unconstrained restoration method. Unconstrained restoration refers to making an estimate of the original image F based on some knowledge of the degradation model H(u,v) and noise N(u,v) when the degraded image G(u,v) is known. '(u, v), making some kind of error criterion determined in advance to be the minimum. In the process of obtaining the solution with the smallest error, there are no constraints. For direct inverse filtering, due to the influence of noise, the estimation formula of the degraded image is:
insert image description here
when performing inverse filtering, if a certain area H(u,v) is 0 or very small, and N(u,v) is not 0 And it is not very small, then the second item in the above formula is often much larger than the first item, so that the noise is amplified and a larger error is generated. In order to prevent the value of H(u,v) from being too small, some restrictions can be added to the inverse filtering, and the restoration is only performed in a limited neighborhood near the origin, which is called pseudo-inverse filtering.

2. Matlab implementation

% 通过逆滤波器对图像进行复原
close all;
clear all;
clc;
I=imread('cameraman.tif');
I=im2double(I);

% 构造高斯低通滤波器
s=fftshift(fft2(im2double(I)));
[a,b]=size(s);
D0=100; % 将高斯低通滤波器的截止频率D0设置为100
a0=round(a/2);
b0=round(b/2);
H=zeros(a,b);
for i=1:a
    for j=1:b
        distance=sqrt((i-a0)^2+(j-b0)^2);    % 根据高斯低通滤波器公式H(u,v)=e^-[D^2(u,v)/2*D0^2] 
        H(i,j)=exp(-(distance^2)/(2*(D0^2))); % exp表示以e为底的指数函数
    end
end

N=0.01*ones(a,b);   % 本例中a=b=256,此处亦有a=size(I,1),b=size(I,2)
N=imnoise(N,'gaussian',0,0.001); % 添加高斯噪声
J=fftfilter(I,H)+N; % 调用频域滤波函数并加入噪声

figure;
subplot(121),imshow(I);
title('原始图像');
subplot(122),imshow(J,[]); % 显示退化图像(采用高斯低通滤波器对图像进行退化,并添加均值为0,方差为0.001的高斯噪声进一步对图像进行退化)
title('退化图像');

HC=zeros(a,b);
M1=H>0.1;
HC(M1)=1./H(M1);
K=fftfilter(J,HC);% 逆滤波操作

HC=zeros(a,b);
M2=H>0.01;
HC(M2)=1./H(M2);
L=fftfilter(J,HC);% 逆滤波操作

figure;
subplot(121),imshow(K,[]);% 逆滤波复原,频率大
title('逆滤波时频率范围较大得到的图像');
subplot(122),imshow(L,[]);% 逆滤波复原,频率小
title('逆滤波时频率范围较小得到的图像');


function Z = fftfilter(X,H)
% 图像的频域滤波处理
% X为输入图像
% H为滤波器
% Z为输出图像
F=fft2(X,size(H,1),size(H,2));% 傅里叶变换
Z=H.*F;                       % 频域滤波
Z=abs(ifft2(ifftshift(Z)));   % 傅里叶反变换
Z=Z(1:size(X,1),1:size(X,2));
end

Realize the effect:
insert image description here
insert image description here

3. Wiener filter restoration

1. Basic principles

Wiener filtering was first proposed by Wiener and applied to one-dimensional signals, and achieved good results. Later, the algorithm was introduced into two-dimensional signal processing and achieved quite satisfactory results, especially in the field of image restoration. Due to its good restoration effect, low calculation amount and excellent anti-noise performance, the Wiener filter has been widely used in the field of image restoration.

In MATLAB software, the function deconvwnr() is used to restore the Wiener filter of the image . The calling format of this function is as follows:
insert image description here

2. Matlab implementation

(1) Restoring motion blurred images through Wiener filtering

close all;
clear all;
clc;
I=imread('onion.png');
I=rgb2gray(I);
I=im2double(I); % 注意一定要将图像类型转换为double类型,方便函数deconvwnr使用
LEN=25;
THETA=20;
% h = fspecial('motion',len,theta)返回一个过滤器,使其在与图像卷积后近似相机的线性运动
% len指定运动的长度,theta指定逆时针方向的运动角度(以度为单位)
% 过滤器将成为水平和垂直运动的矢量。默认的len是9,默认的theta是0,其对应于9个像素的水平运动
PSF=fspecial('motion',LEN,THETA);
J=imfilter(I,PSF,'conv','circular');% 表示加入运动模糊
NSR=0;
% 采用函数deconvwnr()进行图像的维纳滤波复原
% 调用格式为J=deconvwnr(I,PSF,NSR)其中PSF为点扩展函数,NSR为信噪比(电子设备中的信号与噪声的比例)
K=deconvwnr(J,PSF,NSR);

subplot(131),imshow(I);
title('原始图像');
subplot(132),imshow(J);
title('运动模糊图像');
subplot(133),imshow(K);
title('维纳滤波复原后的图像');

Achievement effect:
insert image description here
(2) Restoring noise-containing motion blur images through Wiener filtering

close all;
clear all;
clc;
I=imread('cameraman.tif');
I=im2double(I);
LEN=21;
THETA=11;
% h = fspecial('motion',len,theta)返回一个过滤器,使其在与图像卷积后近似相机的线性运动
% len指定运动的长度,theta指定逆时针方向的运动角度(以度为单位)
% 过滤器将成为水平和垂直运动的矢量。默认的len是9,默认的theta是0,其对应于9个像素的水平运动
PSF=fspecial('motion',LEN,THETA);
J=imfilter(I,PSF,'conv','circular');% 表示加入运动模糊
noise_mean=0;
noise_var=0.0001;
K=imnoise(J,'gaussian',noise_mean,noise_var);% 给运动模糊图像添加均值为0,方差为0.0001的高斯噪声

figure(1);
subplot(121),imshow(I);
title('原始图像');
subplot(122),imshow(K);
title('运动模糊和添加噪声图像');

NSR1=0;
L1=deconvwnr(K,PSF,NSR1);% L1为NSR为0时得到的复原图像
NSR2=noise_var/var(I(:));% 加入的高斯噪声方差0.0001与原始灰度图像的方差0.0598之比
L2=deconvwnr(K,PSF,NSR2);% L2为采用真实NSR时得到的复原图像

figure(2);
subplot(121),imshow(L1);
title('NSR为0时的复原图像');
subplot(122),imshow(L2);
title('NSR为真实值时的复原图像');

Achievement effect:
insert image description here
insert image description here
(3) Image restoration through the autocorrelation information of the image

close all;
clear all;
clc;
I=imread('rice.png');
I=im2double(I);
LEN=20;
THETA=10;
% h = fspecial('motion',len,theta)返回一个过滤器,使其在与图像卷积后近似相机的线性运动
% len指定运动的长度,theta指定逆时针方向的运动角度(以度为单位)
% 过滤器将成为水平和垂直运动的矢量。默认的len是9,默认的theta是0,其对应于9个像素的水平运动
PSF=fspecial('motion',LEN,THETA);
J=imfilter(I,PSF,'conv','circular');% 表示加入运动模糊

figure(1);
subplot(121),imshow(I);
title('原始图像');
subplot(122),imshow(J);
title('运动模糊图像');

noise=0.03*randn(size(I));% 返回一个n*n从标准正态分布中得到的随机项的矩阵
K=imadd(J,noise);% 可以使用函数imadd()为图像添加通用噪声(imnoise函数可以用来为图像添加特定噪声比如高斯/泊松/斑点/椒盐噪声)
% fft2-傅里叶变换、ifft2-傅里叶反变换、fftshift-将零频分量移到频谱中心、ifftshift-逆零频平移
% fft2和fftshift配合使用,ifft2和ifftshift配合使用,两个变换方向相反
% 一般都是先使用fft2或ifft2后再使用abs最后使用fftshift或ifftshift
NP=abs(fft2(noise)).^2;
NCORR=ifftshift(real(ifft2(NP)));% 噪声的自相关函数

IP=abs(fft2(I)).^2;
ICORR=ifftshift(real(ifft2(IP)));% 图像的自相关函数
% L=deconvwnr(K,PSF,NCORR,ICORR)该函数中参数NCORR为噪声的自相关函数,ICORR为原始图像的自相关函数
L=deconvwnr(K,PSF,NCORR,ICORR);

figure(2);
subplot(121),imshow(K);
title('运动模糊和添加噪声图像');
subplot(122),imshow(L);
title('维纳滤波复原后的图像');

Achievement effect:
insert image description here
insert image description here
4. Restoration by constrained least squares method

1. Basic principles

In MATLAB software, the function deconvreg() is used to restore the image by constrained least squares method . The calling format of this function is as follows:
insert image description here

2. Matlab implementation

(1) Image restoration by constrained least squares method

close all;
clear all;
clc;
I=imread('rice.png');
I=im2double(I);
% h = fspecial('gaussian',hsize,sigma)返回大小为hsize且具有标准偏差sigma的旋转对称高斯低通滤波器。
% hsize表示过滤器大小,默认值为3*3(本例中过滤器大小为8*8),sigma为滤波器的标准值,单位为像素,默认值为 0.5
PSF=fspecial('gaussian',8,4);
% g=imfilter(f,w,filtering_mode,boundary_options,size_options)
% f为输入图像,w为滤波掩模,g为滤波后图像.filtering_mode用于指定在滤波过程中是使用“corr(相关-默认)”还是“conv(卷积).boundary_options用于处理边界充零问题,边界的大小由滤波器的大小确定
J=imfilter(I,PSF,'conv');

figure(1);
subplot(121),imshow(I);
title('原始图像');
subplot(122),imshow(J);% 显示退化后的图像
title('退化图像');

v=0.02;
K=imnoise(J,'gaussian',0,v);% 添加高斯噪声
NP=v*numel(I);% 返回数组元素的数目,等同于prod(size(I))(prod用于表示数组元素的乘积)
% J=deconvreg(I,PSF,NOISERPOWER)可用于图像的约束最小二乘法复原,其中PSF为扩展函数,NOISEPOWER为噪声强度,默认为0
L=deconvreg(K,PSF,NP);

figure(2);
subplot(121),imshow(K);
title('添加噪声的退化图像');
subplot(122),imshow(L);
title('约束最小二乘法复原后的图像');

Realize the effect:
insert image description here
insert image description here

(2) Image restoration by Lagrangian operator

close all;
clear all;
clc;
I=imread('rice.png');
I=im2double(I);
% h = fspecial('gaussian',hsize,sigma)返回大小为hsize且具有标准偏差sigma的旋转对称高斯低通滤波器。
% hsize表示过滤器大小,默认值为3*3(本例中过滤器大小为10*10),sigma为滤波器的标准值,单位为像素,默认值为 0.5
PSF=fspecial('gaussian',10,5);
% g=imfilter(f,w,filtering_mode,boundary_options,size_options)
% f为输入图像,w为滤波掩模,g为滤波后图像.filtering_mode用于指定在滤波过程中是使用“corr(相关-默认)”还是“conv(卷积).boundary_options用于处理边界充零问题,边界的大小由滤波器的大小确定
J=imfilter(I,PSF,'conv');
v=0.02;
K=imnoise(J,'gaussian',0,v);% 添加高斯噪声
NP=v*numel(I);% 返回数组元素的数目,等同于prod(size(I))(prod用于表示数组元素的乘积)
% [J,LAGRA]=deconvreg(I,PSF,...)该函数返回LAGRA为最终采用的拉格朗日算子(NP指的是噪声强度)
[L,LAGRA]=deconvreg(K,PSF,NP);
% edgetaper函数用于对图像边缘进行模糊处理
edged=edgetaper(K,PSF);

figure(1);
subplot(131),imshow(I);
title('原始图像');
subplot(132),imshow(K);% 显示退化图像
title('退化图像');
subplot(133),imshow(edged);% 显示图像边缘
title('原始图像边缘');
% J=deconvreg(I,PSF,NOISEPOWER,LRANGE)该函数中对参数LRANGE进行设置,其为拉格朗日算子搜索范围
M1=deconvreg(edged,PSF,[],LAGRA);    % 图像复原
M2=deconvreg(edged,PSF,[],LAGRA*30); % 增大拉格朗日算子
M3=deconvreg(edged,PSF,[],LAGRA/30); % 减小拉格朗日算子

figure(2);
subplot(131),imshow(M1);
title('采用拉格朗日算子复原后的图像');
subplot(132),imshow(M2);
title('增大拉格朗日算子复原后的图像');
subplot(133),imshow(M3);
title('缩小拉格朗日算子复原后的图像');

Achievement effect:
insert image description here
insert image description here
5. Lucy-Richardson restoration

1. Basic principles

In the MATLAB software, the function deconvlucy0) uses the accelerated convergence Lucy-Richardson algorithm to restore the image . The detailed call format of this function is as follows:
insert image description here
2. Matlab implementation

(1) Use the Lucy-Richardson algorithm to restore the motion blur image

close all;
clear all;
clc;
I=imread('rice.png');
I=im2double(I);
LEN=30;
THETA=20;
% h = fspecial('motion',len,theta)返回一个过滤器,使其在与图像卷积后近似相机的线性运动
% len指定运动的长度,theta指定逆时针方向的运动角度(以度为单位)
% 过滤器将成为水平和垂直运动的矢量。默认的len是9,默认的theta是0,其对应于9个像素的水平运动
PSF=fspecial('motion',LEN,THETA);
J=imfilter(I,PSF,'circular','conv');% 表示加入运动模糊

figure(1);
subplot(121),imshow(I);
title('原始图像');
subplot(122),imshow(J);% 显示退化图像
title('退化图像');
% 函数deconvlucy采用加速收敛的Lucy-Richardson算法对图像进行复原
% J=deconvlucy(I,PSF,NUMIT)该函数中参数NUMIT为算法的迭代次数,默认值为10
K=deconvlucy(J,PSF,5);% 复原,迭代次数为5
L=deconvlucy(J,PSF,15);% 复原,迭代次数为15(增加迭代次数后图像变得更加清晰)

figure(2);
subplot(121),imshow(K);
title('5次迭代后的图像');
subplot(122),imshow(L);
title('15次迭代后的图像');

Achievement effect:
insert image description here
insert image description here
(2) Using Lucy-Richardson algorithm for Gaussian noise for image restoration

close all;
clear all;
clc;
I=imread('cameraman.tif');
I=im2double(I);
PSF=fspecial('gaussian',7,10); % 产生PSF
v=0.0001;
J=imnoise(imfilter(I,PSF),'gaussian',0,v);% 添加高斯噪声

figure(1);
subplot(121),imshow(I);
title('原始图像');
subplot(122),imshow(J);% 显示退化图像
title('退化图像');

WT=zeros(size(I));
WT(5:end-4,5:end-4)=1;% 设置参数WEIGHT的数值
% J=deconvlucy(I,PSF,NUMIT,DAMPAR,WEIGHT)中NUMIT为算法迭代次数(本例为20),DAMPAR为偏差阈值默认为0,WEIGHT为像素加权值默认为原始图像的数值
K=deconvlucy(J,PSF,20,sqrt(v));
L=deconvlucy(J,PSF,20,sqrt(v),WT);


figure(2);
subplot(121),imshow(K);
title('WEIGHT为默认值时复原后的图像');
subplot(122),imshow(L);
title('WEIGHT设置复原后的图像');

Realize the effect:
insert image description here
insert image description here

6. Blind Deconvolution Restoration

1. Basic principles

The image restoration method introduced earlier requires prior knowledge of the PSF of the degraded image. In practical applications, images are often restored without knowing the PSF. The blind deconvolution restoration method does not need to know the PSF in advance, and can estimate the PSF . The advantage of the blind deconvolution restoration algorithm is that it can still perform restoration without prior knowledge of the degraded image.

In the MATLAB software, the function deconvblind() is used to restore the degraded blurred image by blind deconvolution . The detailed call format of this function is as follows:
insert image description here
2. Matlab implementation

(1) Blind deconvolution algorithm is used to restore motion blurred images

close all;
clear all;
clc;
I=imread('cameraman.tif');
I=im2double(I);
LEN=25;
THETA=20;
% h = fspecial('motion',len,theta)返回一个过滤器,使其在与图像卷积后近似相机的线性运动
% len指定运动的长度,theta指定逆时针方向的运动角度(以度为单位)
% 过滤器将成为水平和垂直运动的矢量。默认的len是9,默认的theta是0,其对应于9个像素的水平运动
PSF=fspecial('motion',LEN,THETA);
J=imfilter(I,PSF,'conv','circular');% 表示加入运动模糊
% [J,PSF]=deconvblind(I,INITPSF,NUMIT),其中输入参数INITPSF为PSF的估计值
% 输出参数PSF为算法实际采用的PSF值,NUMIT为算法的迭代次数默认为10
[K,PSF2]=deconvblind(J,PSF,30);

figure(1);
subplot(121),imshow(PSF,[]);% 显示原PSF
title('原PSF显示为图像');
subplot(122),imshow(PSF2,[]);% 显示估计的PSF
title('估计得到的PSF显示为图像');
axis auto;% axis auto表示根据x,y,z的范围自动确定坐标轴的显示范围

figure(2);
subplot(121),imshow(J);% 显示退化图像
title('退化图像');
subplot(122),imshow(K);% 显示复原图像
title('复原图像');

Achievement effect:
insert image description here
insert image description here
(2)% Blind deconvolution algorithm is used to restore the degraded image

close all;
clear all;
clc;
I=checkerboard(8);% checkerboard函数用于创建棋盘图像,I=checkerboard(n)指定棋盘图像中每个单元的边长为n个像素
PSF=fspecial('gaussian',7,10);
v=0.001;
J=imnoise(imfilter(I,PSF),'gaussian',0,v);% 添加高斯噪声
WT=zeros(size(I));
WT(5:end-4,5:end-4)=1;
% [J,PSF]=deconvblind(I,INITPSF,NUMIT,DAMPAR,WEIGHT),其中输入参数INITPSF为PSF的估计值
% 输出参数PSF为算法实际采用的PSF值,NUMIT为算法的迭代次数默认为10
% DAMPAR为偏移阈值默认值为0,WEIGHT为像素的加权值默认为原始图像的数值
[K,PFS2]=deconvblind(J,PSF,20,10*sqrt(v),WT);

subplot(131),imshow(I);
title('原始图像');
subplot(132),imshow(J);% 显示退化图像
title('退化图像');
subplot(133),imshow(K);% 显示复原图像
title('复原图像');

Achievement effect:
insert image description here
Refer to blog:
(1) Common image processing technology based on MATLAB - image restoration technology
(2) [Digital image processing] Image restoration
(the second article is strongly recommended to read, the blogger uses the real function body of various image restoration methods matlab implemented)

Since I just started learning image processing, I don't understand a lot of knowledge. If there are any mistakes, please correct me, there is a long way to go, and work slowly!

Guess you like

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