仅获取感兴趣的像素:
MATLAB 中的像素 (4,4) → 像素 (5,5)
要扩展 @beaker's 点,您只需要像素周围的邻域。该邻域将取决于过滤器内核大小。包括感兴趣像素的邻域的大小应与滤波器内核的大小相同。对于这个问题,它会进行如下。您可以简单地将相应/重叠的组件相乘,将所有产品的总和相乘以获得最终的像素值。这本质上是对过滤器/卷积过程进行 sn-p。
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
常见的拉普拉斯滤波器内核:
以下是我尝试各种常见的拉普拉斯滤波器内核的一些尝试,但如果没有关于您的问题的更多细节,您应该参考这个答案,作为练习或将其用作操场脚本的一种手段。可以通过多种方式计算离散导数,这些方式由拉普拉斯核中的正(红色)和负(蓝色)互补分量直观地显示。要考虑的一件事是开始/基本索引。在 MATLAB 中,开始/基本索引是 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 运行