Motion Blur Reduction|Wiener Filter|Image Processing

foreword

那么这里博主先安利一些干货满满的专栏了!

这两个都是博主在学习Linux操作系统过程中的记录,希望对大家的学习有帮助!

操作系统Operating Syshttps://blog.csdn.net/yu_cblog/category_12165502.html?spm=1001.2014.3001.5482Linux Sys https://blog.csdn.net/yu_cblog/category_11786077.html?spm=1001.2014.3001.5482 These two are columns for bloggers to learn data structures and simulate various containers in the STL standard template library.

STL source code analysis https://blog.csdn.net/yu_cblog/category_11983210.html?spm=1001.2014.3001.5482 hand-torn data structure https://blog.csdn.net/yu_cblog/category_11490888.html


1. Summary 

This report uses Matlab to realize Wiener filtering and restore blurred images.

2. Experiment content and purpose

Understand the principles of motion blur and noise, and learn the principle of motion blur reduction Wiener filtering. At the same time, the restoration of blurred pictures is realized through Matlab code.

3. Description of relevant experimental principles

Experimental procedure

The flow is as follows:

  • Motion blur the original image (grayscale image)

  • Add Gaussian noise to the original image

  • Build a Wiener filter

motion blur

Motion blur is the phenomenon in which images are blurred due to camera or object movement. The principle can be described by analog functions. Assuming that over a period of time, an object or camera x_0moves from position to position x_1, the blur over that time period can be described as a formula.
h(t)=\left\{\begin{array}{ll} \frac{1}{t_{\text {exposure }}}, & 0 \leq t \leq t_{\text {exposure }} \\ 0, & \text { otherwise } \end{array}\right.

where t_\text{exposure}is the exposure time, the length of time the camera lens is open. This function indicates that during the exposure time of a moving object or camera, the image of the object will have a certain offset in the image plane, which will cause the image to be blurred. In the air domain, this function can represent a formula.
h(x, y)=\left\{\begin{array}{ll} \frac{1}{t_{\text {exposure }}}, & \text { if } a x+b y+c \leq 0 \\ 0, & \text { otherwise } \end{array}\right.
where a,b,c are parameters describing the direction and speed of motion. It can be seen that this function is expressed as a linear function on the image plane, which represents the moving direction and speed of the object image on the image plane within the camera exposure time.

A blurred image can be obtained by performing a convolution operation on the motion blur function. Assuming that f(x,y) is the original image, g(x,y) is the blurred image, and h(x,y) is the motion blur function, the image calculation is as shown in the formula.

g(x, y)=h(x, y) * f(x, y)=\iint_{-\infty}^{\infty} h(\xi, \eta) f(x-\xi, y- \eta) d \xi d \eta

where \astrepresents the convolution operation. It can be seen that the convolution operation represents the weighted average of the original image under the blur function, and the blurred image is obtained.

In Matlab, we can directly call the 'motion' option of the fspecial function to complete the motion blur transformation, part of the code is as follows:

%% 步骤1:添加运动模糊
img = imread('图像路径'); % 读入图像
img = rgb2gray(img);
motion_kernel_size = 31; % 运动模糊核大小
motion_angle = 30; % 运动方向(角度)
motion_distance = 15; % 运动距离(像素)
motion_kernel = fspecial('motion', motion_distance, motion_angle); % 生成运动模糊核
blurred_img = imfilter(img, motion_kernel, 'conv', 'circular'); % 对原始图像进行运动模糊
figure;
imshow(blurred_img);

Gaussian noise

Gaussian noise is a continuous random signal whose probability density function conforms to a normal distribution. The normal distribution is a common probability distribution, also known as the Gaussian distribution, and its probability density function is shown in the formula.
f(x)=\frac{1}{\sigma \sqrt{2 \pi}} e^{-\frac{(x-\mu)^{2}}{2 \sigma^{2}}}

where \muis the mean of the distribution and \sigmais the standard deviation, xrepresenting a random variable.

%% 步骤2:添加高斯噪声
noise_mean = 0; % 噪声均值
noise_var = 0.01; % 噪声方差
blurred_img = imnoise(blurred_img, 'gaussian', noise_mean, noise_var); % 添加高斯噪声
% figure;
% imshow(blurred_img);

Build a Wiener filter

In the steps of constructing the Wiener filter, the matrix transformation is performed in the frequency domain.

Get the Fourier transform of the motion blur filter

The principle is shown in the formula.
H(u,v) = \mathcal{F} (MotionKernel)

Among them, \mathcal{F}it represents the Fourier transform, H(u,v)represents the frequency domain matrix of the motion blur filter, and MotionKernelrepresents the motion blur kernel generated by the above steps.

Extract noise components

The principle is shown in the formula.
N(u,v) = \mathcal{F}(img_{noise}-img_{original})

Wherein \mathcal{F}represents the Fourier transform, N(u,v)represents the noise component in the frequency domain, img_{noise}represents the noise image matrix after Gaussian noise processing, and img_{original}represents the original matrix.

Get undegraded image

The principle is shown in the formula.
F(u,v) = \mathcal{F}(img_{original})

Building a Venus Filter

The filter principle is shown in the following formula.

The calculation method of signal-to-noise ratio K is shown in the formula.

K = \frac{|N(u,v)|^{2}}{|F(u,v)|^{2}}
get blurry image

The principle is shown in the formula.
G(u,v) = \mathcal{F}(img_{vague})
Among them \mathcal{F}, it represents the Fourier transform, img_{vague}represents the image after motion blur, and G(u,v)represents the frequency domain representation of the blurred picture.

restore picture

The principle is shown in the formula.

F'(u,v) = G(u,v)\cdot \hat{F}(u,v)
The Matlab code for constructing the Wiener filter part is as follows:

%% 步骤2:添加高斯噪声
noise_mean = 0; % 噪声均值
noise_var = 0.01; % 噪声方差
blurred_img = imnoise(blurred_img, 'gaussian', noise_mean, noise_var); % 添加高斯噪声
% figure;
% imshow(blurred_img);
%% 步骤3:构建维纳滤波器
% 1. 构建运动模糊滤波器的傅里叶变化
[m,n] = size(img);
H = fft2(motion_kernel,m,n);
% 2. 提取噪声分量
N = fft2(blurred_img - img);
% 3. 获取未退化图片
F = fft2(img);
% 4. 计算信噪比NSR
K = (abs(N).^2)./(abs(F).^2);
% 5. 搭建维纳滤波器
H_square = abs(H).*abs(H);
F_hat = (1./H).*(H_square./(H_square+K));
% 6. 获取模糊图片
G = fft2(blurred_img);
% 7. 还原图片
F_img = G .* F_hat;

4. Experimental results

Five, the overall code 
 

%% 步骤1:添加运动模糊
img_o = imread('图片路径'); % 读入图像
img = rgb2gray(img_o);
motion_kernel_size = 31; % 运动模糊核大小
motion_angle = 30; % 运动方向(角度)
motion_distance = 15; % 运动距离(像素)
motion_kernel = fspecial('motion', motion_distance, motion_angle); % 生成运动模糊核
G_img = imfilter(img, motion_kernel, 'conv', 'circular'); % 对原始图像进行运动模糊
% figure;
% imshow(blurred_img);
%% 步骤2:添加高斯噪声
noise_mean = 0; % 噪声均值
noise_var = 0.01; % 噪声方差
blurred_img = imnoise(img, 'gaussian', noise_mean, noise_var); % 添加高斯噪声
% figure;
% imshow(blurred_img);
%% 步骤3:构建维纳滤波器
% 1. 构建运动模糊滤波器的傅里叶变化
[m,n] = size(img);
H = fft2(motion_kernel,m,n);
% 2. 提取噪声分量
N = fft2(blurred_img - img);
% 3. 获取未退化图片
F = fft2(img);
% 4. 计算信噪比NSR
K = (abs(N).^2)./(abs(F).^2);
% 5. 搭建维纳滤波器
H_square = abs(H).*abs(H);
F_hat = (1./H).*(H_square./(H_square+K));
% 6. 获取模糊图片
G = fft2(G_img);
% 7. 还原图片
F_img = G .* F_hat;
%% 步骤4:应用维纳滤波器并还原图像
f_img = uint8(ifft2(F_img));
figure;
hold on;
% title('原图');
% imshow(img_o);
% title('灰度图');
% imshow(img);
% title('运动模糊后的图像');
% imshow(G_img);
% title('还原后的图像');
imshow(f_img);

Guess you like

Origin blog.csdn.net/Yu_Cblog/article/details/131751372