Various image filtering Filter algorithms based on Matlab (code open source)

Preface: This article is a step-by-step tutorial on various image filtering algorithms under the Matlab platform . It connects programming code with image filtering knowledge, taking actual combat as an example! The image filtering algorithms in the blog include: mean filtering, median filtering, Gaussian filtering, bilateral filtering, and guided filtering. Image filtering algorithm is a required CV course in the field of computer vision and is widely used in all walks of life, especially in the field of scientific research! I hope this blog can give some help to readers and friends in their engineering projects or scientific research lives. (The code at the end of this article is open source!)

General diagram of image filtering algorithm:

1. Image filtering knowledge

1.1 Basic concepts

Image filtering is a common image processing technology that aims to suppress noise in images and improve image quality while retaining image details as much as possible. This technology is a very important step in image preprocessing and will directly affect the subsequent image data mining and analysis performance ( for example: target detection, target segmentation and defogging and rain removal algorithms, etc. )! In fact, the popular convolutional neural network CNN is actually a type of filtering, which uses convolution kernels to extract feature patterns of images. However, in traditional filtering, the convolution kernel used has fixed parameters and is manually designed by experienced people, also known as manual features. The convolution kernel parameters of the convolutional neural network are unknown. They are parameters learned through data-driven learning according to different tasks, and are more adaptable to different tasks. The author adds: Convolution is a very important knowledge point in image algorithms, and readers must be proficient in it!

1.2 Image noise

In digital image processing, due to imperfections in imaging systems , transmission media , and recording equipment , digital images are often contaminated by a variety of noises during their formation, transmission, and recording processes. These noises often appear as some isolated pixels or pixel blocks in the image. They appear in the form of useless information and disrupt the observable information of the image. Therefore, the core purpose of image filtering is to eliminate these noises in order to better extract the features of the image.

★Common image noise classification: (1) Salt and pepper noise; (2) Gaussian noise; (3) Poisson noise

1.2.1 Salt and pepper noise

Salt-and- pepper noise , also known as impulse noise , randomly changes some pixel values, which manifests itself in binary images as making some pixels white and some pixels black. It is the black and white bright and dark point noise produced by the image sensor, transmission channel, decoding process, etc., which is the so-called "snowflake" that the elderly are more familiar with. The author adds: Salt and pepper noise on RGB images can also be red, green and blue (RGB) pixels.

matlab code:

%%椒盐噪声
close all;
clear all;
clc;
I=imread('test.png');
I=im2double(I);
% J = imnoise(I,‘salt & pepper’,d),默认d=0.05
% 在添加类型为Salt & Pepper的噪声时,符号&的前面和后面必须有空格,否则系统会出错
J=imnoise(I,'salt & pepper',0.01);
K=imnoise(I,'salt & pepper',0.05);
% 图像中黑色的像素点为椒盐噪声,白色的像素点为盐噪声
subplot(131),imshow(I);
subplot(132),imshow(J);
subplot(133),imshow(K);

1.2.2 Gaussian noise

Gaussian noise is a type of noise originating from electronic circuit noise and sensor noise caused by low illumination or high temperature. Gaussian noise, also known as normal noise, is the most common noise in nature. Gaussian noise can be eliminated through smoothing filtering technology or image restoration technology.

matlab code:

%%高斯滤波
close all;
clear all;
clc;
I=imread('test1.jpg');
h=0:0.1:1; % h为在[0,1]之间的向量,表示图像的亮度值
v=0.01:-0.001:0; % v为一个长度和h相同,表示与h中亮度对应的高斯噪声的方差
% J=imnoise(I,'localvar',h,v)在图像的不同亮度值上叠加不同方差的高斯噪声,向量h中没有的亮度值将自动插值得到
J=imnoise(I,'localvar',h,v);
subplot(121),imshow(I);
subplot(122),imshow(J);

1.2.3 Poisson noise

Poisson noise is noise that satisfies the Poisson distribution . You will think that it is very similar to the normal distribution. In fact, the more data we collect, the denser the accuracy will be, and its shape will be closer to the Gaussian distribution function, which is the normal distribution. Distribution is a common discrete model that satisfies the exponential function distribution .

The author adds: The effect of adding noise depends to a certain extent on the initial image. Poisson noise and Gaussian noise seem to be exactly the same, but in fact there are some differences. There is a clear difference between salt and pepper noise and Gaussian noise (exponential noise), which has a particularly random feeling. Readers can use their own images for testing and get familiar with these noises!

