Matlab对含噪声图像的滤波操作。

噪声:

  • 高斯噪声(正态分布)
  • 均匀噪声

用到的滤波器:

  • 高斯滤波器
  • 盒型滤波器
  • 中值滤波器

用到的两种方法:

  • 直接conv2
  • fft2

注释很重要

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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
%%C1
figure;
tiledlayout(1,3);
img = imread("\LenaG.bmp");
fft = fft2(img);
nexttile;
imshow(img);
title("LenaG");
fft_shift = double(fftshift(fft));
fft_magnitude_shift = abs(fft_shift);
nexttile;
imshow(log(fft_magnitude_shift),[]); %加对数以便于显示图像,为了更好地显示细节,不进行log变换的话灰度的动态范围被压缩
title("FFT with shift");
fft_angle_shift = angle(fft_shift);
nexttile;
imshow(fft_angle_shift,[]);%根据 C 中像素值的范围缩放显示.使用 [min(C(:))max(C(:))] 作为显示范围。 imshow 将 C 中的最小值显示为黑色,将最大值显示为白色。使用imshow(A,[]),即可把图像矩阵A显示为正常的灰度图像。本来A是0-1,把double拉伸到[0 255]
title("Phase with shift");

%%C2
figure;
tiledlayout(1,2);
img = imread("\LenaG.bmp");
dct = dct2(img);
nexttile
imshow(img);
title('LenaG');
dct_abs = abs(dct);
nexttile;
imshow(log(dct_abs), []);
%colormap(gca, gray)
colorbar;
title('Cosine transform');

%%C3
figure;
tiledlayout(1,2);
img = imread("\LenaG.bmp");
fft2_img = fft2(img);
hartley = real(fft2_img) - imag(fft2_img);
nexttile;
imshow(img);
title('LenaG')
hartley_abs = abs(hartley);
nexttile;
imshow(log(hartley_abs), []);
colormap(gca, gray);
colorbar;
title('Hatley');

%%C4 (a)
figure;
tiledlayout(1,2);
img = imread("\LenaG.bmp");
kernel_r = fspecial('gaussian', size(img), 10); % Compose Gaussian Kernel S.D. = 10
%两个函数,翻转其中一个,再滑动求积分,叫卷积(convultion);不翻转就滑动求积分,叫做互相关(cross-correlation)。如果其中之一是偶函数,那么卷积和互相关效果相同。
%Two functions, flip one of them, and then slide for integration, called convultion; without flipping, slide for integration, called cross-correlation. If one of them is an even function, then convolution and cross-correlation have the same effect.
%卷积意味着把输入信号在时间轴上翻转,然后跟信号处理系统的描述方程(冲激响应)叠加积分.
%Convolution means flipping the input signal on the time axis, and then
%superimposing the integral with the description equation (impulse response) of the signal processing system.
kernel_r = rot90(kernel_r,2);
gaussian_img = img + uint8(20*randn(size(img)));
img = im2double(gaussian_img);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
nexttile;
imshow(img);
title("LenaG");
conv_img = conv2(img,kernel_r);
nexttile;
imshow(conv_img);
title("conv2 10");

%%C4 (all)
img = imread("\LenaG.bmp");
standard_deviation = [10, 20, 30, 5, 3];
kernels = {};
for i = 1:length(standard_deviation)
kernels{i} = fspecial('gaussian', size(img), standard_deviation(i)); % Compose Gaussian Kernel S.D. = 10
%两个函数,翻转其中一个,再滑动求积分,叫卷积(convultion);不翻转就滑动求积分,叫做互相关(cross-correlation)。如果其中之一是偶函数,那么卷积和互相关效果相同。
%Two functions, flip one of them, and then slide for integration, called convultion; without flipping, slide for integration, called cross-correlation. If one of them is an even function, then convolution and cross-correlation have the same effect.
%卷积意味着把输入信号在时间轴上翻转,然后跟信号处理系统的描述方程(冲激响应)叠加积分.
%Convolution means flipping the input signal on the time axis, and then
%superimposing the integral with the description equation (impulse response) of the signal processing system.
kernels{i} = rot90(kernels{i},2);
end
gaussian_img = img + uint8(20*randn(size(img)));
img = im2double(gaussian_img);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
for j=1:length(standard_deviation)
figure;
tiledlayout(1,2);
nexttile;
imshow(img);
title("LenaG");
conv_img = conv2(img,kernels{j});
nexttile;
imshow(conv_img);
title("conv2 "+standard_deviation(j));
end

