在matlab中使用可分离的Gabor滤波器

时间:2014-01-18 21:46:48

标签: matlab image-processing computer-vision filtering convolution

如果过滤器g可以表示为两个向量growgcol的乘法,则称为可分离过滤器。使用一维滤波器将二维滤波器的计算复杂度从O(M^2 N^2)降低到O(2M N^2),其中M和N分别是滤波器掩码和图像的宽度(和高度)。

this stackoverflow link中,我在空间域中编写了Gabor滤波器的等式,然后我编写了一个matlab代码,用于创建64个gabor特征。

根据可分离滤波器的定义,Gabor滤波器与图像轴平行 - theta = k*pi/2 where k=0,1,2,etc.。所以如果theta = pi / 2 ==> this stackoverflow link中的等式可以改写为:

enter image description here

以上等式摘自this article

注意: theta可以扩展为相等k*pi/4.通过与this stackoverflow link中的等式进行比较,我们可以考虑f= 1 / lambda

通过更改this stackoverflow link中的先前代码,我编写了一个matlab代码,通过使用上面的等式使Gabor过滤器可分离,但我确信下面的代码不正确,尤其是在我初始化时gbpglp等式。这就是我需要你帮助的原因。非常感谢你的帮助。

现在让我们看看我的代码:

    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

2 个答案:

答案 0 :(得分:2)

如果您以前版本的Gabor过滤器代码也正确,我认为代码是正确的。唯一的问题是,如果theta = k * pi/4;,您的公式here应该分开:

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;

或保持f1f2不变,但之后转换filters1filters2。 其他一切对我来说都很好。

修改

我的答案适用于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(theta));
gy = exp(-(G^2 * y^2)/(2*sigmaq))*exp(sqrt(-1)*y*sin(theta));

答案 1 :(得分:0)

最终代码为:

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]; 
                            minFS     = 7; 
                            maxFS     = 37; 
                            sigma  = 0.0036*RF_siz.^2 + 0.35*RF_siz + 0.18; 
                            lambda = sigma/0.8; 
                            G      = 0.3;   


                            numFilterSizes   = length(RF_siz); 

                            numSimpleFilters = length(rot); 

                            numFilters       = numFilterSizes*numSimpleFilters; 

                            fSiz             = zeros(numFilters,1); 
                            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))*exp(sqrt(-1)*x*cos(theta));
                               f1(1, x+center) = fx;
                end
                              for y = -filtSizeL:filtSizeR
                            gy=exp(-(y^2)/(2*sigmaq))*exp(sqrt(-1)*y*sin(theta));
                            f2(y+center,1) = 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(imag(convv2));
            colormap(gray);

        end
    end