matlab code:

%%泊松噪声
close all;
clear all;
clc;
I=imread('test3.png');
% J = imnoise(I,‘poisson’)添加泊松噪声给图像I
J=imnoise(I,'poisson');
subplot(121),imshow(I);
subplot(122),imshow(J);

1.3 Image filtering classification

Common linear filters: 
(1) Low-pass filter: allows low frequencies to pass;
(2) High-pass filter: allows high frequencies to pass; 
(3) Band-pass filter: allows frequencies in a certain area to pass;
(4) Band Stop filter: blocks frequencies within a certain range and allows other frequencies to pass;
(5) All-pass filter: allows all frequencies to pass, only changing the phase;
(6) Notch filter Band stop filter: blocks a narrow frequency range from passing special band stop filter.

★According to mathematical properties, image filtering algorithms are usually divided into: linear filtering and nonlinear filtering

Mean filter –> Blur function –>                         Linear filter 
Gaussian filter –> GaussianBlur function –>          Linear  filter
Median filter –> MedianBlur function –>             Nonlinear filter 
Bilateral filter –> BilateralFilter function –>         Nonlinear filter
Guided filter –> GuideFilter function – >            Nonlinear filtering

1.4 Principle of linear filtering

Image linear filtering principle: The output value of each pixel is the weighted sum of the input pixels. That is, an operator that uses the pixel values ​​around a given pixel to determine the final output value of this pixel . In essence, it is the use of a convolution kernel ! ! ! Convolution is the biggest behind-the-scenes BOSS of smoothing/blurring in image processing!

Suppose there is a 7x7 image pixel matrix. Convolution filtering process: 7x7 The above is a 3x3 window, moving from left to right, from top to bottom, the pixels in the image are convolved with the data in the filter window, and the data is retained as a new pixel value. Move one pixel at a time (the filter window and pace can be set by yourself).

2. Mean filtering

2.1 Algorithm Overview

Mean filtering is the simplest smoothing filter . Usually within a square area ( usually 3*3 ) in the picture, the pixel at the center point is the average of the pixel values ​​of all points. Mean filtering is to perform the above operations on the entire image.

Take the average of all pixels under the convolution kernel (Kernel) area and replace the center element, as follows:

Mean filter algorithm case diagram:

2.2 Code and analysis

matlab code:

%%均值滤波 
close all;
clear all;
clc;
I = imread('Noise Image.jpg');  % 读取图像 
%I = rgb2gray(I);  % 转换为灰度图像(如果需要)  
% 定义均值滤波器  
h = ones(3,3) / 9;  % 3x3的均值滤波器   
J = imfilter(I, h);  % 应用滤波器   
% 显示原始图像和滤波后的图像  
subplot(1,2,1), imshow(I), title('初始图像');  
subplot(1,2,2), imshow(J), title('均值滤波后的图像');

Mean filter graph:

Advantages: The mean filter algorithm is simple, the calculation speed is fast, and the algorithm is easy to implement.

Disadvantages: Since mean filtering replaces the value of the central pixel with the average value of neighborhood pixels, it can easily lead to the loss of image details; mean filtering will smooth the high-frequency parts of the image, including edges and details, making the image edges become Vague.

3. Median filtering

3.1 Algorithm Overview

Median filtering is a nonlinear signal processing technique that sorts all pixel values ​​within a pixel neighborhood and takes the median value as output. Median filtering is particularly effective in removing salt and pepper noise .

3.2 Code and analysis

matlab code:

%%中值滤波
close all;
clear all;
clc;
I = imread('Noise Image.jpg');  % 读取图像  
I = rgb2gray(I);  %转换为灰度图像(如果需要)  
J = medfilt2(I);  % 使用2D中值滤波器  % 应用中值滤波    
% 显示原始图像和中值滤波后的图像
subplot(1,2,1), imshow(I), title('原始图像');  
subplot(1,2,2), imshow(J), title('中值滤波后的图像');

Median filter plot:

Advantages: Median filtering can effectively remove salt and pepper noise, additive Gaussian noise and other noise types in the image, making the image clearer; compared with other filtering methods, median filtering can better retain the edges of the image while removing noise. information; the calculation process of median filtering is also simple and easy.

