[Computer Vision Basics] MATLAB program for image filtering operations such as image linear filtering, filter kernel, image noise, and image nonlinear filtering | CSDN creative check-in

Use the MATLAB program to achieve the following requirements:

  1. Capture a region of interest from a color or grayscale image and display it;
  2. To add different degrees of noise to the region of interest in Requirement 1, it is required to add at least three types of noise, such as Gaussian, salt and pepper, local variance, etc., and each type of noise needs to be added in the form of two different noise parameters.
  3. Design a linear filtering method to denoise the noise image added in requirement 2 (one type of noise is enough), and it is required to remove the noise while retaining the original image information to the greatest extent;
  4. Design a nonlinear filtering method to denoise the noise image added in requirement 2 (one type of noise is enough), and it is required to remove the noise while retaining the original image information to the greatest extent.

 The MATLAB program code is as follows:

%% 1、截取感兴趣区域
I=imread('lenag.bmp');
Ix=I(100:180,100:180);
%Ix=imcrop(I); %可交互式选择感兴趣区域
figure('Name','截取感兴趣区域','NumberTitle','off')
subplot(1,2,1),imshow(I),title('原图像')
subplot(1,2,2),imshow(Ix),title('感兴趣区域')
%% 2、分别添加三种噪声
%Gaussian noise
gn=imnoise(Ix,'gaussian',0,0.02);
gn1=imnoise(Ix,'gaussian',0,0.1);
figure('Name','添加高斯噪声','NumberTitle','off')
subplot(2,3,1),imshow(Ix),title('原感兴趣区域');
subplot(2,3,2),imshow(gn),title('方差0.02高斯噪声');
subplot(2,3,3),imshow(gn1),title('方差0.1高斯噪声');
subplot(2,3,4),imhist(Ix),title('原图直方图');
subplot(2,3,5),imhist(gn),title('方差0.02高斯噪声图像直方图');
subplot(2,3,6),imhist(gn1),title('方差0.1高斯噪声图像直方图');
%Salt & pepper noise
sp=imnoise(Ix,'salt & pepper',0.05);
sp1=imnoise(Ix,'salt & pepper',0.1);
figure('Name','添加椒盐噪声','NumberTitle','off')
subplot(2,3,1),imshow(Ix),title('原感兴趣区域');
subplot(2,3,2),imshow(sp),title('噪声密度0.05椒盐噪声');
subplot(2,3,3),imshow(sp1),title('噪声密度0.1椒盐噪声');
subplot(2,3,4),imhist(Ix),title('原图直方图');
subplot(2,3,5),imhist(sp),title('密度0.05椒盐噪声图像直方图');
subplot(2,3,6),imhist(sp1),title('密度0.1椒盐噪声图像直方图');
%Speckle noise
sn=imnoise(Ix,'speckle',0.02);
sn1=imnoise(Ix,'speckle',0.1);
figure('Name','添加斑点噪声','NumberTitle','off')
subplot(2,3,1),imshow(Ix),title('原感兴趣区域');
subplot(2,3,2),imshow(sn),title('方差0.02斑点噪声');
subplot(2,3,3),imshow(sn1),title('方差0.1斑点噪声');
subplot(2,3,4),imhist(Ix),title('原图直方图');
subplot(2,3,5),imhist(sn),title('方差0.02斑点噪声图像直方图');
subplot(2,3,6),imhist(sn1),title('方差0.1斑点噪声图像直方图');
%% 3、线性滤波
figure('Name','高斯噪声高斯平滑滤波','NumberTitle','off')
gs=fspecial('gaussian',[4 4],1.9);
w1=imfilter(gn,gs);
subplot(2,3,1),imshow(Ix),title('原感兴趣区域');
subplot(2,3,2),imshow(gn),title('方差0.02高斯噪声');
subplot(2,3,3),imshow(w1),title('高斯平滑滤波');
subplot(2,3,4),imhist(Ix),title('原感兴趣区域直方图');
subplot(2,3,5),imhist(w1),title('高斯平滑滤波直方图');
%% 4、非线性滤波
sel=strel(ones(2)); 
C1=imopen(sp,sel); 
D=imclose(C1,sel);
figure('Name','椒盐噪声图像形态学运算滤波','NumberTitle','off')
subplot(1,3,1),imshow(Ix),title('原感兴趣区域');
subplot(1,3,2),imshow(sp),title('噪声密度0.02椒盐噪声');
subplot(1,3,3),imshow(D),title('形态学滤波');

The principle of program algorithm is as follows:

1. To intercept the region of interest, I chose a grayscale image 'lenag.bmp', and saved it under the path of the program in advance, and used the function imread to read the image, and then used the method of directly intercepting the image matrix To intercept the region of interest in the image, you can also use the function imcrop to intercept the image of interest interactively. Here I use the former for convenience. Finally, use the function imshow to display the selected image and the intercepted image of interest respectively.

2. Add three kinds of noise respectively. I choose to use the function imnoise to add noises of different types and under different parameter conditions to the region of interest of the intercepted image by modifying its parameters. First, select the noise type as 'gaussian' to add the variance to the image with a variance of 0.02 and Gaussian noise of 0.1 (mean value is 0) to obtain two images with Gaussian noise with different variances added; then select the noise type as 'salt & pepper' to add salt and pepper noise with noise density of 0.05 and 0.1 to the image respectively, and get Two images with salt and pepper noise added with different noise densities; finally, select the noise type as 'speckle' to add speckle noise with variances of 0.02 and 0.1 to the images respectively, and get two images with speckle noise with different variances added; use the function imhist respectively The histograms showing the original image and the results of the above processing are compared and analyzed. I will analyze the different visual features of the image obtained by adding different types of noise with different parameters in the experiment summary.

3. In linear filtering, the linear filtering method I choose is Gaussian smoothing filter. Use this filtering method to filter the image with Gaussian noise with a variance of 0.02. Use the function fspecial to obtain a specified filter kernel size and standard The Gaussian filter with the difference value, and then use the function imfilter to use this filter to filter the noise image , and adjust the size of the Gaussian smoothing filter kernel and the value of the standard deviation to filter the image multiple times to obtain a better filtering effect , to retain the information of the original image to the greatest extent, and finally I selected a Gaussian smoothing filter with a size of 4×4 and a standard deviation of 1.9 to filter the image, because through trials, this filter is more effective for images with Gaussian noise added. The filtering effect is better.

4. In the nonlinear filtering , the nonlinear filtering method I choose is morphological operation filtering, which achieves the purpose of filtering by performing morphological opening and closing operations on the noise image successively; using this filtering method to add a noise density of Filter the salt and pepper noise image of 0.05, first use the function strel to generate a 2×2 morphological structure element, and then use the function imopen to use the morphological structure element to open the noise image to make the noise image smooth and disconnected Narrow discontinuities and eliminate small protrusions, and then use the function imclose to use the morphological structural elements to perform closing operations on the results obtained by the open operation, so as to eliminate narrow discontinuities and slender gaps, eliminate small holes, and fill in the edge lines Fracture, and finally make the filtered image remove the added noise to the greatest extent, and retain the information of the original image.

The result of the program running is as follows:

1. Intercept the region of interest

2. Add three kinds of noise respectively

 3. Linear filtering

4. Nonlinear filtering

 Friends who see this, remember to like it before leaving! Thanks!

Follow bloggers to learn more basics of computer vision and MATLAB image processing knowledge!

Original content may not be reproduced without permission.

Guess you like

Origin blog.csdn.net/qq_59049513/article/details/122641988