锐化的目的是加强图像的边界和细节,熟悉Robert、Sobel和Laplace算子进行检测,使图像特征(如边缘、轮廓等)进一步增强并突出。
1)编写Robert算子滤波函数;
2)编写Sobel算子滤波函数;
3)编写Laplace算子滤波函数;
4)编写限幅和标定函数,给出增强后的图像。
输出图像排列格式如下:
原图像 | Robert算子滤波结果 | Sobel算子滤波结果 | Laplace算子滤波结果 |
---|---|---|---|
Robert算子增强结果 | Sobel算子增强结果 | Laplace算子增强结果 |
figure('NumberTitle', 'off', 'Name', '实验3:图像的锐化处理');
OI = imread('rice.bmp'); % 原始图像读取
subplot(2,4,1);
imshow(OI);
title('原始图像');
subplot(2,4,2);
r1 = [0 -1; 1 0];
r2 = [-1 0; 0 1];
rob = RobertFilter(OI, r1, r2);
imshow(rob);
title('Robert算子滤波结果');
subplot(2,4,3);
s1 = [-1 -2 -1; 0 0 0; 1 2 1];
s2 = [-1 0 1; -2 0 2; -1 0 1];
sob = SobelFilter(OI, s1, s2);
imshow(sob);
title('Sobel算子滤波结果');
subplot(2,4,4);
l = [1 1 1; 1 -8 1; 1 1 1];
lap = LaplaceFilter(OI, l);
lapcal = Caliberation(lap);
imshow(lapcal);
title('Laplace算子滤波结果');
subplot(2,4,6);
OIrob = OI + rob;
larob = LimitAmplitude(OIrob);
imshow(larob);
title('Robert算子增强结果');
subplot(2,4,7);
OIsob = OI + sob;
lasob = LimitAmplitude(OIsob);
imshow(lasob);
title('Sobel算子增强结果');
subplot(2,4,8);
OIlap = OI - uint8(lap);
lalap = LimitAmplitude(OIlap);
imshow(lalap);
title('Laplace算子增强结果');
function rob = RobertFilter(OI, r1, r2)
[M, N] = size(OI);
f = zeros(M+1, N+1);
% 边界填充
f(1:M, 1:N) = OI(1:M, 1:N);
f(1:M, N+1:N+1) = OI( : , N:N);
f(M+1:M+1, 1:N) = OI(M:M, : );
g = zeros(M+1, N+1);
for x = 1: M
for y = 1: N
% M(x, y) = |z9-z5| + |z8-z6|
mat = [f(x, y) f(x, y+1); f(x+1, y) f(x+1, y+1)];
gr1 = mat .* r1;
gr2 = mat .* r2;
g(x, y) = abs(sum(gr1(:))) + abs(sum(gr2(:)));
end
end
Img = zeros(M, N);
Img(1:M, 1:N) = g(1:M, 1:N);
rob = uint8(Img);
end
function sob = SobelFilter(OI, s1, s2)
[M, N] = size(OI);
[m, n] = size(s1);
f = zeros(M+m-1, N+n-1);
% 边界填充
f((m-1)/2+1:M+(m-1)/2, (n-1)/2+1:N+(n-1)/2) = OI(1:M, 1:N);
f((m-1)/2+1:M+(m-1)/2, 1:(n-1)/2) = OI( : , 1:(n-1)/2);
f((m-1)/2+1:M+(m-1)/2, N+(n-1)/2:N+m-1) = OI( : , N-(n-1)/2:N);
f(1:(m-1)/2, (n-1)/2+1:N+(n-1)/2) = OI(1:(m-1)/2, : );
f(M+(m-1)/2:M+m-1, (n-1)/2+1:N+(n-1)/2) = OI(M-(m-1)/2:M, : );
g = zeros(M+m-1, N+n-1);
for x = (m-1)/2+1 : M+(m-1)/2
for y = (n-1)/2+1 : N+(n-1)/2
% M(x, y) = |(z7+2z8+z9)-(z1+2z2+z3)| + |(z3+2z6+z9)-(z1+2z4+z7)|
mat = [f(x-1,y-1) f(x-1,y) f(x-1,y+1); f(x,y-1) f(x,y) f(x,y+1); f(x+1,y-1) f(x+1,y) f(x+1,y+1)];
gs1 = mat .* s1;
gs2 = mat .* s2;
g(x, y) = abs(sum(gs1(:))) + abs(sum(gs2(:)));
end
end
Img = zeros(M, N);
Img(1:M, 1:N) = g((m-1)/2+1 : M+(m-1)/2, (n-1)/2+1 : N+(n-1)/2);
sob = uint8(Img);
end
function lap = LaplaceFilter(OI, l)
[M, N] = size(OI);
[m, n] = size(l);
f = zeros(M+m-1, N+n-1);
% 边界填充
f((m-1)/2+1:M+(m-1)/2, (n-1)/2+1:N+(n-1)/2) = OI(1:M, 1:N);
f((m-1)/2+1:M+(m-1)/2, 1:(n-1)/2) = OI( : , 1:(n-1)/2);
f((m-1)/2+1:M+(m-1)/2, N+(n-1)/2:N+m-1) = OI( : , N-(n-1)/2:N);
f(1:(m-1)/2, (n-1)/2+1:N+(n-1)/2) = OI(1:(m-1)/2, : );
f(M+(m-1)/2:M+m-1, (n-1)/2+1:N+(n-1)/2) = OI(M-(m-1)/2:M, : );
g = zeros(M+m-1, N+n-1);
for x = (m-1)/2+1 : M+(m-1)/2
for y = (n-1)/2+1 : N+(n-1)/2
mat = [f(x-1,y-1) f(x-1,y) f(x-1,y+1); f(x,y-1) f(x,y) f(x,y+1); f(x+1,y-1) f(x+1,y) f(x+1,y+1)];
gl = mat .* l;
g(x, y) = sum(gl(:));
end
end
Img = zeros(M, N);
Img(1:M, 1:N) = g((m-1)/2+1 : M+(m-1)/2, (n-1)/2+1 : N+(n-1)/2);
lap = Img;
% lap = uint8(Img);
end
% 标定函数 Caliberation
function cal = Caliberation(OI)
OI = double(OI);
[M, N] = size(OI);
fmin = min(min(OI));
fm = OI - fmin * ones(M, N);
fmmax = max(max(fm));
fs = 255 * fm ./ fmmax;
cal = uint8(fs);
end
% 限幅函数 LimitAmplitude
function la = LimitAmplitude(OI)
[M,N]=size(OI);
la = OI;
for x=1:M
for y=1:N
if la(x,y) > 255
la(x,y) = 255;
elseif la(x,y) < 0
la(x,y) = 0;
else
la(x,y) = la(x,y);
end
end
end
end