Disadvantages: For images containing many points, lines, and sharp corner details, median filtering may not be applicable because it may change the pixel values ​​of these details; median filtering is a nonlinear filtering method that uses All pixel values ​​within a pixel neighborhood are sorted to select the median value, which can lead to discontinuities in the image.

4. Gaussian filter

4.1 Algorithm Overview

There are many types of Gaussian filters , including low-pass, band-pass and high-pass. The Gaussian filter we usually talk about on images refers to Gaussian Blur, which is a Gaussian low-pass filter. The high-frequency component of the image (the detailed part of the image) is retained, and the low-frequency component of the image (the smooth area of ​​the image) is retained, so after performing 'Gaussian blur' on the image, the image will become blurry. Gaussian blur is very effective at suppressing Gaussian noise (noise that follows a normal distribution).

★Gaussian filtering, like mean filtering, uses a filter window and image for convolution solution. The difference is:

  • The filter window coefficients of the mean filter are all the same, which is 1.
  • The filter window coefficient of the Gaussian filter decreases as the distance from the center of the template increases (obeying the two-dimensional Gaussian distribution).

Gaussian function distribution:

official:

Distribution: Gaussian distribution (normal distribution)

 The normal distribution (Gaussian distribution) is a bell-shaped curve. The closer to the center , the larger the value , and the further away from the center, the smaller the value. When calculating the smoothing result, you only need to use the "center point" as the origin, and assign weights to other points according to their positions on the normal curve to obtain a weighted average. 

4.2 Code and analysis

matlab code:

%%高斯滤波
close all;
clear all;
clc; 
I = imread('Noise Image.jpg');  % 读取图像   
%I = rgb2gray(I);  % 转换为灰度图像(如果需要)    
J = imgaussfilt(I, 2);  % 使用标准差为2的高斯滤波器  % 应用高斯滤波器    
% 显示原始图像和高斯滤波后的图像  
subplot(1,2,1), imshow(I), title('原始图像');  
subplot(1,2,2), imshow(J), title('高斯滤波后的图像');

Gaussian filter graph:

Advantages: Gaussian filtering can smooth the image, remove noise, and retain the overall structure of the image; Gaussian filtering can be controlled by adjusting the standard deviation of the Gaussian kernel, and has high flexibility; Gaussian filtering is fast in calculation when discretized window sliding window convolution is implemented , suitable for real-time processing.

Disadvantages: Gaussian filter will smooth the high-frequency parts of the image, including edges and details, which may cause image edges to be blurred; Gaussian filter requires convolution operation, the computational complexity is relatively high, and may consume a lot of computing resources and time.

5. Bilateral filtering

5.1 Algorithm Overview

Bilateral filtering was proposed by C. Tomasi in 1998 and is a classic nonlinear spatial filtering method. In the formulation of filter sparseness, bilateral filtering also takes into account the difference in Euclidean distance and value between the output pixel and other pixels in the neighborhood, that is, it also takes into account the difference between the spatial domain and the value domain.

(1) Space domain core

The template weight determined by the Euclidean distance of the pixel position \omega _{d} .

are the coordinates of other coefficients of the template window;

is the center coordinate point of the template window;

\sigma _{d}is the standard deviation of the Gaussian kernel function in the spatial domain, which is used to control the weight of the pixel position.

There is no difference between the filter template generated using the formula and the template used by the Gaussian filter. The weight is called the domain kernel, also known as the spatial coefficient and spatial domain. It can be seen from the calculation formula that it calculates the proximity of the adjacent point q to the center point p, so the domain kernel is used to measure the degree of spatial proximity.

(2) Value range kernel 

Template weight determined by the difference in pixel values \omega _{r} .

Among them, q(i,j)are the coordinates of other coefficients of the template window, f(i,j) indicating q(i,j)the pixel value of the image at the point;

p(k,l)is the center coordinate point of the template window, and the corresponding pixel value is f(k,l);

\sigma _{r}is the standard deviation of the Gaussian kernel function on the pixel value domain, which is used to control the weight of the pixel value.

The weight is generally \omega _{r}called a value domain kernel, or a pixel value domain, but whether it is a value domain kernel \omega _{r}or a spatial domain kernel \omega _{d}, its size is between [0 1].

Finally, the sliding window weights of the bilateral filter are obtained by multiplying the above two templates:

