Skip to content

Commit 5fbfd98

Browse files
committed
Add 04
1 parent 9caf9c8 commit 5fbfd98

File tree

1 file changed

+257
-0
lines changed

1 file changed

+257
-0
lines changed
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
- [图像的滤波与图像增强的Matlab实现](#图像的滤波与图像增强的matlab实现)
2+
- [目的](#目的)
3+
- [内容](#内容)
4+
- [A. 用滤波器祛除图象噪声](#a-用滤波器祛除图象噪声)
5+
- [B. 空间噪声滤波器](#b-空间噪声滤波器)
6+
- [C.用滤波器祛除图象噪声](#c用滤波器祛除图象噪声)
7+
8+
9+
# 图像的滤波与图像增强的Matlab实现
10+
11+
## 目的
12+
13+
1. 了解 MATLAB 工具箱中的滤波器。
14+
2. 掌握空间滤波
15+
3. 学会对图像的空间变换
16+
17+
## 内容
18+
19+
### A. 用滤波器祛除图象噪声
20+
21+
在数字图像处理中,常常会遇到图像中混杂有许多的噪声。因此,在进行图像处理中,有时要先进行祛除噪声的工作。最常用的祛除噪声的方法是用滤波器进行滤波处理。MATLAB 的图像处理工具箱里也设计了许多的滤波器。如均值滤波器、中值滤波器、维纳滤波器等。
22+
23+
(分别用均值滤波,中值滤波,及维纳滤波器祛除加入高斯噪声的图象)
24+
25+
```matlab
26+
I=imread('D:\pic\DIP3E_CH04\FigP0438(left).tif ');
27+
J=imnoise(I,'gaussian',0,0.002);
28+
%进行均值滤波
29+
h=fspecial('average',3);
30+
I2=uint8(round(filter2(h,I)));
31+
%进行中值滤波
32+
I3=medfilt2(J,[3,3]);
33+
%进行维纳滤波
34+
I4=wiener2(J,[3,3]); %进行一次维纳滤波
35+
I5=wiener2(I4,[3,3]);%进行二次维纳滤波
36+
subplot(2,3,1),imshow(I),title('原图象')
37+
subplot(2,3,2),imshow(J),title('加噪声图象')
38+
subplot(2,3,3),imshow(I2),title('均值滤波后图象')
39+
subplot(2,3,4),imshow(I3),title('中值滤波后图象')
40+
subplot(2,3,5),imshow(I4),title('维纳滤波后图象')
41+
subplot(2,3,6),imshow(I5),title('两次维纳滤波后图象')
42+
```
43+
44+
### B. 空间噪声滤波器
45+
46+
```matlab
47+
%用函数imnoise2 生成具有表5.1 中的CDF 的随机数
48+
function R=imnoise2(type,M,N,a,b)
49+
if nargin ==1
50+
a=0;b=1;
51+
M=1;N=1;
52+
elseif nargin ==3
53+
a=0;b=1;
54+
end
55+
switch lower(type)
56+
case 'uniform'
57+
R=a+(b-a)*rand(M,N);
58+
case 'gaussian'
59+
R=a+b*randn(M,N);
60+
case 'salt & pepper'
61+
if nargin <=3
62+
a=0.05;b=0.05;
63+
end
64+
if (a+b)>1;
65+
error('The sum Pa+Pb must not exceed 1.')
66+
end
67+
R(1:M, 1:N) = 0.5;
68+
X=rand(M,N);
69+
c=find(X<=a);
70+
R(c)=0;
71+
u=a+b;
72+
c=find(X>a & X<=u);
73+
R(c)=1;
74+
case 'rayleigh'
75+
R=a+(-b*log(1-rand(M,N))).^0.5;
76+
case 'exponential'
77+
if nargin <=3;
78+
a=1;
79+
end
80+
if a<=0
81+
error('Parameter a must be positive for exponential type.')
82+
end
83+
k=-1/a;
84+
R=k*log(1-rand(M,N));
85+
case 'erlang'
86+
if nargin<=3
87+
a=2;b=5;
88+
end
89+
if (b~=round(b)|b<=0)
90+
error('Parameter b must be a positive integer for Erlang')
91+
end
92+
k=-1/a;
93+
R=zeros(M,N);
94+
for j=1:b
95+
R=R+k*log(1-rand(M,N));
96+
end
97+
otherwise
98+
error('unknown distribution type.')
99+
end
100+
101+
function image=changeclass(class,varargin)
102+
switch class
103+
case 'uint8'
104+
image=im2uint8(varargin{:});
105+
case 'uint16'
106+
image=im2uint16(varargin{:});
107+
case 'double'
108+
image=im2double(varargin{:});
109+
otherwise
110+
error('Unsupported IPT data class.');
111+
end
112+
%%%%% spfilt 函数与表中列出的任何滤波器在空间域执行滤波。
113+
function f = spfilt(g,type,m,n,parameter)
114+
if nargin ==2
115+
m=3;n=3;Q=1.5;d=2;
116+
elseif nargin == 5
117+
Q=parameter;d=parameter;
118+
elseif nargin== 4
119+
Q=1.5; d=2;
120+
else
121+
error ('wrong number of inputs');
122+
end
123+
switch type
124+
case 'amean'
125+
w=fspecial('average',[m,n]);
126+
f=imfilter(g,w, 'replicate');
127+
case 'gmean'
128+
f=gmean(g,m,n);
129+
case 'hmean'
130+
f=harmean(g,m,n);
131+
case 'chmean'
132+
%f=charmean(g,m,n,Q);
133+
f=charmean(g,m,n,Q);
134+
case 'median'
135+
f=medfilt2(g,[m n], 'symmetric');
136+
case 'max'
137+
f=ordfilt2(g,m*n,ones(m,n),'symmetric');
138+
case 'min'
139+
f=ordfilt2(g,1,ones(m,n), 'symmetric');
140+
case 'midpoint'
141+
f1=ordfilt2(g,1,ones(m,n), 'symmetric');
142+
f2=ordfilt2(g,m*n,ones(m,n), 'symmetric');
143+
f=imlincomb(0.5,f1,0.5,f2);
144+
case 'atrimmed'
145+
if(d<0)|(d/2~=round(d/2))
146+
error('d must be a nonnegative, even integer.')
147+
end
148+
f=alphatrim(g,m,n,d);
149+
otherwise
150+
error('Unknown filter type.')
151+
end
152+
153+
154+
function f=gmean(g,m,n)
155+
inclass =class (g);
156+
g=im2double(g);
157+
warning off;
158+
f=exp(imfilter(log(g),ones(m,n),'replicate')).^(1/m/n);
159+
warning on;
160+
f=changeclass(inclass, f);
161+
162+
163+
function f=harmean(g,m,n)
164+
inclass=class(g);
165+
g=im2double(g);
166+
f=m*n./imfilter(1./(g+eps),ones(m,n),'replicate');
167+
f=changeclass(inclass,f);
168+
169+
function f=charmean(g,m,n,q)
170+
inclass=class(g);
171+
g=im2double(g);
172+
f= imfilter(g.^(q+1),ones(m,n),'replicate');
173+
f=f./ (imfilter(g.^q,ones(m,n),'replicate')+eps);
174+
f=changeclass(inclass,f);
175+
176+
function f=alphatrim(g,m,n,d)
177+
inclass = class(g);
178+
g=im2double(g);
179+
f=imfilter(g,ones(m,n),'symmetric');
180+
for k=1:d/2
181+
f=imsubtract(f,ordfilt2(g,k,ones(m,n),'symmetric'));
182+
end
183+
for k=(m*n – (d/2)+1):m*n
184+
f=imsubtract(f,ordfilt2(g,k,ones(m,n),'symmetric'));
185+
end
186+
f=f/(m*n-d);
187+
f=changeclass(inclass,f);
188+
189+
%使用函数spfilt
190+
clear all
191+
clc
192+
f=imread('D:\pic\DIP3E_CH04\FigP0438(left).tif');
193+
[M,N]=size(f);
194+
R=imnoise2('salt & pepper',M,N,0.1,0);%被概率只有0.1 的胡椒噪声污染
195+
c=find(R==0);
196+
gp=f;
197+
gp(c)=0;
198+
figure, imshow(gp);
199+
200+
R=imnoise2('salt & pepper',M,N,0,0.1);
201+
c=find(R==1);
202+
gs=f;
203+
gs(c)=255;
204+
figure,imshow(gs)
205+
206+
fp=spfilt(gp,'chmean',3,3,1.5);%使用Q 为正值的反调和滤波器
207+
figure, imshow(gp);
208+
fs=spfilt(gs,'chmean',3,3,-1.5);
209+
figure, imshow(gs);
210+
211+
fpmax=spfilt(gp,'max',3,3); %使用最大最小滤波器
212+
figure, imshow(gp);
213+
fsmin=spfilt(gs,'min',3,3);
214+
figure, imshow(gs);
215+
```
216+
217+
### C.用滤波器祛除图象噪声
218+
219+
```matlab
220+
%产生一个等角变换用于测试图像
221+
f=checkerboard(50);
222+
s=0.8;
223+
theta=pi/6;
224+
T=[s*cos(theta) s*sin(theta) 0; -s*sin(theta) s*cos(theta) 0; 0 0 1];
225+
tform=maketform('affine',T);
226+
g=imtransform(f,tform);
227+
figure, imshow(g);
228+
229+
g2=imtransform(f,tform,'nearest');
230+
figure, imshow(g2);
231+
232+
g3=imtransform(f,tform,'FillValue',0.5);
233+
figure, imshow(g3);
234+
235+
T2=[1 0 0;0 1 0; 50 50 1];
236+
tform2=maketform('affine',T2);
237+
g4=imtransform(f,tform2);
238+
figure, imshow(g4);
239+
240+
g5=imtransform(f,tform2,'XData',[1 400],'YData',[1
241+
400],'FillValue',0.5);
242+
figure, imshow(g5);
243+
```
244+
245+
246+
247+
参考文献:
248+
249+
[1] [Rafael C. Gonzalez, Richard E. Woods, and Steven L. Eddins. 2003. Digital Image Processing Using MATLAB. Prentice-Hall, Inc., USA.](https://github.com/timerring/digital-image-processing-matlab/blob/main/reference/Digital_Image_Processing_Using_Matlab.pdf)
250+
251+
[2] [阮秋琦. 数字图像处理(MATLAB版)\[M\]. 北京:电子工业出版社, 2014.](https://github.com/timerring/digital-image-processing-matlab/blob/main/reference/Digital_Image_Processing_(MATLAB_version).pdf)
252+
253+
[3] [冈萨雷斯. 数字图像处理(第三版)\[M\]. 北京:电子工业出版社, 2011.](https://github.com/timerring/digital-image-processing-matlab/blob/main/reference/Digital_Image_Processing_(Third_Edition).pdf)
254+
255+
256+
257+
[返回首页](https://github.com/timerring/digital-image-processing-matlab)

0 commit comments

Comments
 (0)