在Matlab中使用可分离Gabor滤波器 [英] Working on Separable Gabor filters in matlab
问题描述
如果可以将过滤器g表示为两个向量 grow
和 gcol
的乘法,则过滤器g称为可分离。使用一维滤波器将二维滤波器的计算复杂度从 O(M ^ 2 N ^ 2)
减少到 O(2M N ^ 2) code>其中M和N分别是过滤器掩码和图片的宽度(和高度)。
在。
注意:θ可以扩展到等于 k * pi / 4。
通过与这个stackoverflow链接,我们可以认为 f = 1 / lambda
。
通过更改我之前的代码,我写了一个matlab代码,使Gabor过滤器可分离通过使用上面的等式,但我是确保我的代码下面是不正确的,特别是当我初始化 gbp
和 glp
方程。这就是为什么我需要你的帮助。
现在让我们来看看我的代码:
function [fSiz,filters1,filters2,c1OL,numSimpleFilters] = init_gabor(rot,RF_siz)
image = imread('xxx.jpg' );
image_gray = rgb2gray(image);
image_gray = imresize(image_gray,[100 100]);
image_double = double(image_gray);
rot = [0 45 90 135]; %我们有四个方向
RF_siz = [7:2:37]; %我们得到16个刻度(7x7到37x37,以两个像素为单位)
minFS = 7; %最小接受字段
maxFS = 37; %最大接受场
sigma = 0.0036 * RF_siz。^ 2 + 0.35 * RF_siz + 0.18; %定义有效宽度方程
lambda = sigma / 0.8; %it波长方程(λ)
G = 0.3; %空间纵横比:0.23 < γ, 0.92
numFilterSizes = length(RF_siz); %我们得到16
numSimpleFilters = length(rot); %我们得到4
numFilters = numFilterSizes * numSimpleFilters; %我们得到16x4 = 64个过滤器
fSiz = zeros(numFilters,1); %它是一个大小为numFilters的向量,其中每个单元格包含过滤器的大小(7,7,7,7,9,9,9,9,11,11,11,11,......,37 ,37,37,37)
filters1 = zeros(max(RF_siz),numFilters);
filters2 = zeros(numFilters,max(RF_siz));
for k = 1:numFilterSizes
for r = 1:numSimpleFilters
theta = rot(r)* pi / 180;
filtSize = RF_siz(k);
center = ceil(filtSize / 2);
filtSizeL = center-1;
filtSizeR = filtSize-filtSizeL-1;
sigmaq = sigma(k)^ 2;
for x = -filtSizeL:filtSizeR
fx = exp( - (x ^ 2)/(2 * sigmaq))* cos(2 * pi * x / lambda (k));
f1(x + center,1)= fx;
end
for y = -filtSizeL:filtSizeR
gy = exp( - (y ^ 2)/(2 * sigmaq)
f2(1,y + center)= gy;
end
f1 = f1 - mean(mean(f1));
f1 = f1。/ sqrt(sum(sum(f1。^ 2)));
f2 = f2 - mean(mean(f2));
f2 = f2 ./ sqrt(sum(sum(f2。^ 2)));
p = numSimpleFilters *(k-1)+ r;
filters1(1:filtSize,p)= f1;
filters2(p,1:filtSize)= f2;
convv1 = imfilter(image_double,filters1(1:filtSize,p),'conv');
convv2 = imfilter(double(convv1),filters2(p,1:filtSize),'conv');
figure
imagesc(convv2);
colormap(gray);
end
end
我认为代码是正确的,只要您的以前版本的Gabor过滤器代码也是正确的。唯一的是如果 theta = k * pi / 4;
,你的公式这里应该分隔:
fx = exp( - (x ^ 2)/(2 * sigmaq))* cos(2 * pi * x / lambda(k)
gy = exp( - (G ^ 2 * y ^ 2)/(2 * sigmaq));
为了保持一致,您可以使用
f1(1,x + center)= fx;
f2(y + center,1)= gy;
或保留 f1
和 f2
,但它之后转换你的 filters1
和 filters2
其他一切对我看起来不错。
EDIT
回答上面的工作为 theta = k * pi / 4;
,其他角度,根据你的论文,
x = i * cos(theta)-j * sin(theta);
y = i * sin(theta)+ j * cos(theta);
fx = exp( - (x ^ 2)/(2 * sigmaq))* exp(sqrt(-1)* x * cos
gy = exp( - (G ^ 2 * y ^ 2)/(2 * sigmaq))* exp(sqrt(-1)* y * sin
A filter g is called separable if it can be expressed as the multiplication of two vectors grow
and gcol
. Employing one dimensional filters decreases the two dimensional filter's computational complexity from O(M^2 N^2)
to O(2M N^2)
where M and N are the width (and height) of the filter mask and the image respectively.
In this stackoverflow link, I wrote the equation of a Gabor filter in the spatial domain, then I wrote a matlab code which serves to create 64 gabor features.
According to the definition of separable filters, the Gabor filters are parallel to the image axes - theta = k*pi/2 where k=0,1,2,etc.
. So if theta=pi/2 ==> the equation in this stackoverflow link can be rewritten as:
The equation above is extracted from this article.
Note: theta can be extented to be equal k*pi/4.
By comparing to the equation in this stackoverflow link, we can consider that f= 1 / lambda
.
By changing my previous code in this stackoverflow link, I wrote a matlab code to make the Gabor filters separable by using the equation above, but I am sure that my code below is not correct especially when I initialized the gbp
and glp
equations. That is why I need your help. I will appreciate your help very much.
Let's show now my code:
function [fSiz,filters1,filters2,c1OL,numSimpleFilters] = init_gabor(rot, RF_siz)
image=imread('xxx.jpg');
image_gray=rgb2gray(image);
image_gray=imresize(image_gray, [100 100]);
image_double=double(image_gray);
rot = [0 45 90 135]; % we have four orientations
RF_siz = [7:2:37]; %we get 16 scales (7x7 to 37x37 in steps of two pixels)
minFS = 7; % the minimum receptive field
maxFS = 37; % the maximum receptive field
sigma = 0.0036*RF_siz.^2 + 0.35*RF_siz + 0.18; %define the equation of effective width
lambda = sigma/0.8; % it the equation of wavelength (lambda)
G = 0.3; % spatial aspect ratio: 0.23 < gamma < 0.92
numFilterSizes = length(RF_siz); % we get 16
numSimpleFilters = length(rot); % we get 4
numFilters = numFilterSizes*numSimpleFilters; % we get 16x4 = 64 filters
fSiz = zeros(numFilters,1); % It is a vector of size numFilters where each cell contains the size of the filter (7,7,7,7,9,9,9,9,11,11,11,11,......,37,37,37,37)
filters1 = zeros(max(RF_siz),numFilters);
filters2 = zeros(numFilters,max(RF_siz));
for k = 1:numFilterSizes
for r = 1:numSimpleFilters
theta = rot(r)*pi/180;
filtSize = RF_siz(k);
center = ceil(filtSize/2);
filtSizeL = center-1;
filtSizeR = filtSize-filtSizeL-1;
sigmaq = sigma(k)^2;
for x = -filtSizeL:filtSizeR
fx = exp(-(x^2)/(2*sigmaq))*cos(2*pi*x/lambda(k));
f1(x+center,1) = fx;
end
for y = -filtSizeL:filtSizeR
gy = exp(-(y^2)/(2*sigmaq));
f2(1,y+center) = gy;
end
f1 = f1 - mean(mean(f1));
f1 = f1 ./ sqrt(sum(sum(f1.^2)));
f2 = f2 - mean(mean(f2));
f2 = f2 ./ sqrt(sum(sum(f2.^2)));
p = numSimpleFilters*(k-1) + r;
filters1(1:filtSize,p)=f1;
filters2(p,1:filtSize)=f2;
convv1=imfilter(image_double, filters1(1:filtSize,p),'conv');
convv2=imfilter(double(convv1), filters2(p,1:filtSize),'conv');
figure
imagesc(convv2);
colormap(gray);
end
end
I think the code is correct provided your previous version of Gabor filter code is correct too. The only thing is that if theta = k * pi/4;
, your formula here should be separated to:
fx = exp(-(x^2)/(2*sigmaq))*cos(2*pi*x/lambda(k));
gy = exp(-(G^2 * y^2)/(2*sigmaq));
To be consistent, you may use
f1(1,x+center) = fx;
f2(y+center,1) = gy;
or keep f1
and f2
as it is but transpose your filters1
and filters2
thereafter.
Everything else looks good to me.
EDIT
My answer above works for theta = k * pi/4;
, with other angles, based on your paper,
x = i*cos(theta) - j*sin(theta);
y = i*sin(theta) + j*cos(theta);
fx = exp(-(x^2)/(2*sigmaq))*exp(sqrt(-1)*x*cos(theta));
gy = exp(-(G^2 * y^2)/(2*sigmaq))*exp(sqrt(-1)*y*sin(theta));
这篇关于在Matlab中使用可分离Gabor滤波器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!