如何使用Gabor过滤器检测物体?

时间:2018-07-04 14:33:48

标签: image matlab image-processing video-processing gabor-filter

我想应用Gabor过滤器来检测图像中所示的车辆。 这是我的代码:

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');

上面的代码是从MathWork: Texture Segmentation Using Gabor Filters复制的

这是我要应用Gabor滤镜检测车辆的图像(image4.jpg):The size of the image is initially 1000x557.

好。关于上述问题,还有其他几点: 1.每次运行此代码,我都会得到不同的输出。 2.我可以在对象周围插入一个盒子吗?如果是,请建议我。 3. Gabor滤波器的输出到底能给我什么?

谢谢你:)

0 个答案:

没有答案