Matlab 2022: 使用二维 Gabor 滤波器对风景画进行方向滤波
以下是使用 Matlab 2022 编写的 M 文件代码,实现对一幅风景画进行垂直、水平和水平与垂直组合方向的 Gabor 滤波。
% 读入图像
image = imread('scenery.jpg');
% 转换为灰度图像
gray_image = rgb2gray(image);
% 定义 Gabor 滤波器参数
lambda = 10; % 载波波长
theta = [0, 90]; % 方向
sigma = 5; % 高斯函数标准差
gamma = 0.5; % 空间长宽比
% 创建二维 Gabor 滤波器
gabor_filter = gabor(lambda, theta, sigma, gamma);
% 对图像进行垂直方向的 Gabor 滤波
vertical_filtered_image = imfilter(gray_image, gabor_filter, 'symmetric');
% 对图像进行水平方向的 Gabor 滤波
horizontal_filtered_image = imfilter(gray_image, gabor_filter, 'symmetric', 'conv', 'h');
% 对图像进行水平和垂直组合方向的 Gabor 滤波
combined_filtered_image = sqrt(vertical_filtered_image.^2 + horizontal_filtered_image.^2);
% 显示原始图像和三种方向的 Gabor 滤波结果
subplot(2, 2, 1);
imshow(image);
title('Original Image');
subplot(2, 2, 2);
imshow(vertical_filtered_image);
title('Vertical Filtered Image');
subplot(2, 2, 3);
imshow(horizontal_filtered_image);
title('Horizontal Filtered Image');
subplot(2, 2, 4);
imshow(combined_filtered_image);
title('Combined Filtered Image');
代码中首先读入一幅图像,并将其转换为灰度图像。然后定义 Gabor 滤波器的参数,包括载波波长、方向、高斯函数标准差和空间长宽比。接着通过调用 gabor 函数创建二维 Gabor 滤波器。
然后分别对图像进行垂直方向、水平方向和水平与垂直组合方向的 Gabor 滤波,使用 imfilter 函数实现。其中,对于水平方向的滤波,需要设置参数 'conv', 'h',表示进行水平卷积。
最后,使用 subplot 函数将原始图像和三种方向的 Gabor 滤波结果显示在同一幅图像中,方便进行比较。
原文地址: https://www.cveoy.top/t/topic/nDVF 著作权归作者所有。请勿转载和采集!