-
Notifications
You must be signed in to change notification settings - Fork 64
/
code.txt
205 lines (184 loc) · 3.78 KB
/
code.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
%% 中值滤波
clear;clc;
[I,map]=imread('test2.tif');
figure(1);
subplot(2,2,1);
imshow(I,map);
title('原始图像');
[x,y]=size(I);
n1=3;
n2=5;
n3=7;
N1=ones(n1,n1);
N2=ones(n2,n2);
N3=ones(n3,n3);
x1=double(I);
x2=x1;
for i=2:x-1
for j=2:y-1
N1=[x1(i-1,j-1:j+1),x1(i+1,j-1:j+1)];
N1=sort(N1);
x2(i,j)=N1((n1*n1+1)/2);
end
end
I2=uint8(x2);
subplot(2,2,2);
imshow(I2,map);
title('中值滤波(3x3)')
for i=3:x-2
for j=3:y-2
N2=[x1(i-2,j-2:j+2),x1(i-1,j-2:j+2),x1(i,j-2:j+2),x1(i+1,j-2:j+2),x1(i+2,j-2:j+2)];
N2=sort(N2);
x2(i,j)=N2((n2*n2+1)/2);
end
end
I2=uint8(x2);
subplot(2,2,3);
imshow(I2,map);
title('中值滤波(5x5)')
for i=4:x-3
for j=4:y-3
N3=[x1(i-3,j-3:j+3),x1(i-2,j-3:j+3),x1(i-1,j-3:j+3),x1(i,j-3:j+3),x1(i+1,j-3:j+3),x1(i+2,j-3:j+3),x1(i+3,j-3:j+3)];
N3=sort(N3);
x2(i,j)=N3((n3*n3+1)/2);
end
end
I2=uint8(x2);
subplot(2,2,4);
imshow(I2,map);
title('中值滤波(7x7)')
%% 高斯
[I,map]=imread('test2.tif');
figure(1);
subplot(2,2,1);
imshow(I,map);
title('原始图像');
k=1.5;
n=3;
Img = double(I);
n1=floor((n+1)/2);
for i=1:n
for j=1:n
b(i,j) =exp(-((i-n1)^2+(j-n1)^2)/(4*k))/(4*pi*k);
end
end
Img1=conv2(Img,b,'same');
d=uint8(Img1);
subplot(2,2,2);
imshow(d,map);
title('高斯滤波(3x3)')
n=5;
n1=floor((n+1)/2);%计算图象中心
for i=1:n
for j=1:n
b(i,j) =exp(-((i-n1)^2+(j-n1)^2)/(4*k))/(4*pi*k);
end
end
%生成高斯序列b。
Img1=conv2(Img,b,'same'); %用生成的高斯序列卷积运算,进行高斯滤波
d=uint8(Img1);
subplot(2,2,3);
imshow(d,map);
title('高斯滤波(5x5)')
n=7;
n1=floor((n+1)/2);%计算图象中心
for i=1:n
for j=1:n
b(i,j) =exp(-((i-n1)^2+(j-n1)^2)/(4*k))/(4*pi*k);
end
end
%生成高斯序列b。
Img1=conv2(Img,b,'same'); %用生成的高斯序列卷积运算,进行高斯滤波
d=uint8(Img1);
subplot(2,2,4);
imshow(d,map);
title('高斯滤波(7x7)')
%%调用函数:
%% 中值
[I,map]=imread('test2.tif');
I1=medfilt2(I,[3 3]);
figure(3);
imshow(I1,map);
title('中值滤波平滑后的test1.pgm(3x3)(调用medfilt2函数)');
%% 高斯
[I,map]=imread('test2.tif');
h=fspecial('gaussian',[3 3],1);%建立一个3*3模板的高斯滤波器
I1=imfilter(I,h,'conv');
figure;
imshow(I1,map);
title('高斯滤波平滑后的test1.pgm(3x3)(调用fspecial函数)');
%% Unsharp masking:
IM=imread('test3_corrupt.pgm');
I=IM;
IMSize=size(IM);
IM=cast(IM,'int32');
t=zeros(IMSize(1)+2,IMSize(2)+2);
t=cast(t,'int32');
t(2:IMSize(1)+1,2:IMSize(2)+1) = IM;
t(:,1)=t(:,2);
t(:,IMSize(2)+2)=t(:,IMSize(2)+1);
t(1,:)=t(2,:);
t(IMSize(1)+2,:)=t(IMSize(1)+1,:);
A=1.0;
for i=2:1:IMSize(1)+1
for j=2:1:IMSize(2)+1
IM(i-1,j-1)=t(i,j)*A-(t(i,j)*(-8)+t(i,j-1)+t(i-1,j-1)+t(i-1,j)+t(i,j+1)+t(i+1,j)+t(i+1,j+1)+t(i+1,j-1)+t(i-1,j+1));
end
end
IM=cast(IM,'uint8');
figure(1);
subplot(1,2,1)
imshow(I);
title('原图像');
subplot(1,2,2)
imshow(IM);
title('usharp masking');
%% Sobel:
I=imread('test3_corrupt.pgm');
figure(2);
subplot(1,2,1);
imshow(I);
title('原图像');
[N,M]=size(I);
I=double(I);
h1=[-1,0,1;-2,0,2;-1,0,1];
h2=[-1,-2,-1;0,0,0;1,2,1];
Gx=conv2(I,h1,'same');
Gy=conv2(I,h2,'same');
F=abs(Gx)+abs(Gy);
for i=1:N
for j=1:M
I(i,j)=F(i,j);
end
end
I=uint8(I);
subplot(1,2,2);
imshow(I);
title('Sobel');
%% Laplace:
I = imread('test3_corrupt.pgm');
[N,M]=size(I);
figure(3);
subplot(1,2,1);
imshow(I);
title('原图像');
t=ones(N,M);
t=I;
for i=2:N-1
for j=2:M-1
I(i,j)=t(i+1,j)+t(i-1,j)+t(i,j+1)+t(i,j-1)-4*t(i,j);
end
end
subplot(1,2,2);
imshow(I);
title('Laplace');
%% Canny:
I=imread('test3_corrupt.pgm');
figure(4);
subplot(1,2,1);
imshow(I);
title('原图像');
I1=edge(I,'canny');
subplot(1,2,2);
imshow(I1);
title('Canny');