Digital Image Processing---Adaptive Median Filtering Experiment (MATLAB Realization with Source Code)

Adaptive median filter experiment (MATLAB implementation)

【Purpose】

1. Master the principle of median filter and adaptive median filter and the filtering process
2. Master the algorithm design of adaptive median filter
3. Further familiarize yourself with MATLAB programming

【Experimental principle】

The idea of ​​median filtering is to compare the size of pixel values ​​in a certain area, and take out the median value as the new value of the center pixel in this area. Assuming that all pixels in a certain area are sorted from small to large, if there are isolated noise points, such as salt and pepper noise, then in the array sorted from small to large, those isolated noises must be distributed on both sides, and the middle Value points can preserve pixel information well, while filtering out the influence of noise points.
The median filter is greatly affected by the size of the filtering window, and is used to eliminate noise and protect image details, and there will be conflicts between the two. If the window size is small, it can better protect some details in the image, but the noise filtering effect will be compromised; conversely, if the window size is larger, it will have a better noise filtering effect, but it will also affect the image Causes a certain blur effect, thus losing part of the detailed information.
The purpose of using the adaptive median filter is to dynamically change the window size of the median filter according to preset conditions, so as to simultaneously take into account the effects of denoising and protecting details. At the same time, it will also judge whether the current pixel is noise according to certain conditions. If it is, the current pixel will be replaced with the median value of the neighborhood; if not, it will not be changed.
The adaptive median filter has three purposes:
(1) to filter salt and pepper noise
(2) to smooth other non-impulse noise
(3) to protect the details and boundary information of the image in the image as much as possible, avoiding fine details of the image edge thicken or thicken.
(4) When the detected noise is not very strong, there is no need to increase the size of the window, which not only reflects the adaptability, but also reduces the time overhead and improves the speed.

【Experimental content】

For a grayscale image I(x,y):
(1) Add salt and pepper noise to I(x,y);
(2) Draw the original image and the image after adding noise;
(3) Use the adaptive median Filtering and traditional median filtering were used to filter the salt and pepper noise images, and compared.

【Analysis of results】

The edge of the image is filled, so the extra area of ​​the edge of the image is also processed by median filtering, and the edge noise is small. Whether it is a median filter or an adaptive median filter, the noise in the image can be filtered out. The effect of the adaptive median filter is better, and the median filter still has some noise that is not filtered out. Moreover, the blurring caused by the traditional median filter is more obvious, while the adaptive median filter preserves the details of the image well. As shown in Figure 1:
insert image description here
**For the nth traditional median filter, the effect of removing salt and pepper noise from the image is better, but the image is more blurred and smooth. **As shown in Figure 2:
insert image description here

【Experiment code】

clc;
I=imread('15.jpg');
I=rgb2gray(I);
I_noise = imnoise(I,'salt & pepper', 0.25);
subplot(231),imshow(I),xlabel("原始图像");
subplot(232),imshow(I_noise),xlabel("受椒盐噪声干扰的图像");
[Im,In]=size(I_noise);
nmin = 3;
nmax = 7;
I_out = I_noise;
%% 边界扩充
I_ex = [zeros((nmax-1)/2,In+(nmax-1));zeros(Im,(nmax-1)/2),I_noise,zeros(Im,(nmax-1)/2);zeros((nmax-1)/2,In+(nmax-1))];
%% 自适应中值滤波
for x =1:Im
    for y = 1:In
        for n = nmin:2:nmax
            Sxy = I_ex(x+(nmax-1)/2-(n-1)/2:x+(nmax-1)/2+(n-1)/2,y+(nmax-1)/2-(n-1)/2:y+(nmax-1)/2+(n-1)/2);
            Smin = min(min(Sxy));
            Smax = max(max(Sxy));
            Smed = median(median(Sxy));
            if Smed > Smin && Smed < Smax                              
                if I_out(x,y) <= Smin || I_out(x,y) >= Smax                                     
                    I_out(x,y) = Smed;                 
                end
                break             
            end
        end     
        I_out(x,y) = Smed;   
    end
end
subplot(233),imshow(I_out),xlabel('尺寸大小为7的中值滤波效果')

%% 传统中值滤波器
Iout = medfilt2(I_noise,[3 3]);
subplot(234),imshow(Iout),xlabel('尺寸为3*3的一次中值滤波')
Iout_1 = medfilt2(Iout,[3 3]);
subplot(235),imshow(Iout_1),xlabel('尺寸为3*3的二次中值滤波')
Iout_2 = medfilt2(Iout_1,[3 3]);
subplot(236),imshow(Iout_2),xlabel('尺寸为3*3的三次中值滤波')

Guess you like

Origin blog.csdn.net/weixin_45818370/article/details/124654001