Anda di halaman 1dari 3

Step 1

HOG transformation

I4= imread('21_test.tif');
>> [hog3,visualization] = extractHOGFeatures(I4,'CellSize',[4
4],'BlockOverlap' ,[1 1]);
>> figure;
>> plot(visualization);

Step 2

SVM based pattern recognition from HOG images

basic structure as follows:


Obtaining the Image Datasets - ( I will be HOG dataset )
Separate Training set and Test set images.
Creating Lables for SVM train to distinct class.
Training SVM
Classify Test set images.

At the moment, I will assume that you are familiar with the term machine learning algorithms. I have
absolutely zero intention to discuss theory over here.

Training set - This set of images will be used to train our SVM.
Test set - In the end of the svm training we will use these images for classification.
Label - I will use test1 and test2 retina HOG transformed images, these are two objects so we will give
them two "labels".
Classify - Distinguish our test set images.

Finally, I will present you a simple code for classification using SVM. I have used the Caltech101
dataset for this experiment. Train dataset will consist of 20 images divided in two class and two labels
will be provided to them. The program goes as follows:

Prepatory steps:
Training set - Create a folder with 10 images HOG transformed- and next 10 images HOG transformed
, this will be our dataset.
Test set - Create another folder with images, this will be our testset, basically .
--------------------------------------------------------------------------------------------------------
clc
clear all
% Load Datasets

Dataset = 'absolute path of the folder';


Testset = 'absolute path of the folder';
Dataset = 'C:\Users\Mircea\Desktop\DRIVE\training\images\hogtraining';
Testset = 'C:\Users\Mircea\Desktop\DRIVE\test\images\hogtest';

% we need to process the images first.


% Convert your images into grayscale
% Resize the images

Dataset = 'C:\Users\Mircea\Desktop\mircea\DRIVE\training\hogtraining';
Testset = 'C:\Users\Mircea\Desktop\mircea\DRIVE\test\hogtest';

Dataset = 'C:\Users\Mircea\Desktop\mircea\DRIVE\training\images';
Testset = 'C:\Users\Mircea\Desktop\mircea\DRIVE\test\images';

width=100; height=100;
DataSet = cell([], 1);

for i=1:length(dir(fullfile(Dataset,'*.jpg')))

% Training set process


k = dir(fullfile(Dataset,'*.jpg'));
k = {k(~[k.isdir]).name};
for j=1:length(k)
tempImage = imread(horzcat(Dataset,filesep,k{j}));
imgInfo = imfinfo(horzcat(Dataset,filesep,k{j}));

% Image transformation
if strcmp(imgInfo.ColorType,'grayscale')
DataSet{j} = double(imresize(tempImage,[width height])); % array of images
else
DataSet{j} = double(imresize(rgb2gray(tempImage),[width height])); % array of images
end
end
end
TestSet = cell([], 1);
for i=1:length(dir(fullfile(Testset,'*.jpg')))

% Training set process


k = dir(fullfile(Testset,'*.jpg'));
k = {k(~[k.isdir]).name};
for j=1:length(k)
tempImage = imread(horzcat(Testset,filesep,k{j}));
imgInfo = imfinfo(horzcat(Testset,filesep,k{j}));

% Image transformation
if strcmp(imgInfo.ColorType,'grayscale')
TestSet{j} = double(imresize(tempImage,[width height])); % array of images
else
TestSet{j} = double(imresize(rgb2gray(tempImage),[width height])); % array of images
end
end
end

% Prepare class label for first run of svm


% I have arranged labels 1 & 2 as per my convenience.
% It is always better to label your images numerically
% Please note that for every image in our Dataset we need to provide one label.
% we have 20 images and we divided it into two label groups here.
train_label = zeros(size(20,1),1);
train_label(1:10,1) = 1; % 1 = aaa
train_label(11:20,1) = 2; % 2 = bbb

% Prepare numeric matrix for svmtrain


Training_Set=[];
for i=1:length(DataSet)
Training_Set_tmp = reshape(DataSet{i},1, 100*100);
Training_Set=[Training_Set;Training_Set_tmp];
end

Test_Set=[];
for j=1:length(TestSet)
Test_set_tmp = reshape(TestSet{j},1, 100*100);
Test_Set=[Test_Set;Test_set_tmp];
end

% Perform first run of svm


SVMStruct = svmtrain(Training_Set , train_label, 'kernel_function', 'linear');
Group = svmclassify(SVMStruct, Test_Set);

------------------------------------------------------------------------------------------------------------

Finally, you can check you Image recognition performance by seeing Group variable.
Group = [ 1 2 2 2 1 2 1 1 1 2 2 1 2 2 2 1 2 1 1 1 ]

Anda mungkin juga menyukai