编写一个能够完成两幅图像之间加、减、乘、除四种算术运算的函数。另外,对于两幅图像的乘法,所编写的乘法程序还要能够完成一幅图像乘以一个常数的功能。使用图Fig1.10(4)和Fig1.10(5)验证实验。
编写一个对图像进行空域滤波的函数(实现方法见课本3.5 节)。空域模板的尺寸固定为3 x 3 大小, 但模板中的系数要做为程序的输入参数。
(1.a) 使用上述函数实现图像的平滑滤波,其中的模板使用图3.32 所示的掩模。
(1.b) 使用上述函数实现公式(3.6-8)和公式(3.6-9)描述的拉普拉斯图像锐化增强技术,其中的拉普拉斯模板使用图3.37(c)(d)所示的掩模。
(1.c) 使用上述函数实现公式(3.6-8)和公式(3.6-9)描述的一阶导数锐化增强技术,其中的一阶导数模板使用图3.41(d)(e)所示的掩模。
(1.d) 使用课本图3.38(a),对(1.a)(1.b)(1.c)中的程序实现进行验证,并对结果进行简要对比分析。
(2.a) 编写一个给图像中添加椒盐噪声的程序,程序的输入参数为两个噪声分量的概率值。
(2.b) 编写的程序,实现3 x 3 中值滤波。
(2.c) 给图5.7(a)添加Pa = Pb = 0.2 的椒盐噪声。
(2.d) 对 (2.c)中得到的图像进行中值滤波。说明实验结果与课本图5.10(b)的主要差异。
1.图像间算术运算
图像相加一般用于对同一场景的多幅图像求平均效果,以便有效地降低具有叠加性质的随机噪声。
图像减法也称为差分方法,是一种常用于检测图像变化及运动物体的图像处理方法。
图像乘法运算可以实现掩模操作。图像乘以一个常数通常被称为缩放,是一种常见的图像处理操作。当缩放因数大于1时图像亮度将增强,当缩放因数小于1则会使图像变暗。
图像除法运算可用于校正成像设备的非线性影响,这在特殊形态的图像处理中常常用到,也常用语检测两幅图像间的区别。
2.图像空间滤波
本实验所用空间滤波方法有平滑滤波、拉普拉斯滤波、sobel滤波。
平滑滤波器用于模糊处理和降低噪声。模糊处理经常用于预处理任务中,例如在(大)目标提取之前去除图像中的一些琐碎细节,以及其桥接直线或曲线的缝隙。通过线性滤波和非线性滤波模糊处理,可以降低噪声。
一幅M×N的图像经过一个大小为m×n(m和n是奇数)的加权均值滤波器滤波的过程可由下式给出:
拉普拉斯滤波是通过二阶微分来对图像进行锐化。一个图像函数f(x,y)的拉普拉斯算子定义为
3.非锐化掩蔽
非锐化掩蔽的处理过程由下列步骤组成:
(1) 模糊原图像。
(2) 从原图像中减去模糊图像(产生的差值图像称为模板)。
(3) 将模板加到原图像上。
4.椒盐噪声
对于一幅8比特图像,a =0(黑)和b =255(白)。黑点是胡椒点,白点是盐粒点。
5.中值滤波
中值滤波器是常用的统计排序滤波器,是一种非线性平滑技术,使用一个像素邻域中的灰度级的中值来替代该像素的值。对于某些类型的随机噪声如单极或双极脉冲噪声,中值滤波器可以提供良好的去噪能力,而且比相同尺寸的线性平滑滤波器引起的模糊更少。
要实现中值滤波,首先将像素邻域中所有像素点的灰度值进行排序,然后用排序后的中值替代该像素的值即可。中值滤波对脉冲噪声有良好的滤除作用,特别是在滤除噪声的同时,能够保护信号的边缘,使之不被模糊。
% 主函数
img_A = imread('Fig1.10(4).jpg');
img_B = imread('Fig1.10(5).jpg');
img1 = compute(img_A,'+',img_B); % 加法
img2 = compute(img_A,'-',img_B); % 减法
img3 = compute(img_A,'*',img_B); % 乘法
img4 = compute(img_A,'/',img_B); % 除法
img5 = compute(img_A,'*',0.5,1); % 图像乘以0.5
img6 = compute(img_A,'*',2,1); % 图像乘以2
subplot(1,2,1);imshow(img_A);title('原图像Fig1.10(4)');
subplot(1,2,2);imshow(img_B);title('原图像Fig1.10(5)');
figure;
subplot(3,2,1);imshow(img1);title('图像相加');
subplot(3,2,2);imshow(img2);title('图像相减');
subplot(3,2,3);imshow(img3);title('图像相乘');
subplot(3,2,4);imshow(img4);title('图像相除');
subplot(3,2,5);imshow(img5);title('图像Fig1.10(4)乘以0.5');
subplot(3,2,6);imshow(img6);title('图像Fig1.10(4)乘以2');
function img_out = compute(A,param,B,is_num)
% 该函数用于完成两幅图像之间的加、减、乘、除四种算术运算,
% 其中乘法还能实现一幅图像与一个常数相乘。
% A为输入的第一幅图像,B为输入的第二幅图像或者常数,由is_num确定,
% param指+、-、*、/四个运算符。
[M,N]= size(A);
img_out = zeros(M,N);
if nargin == 3
for m = 1:M
for n = 1:N
if param == '+' % 加法
img_out(m,n) = A(m,n)+B(m,n);
elseif param == '-' % 减法
img_out(m,n) = abs(A(m,n)-B(m,n));
elseif param == '*' % 乘法
img_out(m,n) = A(m,n)*B(m,n);
elseif param == '/' % 除法(考虑除数是否为零)
if(B(m,n) ~= 0)
img_out(m,n) = A(m,n)/B(m,n);
else
img_out(m,n) = 255;
end
end
end
end
elseif nargin ==4 && param == '*' && is_num == 1 % 图像乘以一个常数
for m = 1:M
for n = 1:N
img_out(m,n) = A(m,n)*B;
end
end
else
disp("Input Error");
end
img_out = uint8(img_out);
end
% 主函数
img = imread('Fig3.38(a).jpg');
M_smooth1 = [1 1 1;1 1 1;1 1 1]/9; % 掩模
M_smooth2 = [1 2 1;2 4 2;1 2 1]/16;
M_lap1 = [0 -1 0;-1 -4 -1;0 -1 0];
M_lap2 = [-1 -1 -1;-1 8 -1;-1 -1 -1];
M_sobel1 = [-1 -2 -1;0 0 0;1 2 1];
M_sobel2 = [-1 0 1;-2 0 2;-1 0 1];
img_smooth1 = filter_spatial(img,M_smooth1); % 平滑滤波
img_smooth2 = filter_spatial(img,M_smooth2);
f_lap1 = filter_spatial(img,M_lap1); % 拉普拉斯图像锐化增强
mask_lap1 = compute(img,'-',f_lap1);
img_lap1 = compute(img,'+', mask_lap1);
f_lap2 = filter_spatial(img,M_lap2);
mask_lap2 = compute(img,'-',f_lap2);
img_lap2 = compute(img,'+', mask_lap2);
f_sobel1 = filter_spatial(img,M_sobel1); % 一阶导数锐化增强
mask_sobel1 = compute(img,'-',f_sobel1);
img_sobel1 = compute(img,'+', mask_sobel1);
f_sobel2 = filter_spatial(img,M_sobel2);
mask_sobel2 = compute(img,'-',f_sobel2);
img_sobel2 = compute(img,'+', mask_sobel2);
subplot(2,2,1);imshow(img);title('原图像Fig3.38(a)');
subplot(2,2,3);imshow(img_smooth1);title('平滑滤波:模板a');
subplot(2,2,4);imshow(img_smooth2);title('平滑滤波:模板b');
figure;
subplot(2,2,1);imshow(img_lap1);title('拉普拉斯锐化:模板c');
subplot(2,2,2);imshow(img_lap2);title('拉普拉斯锐化:模板d');
subplot(2,2,3);imshow(img_sobel1);title('sobel锐化:模板d');
subplot(2,2,4);imshow(img_sobel2);title('sobel锐化:模板e');
function img_out = filter_spatial(img_in,M)
% 该函数用于对图像进行空间滤波,空域模板M的尺寸固定为3 x 3 大小。
[X,Y] = size(img_in);
img_out = zeros(X,Y);
for x = 2:X-1
for y = 2:Y-1
img_out(x,y) = img_in(x-1,y-1)*M(1,1)+img_in(x-1,y)*M(1,2)+img_in(x-1,y+1)*M(1,3)...
+img_in(x,y-1)*M(2,1)+img_in(x,y)*M(2,2)+img_in(x,y+1)*M(2,3)...
+img_in(x+1,y-1)*M(3,1)+img_in(x+1,y)*M(3,2)+img_in(x+1,y+1)*M(3,3);
end
end
img_out = uint8(img_out);
end
% 主函数
img = imread('Fig5.07(a).jpg');
img_sp = salt_pepper(img,0.2,0.2); % 椒盐噪声
img_med = filter_median(img_sp); % 中值滤波
subplot(3,1,1);imshow(img);title('原图像Fig5.07(a)');
subplot(3,1,2);imshow(img_sp);title('添加椒盐噪声');
subplot(3,1,3);imshow(img_med);title('中值滤波');
function img_out = salt_pepper(img_in,Pa,Pb)
% 该函数用于给图像添加椒盐噪声。
if Pa>=1 || Pb>=1 % 检测输入参数
error('Input Error');
end
[M,N] = size(img_in);
img_out = img_in;
s = rand(M,N); % 产生M行N列的位于(0,1)区间的随机数
p = rand(M,N);
for m = 1:M
for n = 1:N
if s(m,n)<=Pa
img_out(m,n)=0; % 盐粒噪声
end
if p(m,n)<=Pb
img_out(m,n)=255; % 胡椒噪声
end
end
end
end
function img_out = filter_median(img_in)
% 该函数用于实现中值滤波
[R,C] = size(img_in);
img_in = double(img_in);
img_out = zeros(R,C);
for m = 2:R-1 % 冒泡排序
for n = 2:C-1
M = [img_in(m-1,n-1),img_in(m-1,n),img_in(m-1,n+1);...
img_in(m,n-1),img_in(m,n),img_in(m,n+1);...
img_in(m+1,n-1),img_in(m+1,n),img_in(m+1,n+1)];
img_out(m,n) = median(M);
end
end
img_out = uint8(img_out);
end
function m = median(M)
% 该函数用于计算3×3矩阵的中值。
for i = 1:8
for j = 1:8
if M(j)>M(j+1)
t = M(j);
M(j) = M(j+1);
M(j+1) = t;
end
end
end
m = M(5);
end