How to create 64 Gabor features at each scale and orientation in the spatial and frequency domain

后端 未结 4 1808
无人及你
无人及你 2021-01-02 22:53

Normally, a Gabor filter, as its name suggests, is used to filter an image and extract everything that it is oriented in the same direction of the filtering.

4条回答
  •  既然无缘
    2021-01-02 23:19

    In the spatial domain:

    function [fSiz,filters,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)
    
                    filters          = zeros(max(RF_siz)^2,numFilters); % Matrix of Gabor filters of size %max_fSiz x num_filters, where max_fSiz is the length of the largest filter and num_filters the total number of filters. Column j of filters matrix contains a n_jxn_j filter (reshaped as a column vector and padded with zeros).
    
    
    
    
                for k = 1:numFilterSizes  
                    for r = 1:numSimpleFilters
                        theta     = rot(r)*pi/180; % so we get 0, pi/4, pi/2, 3pi/4
                        filtSize  = RF_siz(k); 
                        center    = ceil(filtSize/2);
                        filtSizeL = center-1;
                        filtSizeR = filtSize-filtSizeL-1;
                        sigmaq    = sigma(k)^2;
    
                        for i = -filtSizeL:filtSizeR
                            for j = -filtSizeL:filtSizeR
    
                                if ( sqrt(i^2+j^2)>filtSize/2 )
                                    E = 0;
                                else
                                    x = i*cos(theta) - j*sin(theta);
                                    y = i*sin(theta) + j*cos(theta);
                                    E = exp(-(x^2+G^2*y^2)/(2*sigmaq))*cos(2*pi*x/lambda(k));
                                end
                                f(j+center,i+center) = E;
                            end
                        end
    
                        f = f - mean(mean(f));
                        f = f ./ sqrt(sum(sum(f.^2)));
                        p = numSimpleFilters*(k-1) + r;
                        filters(1:filtSize^2,p)=reshape(f,filtSize^2,1);
                        fSiz(p)=filtSize;
                    end
                end
    
                % Rebuild all filters (of all sizes)
    
                nFilts = length(fSiz);
                for i = 1:nFilts
                  sqfilter{i} = reshape(filters(1:(fSiz(i)^2),i),fSiz(i),fSiz(i));
    
                %if you will use conv2 to convolve an image with this gabor, so you should also add the equation below. But if you will use imfilter instead of conv2, so do not add the equation below.
    
                        sqfilter{i} = sqfilter{i}(end:-1:1,end:-1:1); %flip in order to use conv2 instead of imfilter (%bug_fix 6/28/2007);
    
        convv=imfilter(image_double, sqfilter{i}, 'same', 'conv');
        figure;
    
            imagesc(convv);
            colormap(gray);
    
                          end 
    

提交回复
热议问题