在 Matlab 中应用拉普拉斯滤波器

Apply the Laplacian Filter in Matlab

我有一个问题。我用不同的代码尝试了很多次,但没有找到答案。我不确定每次我的回答。我想知道如何使用 matlab 找到答案。 我试过代码:

A=[14 12 10 12 11 10 13 7 9 16; 
16 14 13 13 12 6 9 10 13 11; 
16 14 12 13 11 8 9 11 11 3; 
13 13 12 12 15 11 12 12 4 3, 
16 9 4 12 14 8 9 21 11 5; 
16 15 15 12 8 8 5 5 6 12; 
12 11 13 11 13 4 4 3 2 5; 
7 7 13 13 14 4 4 3 4 5; 
8 11 5 12 12 4 5 4 4 5; 
14 14 12 6 12 5 2 3 5 3]
%my first try
kernel = -1 * ones(3);
kernel(2,2) = 8;  % Now kernel = [-1,-1,-1; -1,8,-1; -1,-1,-1]
output = conv2(A,kernel,'same');
%my second try
b=[0 1 0; 1 -4 1; 0 1 0]
 c=conv2(A,b,'valid')
%my third try
b=[-1,-1,-1; -1,8,-1; -1,-1,-1]
c=conv2(A,b,'valid')
%my 4th try
A=uint8(A);
H = fspecial('laplacian',0,2)
T=imfilter(A,H);
   

如果你能帮上忙,我会很高兴。

通过只拍摄感兴趣的像素:

MATLAB 中的像素 (4,4) → 像素 (5,5)

要扩展 @beaker 的 点,您只需要像素周围的邻域。该邻域将取决于滤波器内核大小。包括感兴趣像素在内的邻域大小应与滤波器内核大小相同。对于这个问题,它将进行如下处理。您可以简单地将 corresponding/overlapping 分量乘以所有乘积的总和以获得合成像素值。这实际上是 filter/convolution 过程的一个片段。

Neighbourhood = [12 15 11; 12 14 8; 12 8 8];
Laplacian_Kernel = [0 -1 0; -1 4 -1; 0 -1 0];
Result = sum(Neighbourhood.*Laplacian_Kernel,'all');
Result

常见的拉普拉斯滤波器内核:

以下是我尝试各种常见的拉普拉斯滤波器内核的一些尝试,但没有关于您的问题的更多细节,您应该参考这个答案,只是作为练习或将其用作游乐场脚本的一种方式。离散导数可以通过多种方式计算,这些方式通过拉普拉斯核中的正(红色)和负(蓝色)互补分量直观地显示出来。要考虑的一件事是 start/base 索引。在 MATLAB 中,start/base 索引是 1。因此,左上角的像素 (1,1)(0,0) 相对。知道这个事实我们可以推断问题中的像素 (4,4) 是指 MATLAB 数组中的像素 (5,5) 。有关卷积和过滤的更多详细信息,请参阅此问题:Moving Filter/Mask Across Given Image (No Function)

A = [14 12 10 12 11 10 13 7 9 16; 
16 14 13 13 12 6 9 10 13 11; 
16 14 12 13 11 8 9 11 11 3; 
13 13 12 12 15 11 12 12 4 3; 
16 9 4 12 14 8 9 21 11 5; 
16 15 15 12 8 8 5 5 6 12; 
12 11 13 11 13 4 4 3 2 5; 
7 7 13 13 14 4 4 3 4 5; 
8 11 5 12 12 4 5 4 4 5; 
14 14 12 6 12 5 2 3 5 3];

%Common Laplacian filter kernels%
%Kernel 1%
Laplacian_Kernel_1 = [0 -1 0; -1 4 -1; 0 -1 0];
Filtered_Image_1 = conv2(A,Laplacian_Kernel_1,'same');
Result_1 = Filtered_Image_1(5,5);
fprintf("Filter 1: %d\n",Result_1);

%Kernel 2%
Laplacian_Kernel_2 = [-1 -1 -1; -1 8 -1; -1 -1 -1];
Filtered_Image_2 = conv2(A,Laplacian_Kernel_2,'same');
Result_2 = Filtered_Image_2(5,5);
fprintf("Filter 2: %d\n",Result_2);

%Kernel 3%
Laplacian_Kernel_3 = [1 -2 1; -2 4 -2; 1 -2 1];
Filtered_Image_3 = conv2(A,Laplacian_Kernel_3,'same');
Result_3 = Filtered_Image_3(5,5);
fprintf("Filter 3: %d\n",Result_3);

%Kernel 4%
Laplacian_Kernel_4 = [1 2 1; 2 -12 2; 1 2 1];
Filtered_Image_4 = conv2(A,Laplacian_Kernel_4,'same');
Result_4 = Filtered_Image_4(5,5);
fprintf("Filter 4: %d\n",Result_4);


拉普拉斯滤波器内核的负数也有效:

%Negative Laplacian filter kernels%
%Kernel 1%
Laplacian_Kernel_1 = -[0 -1 0; -1 4 -1; 0 -1 0];
Filtered_Image_1 = conv2(A,Laplacian_Kernel_1,'same');
Result_1 = Filtered_Image_1(5,5);
fprintf("Filter 1: %d\n",Result_1);

%Kernel 2%
Laplacian_Kernel_2 = -[-1 -1 -1; -1 8 -1; -1 -1 -1];
Filtered_Image_2 = conv2(A,Laplacian_Kernel_2,'same');
Result_2 = Filtered_Image_2(5,5);
fprintf("Filter 2: %d\n",Result_2);

%Kernel 3%
Laplacian_Kernel_3 = -[1 -2 1; -2 4 -2; 1 -2 1];
Filtered_Image_3 = conv2(A,Laplacian_Kernel_3,'same');
Result_3 = Filtered_Image_3(5,5);
fprintf("Filter 3: %d\n",Result_3);

%Kernel 4%
Laplacian_Kernel_4 = -[1 2 1; 2 -12 2; 1 2 1];
Filtered_Image_4 = conv2(A,Laplacian_Kernel_4,'same');
Result_4 = Filtered_Image_4(5,5);
fprintf("Filter 4: %d\n",Result_4);


扩展:拉普拉斯滤波器的结果及其相应的负数

拉普拉斯过滤游乐场脚本:

Image = rgb2gray(imread("peppers.png"));

subplot(1,3,1); imshow(Image);
title("Original Image");

Laplacian_Kernel = [0 -1 0; -1 4 -1; 0 -1 0];
Filtered_Image_1 = conv2(Image,Laplacian_Kernel_1,'same');
subplot(1,3,2); imshow(Filtered_Image_1);
title("Laplacian Filtered Image");

Negative_Laplacian_Kernel = -[0 -1 0; -1 4 -1; 0 -1 0];
Filtered_Image_2 = conv2(Image,Negative_Laplacian_Kernel,'same');
subplot(1,3,3); imshow(Filtered_Image_2);
title("Negative Laplacian Filtered Image");

运行 使用 MATLAB R2019b