%模糊半径越大,图像就越模糊。从数值角度看,数值越平滑
%The larger the blur radius, the more blurred the image. From a numerical
%point of view, the smoother the value。
%平滑线性滤波器,它可以降低图像灰度的“尖锐”变化
%Gaussian filter is a smooth linear filter, which can reduce the "sharp" changes in the image gray level


%%C5
tiledlayout(1,3);
img = imread("\LenaG.bmp");
kernel = fspecial('gaussian', size(img), 10); % Compose Gaussian Kernel S.D. = 10
%两个函数,翻转其中一个,再滑动求积分,叫卷积(convultion);不翻转就滑动求积分,叫做互相关(cross-correlation)。如果其中之一是偶函数,那么卷积和互相关效果相同。
%Two functions, flip one of them, and then slide for integration, called convultion; without flipping, slide for integration, called cross-correlation. If one of them is an even function, then convolution and cross-correlation have the same effect.
%卷积意味着把输入信号在时间轴上翻转,然后跟信号处理系统的描述方程(冲激响应)叠加积分.
%Convolution means flipping the input signal on the time axis, and then
%superimposing the integral with the description equation (impulse response) of the signal processing system.
kernel_r = rot90(kernel,2);
gaussian_img = img + uint8(20*randn(size(img)));
img = im2double(gaussian_img);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
nexttile;
imshow(img);
title("LenaG");
conv_img = conv2(img,kernel_r);
nexttile;
imshow(conv_img);
title("conv2 10");
img_fft = fft2(img);
%fft_magnitude_shift = abs(fftshift(img_fft));
%nexttile;
%imshow(log(fft_magnitude_shift),[]);
%title("magnitude 10");
kernel_fft = fft2(kernel);
multiply = img_fft .* kernel_fft;
img_ifft = ifft2(multiply);
result = fftshift(img_ifft);
nexttile;
imshow(result);
title("FFT convolution 10");
assert(isreal(result), "imaginary parts are not zero");

%%C5 (all)
img = imread("\LenaG.bmp");
standard_deviation = [10, 20, 30, 5, 3];
kernels = {};
for i = 1:length(standard_deviation)
kernels{i} = fspecial('gaussian', size(img), standard_deviation(i)); % Compose Gaussian Kernel S.D. = 10
%两个函数,翻转其中一个,再滑动求积分,叫卷积(convultion);不翻转就滑动求积分,叫做互相关(cross-correlation)。如果其中之一是偶函数,那么卷积和互相关效果相同。
%Two functions, flip one of them, and then slide for integration, called convultion; without flipping, slide for integration, called cross-correlation. If one of them is an even function, then convolution and cross-correlation have the same effect.
%卷积意味着把输入信号在时间轴上翻转,然后跟信号处理系统的描述方程(冲激响应)叠加积分.
%Convolution means flipping the input signal on the time axis, and then
%superimposing the integral with the description equation (impulse response) of the signal processing system.
kernels{i} = rot90(kernels{i},2);
end
gaussian_img = img + uint8(20*randn(size(img)));
img = im2double(gaussian_img);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
for j=1:length(standard_deviation)
figure;
tiledlayout(1,3);
nexttile;
imshow(img);
title("LenaG Gaussian");
conv_img = conv2(img,kernels{j});
nexttile;
imshow(conv_img);
title("conv2 "+standard_deviation(j));
img_fft = fft2(img);
%fft_magnitude_shift = abs(fftshift(img_fft));
%nexttile;
%imshow(log(fft_magnitude_shift),[]);
%title("magnitude "+standard_deviation(j));
kernel_fft = fft2(kernels{j});
multiply = img_fft .* kernel_fft;
img_ifft = ifft2(multiply);
result = fftshift(img_ifft);
nexttile;
imshow(result);
title("FFT convolution "+standard_deviation(j));
assert(isreal(result), "imaginary parts are not zero");
end

