Matlab code to generate a two-dimensional Gabor filter

Source: Internet
Author: User

function Hp=gbfilter2 (SIGMAX,MU,MV)
% function function: Generate two-dimensional discrete Gabor function template hp. May be mainly used in image texture analysis;

% input parameter: variance of sigmax--x direction;

% mu--x Direction center frequency; mv--y Direction center frequency;

% output Parameters: hp--Two-dimensional discrete Gabor template;

% according to the study of physiological vision the Y-direction standard deviation is 1.5-twice times the X-direction standard deviation, where 1.7 is taken.

sigmay=sigmax*1.7;

% according to the error principle, ( -3sigama,3sigma) contains 97.4% of the energy. N is also the filter length.

% usually requires a template length of n prior to texture analysis, which can be N=fix (6*sigmax*1.7); This statement is evaluated.
N=fix (6*sigmay);

% The sentence is mainly prevented by 0 except
If mu==0
mu=1e-12;
End
% theta angle is the texture direction
Theta=atan (MV/MU);

% sampling interval and interval, where oversampling is done. Critical sampling is not advisable.
kx=-(N-1)/2: (N-1)/2;
Ky=kx;

% generates x, y coordinates.
[Tpx,tpy]=meshgrid (Kx,ky);

% calculates the new coordinates generated by the Gabor function rotation
X=tpx*cos (theta) +tpy*sin (theta);
Y=-tpx*sin (theta) +tpy*cos (theta);

% calculation Gauss function
z=1/(2*pi*sigmax*sigmay) *exp ( -0.5* (X/sigmax). ^2+ (Y/sigmay). ^2));

% calculated modulation factor
Exptab=exp (-j2*pi* (TPX*MU+TPY*MV)/n);

%gauss function modulated by modulation factor
Hp=z.*exptab;

% matrix transpose to generate X, Y axes that match the custom
Hp=hp ';
End

The second program to produce a two-dimensional Gabor filter

function [Hp,sigmax]=gbfilt2 (AX,MU,MV,N)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function function: Generate templates for the Gabor function for image filtering
% input parameters: ax--The sigma Factor on the x axis, ax=n/(2*sigma),3.5<=ax<=n/2;
% mu--x Direction Center frequency, mu<=n/4;mv--y Direction Center frequency;mv<=n/4;
% n--filter length, same as filter width;
% output parameters: hp--generated Gabor template; Sigmax corresponds to X-direction Sigama;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% definition Sigmay=1.7*sigmax
ay=ax/1.7;
% prevented by 0 apart
If mu==0
mu=1e-16;
End
% calculate the orientation of the Gabor filter
Theta=atan (MV/MU);
% definition x, Y value range is length n
kx=-(N-1)/2: (N-1)/2;
Ky=kx;
% generates x, y coordinates
[Tpx,tpy]=meshgrid (Kx,ky);
% Swivel theta Angle
X=tpx*cos (theta) +tpy*sin (theta);
Y=-tpx*sin (theta) +tpy*cos (theta);
% Gauss function
z=2*ax*ay/(pi*n^2) *exp (( -1/2) * ((ax*x/(N/2)). ^2+ (ay*y/(N/2)). ^2));
% Frequency modulation factor
Facmod=cos (2*pi* (TPX*MU+TPY*MV)/n);
% return Template
hp= (Z.*facmod) ';
% return Sigmax
sigmax=n/(2*ax);
End

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2008-09-02
% Wang Zehua
% Qingdao University of Science and Technology
% email:scunwzh@163.com
% References:
% Harris, F.J. On the as of Windows for Harmonic
% discrete Fourier transform.proceedings of the IEEE. 1978,66 (1): 51-83.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

I do not want to say more about this program, which is basically generated by the standard, the specific criteria can be read the literature listed below, and this article can be obtained on the internet for free, but only if your e-text is good enough.

Harris,f. J On the as of Windows for Harmonic analysis with the discrete Fourier Transform. Proceedings of the IEEE,1978,66 (1): 51-83.

Reprint Address: http://blog.csdn.net/dadaadao/article/details/6225411

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.