gpt4 book ai didi

image - 如何将 Gabor 小波应用于图像?

转载 作者:太空宇宙 更新时间:2023-11-03 19:08:13 28 4
gpt4 key购买 nike

我如何在图像上应用这些 Gabor 滤波器小波?

enter image description here

close all;
clear all;
clc;

% Parameter Setting
R = 128;
C = 128;
Kmax = pi / 2;
f = sqrt( 2 );
Delt = 2 * pi;
Delt2 = Delt * Delt;

% Show the Gabor Wavelets
for v = 0 : 4
for u = 1 : 8
GW = GaborWavelet ( R, C, Kmax, f, u, v, Delt2 ); % Create the Gabor wavelets
figure( 2 );
subplot( 5, 8, v * 8 + u ),imshow ( real( GW ) ,[]); % Show the real part of Gabor wavelets

end

figure ( 3 );
subplot( 1, 5, v + 1 ),imshow ( abs( GW ),[]); % Show the magnitude of Gabor wavelets

end



function GW = GaborWavelet (R, C, Kmax, f, u, v, Delt2)


k = ( Kmax / ( f ^ v ) ) * exp( 1i * u * pi / 8 );% Wave Vector

kn2 = ( abs( k ) ) ^ 2;

GW = zeros ( R , C );

for m = -R/2 + 1 : R/2

for n = -C/2 + 1 : C/2

GW(m+R/2,n+C/2) = ( kn2 / Delt2 ) * exp( -0.5 * kn2 * ( m ^ 2 + n ^ 2 ) / Delt2) * ( exp( 1i * ( real( k ) * m + imag ( k ) * n ) ) - exp ( -0.5 * Delt2 ) );

end

end

编辑:这是我图片的尺寸

enter image description here

最佳答案

Gabor 滤波器的典型用途是计算多个方向中每个方向的滤波器响应,例如用于边缘检测。

您可以使用 Convolution Theorem 将过滤器与图像进行卷积,通过对图像和滤波器的傅里叶变换的逐元素乘积进行傅里叶逆变换。这是基本公式:

%# Our image needs to be 2D (grayscale)
if ndims(img) > 2;
img = rgb2gray(img);
end
%# It is also best if the image has double precision
img = im2double(img);

[m,n] = size(img);
[mf,nf] = size(GW);
GW = padarray(GW,[n-nf m-mf]/2);
GW = ifftshift(GW);
imgf = ifft2( fft2(img) .* GW );

通常,对于大小 > 20 的内核,FFT 卷积更胜一筹。有关详细信息,我推荐C 中的数值食谱,它对方法及其注意事项有很好的语言无关描述。

您的内核已经很大,但是使用 FFT 方法它们可以和图像一样大,因为无论如何它们都会被填充到那个大小。由于 FFT 的周期性,该方法执行循环卷积。这意味着滤镜将环绕图像边界,因此我们还必须填充图像本身以消除这种边缘效应。最后,由于我们想要对所有过滤器的总响应(至少在典型的实现中),我们需要依次将每个过滤器应用于图像,然后对响应求和。通常一个人只使用 3 到 6 个方向,但在多个尺度(不同的内核大小)上进行过滤也很常见,因此在这种情况下会使用更多的过滤器。

你可以用这样的代码完成整个事情:

img = im2double(rgb2gray(img)); %# 
[m,n] = size(img); %# Store the original size.

%# It is best if the filter size is odd, so it has a discrete center.
R = 127; C = 127;

%# The minimum amount of padding is just "one side" of the filter.
%# We add 1 if the image size is odd.
%# This assumes the filter size is odd.
pR = (R-1)/2;
pC = (C-1)/2;
if rem(m,2) ~= 0; pR = pR + 1; end;
if rem(n,2) ~= 0; pC = pC + 1; end;
img = padarray(img,[pR pC],'pre'); %# Pad image to handle circular convolution.

GW = {}; %# First, construct the filter bank.
for v = 0 : 4
for u = 1 : 8
GW = [GW {GaborWavelet(R, C, Kmax, f, u, v, Delt2)}];
end
end

%# Pad all the filters to size of padded image.
%# We made sure padsize will only be even, so we can divide by 2.
padsize = size(img) - [R C];
GW = cellfun( ...
@(x) padarray(x,padsize/2), ...
GW, ...
'UniformOutput',false);

imgFFT = fft2(img); %# Pre-calculate image FFT.

for i=1:length(GW)
filter = fft2( ifftshift( GW{i} ) ); %# See Numerical Recipes.
imgfilt{i} = ifft2( imgFFT .* filter ); %# Apply Convolution Theorem.
end

%# Sum the responses to each filter. Do it in the above loop to save some space.
imgS = zeros(m,n);
for i=1:length(imgfilt)
imgS = imgS + imgfilt{i}(pR+1:end,pC+1:end); %# Just use the valid part.
end

%# Look at the result.
imagesc(abs(imgS));

请记住,这基本上是最低限度的实现。您可以选择用边界的复制品而不是零来填充,对图像应用窗口函数或使填充尺寸更大以获得频率分辨率。这些中的每一个都是对我上面概述的技术的标准增强,通过 Google 进行研究应该是微不足道的。和 Wikipedia .另请注意,我没有添加任何基本的 MATLAB 优化,例如预分配等。

最后一点,如果您的过滤器总是比图像小得多,您可能希望跳过图像填充(即使用第一个代码示例)。这是因为,向图像添加零会在填充开始的地方创建一个人工边缘特征。如果过滤器很小,循环卷积的环绕不会导致问题,因为只有过滤器填充中的零会涉及。但是一旦滤波器足够大,环绕效应就会变得严重。如果必须使用大过滤器,则可能必须使用更复杂的填充方案,或裁剪图像的边缘。

关于image - 如何将 Gabor 小波应用于图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9003147/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com