Digital Image Processing: Experiment 4

  1. Neighborhood averaging method is used to filter the images polluted by salt and pepper noise and Gaussian noise respectively (the noise intensity is set to 0.05), and the following three different masks are required to be used respectively, and the comparisons are made respectively;

insert image description here

  1. Matlab program: (version 2016a)
1.	clc;  
2.	clear;  
3.	close all;  
4.	img=imread('hua.jpg');   
5.	img1=imnoise(img,'salt & pepper',0.05);% 0.05的椒盐噪声
6.	img2 = imnoise(img,'gaussian',0.05);% 0.05的高斯噪声  
7.	H1=[0,1,0;1,1,1;0,1,0]/4;%定义H1掩模  
8.	H2=ones(3,3)/9;%定义H2掩模  
9.	H3=[1,2,1;2,4,2;1,2,1]/16;%定义H3掩模  
10.	
11.	M1=imfilter(img1,H1);%采用H1掩模处理椒盐噪声  
12.	M2=imfilter(img1,H2);%采用H2掩模处理椒盐噪声  
13.	M3=imfilter(img1,H3);%采用H3掩模处理椒盐噪声  
14.	M4=imfilter(img2,H1);%采用H1掩模处理高斯噪声  
15.	M5=imfilter(img2,H2);%采用H2掩模处理高斯噪声  
16.	M6=imfilter(img2,H1);%采用H3掩模处理高斯噪声  
17.	  
18.	subplot(3,3,1),imshow(img); title('原图');   
19.	subplot(3,3,2),imshow(img1); title('加入椒盐噪声(密度:0.05)后效果');   
20.	subplot(3,3,3),imshow(img2); title('加入高斯噪声(密度:0.05)后效果');  
21.	subplot(3,3,4),imshow(M1); title('采用H1掩模处理椒盐噪声图像效果');  
22.	subplot(3,3,5),imshow(M2); title('采用H2掩模处理椒盐噪声图像效果');  
23.	subplot(3,3,6),imshow(M3); title('采用H3掩模处理椒盐噪声图像效果');  
24.	subplot(3,3,7),imshow(M4); title('采用H1掩模处理高斯噪声图像效果');  
25.	subplot(3,3,8),imshow(M5); title('采用H2掩模处理高斯噪声图像效果');  
26.	subplot(3,3,9),imshow(M6); title('采用H3掩模处理高斯噪声图像效果');  

  1. Original image, processed image:
    insert image description here

  2. Explanation of the processing process and results:
    the above program processes images polluted by salt and pepper noise and Gaussian noise by defining three neighborhood average templates H1, H2, and H3 respectively. From the simulation results, it can be seen that the neighborhood average method is very convenient to implement , which is suitable for removing grain noise in the image, but this method not only smooths the image signal, but also blurs the details of the image. From the above processed image, it can be seen that as shown in the figure, the H2 and H3 masks are better at suppressing the salt and pepper noise, and the salt and pepper noise is removed more than H1. For Gaussian noise, the H1 and H3 masks are not as effective in suppressing noise as the H2 mask, and the Gaussian salt noise of H3 still exists, but it is weakened, while the H1 mask has blurred edges, so overall The H2 mask is better for both types of noise.

(Optional) Use the overlimit neighborhood averaging method (threshold method) to filter the image polluted by Gaussian noise (the noise intensity is set to 0.05), and use the Gaussian mask for neighborhood averaging (as shown below).
Matlab program: (version 2016a)
script: test2.m:


1.	% 超限邻域滤波  
2.	clc,clear,close all % 清理命令区、清理工作区、关闭显示图形  
3.	feature jit off % 加速代码运行  
4.	img = imread('hua.jpg');  
5.	img1 = imnoise(img,'gaussian',0.05);%加入噪声密度:0.05的高斯噪声  
6.	img2=rgb2gray(img1);  
7.	img3 = threddmean_filter( img2,5, 5/255 ); % 应用超限邻域滤波  
8.	figure('color',[1,1,1])  
9.	  
10.	subplot(221),imshow(img,[]),title('original image')  
11.	subplot(222),imshow(img1,[]),title('gaussian image')  
12.	subplot(223),imshow(img2,[]),title('灰度图')  
13.	subplot(224),imshow(img3,[]),title('超限邻域滤波效果图')  
可选:效果优化
1.	colormap(jet) % 颜色  
2.	shading interp % 消隐  

Function: threddmean_filter.m:


1.	function Z = threddmean_filter(X,n,thred)  
2.	% 函数对输人图像进行超限邻域平均法滤波  
3.	% 函数输入  
4.	% X:输人二维图像矩阵  
5.	% n:掩膜尺寸  
6.	% thred:阈值  
7.	% 函数输出  
8.	% Z:输出图像矩阵,数据类型与输人相同  
9.	if size(X,3)~=1  
10.	error('图像应该为2维矩阵')  
11.	end  
12.	if ~isa(X,'double')  
13.	X = double(X)/255; % 数据类型  
14.	end  
15.	H = fspecial('average',n); % 均值模板  
16.	Y = imfilter(X, H);  
17.	thre = abs(X-Y)>thred; % 判断哪些是门限  
18.	Z = X; % 赋值  
19.	Z(thre)=Y(thre);  
20.	Z = im2uint8(Z); % 类型转换  
21.	end  
  1. Original image, processed image;
    without color effect display:
    insert image description here