%卷积是一种运算操作,傅里叶变换是一种变换操作。卷积在图像处理的应用中一般是卷积滤波,
%即用一个卷积模板(卷积核/滤波器)去进行滤波,而傅里叶变换在信号处理中往往是变换时域和频域,在图像处理中便是空域和频域。
%Convolution is an arithmetic operation, and Fourier transform is a transformation operation.
%Convolution in the application of image processing is generally convolution filtering, that is, a convolution template (convolution kernel/filter)
%is used to filter, and the Fourier transform in signal processing often transforms the time domain and the frequency domain. In image processing, it is the spatial domain and the frequency domain.

%Con2: 一个卷积核去对图像进行卷积操作,算法复杂度低
%FFT: 傅里叶变换到时域->在时域进行操作->傅里叶反变换回空域。可能会引入高频分量干扰(Introduce high-frequency component interference)

%%C6
img = imread("\LenaG.bmp");
box = {[5, 5], [10, 10], [20, 20]};

kernels = {};
for i = 1:length(box)
img_size = size(img);
kernels{i} = zeros(img_size, 'double');
x_number_range = fix(img_size(1)/2 - box{i}(1)/2) : fix(img_size(1)/2 + box{i}(1)/2);
y_number_range = fix(img_size(2)/2 - box{i}(2)/2) : fix(img_size(2)/2 + box{i}(2)/2);
kernels{i}(x_number_range, y_number_range) = 1/(numel(x_number_range) * numel(y_number_range));
%box不需要反转
end
gaussian_img = img + uint8(20*randn(size(img)));
img = im2double(gaussian_img);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
for j=1:length(box)
figure;
tiledlayout(1,3);
nexttile;
imshow(img);
title("LenaG Box");
conv_img = conv2(img,kernels{j});
nexttile;
imshow(conv_img);
title("conv2 "+box{j}(1)+" * "+box{j}(1));
img_fft = fft2(img);
%fft_magnitude_shift = abs(fftshift(img_fft));
%nexttile;
%imshow(log(fft_magnitude_shift),[]);
%title("magnitude "+standard_deviation(j));
kernel_fft = fft2(kernels{j});
multiply = img_fft .* kernel_fft;
img_ifft = ifft2(multiply);
result = fftshift(img_ifft);
nexttile;
imshow(result);
title("FFT convolution "+standard_deviation(j));
assert(isreal(result), "imaginary parts are not zero");
end
%box滤波是一种averaging filter,是方块区域 N*M 内,中心点的像素为全部点像素值的平均值。不能很好地保护图像细节,在图像去噪的同时也破坏了图像的细节,从而使图像变得模糊
%Box filtering means that the pixel at the center point in the box area N*M is the average of all pixel values.
%Can not protect the image details well, it also destroys the details of the image while denoising the image, which makes the image blurry
%所以盒子越大,需要平均的范围越大,越模糊
%The larger the range of the mean,the larger range needs average, the more fuzzy


%%C7
img = imread("\LenaG.bmp");
gaussian_img = img + uint8(20*randn(size(img)));
img = im2double(gaussian_img);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
medfilt = {[3, 3],[5, 5],[7, 7]};
kernels = {};
for i = 1:length(medfilt)
figure;
tiledlayout(1, 2);
nexttile;
imshow(img);
title("LenaG medfilt");
img_medfilt = medfilt2(img, medfilt{i});
nexttile;
imshow(img_medfilt);
title("medfilt "+medfilt{i}(1)+" * "+medfilt{i}(1));
end


%中值滤波是一种非线性数字滤波器,半径越大,细节越少,所以越大,细节越看不清楚
%Median filter is a non-linear digital filter, the less the details, so when the parameter is bigger, the less clear the details
%Set the gray value of each pixel to the median of the gray values of all pixels in a certain area window at that point.
%在均值滤波器中,由于噪声成分被放入平均计算中,所以输出受到了噪声的影响,但是在中值滤波器中,
%由于噪声成分很难选上,所以几乎不会影响到输出。因此同样用3x3区域进行处理,中值滤波消除的噪声能力更好
%In the average filter, because the noise component is put into the average calculation,
%the output is affected by the noise, but in the median filter, because the noise component is difficult to select,
%it hardly affects the output. Therefore, the 3x3 area is also used for processing, and the noise removal ability of the median filter is better.

