Transferred from: http://blog.csdn.net/abcjennifer/article/details/7360436
1.SPATIALGABOR.M describing the Gabor function
% spatialgabor-applies single oriented Gabor filter to an image
%
% Usage:
% [Eim, Oim, Aim] = Spatialgabor (IM, wavelength, angle, KX, KY, Showfilter)
%
% Arguments:
% im-image to be processed.
% wavelength-wavelength in pixels of Gabor filter to construct
% Angle-angle of filter in degrees. An angle of 0 gives a
% filter, responds to vertical features.
% kx, Ky-scale factors specifying the filter sigma relative
% to the wavelength of the filter. This is do so
% that the shapes of the filters is invariant to the
% scale. KX controls the Sigma in the x direction
% which is along the filter, and hence controls the
% bandwidth of the filter. KY controls the Sigma
% across the filter and hence controls the
% orientational selectivity of the filter. A value of
% 0.5 for both KX and KY are a good starting point.
% Showfilter-an Optional flag 0/1. When set an image of the
% even filter is displayed for inspection.
%
% Returns:
% Eim-result from filtering with the even (cosine) Gabor filter
% Oim-result from filtering with the odd (sine) Gabor filter
% aim-amplitude image = sqrt (eim.^2 + oim.^2)
%
% Peter Kovesi
% School of computer Science & software Engineering
% The University of Western Australia
% PK at Csse UWA edu au
% HTTP://WWW.CSSE.UWA.EDU.AU/~PK
%
% October 2006
function [Eim, Oim, Aim] = Spatialgabor (IM, wavelength, angle, KX, KY, Showfilter)
if Nargin = = 5
showfilter = 0;
End
im = double (IM);
[Rows, cols] = size (IM);
Newim = zeros (rows,cols);
% Construct even and odd Gabor filters
Sigmax = wavelength*kx;
Sigmay = Wavelength*ky;
Sze = Round (3*max (Sigmax,sigmay));
[x, Y] = Meshgrid (-sze:sze);
Evenfilter = exp (-(x.^2/sigmax^2 + y.^2/sigmay^2)/2) ...
. *cos (2*pi* (1/wavelength) *x);
Oddfilter = exp (-(x.^2/sigmax^2 + y.^2/sigmay^2)/2) ...
. *sin (2*pi* (1/wavelength) *x);
Evenfilter = Imrotate (evenfilter, Angle, ' bilinear ');
Oddfilter = Imrotate (oddfilter, Angle, ' bilinear ');
% do the filtering
Eim = Filter2 (evenfilter,im);% even filter result
Oim = Filter2 (Oddfilter,im); % Odd Filter result
Aim = sqrt (eim.^2 + oim.^2); % amplitude
If showfilter% Display filter for inspection figure
(1), Imshow (evenfilter,[]), title (' Filter '); C24/>end
2.main.m
Ori=imread (' D:\Images\New\Cars\image_0001.jpg ');
Grayimg=rgb2gray (ORI);
Gim=im2double (GRAYIMG);
[Eim,oim,aim]=spatialgabor (gim,3,90,0.5,0.5,1);%90-vertical===0-horizontal
Imshow (Aim);