Therefore, the data formula of the bilateral filter can be expressed as follows:

One side of the bilateral filter is the Gaussian smoothing filter mentioned above. The greater the physical distance between the two pixels, the smaller the weight, and vice versa. The other side of the bilateral filter is determined by the pixel value range. The two pixels , the smaller the difference in gray value, the less likely it is an edge, and the more it should be smoothed, that is, its weight in the filter should be increased. On the contrary, the larger the difference in pixel values, the more likely it is an edge. You should try your best to retain it.

The process of bilateral filtering is as shown below. On the abrupt edges, pixel difference weights are used, so the edges are well preserved:

5.2 Code and Analysis

matlab code:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%彩色图双边滤波%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function g = bfilt_rgb(f,r,a,b)
% f灰度图;r滤波半径;a全局方差;b局部方差
[x,y] = meshgrid(-r:r);
w1 = exp(-(x.^2+y.^2)/(2*a^2));
f = tofloat(f);%f = im2double(f);
 
h = waitbar(0,'Applying bilateral filter...');
set(h,'Name','Bilateral Filter Progress');
 
fr = f(:,:,1);
fg = f(:,:,2);
fb = f(:,:,3);
[m,n] = size(fr);
fr_temp = padarray(fr,[r r],'symmetric');
fg_temp = padarray(fg,[r r],'symmetric');
fb_temp = padarray(fb,[r r],'symmetric');
[gr,gg,gb] = deal(zeros(size(fr)));
 
 
for i = r+1:m+r
    for j = r+1:n+r
        temp1 = fr_temp(i-r:i+r,j-r:j+r);
        temp2 = fg_temp(i-r:i+r,j-r:j+r);
        temp3 = fb_temp(i-r:i+r,j-r:j+r);
        dr = temp1 - fr_temp(i,j);
        dg = temp2 - fg_temp(i,j);
        db = temp3 - fb_temp(i,j);
        w2 = exp(-(dr.^2+dg.^2+db.^2)/(2*b^2));
        w = w1.*w2;
        gr(i-r,j-r) = sum(sum(temp1.*w))/sum(w(:));
        gg(i-r,j-r) = sum(sum(temp2.*w))/sum(w(:));
        gb(i-r,j-r) = sum(sum(temp3.*w))/sum(w(:));
    end
    waitbar((i-r)/n);
end
g = cat(3,gr,gg,gb);
% g = revertclass(g);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%可以用到的函数%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [out,revertclass]=tofloat(in)
identity=@(x) x;
tosingle=@im2single;
 
table={'uint8',tosingle,@im2uint8
'uint16',tosingle,@im2uint16
'int16',tosingle,@im2int16
'logical',tosingle,@logical
'double',identity,identity
'single',identity,identity};
 
classIndex=find(strcmp(class(in),table(:,1)));
 
if isempty(classIndex)
    error('unsupported input immage class.');
end
 
out=table{classIndex,2}(in);
revertclass=table{classIndex,3};
end
%%双边滤波
close all;
clear all;
clc; 
I = imread('Noise Image.jpg');  % 读取图像   
%I = rgb2gray(I);  % 转换为灰度图像(如果需要)   
J = bfilt_rgb(I, 3, 9, 15);  % 9x9滤波器,空间标准差为15,灰度标准差为15  % 应用双边滤波器    
% 显示原始图像和双边滤波后的图像  
subplot(1,2,1), imshow(I);  
subplot(1,2,2), imshow(J);

Bilateral filter graph:

Advantages: Bilateral filtering can consider spatial neighborhoods and gray value differences at the same time, so it can better retain edge information while smoothing the image; bilateral filtering can effectively remove noise in the image, especially salt and pepper noise; the parameters of bilateral filtering can be Adjust the parameters according to different application scenarios to obtain the best results.

Disadvantages: Bilateral filtering requires convolution operations and sorting operations, which is computationally complex and consumes a lot of computational costs; the parameter selection of bilateral filtering has a great impact on the filtering effect and needs to be carefully adjusted to obtain the best effect.

6. Guided filtering

6.1 Algorithm Overview

The process of guided filtering is shown in the figure below, which achieves excellent edge preservation performance and computational complexity: 

6.2 Code and Analysis

