clc,clear,close all;
img = imread('sjtu.jpg');%读取原图像
gray = rgb2gray(img);
[height,width] = size(gray); %输入图像是height x width的,且hight>n,width>n
x1 = double(gray);
x2 = x1;
name={'3x3 Laplace','5x5 Laplace','7x7 Laplace','9x9 Laplace'}
sort=1;
n1=[3 5 7 9]
for n = n1;
a(1:n,1:n) = 1; %n×n模板,元素全是1
for i = 1:height-n+1
for j = 1:width-n+1
c = x1(i:i+(n-1),j:j+(n-1)).*a; %取出x1中从(i,j)开始的n行n列元素与模板相乘
s = sum(sum(c)); %求c矩阵中各元素之和
x2(i+(n-1)/2,j+(n-1)/2) = s/(n*n); %将与模板运算后的各元素的均值赋给模板中心位置的元素
end
end
final = uint8(x2);
subplot(2,2,sort);
% Laplace
H = fspecial('unsharp');
sharpened = imfilter(final,H,'replicate');
imshow(sharpened);
title(name{sort});
sort=sort+1;
end