%%C8.C4
% rand 生成均匀分布的伪随机数。分布在(0~1)之间
% randn 生成标准正态分布的伪随机数(均值为0,方差为1)
img = imread("\LenaG.bmp");
standard_deviation = [10, 20, 30, 5, 3];
kernels = {};
for i = 1:length(standard_deviation)
kernels{i} = fspecial('gaussian', size(img), standard_deviation(i)); % Compose Gaussian Kernel S.D. = 10
%两个函数,翻转其中一个,再滑动求积分,叫卷积(convultion);不翻转就滑动求积分,叫做互相关(cross-correlation)。如果其中之一是偶函数,那么卷积和互相关效果相同。
%Two functions, flip one of them, and then slide for integration, called convultion; without flipping, slide for integration, called cross-correlation. If one of them is an even function, then convolution and cross-correlation have the same effect.
%卷积意味着把输入信号在时间轴上翻转,然后跟信号处理系统的描述方程(冲激响应)叠加积分.
%Convolution means flipping the input signal on the time axis, and then
%superimposing the integral with the description equation (impulse response) of the signal processing system.
kernels{i} = rot90(kernels{i},2);
end
uniform = mean(img, 'all') * 0.2 * 2; % Uniform power is 0.2
uniform_image = img + uint8(uniform * (rand(size(img)) - 0.5));
img = im2double(uniform_image);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
for j=1:length(standard_deviation)
figure;
tiledlayout(1,2);
nexttile;
imshow(img);
title("LenaG");
conv_img = conv2(img,kernels{j});
nexttile;
imshow(conv_img);
title("conv2 "+standard_deviation(j));
end

%%C8.C6
img = imread("\LenaG.bmp");
box = {[5, 5], [10, 10], [20, 20]};

kernels = {};
for i = 1:length(box)
img_size = size(img);
kernels{i} = zeros(img_size, 'double');
x_number_range = fix(img_size(1)/2 - box{i}(1)/2) : fix(img_size(1)/2 + box{i}(1)/2);
y_number_range = fix(img_size(2)/2 - box{i}(2)/2) : fix(img_size(2)/2 + box{i}(2)/2);
kernels{i}(x_number_range, y_number_range) = 1/(numel(x_number_range) * numel(y_number_range));
%box不需要反转
end
uniform = mean(img, 'all') * 0.2 * 2; % Uniform power is 0.2
uniform_image = img + uint8(uniform * (rand(size(img)) - 0.5));
img = im2double(uniform_image);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
for j=1:length(box)
figure;
tiledlayout(1,3);
nexttile;
imshow(img);
title("LenaG Box");
conv_img = conv2(img,kernels{j});
nexttile;
imshow(conv_img);
title("conv2 "+box{j}(1)+" * "+box{j}(1));
img_fft = fft2(img);
%fft_magnitude_shift = abs(fftshift(img_fft));
%nexttile;
%imshow(log(fft_magnitude_shift),[]);
%title("magnitude "+standard_deviation(j));
kernel_fft = fft2(kernels{j});
multiply = img_fft .* kernel_fft;
img_ifft = ifft2(multiply);
result = fftshift(img_ifft);
nexttile;
imshow(result);
title("FFT Convolution");
assert(isreal(result), "imaginary parts are not zero");
end

%%C8.C7
img = imread("\LenaG.bmp");
uniform = mean(img, 'all') * 0.25 * 2; % Uniform power is 0.25
uniform_image = img + uint8(uniform * (rand(size(img)) - 0.5));
img = im2double(uniform_image);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
medfilt = {[3, 3],[5, 5],[7, 7]};
kernels = {};
for i = 1:length(medfilt)
figure;
tiledlayout(1, 2);
nexttile;
imshow(img);
title("LenaG medfilt");
img_medfilt = medfilt2(img, medfilt{i});
nexttile;
imshow(img_medfilt);
title("medfilt "+medfilt{i}(1)+" * "+medfilt{i}(1));
end

%高斯滤波对于抑制服从正态分布的噪声效果非常好,因为高斯噪声是指它的概率密度函数服从高斯分布(即正态分布)的一类噪声。
%Gaussian filtering is very effective in suppressing noise that obeys the normal distribution
%Gaussian noise refers to a type of noise whose probability density function obeys Gaussian distribution (ie normal distribution)

评论