其对应的模板为:
Laplacian算子的定义式为:
其对应的模板为:
(1)读入原图像lena.bmp并显示;
(2)分别采用Roberts算子、Prewitt算子、Sobel算子计算图像的梯度;
(3)采用Laplacian增强算子对图像进行增强,并显示增强结果;
(4)比较各种锐化算子对图像边缘的增强效果。
%------------------------------------------------------------------------
% File name: third_2
% Last modified Date: 2021年6月10日20点24分
% Author: Jasmine
% Descriptions: 空间域锐化
%------------------------------------------------------------------------
%清空工作区
clc,clear,close all;
%读入原图像
lena = imread('D:\_1Course\Digital_image_processing\photo\lena.bmp');
lena=rgb2gray(lena);
%显示原图像
subplot(3,2,1),imshow(lena);title('原图');
[ROW,COL] = size(lena);
lena = double(lena);
new_img = zeros(ROW,COL); %新建画布
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%定义robert算子
roberts_x = [-1,0;0,1];
roberts_y = [0,-1;1,0];
for i = 1:ROW - 1
for j = 1:COL - 1
funBox = lena(i:i+1,j:j+1);
G_x = roberts_x .* funBox;
G_x = abs(sum(G_x(:)));
G_y = roberts_y .* funBox;
G_y = abs(sum(G_y(:)));
roberts_xy = G_x * 0.5 + G_y * 0.5;
new_img(i,j) = roberts_xy;
end
end
subplot(3,2,3),imshow(new_img/255),title("robert算子的图像");
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 定义laplace算子
laplace = [0,1,0;1,-4,1;0,1,0];
for i = 1:ROW - 2
for j = 1:COL - 2
funBox = lena(i:i+2,j:j+2);
G = laplace .* funBox;
G = abs(sum(G(:)));
new_img(i+1,j+1) = G;
end
end
subplot(3,2,4),imshow(new_img/255),title("laplace算子的图像");
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%定义sobel算子
sobel_x = [-1,0,1;-2,0,2;-1,0,1];
sobel_y = [-1,-2,-1;0,0,0;1,2,1];
for i = 1:ROW - 2
for j = 1:COL - 2
funBox = lena(i:i+2,j:j+2);
G_x = sobel_x .* funBox;
G_x = abs(sum(G_x(:)));
G_y = sobel_y .* funBox;
G_y = abs(sum(G_y(:)));
sobelxy = G_x * 0.5 + G_y * 0.5;
new_img(i+1,j+1) = sobelxy;
end
end
subplot(3,2,5),imshow(new_img/255),title("sobel算子的图像");
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%定义Prewitt算子
sobel_x = [-1,0,1;-1,0,1;-1,0,1];
sobel_y = [-1,-1,-1;0,0,0;1,1,1];
for i = 1:ROW - 2
for j = 1:COL - 2
funBox = lena(i:i+2,j:j+2);
G_x = sobel_x .* funBox;
G_x = abs(sum(G_x(:)));
G_y = sobel_y .* funBox;
G_y = abs(sum(G_y(:)));
sobelxy = G_x * 0.5 + G_y * 0.5;
new_img(i+1,j+1) = sobelxy;
end
end
subplot(3,2,6),imshow(new_img/255),title("Prewitt算子的图像");