# How to detect object using Gabor Filter?

I want to apply Gabor Filter to detect the vehicle shown in the image. Here is my code:

```
clear all
close all
clc
A=imread('image4.jpg'); %read image
A = imresize(A,0.25); %resize image by 25% to inc. speed
Agray=rgb2gray(A); %convert to gray to inc. ops
figure
imshow(A)
imageSize = size(A); %calculate the image size A
numRows = imageSize(1); %number of rows
numCols = imageSize(2); %number of columns
wavelengthMin = 4/sqrt(2); %wavlength in increasing powers of two starting from 4/sqrt(2) up to the hypotenuse length of the input image
wavelengthMax = hypot(numRows,numCols); %max wavelength = hypot of rows and columns
n = floor(log2(wavelengthMax/wavelengthMin)); %calculating floor points
wavelength = 2.^(0:(n-2)) * wavelengthMin; %wavelength calculation
deltaTheta = 45; %choose between 0 and 150 in steps of 30 degrees
orientation = 0:deltaTheta:(180-deltaTheta); %orientation of source image
g = gabor(wavelength,orientation); %calculating gabor function values g = 1*24
gabormag = imgaborfilt(Agray,g); %gabor magnitude from source image
for i = 1:length(g) % length of g = 24
sigma = 0.5*g(i).Wavelength; %choose a sigma that is matched to the Gabor filter that extracted each feature
K = 2; % smoothing term K random value
gabormag(:,:,i) = imgaussfilt(gabormag(:,:,i),K*sigma); %imgaussfilt Gaussian Smoothing Filters to Images
end
X = 1:numCols; %1 to 317 columns
Y = 1:numRows; %1 to 176 rows
[X,Y] = meshgrid(X,Y); %Create 2-D grid coordinates with X-coordinates defined by the vector X and Y-coordinates defined by the vector Y
featureSet = cat(3,gabormag,X);
featureSet = cat(3,featureSet,Y);
numPoints = numRows*numCols; %numPoints = 124848
X = reshape(featureSet,numRows*numCols,[]); %Reshaping data into a matrix X of the form expected by the kmeans function
X = bsxfun(@minus, X, mean(X)); %Normalize features to be zero mean
X = bsxfun(@rdivide,X,std(X)); %Normalize features to be unit variance
coeff = pca(X); %returns the principal component coefficients
feature2DImage = reshape(X*coeff(:,1),numRows,numCols); %returns the numRows-by-numCols matrix, which has the same elements as X*coeff(:,1). The elements are taken column-wise from X*coeff(:,1) to fill in the elements of the numRows-by-numCols matrix
figure
imshow(feature2DImage,[])
L = kmeans(X,4,'Replicates',12); %
L = reshape(L,[numRows numCols]);
figure
imshow(label2rgb(L)) %label matrix to rgb image
Aseg1 = zeros(size(A),'like',A);
Aseg2 = zeros(size(A),'like',A);
BW = L == 2;
BW = repmat(BW,[1 1 3]);
Aseg1(BW) = A(BW);
Aseg2(~BW) = A(~BW);
figure
imshowpair(Aseg1,Aseg2,'montage');
```

The above code is copied from MathWork: Texture Segmentation Using Gabor Filters

Here is my image (image4.jpg) for which I'm applying Gabor Filter to detect the vehicle: The size of the image is initially 1000x557.

OK. Here are some more points regarding my above question: 1. Every time I run this code, I'm getting different outputs. 2. Can I insert a box around the object? If yes, please suggest me. 3. What exactly I get at the output of Gabor Filter?

Thanks is advance:)