Add color effect display:
insert image description here

  1. Explanation of the process and results of the processing:
    Reference resources: Web page link
    Consulting information: This program mainly refers to the csdn website, but an error occurs when it is run for the first time, prompting that the image input by the overlimit neighborhood filter function is not a two-dimensional matrix, The error analysis is that there is no image grayscale processing, use the data and rgb2gray function to process the image, and then input the result map into the overlimit neighborhood filter function.
    Programming logic:
    Similar to the previous processing method, the image is preprocessed first. The core of programming is the over-limit neighborhood method, and an over-limit neighborhood filter function is used to complete the function of the over-limit neighborhood filter;

The idea of ​​overlimit filter function:

  1. The overlimit filter function has three input values: image, mask, and filter threshold (threshold range is 0-255)
  2. Determine whether the input image format is correct
  3. After the input image format is correct, set the mean template H
  4. Use the imfilter function to obtain the pixel average value of the input image X and assign it to Y
  5. Use abs(XY) to find points in the image greater than the threshold thred

  6. The idea of ​​​​replacing these points with the mean value of the neighborhood pixels is to use the mean value or weighted mean value of the pixel and the pixels in the specified neighborhood as the new value of the pixel, so as to remove the abrupt pixel points, thereby filtering out certain noise. In order to solve the image blur problem caused by the neighborhood averaging method, the threshold method (also known as the overlimit neighborhood averaging method, if the gray value of a pixel is greater than the average value of its neighboring pixels and reaches a certain level, the pixel is judged If it is noise, then replace this pixel value with the mean value of the neighboring pixels; otherwise, the pixel is considered not to be a noise point and will not be replaced.
    Analysis: When the threshold is different, the effect is also different, and the small noise is always filtered out first, However, the threshold value required for large noise points is reduced; when the threshold value is increased, the noise filtering effect is very good, but at the same time, some details of the original image are lost.

2. Use the median filter method to filter the images shown in the figure below. The median filter template is not limited, and you can choose it yourself, and the best effect is appropriate.
insert image description here

  1. Matlab program: (version 2016a)
1.	clc;  
2.	clear;  
3.	close all;  
4.	%初始化  
5.	img=imread('tupian.png');   
6.	img1 = imnoise(img,'gaussian',0.05);%加入噪声密度:0.05的高斯噪声  
7.	img2 = imnoise(img,'salt & pepper',0.05);%加入噪声密度:0.05的椒盐噪声 
8.	
9.	img3=rgb2gray(img1);%灰度处理,灰度处理后的图像是二维矩阵  
10.	img4=rgb2gray(img2);%灰度处理,灰度处理后的图像是二维矩阵  
11.	%降噪处理  
12.	M1=medfilt2(img3,[5 5]);%对有高斯噪声图像进行3x3中值滤波  
13.	M2=medfilt2(img3,[9 9]);  
14.	M3=medfilt2(img3,[16 16]);  
15.	M4=medfilt2(img4,[5 5]);%对有椒盐噪声图像进行3x3中值滤波  
16.	M5=medfilt2(img4,[9 9]);  
17.	M6=medfilt2(img4,[16 16]);  
18.	%显示图像  
19.	subplot(3,3,1),imshow(img); title('原图');   
20.	subplot(3,3,2),imshow(img1); title('加入高斯噪声(密度:0.05)后效果');   
21.	subplot(3,3,3),imshow(img2); title('加入椒盐噪声(密度:0.05)后效果');   
22.	subplot(3,3,4),imshow(M1); title('对有高斯噪声图像进行5x5中值滤波');  
23.	subplot(3,3,5),imshow(M2); title('对有高斯噪声图像进行9x9中值滤波');  
24.	subplot(3,3,6),imshow(M3); title('对有高斯噪声图像进行16x16中值滤波');  
25.	subplot(3,3,7),imshow(M4); title('对有椒盐噪声图像进行5x5中值滤波');  
26.	subplot(3,3,8),imshow(M5); title('对有椒盐噪声图像进行9x9中值滤波');  
27.	subplot(3,3,9),imshow(M6); title('对有jiaoyan噪声图像进行16x16中值滤波');  
  1. Original image, processed image;
    insert image description here

  2. Description of the process and results of the processing;
    the median filter method is used to denoise the image containing Gaussian noise. Because the median filter is a commonly used nonlinear filtering method and the most commonly used preprocessing technology in image processing technology. It can overcome the blurring brought by the linear filter to the image. While effectively removing the grain noise, it can also maintain good edge characteristics, so as to obtain a satisfactory filtering effect. It is especially suitable for removing the salt and pepper noise of the image, so the salt and pepper noise is added. , comparing its different effects on Gaussian noise and salt and pepper noise filtering.
    Programming idea:
    first read and process the image to obtain the grayscale image with different noises, and then use the medfilt2 function in the matlab toolbox to perform median filtering using the templates of 3x3, 5x5, 7x7, 9x9, and 16x16 respectively. The experiment found that the effect of the 5x5.9x9.16x16 templates is more obvious, so these three templates are finally selected as the filtering templates.
    Experimental analysis:
    It can be seen from the simulation results that the median filtering method is simple in operation, easy to implement, and can better protect the boundary, but sometimes it will lose the thin lines and small areas in the image. From the processing effect in the third line of the experimental result figure, it can be seen that the spots of salt and pepper noise are almost completely filtered out, which is very effective for filtering the salt and pepper noise of the image. Therefore, after adding salt and pepper noise to the image, median filtering is applied. For Gaussian noise, as shown in the second row of the result graph, although there are some denoising effects, the effect is not good. And when using the median filter, the size of the window will directly affect the filtering effect, and the two are directly proportional, that is, the larger the window, the better the image denoising effect, but the price is the greater the degree of image blur. Therefore, when we choose the median filter, we should consider it comprehensively.

Guess you like

Origin blog.csdn.net/TianHW103/article/details/127956530