【问题标题】:Apply the Laplacian Filter in Matlab在 Matlab 中应用拉普拉斯滤波器
【发布时间】:2021-02-26 17:46:47
【问题描述】:

我有一个问题。我用不同的代码尝试了很多次,但我没有找到答案。我每次都不确定我的答案。我想知道如何使用 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);
   

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

【问题讨论】:

  • 请输入您的问题,不要发布您老师问题的屏幕截图。
  • 有很多方法可以离散化拉普拉斯算子,您的老师正在考虑哪种方法可以通过查看您的教科书和/或讲义来了解。
  • @CrisLuengo 我尝试了所有这些,但我找不到这个问题的答案。
  • 此外,“(4,4) 处的像素”可以是 MATLAB 中的基于 1 的索引,也可以是大多数其他语言中的基于 0 的索引。
  • 无论矩阵大小如何,您仍然只计算每个像素的内核大小。

标签: matlab image-processing matlab-figure


【解决方案1】:

仅获取感兴趣的像素:

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 运行

【讨论】:

  • 我知道这是不久前发布的,而不是我的帖子,但我发现您的回复非常有帮助,谢谢。
  • @Ben 很高兴它有帮助。真的很感谢你的客气话! :)
猜你喜欢
  • 2011-04-28
  • 2013-09-21
  • 2019-02-02
  • 2011-02-02
  • 2023-03-24
  • 2019-05-01
  • 1970-01-01
  • 2011-12-25
相关资源
最近更新 更多