matlab code:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function q = guidedfilter(I, p, r, eps)
%   GUIDEDFILTER   O(1) time implementation of guided filter.
%
%   - guidance image: I (should be a gray-scale/single channel image)
%   - filtering input image: p (should be a gray-scale/single channel image)
%   - local window radius: r
%   - regularization parameter: eps

[hei, wid] = size(I);
N = boxfilter(ones(hei, wid), r); % the size of each local patch; N=(2r+1)^2 except for boundary pixels.

mean_I = boxfilter(I, r) ./ N;
mean_p = boxfilter(p, r) ./ N;
mean_Ip = boxfilter(I.*p, r) ./ N;
cov_Ip = mean_Ip - mean_I .* mean_p; % this is the covariance of (I, p) in each local patch.

mean_II = boxfilter(I.*I, r) ./ N;
var_I = mean_II - mean_I .* mean_I;

a = cov_Ip ./ (var_I + eps); % Eqn. (5) in the paper;
b = mean_p - a .* mean_I; % Eqn. (6) in the paper;

mean_a = boxfilter(a, r) ./ N;
mean_b = boxfilter(b, r) ./ N;

q = mean_a .* I + mean_b; % Eqn. (8) in the paper;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function imDst = boxfilter(imSrc, r)

%   BOXFILTER   O(1) time box filtering using cumulative sum
%
%   - Definition imDst(x, y)=sum(sum(imSrc(x-r:x+r,y-r:y+r)));
%   - Running time independent of r; 
%   - Equivalent to the function: colfilt(imSrc, [2*r+1, 2*r+1], 'sliding', @sum);
%   - But much faster.

[hei, wid] = size(imSrc);
imDst = zeros(size(imSrc));

%cumulative sum over Y axis
imCum = cumsum(imSrc, 1);
%difference over Y axis
imDst(1:r+1, :) = imCum(1+r:2*r+1, :);
imDst(r+2:hei-r, :) = imCum(2*r+2:hei, :) - imCum(1:hei-2*r-1, :);
imDst(hei-r+1:hei, :) = repmat(imCum(hei, :), [r, 1]) - imCum(hei-2*r:hei-r-1, :);

%cumulative sum over X axis
imCum = cumsum(imDst, 2);
%difference over Y axis
imDst(:, 1:r+1) = imCum(:, 1+r:2*r+1);
imDst(:, r+2:wid-r) = imCum(:, 2*r+2:wid) - imCum(:, 1:wid-2*r-1);
imDst(:, wid-r+1:wid) = repmat(imCum(:, wid), [1, r]) - imCum(:, wid-2*r:wid-r-1);
end
%%灰度图引导滤波
close all;
clear all;
clc; 
I = imread('Noise Image.jpg');  % 读取图像
if length(size(I))>2
    I=rgb2gray(I);
end
I = double(I) / 255;
p = I;
r = 9; 
eps = 0.4^2;      %0.021^2  0.04^2
J = guidedfilter(I,p,r,eps);           %引导滤波
% 显示原始图像和双边滤波后的图像  
subplot(1,2,1), imshow(I);  
subplot(1,2,2), imshow(J);

Guided filter map:

Advantages: Guided filtering can use the edge information in the guide image to make the edges of the output image similar to the guide image, so it can better retain the edge information while smoothing the image; because the guide filter uses the information of the guide image, it has good immunity to noise. A certain degree of robustness.

Disadvantages: Guided filtering requires convolution operations and iterative optimization, which may consume a lot of computing resources and time (shorter than bilateral filtering); if there is a large range of noise in the input image, the effect of guided filtering may be affected.

7. Algorithm summary

Image filtering algorithm is a skill that must be mastered in the field of computer vision . Engineers engaged in algorithm research or the embedded AI industry should be proficient in it! Image filtering algorithm With the advancement of science in the times, many new image filtering algorithms have emerged, such as anti-blind convolution filtering, side window box filtering and Wiener filtering. I hope that readers will not blindly pursue various awesome image filtering algorithms in actual engineering projects. In fact, the filter that is suitable for the project is a good filter!

8. Code open source

Code address:  Code resources for various image filtering algorithms based on Matlab - CSDN library

If you don’t have enough points, please follow us and leave your email address in the comment area . The author will provide source code and follow-up questions for free . Please give me a follow! ! !

Guess you like

Origin blog.csdn.net/black_sneak/article/details/134427624