文章目录
- 一、空间滤波:平滑滤波器---均值滤波 P95 - P96
- 1.1 实验内容
- 1.2 文件目录结构
- 1.3 Matlab代码
- 1.4 实验结果
- 二、空间滤波:平滑滤波器---中值滤波 P97
- 2.1 实验内容
- 2.2 文件目录结构
- 2.3 Matlab代码
- 2.4 实验结果
- 三、空间滤波:锐化空间滤波器---拉普拉斯算子 P100
- 3.1 实验内容
- 3.2 文件目录结构
- 3.3 Matlab代码
- 3.4 实验结果
- 四、空间滤波:锐化空间滤波器---梯度 P103
- 4.1 实验内容
- 4.2 文件目录结构
- 4.3 Matlab代码
- 4.4 实验结果
- 五、混合空间算法 P104
- 5.1 实验内容
- 5.2 文件目录结构
- 5.3 Matlab代码
- 5.4 实验结果
实验所用教材配图资源:数字图像处理-冈萨雷斯-第三版(教材配图)
实验Matlab代码资源:数字图像处理 第三章 灰度变换与空间变换(Matlab实验代码)
一、空间滤波:平滑滤波器—均值滤波 P95 - P96
1.1 实验内容
实验一:依次增大掩模尺寸,观察对图像滤波后的效果
实验二:使用15*15的掩模对图像进行滤波 与 阈值处理
1.2 文件目录结构
1.3 Matlab代码
- 实验一的代码
% 对图像进行均值滤波的实验 P95
close all;clear all;clc;
% 读入图片
in_sub = imread('Fig0333(a).tif');
in = int32(in_sub);
% 简单均值滤波过程
[row,column] = size(in);
s_3 = int32(zeros(row,column));
s_5 = int32(zeros(row,column));
s_9 = int32(zeros(row,column));
s_15 = int32(zeros(row,column));
s_35 = int32(zeros(row,column));for r = 1:rowfor c = 1:columna = 17; % 对应 m = 35sum_3 = int32(0); % 3*3 邻域是像素之和sum_5 = int32(0); % 5*5 邻域是像素之和sum_9 = int32(0); % 9*9 邻域是像素之和sum_15 = int32(0); % 15*15 邻域是像素之和sum_35 = int32(0); % 35*35 邻域是像素之和for i = (r - a):( r + a)if i < 1 || i > row %在界外continue;endfor j = (c - a):(c + a)if j < 1 || j > column %在界外continue;end% 35*35 邻域之和sum_35 = sum_35 + in(i,j);% 3*3 邻域是像素之和b = 1;if (i >= (r - b) && i <= (r + b)) && (j >= (c - b) && (j <= (c + b)))sum_3 = sum_3 + in(i,j);end% 5*5 邻域是像素之和b = 2;if (i >= (r - b) && i <= (r + b)) && (j >= (c - b) && (j <= (c + b)))sum_5 = sum_5 + in(i,j);end% 9*9 邻域是像素之和b = 4;if (i >= (r - b) && i <= (r + b)) && (j >= (c - b) && (j <= (c + b)))sum_9 = sum_9 + in(i,j);end% 15*15 邻域是像素之和b = 7;if (i >= (r - b) && i <= (r + b)) && (j >= (c - b) && (j <= (c + b)))sum_15 = sum_15 + in(i,j);end end end% 对3*3均值滤波器中心对应的新像素赋值s_3(r,c) = sum_3 / 3;% 对5*5均值滤波器中心对应的新像素赋值s_5(r,c) = sum_5 / 5;% 对9*9均值滤波器中心对应的新像素赋值s_9(r,c) = sum_9 / 9;% 对15*15均值滤波器中心对应的新像素赋值s_15(r,c) = sum_15 / 15;% 对35*35均值滤波器中心对应的新像素赋值s_35(r,c) = sum_35 / 35;end
end% 图像显示
figure('name','均值滤波器');
subplot(2,3,1);
% it矩阵元素若为double型,imshow默认灰度级为[0 1]
imshow(in_sub);
title('大小为500*500像素的原图像')subplot(2,3,2);
imshow(mat2gray(s_3));
title('m = 3 的方形均值滤波模板平滑处理的结果')subplot(2,3,3);
imshow(mat2gray(s_5));
title('m = 5 的方形均值滤波模板平滑处理的结果')subplot(2,3,4);
imshow(mat2gray(s_9));
title('m = 9 的方形均值滤波模板平滑处理的结果')subplot(2,3,5);
imshow(mat2gray(s_15));
title('m = 15 的方形均值滤波模板平滑处理的结果')subplot(2,3,6);
imshow(mat2gray(s_35));
title('m = 35 的方形均值滤波模板平滑处理的结果')
- 实验二的代码
% 对图像进行均值滤波、阈值处理的实验 P96
close all;clear all;clc;
% 读入图片
in = imread('Fig0334(outer_space).tif');
in_sub = int16(imread('Fig0334(outer_space).tif'));
% 均值滤波
L = 256;
[row,column] = size(in);
s = int16(zeros(row,column));
s_threshold = int16(zeros(row,column));
for r = 1:rowfor c = 1:columnsum_7 = int16(0);% 对滤波器中的像素进行处理for i = (r - 3):(r + 3)% 越界if i < 1 || i > rowcontinue;endfor j = (c - 3):(c + 3)% 越界if j < 1 || j > columncontinue;endsum_7 = sum_7 + in_sub(i,j);endend% 均值赋值s(r,c) = sum_7 / 7;% 阈值判断if s(r,c) < ((L - 1) / 0.31)s_threshold(r,c) = 0;elses_threshold(r,c) = 1;end end
end%图片显示
figure;
subplot(1,3,1);
imshow(in);
title('(a)来自哈勃太空望远镜的大小为528*485像素的图像');subplot(1,3,2);
imshow(mat2gray(s));
title('(b)由15*15均值模板滤波后的图像');subplot(1,3,3);
imshow(s_threshold,[0 1]);
title('(c)对图像(b)进行阈值处理后的结果');
1.4 实验结果
二、空间滤波:平滑滤波器—中值滤波 P97
2.1 实验内容
使用中值滤波器对椒盐噪声进行滤波
2.2 文件目录结构
2.3 Matlab代码
% 对图像进行中值滤波的实验 P97
close all;clear all;clc;
% 读入图片
in = imread('Fig0335(median_filter).tif');
in_sub = int16(in);
% 中值滤波
[row,column] = size(in);
s_aver = int16(zeros(row,column));
s_med = zeros(row,column);
for r = 1:rowfor c = 1:columnsum = int16(0); % 用于均值滤波的累加a = []; % 用于存储邻域的数值for i = (r - 1):(r + 1)% 越界if i < 1 || i > rowcontinue;endfor j = (c - 1):(c + 1)% 越界if j < 1 || j > columncontinue;end% 累加到sum中sum = sum + in_sub(i,j);% 添加数到数组中a(end+1) = in(i,j);endends_aver(r,c) = sum / 3;s_med(r,c) = median(a); % 计算中位数并赋值给新像素end
end% 显示图像
figure;
subplot(1,3,1);
imshow(in);
title('(a)被椒盐噪声污染的电路板的X射线图像');subplot(1,3,2);
imshow(mat2gray(s_aver));
title('(b)用3*3均值模板降噪后的图像');subplot(1,3,3);
imshow(s_med,[0 255]);
title('(c)用3*3中值滤波器降噪后的图像');
2.4 实验结果
三、空间滤波:锐化空间滤波器—拉普拉斯算子 P100
3.1 实验内容
使用二阶微分的拉普拉斯算子对图像进行锐化处理
3.2 文件目录结构
3.3 Matlab代码
% 对于图像使用拉普拉斯算子滤波器进行锐化处理的实验 P100
close all;clear all;clc;
% 读入图片in = imread('Fig0338(blurry_moon).tif');
in_sub = int32(in);% 拉普拉斯滤波
L = 256;
[row,column] = size(in);
s_laplace_result_4 = int32(zeros(row,column));
s_laplace_8 = int32(zeros(row,column));
for r = 1:rowfor c = 1:column% 将在边界上的像素原样赋值给新的图像if r == 1 || r == row || c == 1 || c == columns_laplace_result_4(r,c) = in_sub(r,c);s_laplace_8(r,c) = in_sub(r,c);continue;end% 使用简化后的中心为5的模板,单个像素输出值g_5 = int32(0);% 中心为-8的模板,单个像素输出值g_8 = int32(0);% 在滤波器模板中处理for i = (r - 1):(r + 1) for j = (c - 1):(c + 1)% 模板中心位置if i == r && j == cg_5 = g_5 + 5 * in_sub(i,j);g_8 = g_8 + (-8) * in_sub(i,j);elseg_8 = g_8 + in_sub(i,j);% 只加水平、垂直方向的值if i == r || j == cg_5 = g_5 - in_sub(i,j);endendendend% 将原图对应像素灰度 加上 使用中心为-4的拉普拉斯滤波器产生的新的像素点灰度 的结果输出if g_5 < 0g_5 = 0;ends_laplace_result_4(r,c) = g_5;% 使用中心为-8的拉普拉斯滤波器产生的新的像素点灰度输出s_laplace_8(r,c) = g_8;end
end
s_laplace_sign_8 = s_laplace_8;% 显示图像
figure;
subplot(1,5,1);
imshow(in);
title('(a)月球北极的模糊图像');subplot(1,5,2);
s_laplace_8(s_laplace_8 < 0) = 0;
imshow(mat2gray(s_laplace_8));
title('(b)未标定图像经拉普拉斯滤波后的结果');subplot(1,5,3);
% inshow()会自动对输出图像做标定,即从最小值到最大值映射到指定的灰度级
imshow(mat2gray(s_laplace_sign_8));
title('(c)标定图像经拉普拉斯滤波后的结果');subplot(1,5,4);
imshow(mat2gray(s_laplace_result_4));
title('(d)用滤波模板中心为-4的滤波器锐化后的图像');subplot(1,5,5);
temp = in_sub - s_laplace_8;
temp(temp<0) = 0;
imshow(temp,[0 255]);
title('(e)用滤波模板中心为-8的滤波器锐化后的图像');
3.4 实验结果
四、空间滤波:锐化空间滤波器—梯度 P103
4.1 实验内容
使用一阶微分的梯度对图像进行锐化处理
4.2 文件目录结构
4.3 Matlab代码
% 对图像进行梯度滤波的实验 P103
close all;clear all;clc;
% 读入图片
in = imread('Fig0342contact_lens_original).tif');
in_sub = int32(in);% 梯度滤波
[row,column] = size(in);
s = int32(zeros(row,column));for r = 1:rowfor c=1:column% 对边界处的像素直接复制if r == 1 || r == row || c == 1 || c == columns(r,c) = in_sub(r,c);continue;end% 掩模第一行的计算结果m_1 = -1 * in_sub(r-1,c-1) - 2 * in_sub(r-1,c) - in_sub(r-1,c+1);% 掩模第三行的计算结果m_1 = m_1 + in_sub(r+1,c-1) + 2 * in_sub(r+1,c) + in_sub(r+1,c+1);m = abs(m_1);% 掩模第一列的计算结果m_2 = -1 * in_sub(r-1,c-1) - 2 * in_sub(r,c-1) - in_sub(r+1,c-1);% 掩模第三列的计算结果m_2 = m_2 + in_sub(r-1,c+1) + 2 * in_sub(r,c+1) + in_sub(r+1,c+1);m = m + abs(m_2);s(r,c) = m;end
end% 显示图像
figure;
subplot(1,2,1);
imshow(in);
title('隐形眼镜的光学图像');subplot(1,2,2);
imshow(s,[0 255]);
title('使用Sobel梯度滤波后的结果');
4.4 实验结果
五、混合空间算法 P104
5.1 实验内容
结合使用之前介绍空间滤波方法,对图片进行增强
5.2 文件目录结构
5.3 Matlab代码
% 对图像进行混合滤波的实验 P104
close all;clear all;clc;
in = imread('Fig0343(skeleton_orig).tif');
L = 256;s_laplace = space_filter(in,'laplace');
s_laplace_result = space_filter(in,'laplace_result');
s_gradient = space_filter(in,'gradient');s_5_gradient = space_filter(s_gradient,'average',5);
% s_5_gradient = space_filter(s_5,'gradient');
s_f = mat2gray(s_laplace_result) .* mat2gray(s_5_gradient);
s_g = mat2gray(int32(in)) + mat2gray(s_f);
s_g_1 = s_g;
s_g_1(s_g_1 < 0) = 0;
s_h = double(s_g_1).^0.5;% 显示图像
% 第一个窗口的图
figure;
subplot(2,4,1);
imshow(in);
title('(a)全身骨骼扫面图像');subplot(2,4,2);
imshow(mat2gray(s_laplace));
title('(b)图(a)经拉普拉斯操作后的结果');subplot(2,4,3);
s_laplace_result(s_laplace_result < 0 ) = 0;
imshow(mat2gray(s_laplace_result));
title('(c)图(a)和图(b)相加得到的锐化图像');subplot(2,4,4);
imshow(mat2gray(s_gradient));
title('(d)图(a)经Sobel梯度处理后的结果');% 第二个窗口的图
% figure;
subplot(2,4,5);
imshow(mat2gray(s_5_gradient));
title('(e)使用 5 * 5 均值滤波器平滑后的Sobel图像');subplot(2,4,6);
imshow(mat2gray(s_f));
title('(f)有图(c)和图(e)相乘形成的掩蔽(滤波器模板)图像');subplot(2,4,7);
imshow(mat2gray(s_g));
title('(g)有图(a)和图(f)求和得到的锐化后的图像');subplot(2,4,8);
imshow(mat2gray(s_h));
title('(h)对图(g)应用幂律变换(γ = 0.5,c = 1) 得到最终的结果');