├── DirRead.m ├── Filter.m ├── Gentle Boost ├── DirRead.m ├── Gentle_boost_with_Gabor.m ├── Gentle_boost_without_Gabor.m ├── ImgGabor.m ├── SVM_datasets.m ├── gaborBeuthMod.m └── multiBoost.m ├── ImgGabor.m ├── Knn ├── DirRead.m ├── ImgGabor.m ├── SVM_datasets.m ├── gaborBeuthMod.m ├── knnClassify_with_Gabor_Filter.m └── knnClassify_without_Gabor_Filter.m ├── LDA ├── DirRead.m ├── ImgGabor.m ├── LDA_with_Gabor_Filter.m ├── LDA_without_Gabor_Filter.m ├── SVM_datasets.m └── gaborBeuthMod.m ├── New folder └── Version control.rar ├── SVM ├── DirRead.m ├── Filter.m ├── ImgGabor.m ├── SVM_datasets.m ├── gaborBeuthMod.m └── multisvm.m ├── SVM_datasets.m ├── gaborBeuthMod.m ├── multisvm.m ├── test.m ├── test1.m └── version 3,0 └── version 1.0 └── gaborBeuthMod.m /DirRead.m: -------------------------------------------------------------------------------- 1 | function [DirLoc, nameFolds, No_of_files, Name_of_files] = DirRead(Location, Extension ) 2 | 3 | % This function is specialy written for "SVM_datasets" function. 4 | % This function checks if there exists another subdirectory. 5 | % If there exists another dir then save the name and address 6 | %---------------------------------------------------------- 7 | % Inputs: Location 8 | % Location Path to the folder contaning the subfolder which has images 9 | % Extension : please use format like this '*.jpg' 10 | %----------------------------------------------------------- 11 | % Outputs: DirLoc, No_of_files, Name_of_folders 12 | % DirLoc Full address of the subdirectory - Array 13 | % No_of_files Number of files in the directory 14 | % nameFolds Name of Folders or Training level is named accordingly 15 | % incase if we are using it for my SVM_datasets specially. 16 | % Name_of_files : This will provide name of the files if there is no sub 17 | % directory 18 | % ---------------------------------------------------------- 19 | 20 | x=dir(Location); 21 | isub = [x(:).isdir]; 22 | nameFolds = {x(isub).name}'; 23 | nameFolds(ismember(nameFolds,{'.','..'})) = []; 24 | 25 | % If no subfolders then process all the files and save the names. 26 | list = dir(Location); 27 | isfile=~[list.isdir]; 28 | filenames={list(isfile).name}; 29 | Name_of_files = filenames.'; 30 | 31 | Dir_location=cell(length(nameFolds),1); 32 | NoFiles = zeros(length(nameFolds),1); 33 | 34 | for i=1:length(nameFolds) 35 | Dir_location{i} = horzcat(Location,filesep,nameFolds{i}); 36 | NoFiles(i) = length(dir(horzcat(Dir_location{i},filesep,Extension))); 37 | end 38 | if isempty(Dir_location) 39 | DirLoc = Location; 40 | No_of_files = length(dir(horzcat(Location,filesep,Extension))); 41 | else 42 | DirLoc = Dir_location; 43 | No_of_files = NoFiles; 44 | end 45 | end 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Filter.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear all 3 | 4 | % Set constants 5 | f = 5.5/24; % Frequency 6 | halfSDsq = 3/( 2*pi*f*24 ); % Sigma 7 | gamma = 0.75; 8 | phases = 2; 9 | ori = 4; %orientation 10 | 11 | % Hard coded filter properties 12 | kernelSize = 11; % Windowsize 13 | 14 | % Training set location & no of test image to use per class 15 | TrainingSet_add = 'F:\101_ObjectCategories'; 16 | No_Testset = 50; 17 | No_Trainset = 15; 18 | 19 | % Process the Training set location 20 | TrainingSet_location = DirRead(TrainingSet_add,'*.jpg'); 21 | 22 | % Prepare Training set & Test set 23 | [TrainingSet,TestSet,train_label] = SVM_datasets(TrainingSet_location,No_Trainset, No_Testset,140, 140); 24 | 25 | % Apply Gabour Filter to Training set & Testset images 26 | Training_Set = ImgGabor(TrainingSet, kernelSize, ori, phases, gamma, f, halfSDsq); 27 | TestSet = ImgGabor(TestSet, kernelSize, ori, phases, gamma, f, halfSDsq); 28 | 29 | % Train the classifier 30 | PredictedLabels = multisvm(TrainingSet,train_label,TestSet,'kernel_function','linear','boxconstraint',2,'kktviolationlevel',0.1,'kernelcachelimit',10000,'options',statset('MaxIter',1000000)); 31 | -------------------------------------------------------------------------------- /Gentle Boost/DirRead.m: -------------------------------------------------------------------------------- 1 | function [DirLoc, nameFolds, No_of_files, Name_of_files] = DirRead(Location, Extension ) 2 | 3 | % This function is specialy written for "SVM_datasets" function. 4 | % This function checks if there exists another subdirectory. 5 | % If there exists another dir then save the name and address 6 | %---------------------------------------------------------- 7 | % Inputs: Location 8 | % Location Path to the folder contaning the subfolder which has images 9 | % Extension : please use format like this '*.jpg' 10 | %----------------------------------------------------------- 11 | % Outputs: DirLoc, No_of_files, Name_of_folders 12 | % DirLoc Full address of the subdirectory - Array 13 | % No_of_files Number of files in the directory 14 | % nameFolds Name of Folders or Training level is named accordingly 15 | % incase if we are using it for my SVM_datasets specially. 16 | % Name_of_files : This will provide name of the files if there is no sub 17 | % directory 18 | % ---------------------------------------------------------- 19 | 20 | x=dir(Location); 21 | isub = [x(:).isdir]; 22 | nameFolds = {x(isub).name}'; 23 | nameFolds(ismember(nameFolds,{'.','..'})) = []; 24 | 25 | % If no subfolders then process all the files and save the names. 26 | list = dir(Location); 27 | isfile=~[list.isdir]; 28 | filenames={list(isfile).name}; 29 | Name_of_files = filenames.'; 30 | 31 | Dir_location=cell(length(nameFolds),1); 32 | NoFiles = zeros(length(nameFolds),1); 33 | 34 | for i=1:length(nameFolds) 35 | Dir_location{i} = horzcat(Location,filesep,nameFolds{i}); 36 | NoFiles(i) = length(dir(horzcat(Dir_location{i},filesep,Extension))); 37 | end 38 | if isempty(Dir_location) 39 | DirLoc = Location; 40 | No_of_files = length(dir(horzcat(Location,filesep,Extension))); 41 | else 42 | DirLoc = Dir_location; 43 | No_of_files = NoFiles; 44 | end 45 | end 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Gentle Boost/Gentle_boost_with_Gabor.m: -------------------------------------------------------------------------------- 1 | 2 | % Set constants 3 | f = 5.5/24; % Frequency 4 | halfSDsq = 3/( 2*pi*f*24 ); % Sigma 5 | gamma = 0.75; 6 | phases = 2; 7 | ori = 4; %orientation 8 | 9 | % Hard coded filter properties 10 | kernelSize = 11; % Windowsize 11 | 12 | % Please provide the Training set location & 'No_Testset' 13 | TrainingSet_add = 'F:\Matlab\new'; 14 | % TrainingSet_add = 'F:\101_ObjectCategories'; 15 | No_Testset = 15; 16 | 17 | [TrainingSet_location, Train_label_Names, noFiles] = DirRead(TrainingSet_add,'*.jpg'); 18 | 19 | % Process Training set & Test set 20 | [TrainingSet,TestSet,train_label] = SVM_datasets(TrainingSet_location, No_Testset,51, 51); 21 | 22 | Training_Set = ImgGabor(TrainingSet, kernelSize, ori, phases, gamma, f, halfSDsq); 23 | TestSet = ImgGabor(TestSet, kernelSize, ori, phases, gamma, f, halfSDsq); 24 | 25 | result=multiBoost(Training_Set,train_label,Test_Set,'GentleBoost',50,'tree'); 26 | -------------------------------------------------------------------------------- /Gentle Boost/Gentle_boost_without_Gabor.m: -------------------------------------------------------------------------------- 1 | % Set constants 2 | f = 5.5/24; % Frequency 3 | halfSDsq = 3/( 2*pi*f*24 ); % Sigma 4 | gamma = 0.75; 5 | phases = 2; 6 | ori = 4; %orientation 7 | 8 | % Hard coded filter properties 9 | kernelSize = 11; % Windowsize 10 | 11 | % Please provide the Training set location & 'No_Testset' 12 | TrainingSet_add = 'F:\Matlab\new'; 13 | % TrainingSet_add = 'F:\101_ObjectCategories'; 14 | No_Testset = 15; 15 | 16 | [TrainingSet_location, Train_label_Names, noFiles] = DirRead(TrainingSet_add,'*.jpg'); 17 | 18 | % Process Training set & Test set 19 | [TrainingSet,TestSet,train_label] = SVM_datasets(TrainingSet_location, No_Testset,51, 51); 20 | 21 | Training_Set=[]; 22 | 23 | for i=1:length(TrainingSet) 24 | Training_Set_tmp = reshape(TrainingSet{i},1, 51*51); 25 | Training_Set=[Training_Set;Training_Set_tmp]; 26 | end 27 | 28 | Test_Set=[]; 29 | for j=1:length(TestSet) 30 | Test_set_tmp = reshape(TestSet{j},1, 51*51); 31 | Test_Set=[Test_Set;Test_set_tmp]; 32 | end 33 | 34 | result=multiBoost(Training_Set,train_label,Test_Set,'GentleBoost',50,'tree'); -------------------------------------------------------------------------------- /Gentle Boost/ImgGabor.m: -------------------------------------------------------------------------------- 1 | function gaborProcessed = ImgGabor(ImgLocation,kernelSize,ori,phases,gamma,f,halfSDsq) 2 | 3 | if nargin < 3 4 | ori=4; %ori is a constant (orientation) 5 | end 6 | 7 | fprintf(1, '\nInitialising data structure\n'); 8 | gaborProcessed=zeros(length(ImgLocation),ori*phases*2*prod(size(ImgLocation{1})-(kernelSize-1))); 9 | 10 | 11 | fprintf(1, 'Now processing the images with Gabor filter\n'); 12 | output=''; 13 | for i=1:ori 14 | for j=1:phases 15 | %% Generate the Gabor function 16 | % Convert the orientation number into an angle (theta) 17 | theta = 0 + (i-1)*pi/ori; 18 | 19 | % Generate a matrix of size 11x11 (kernelSize), containing the Gabor weights 20 | RF = gaborBeuthMod(kernelSize, halfSDsq, theta, f, pi/phases * (j-1), gamma); 21 | 22 | % Normalizes the Gabor filter to a maximum value of 1 23 | RF = RF.*1/max([abs(max(max(RF))) abs(min(min(RF)))]); 24 | 25 | % Normalize the Gabor filter to zero mean 26 | RF = RF-sum(sum(RF))/kernelSize^2; 27 | 28 | %% Convolute each image 29 | for k = 1:length(ImgLocation) 30 | 31 | % Convolve the given image (IMAGE) using the Gabor filter (RF) 32 | E = convn(ImgLocation{k}, RF, 'valid'); 33 | 34 | % split the result into a positive and negative part, using only positive values 35 | E_p = E; 36 | E_p(E_p<0) = 0; 37 | E_n = -E; 38 | E_n(E_n<0) = 0; 39 | 40 | % Store the part results at the right position in the data vector 41 | lowerindex=(i-1)* phases*2*prod(size(E)) + (j-1)*2*prod(size(E)) + 1; 42 | upperindex=(i-1)* phases*2*prod(size(E)) + j*2*prod(size(E)); 43 | gaborProcessed(k,lowerindex:upperindex) = reshape([E_p;E_n],[],1); 44 | 45 | %Progress measurement 46 | outputOld=output; 47 | outputNr=sprintf('%g',floor(( k+(j-1)*length(ImgLocation)+(i-1)*phases*length(ImgLocation) ) * 100 / (ori*phases*length(ImgLocation)) )); 48 | output=[outputNr,'%%']; 49 | if strcmp(output,outputOld)==0 50 | for l=1:length(outputOld)-1 51 | fprintf('\b') 52 | end 53 | fprintf(output) 54 | end 55 | end 56 | end 57 | end 58 | end -------------------------------------------------------------------------------- /Gentle Boost/SVM_datasets.m: -------------------------------------------------------------------------------- 1 | 2 | function [Train_Set,Test_Set,train_label] = SVM_datasets(TrainingSet_location, No_Trainset, No_Testset,width, height) 3 | % This function will generate the Training set, random Test set, and 4 | % training class for multiSVM 5 | % Inputs : TraininSet_location, No_Testset,width,height 6 | % TraininSet_location : Only o/p of function DirRead is accepted or an 7 | % array of locations 8 | % No_Testset : No of test set needed per class for svm train 9 | % width, height : Resize parameters 10 | % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 11 | % Outputs : Train_set, Test_set, train_label 12 | % Train_set : Training set ready for svm training 13 | % Test_set : Test set ready for svm classify 14 | % train_label : Training set class label 15 | % ---------------------------------------------------------- 16 | 17 | Train_Set = []; 18 | Train_Set_tmp = []; 19 | Test_Set = []; 20 | imageArray = cell(No_Testset, 1); 21 | train_label = []; 22 | for i=1:length(TrainingSet_location) 23 | 24 | % Training set process 25 | No_of_Files=length(dir(fullfile(TrainingSet_location{i},'*.jpg'))); 26 | k = dir(fullfile(TrainingSet_location{i},'*.jpg')); 27 | k = {k(~[k.isdir]).name}; 28 | No = No_Trainset+No_Testset; 29 | if (No > No_of_Files) 30 | No = No_of_Files; 31 | end 32 | for j=1:No 33 | tempImage = imread(horzcat(TrainingSet_location{i},filesep,k{j})); 34 | imgInfo = imfinfo(horzcat(TrainingSet_location{i},filesep,k{j})); 35 | 36 | % Image transformation 37 | if strcmp(imgInfo.ColorType,'grayscale') 38 | imageArray{j} = double(imresize(tempImage,[width height])); % array of images 39 | else 40 | imageArray{j} = double(imresize(rgb2gray(tempImage),[width height])); % array of images 41 | end 42 | end 43 | Train_Set_tmp = imageArray; 44 | imageArray = cell(No_Trainset, 1); 45 | 46 | % Training set 47 | for k=1:No_Trainset 48 | Train_Set = [Train_Set; Train_Set_tmp(k) ]; 49 | Train_Set_tmp{k} = {}; 50 | 51 | % Training set label 52 | train_label = [train_label; i ]; 53 | end 54 | Train_Set_tmp = Train_Set_tmp(~cellfun(@isempty, Train_Set_tmp)); 55 | 56 | % Test set 57 | for l=1:length(Train_Set_tmp) 58 | Test_Set = [Test_Set; Train_Set_tmp(l) ]; 59 | end 60 | end 61 | end 62 | 63 | -------------------------------------------------------------------------------- /Gentle Boost/gaborBeuthMod.m: -------------------------------------------------------------------------------- 1 | %Gabor filtering 2 | % 3 | % authors: beuth 4 | % modified: temi 5 | % 6 | % theta => orientation 7 | % psi => phase shift 8 | 9 | function RF = gaborBeuthMod(kernelSize, halfSDsq, theta, f, psi, gamma) 10 | 11 | %some terms for the gabor equation 12 | twopiF = 2 * pi *f; 13 | 14 | %calculate grids (calculation points) for RF 15 | a = floor(kernelSize/2); %half support upper boundary 16 | [X,Y] = meshgrid(-a:1:a, -a:1:a); 17 | XT1 = (X) .* cos(theta) + Y.*sin(theta); 18 | YT1 = Y.*cos(theta) - (X) .* sin(theta); 19 | 20 | % Receptive Field (RF) 21 | RF = exp( - halfSDsq*(XT1.*XT1+gamma^2*YT1.*YT1)) .* cos(twopiF*XT1 + psi); 22 | 23 | end -------------------------------------------------------------------------------- /Gentle Boost/multiBoost.m: -------------------------------------------------------------------------------- 1 | function [result] = multiBoost(TrainingSet,GroupTrain,TestSet, varargin) 2 | 3 | 4 | u=unique(GroupTrain); 5 | numClasses=length(u); 6 | result = zeros(length(TestSet(:,1)),1); 7 | 8 | %build models 9 | for k=1:numClasses 10 | %Vectorized statement that binarizes Group 11 | %where 1 is the current class and 0 is all other classes 12 | G1vAll=(GroupTrain==u(k)); 13 | models = fitensemble(TrainingSet,G1vAll,varargin{:}); 14 | p{k}= models; 15 | end 16 | 17 | %classify test cases 18 | for j=1:size(TestSet,1) 19 | for k=1:numClasses 20 | if(predict(p{k},TestSet(j,:))) 21 | break; 22 | end 23 | end 24 | result(j) = k; 25 | end 26 | 27 | -------------------------------------------------------------------------------- /ImgGabor.m: -------------------------------------------------------------------------------- 1 | function gaborProcessed = ImgGabor(ImgLocation,kernelSize,ori,phases,gamma,f,halfSDsq) 2 | 3 | if nargin < 3 4 | ori=4; %ori is a constant (orientation) 5 | end 6 | 7 | fprintf(1, '\nInitialising data structure\n'); 8 | gaborProcessed=zeros(length(ImgLocation),ori*phases*2*prod(size(ImgLocation{1})-(kernelSize-1))); 9 | 10 | 11 | fprintf(1, 'Now processing the images with Gabor filter\n'); 12 | output=''; 13 | for i=1:ori 14 | for j=1:phases 15 | %% Generate the Gabor function 16 | % Convert the orientation number into an angle (theta) 17 | theta = 0 + (i-1)*pi/ori; 18 | 19 | % Generate a matrix of size 11x11 (kernelSize), containing the Gabor weights 20 | RF = gaborBeuthMod(kernelSize, halfSDsq, theta, f, pi/phases * (j-1), gamma); 21 | 22 | % Normalizes the Gabor filter to a maximum value of 1 23 | RF = RF.*1/max([abs(max(max(RF))) abs(min(min(RF)))]); 24 | 25 | % Normalize the Gabor filter to zero mean 26 | RF = RF-sum(sum(RF))/kernelSize^2; 27 | 28 | %% Convolute each image 29 | for k = 1:length(ImgLocation) 30 | 31 | % Convolve the given image (IMAGE) using the Gabor filter (RF) 32 | E = convn(ImgLocation{k}, RF, 'valid'); 33 | 34 | % split the result into a positive and negative part, using only positive values 35 | E_p = E; 36 | E_p(E_p<0) = 0; 37 | E_n = -E; 38 | E_n(E_n<0) = 0; 39 | 40 | % Store the part results at the right position in the data vector 41 | lowerindex=(i-1)* phases*2*prod(size(E)) + (j-1)*2*prod(size(E)) + 1; 42 | upperindex=(i-1)* phases*2*prod(size(E)) + j*2*prod(size(E)); 43 | gaborProcessed(k,lowerindex:upperindex) = reshape([E_p;E_n],[],1); 44 | 45 | %Progress measurement 46 | outputOld=output; 47 | outputNr=sprintf('%g',floor(( k+(j-1)*length(ImgLocation)+(i-1)*phases*length(ImgLocation) ) * 100 / (ori*phases*length(ImgLocation)) )); 48 | output=[outputNr,'%%']; 49 | if strcmp(output,outputOld)==0 50 | for l=1:length(outputOld)-1 51 | fprintf('\b') 52 | end 53 | fprintf(output) 54 | end 55 | end 56 | end 57 | end 58 | end -------------------------------------------------------------------------------- /Knn/DirRead.m: -------------------------------------------------------------------------------- 1 | function [DirLoc, nameFolds, No_of_files, Name_of_files] = DirRead(Location, Extension ) 2 | 3 | % This function is specialy written for "SVM_datasets" function. 4 | % This function checks if there exists another subdirectory. 5 | % If there exists another dir then save the name and address 6 | %---------------------------------------------------------- 7 | % Inputs: Location 8 | % Location Path to the folder contaning the subfolder which has images 9 | % Extension : please use format like this '*.jpg' 10 | %----------------------------------------------------------- 11 | % Outputs: DirLoc, No_of_files, Name_of_folders 12 | % DirLoc Full address of the subdirectory - Array 13 | % No_of_files Number of files in the directory 14 | % nameFolds Name of Folders or Training level is named accordingly 15 | % incase if we are using it for my SVM_datasets specially. 16 | % Name_of_files : This will provide name of the files if there is no sub 17 | % directory 18 | % ---------------------------------------------------------- 19 | 20 | x=dir(Location); 21 | isub = [x(:).isdir]; 22 | nameFolds = {x(isub).name}'; 23 | nameFolds(ismember(nameFolds,{'.','..'})) = []; 24 | 25 | % If no subfolders then process all the files and save the names. 26 | list = dir(Location); 27 | isfile=~[list.isdir]; 28 | filenames={list(isfile).name}; 29 | Name_of_files = filenames.'; 30 | 31 | Dir_location=cell(length(nameFolds),1); 32 | NoFiles = zeros(length(nameFolds),1); 33 | 34 | for i=1:length(nameFolds) 35 | Dir_location{i} = horzcat(Location,filesep,nameFolds{i}); 36 | NoFiles(i) = length(dir(horzcat(Dir_location{i},filesep,Extension))); 37 | end 38 | if isempty(Dir_location) 39 | DirLoc = Location; 40 | No_of_files = length(dir(horzcat(Location,filesep,Extension))); 41 | else 42 | DirLoc = Dir_location; 43 | No_of_files = NoFiles; 44 | end 45 | end 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Knn/ImgGabor.m: -------------------------------------------------------------------------------- 1 | function gaborProcessed = ImgGabor(ImgLocation,kernelSize,ori,phases,gamma,f,halfSDsq) 2 | 3 | if nargin < 3 4 | ori=4; %ori is a constant (orientation) 5 | end 6 | 7 | fprintf(1, '\nInitialising data structure\n'); 8 | gaborProcessed=zeros(length(ImgLocation),ori*phases*2*prod(size(ImgLocation{1})-(kernelSize-1))); 9 | 10 | 11 | fprintf(1, 'Now processing the images with Gabor filter\n'); 12 | output=''; 13 | for i=1:ori 14 | for j=1:phases 15 | %% Generate the Gabor function 16 | % Convert the orientation number into an angle (theta) 17 | theta = 0 + (i-1)*pi/ori; 18 | 19 | % Generate a matrix of size 11x11 (kernelSize), containing the Gabor weights 20 | RF = gaborBeuthMod(kernelSize, halfSDsq, theta, f, pi/phases * (j-1), gamma); 21 | 22 | % Normalizes the Gabor filter to a maximum value of 1 23 | RF = RF.*1/max([abs(max(max(RF))) abs(min(min(RF)))]); 24 | 25 | % Normalize the Gabor filter to zero mean 26 | RF = RF-sum(sum(RF))/kernelSize^2; 27 | 28 | %% Convolute each image 29 | for k = 1:length(ImgLocation) 30 | 31 | % Convolve the given image (IMAGE) using the Gabor filter (RF) 32 | E = convn(ImgLocation{k}, RF, 'valid'); 33 | 34 | % split the result into a positive and negative part, using only positive values 35 | E_p = E; 36 | E_p(E_p<0) = 0; 37 | E_n = -E; 38 | E_n(E_n<0) = 0; 39 | 40 | % Store the part results at the right position in the data vector 41 | lowerindex=(i-1)* phases*2*prod(size(E)) + (j-1)*2*prod(size(E)) + 1; 42 | upperindex=(i-1)* phases*2*prod(size(E)) + j*2*prod(size(E)); 43 | gaborProcessed(k,lowerindex:upperindex) = reshape([E_p;E_n],[],1); 44 | 45 | %Progress measurement 46 | outputOld=output; 47 | outputNr=sprintf('%g',floor(( k+(j-1)*length(ImgLocation)+(i-1)*phases*length(ImgLocation) ) * 100 / (ori*phases*length(ImgLocation)) )); 48 | output=[outputNr,'%%']; 49 | if strcmp(output,outputOld)==0 50 | for l=1:length(outputOld)-1 51 | fprintf('\b') 52 | end 53 | fprintf(output) 54 | end 55 | end 56 | end 57 | end 58 | end -------------------------------------------------------------------------------- /Knn/SVM_datasets.m: -------------------------------------------------------------------------------- 1 | 2 | function [Train_Set,Test_Set,train_label] = SVM_datasets(TrainingSet_location, No_Trainset, No_Testset,width, height) 3 | % This function will generate the Training set, random Test set, and 4 | % training class for multiSVM 5 | % Inputs : TraininSet_location, No_Testset,width,height 6 | % TraininSet_location : Only o/p of function DirRead is accepted or an 7 | % array of locations 8 | % No_Testset : No of test set needed per class for svm train 9 | % width, height : Resize parameters 10 | % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 11 | % Outputs : Train_set, Test_set, train_label 12 | % Train_set : Training set ready for svm training 13 | % Test_set : Test set ready for svm classify 14 | % train_label : Training set class label 15 | % ---------------------------------------------------------- 16 | 17 | Train_Set = []; 18 | Train_Set_tmp = []; 19 | Test_Set = []; 20 | imageArray = cell(No_Testset, 1); 21 | train_label = []; 22 | for i=1:length(TrainingSet_location) 23 | 24 | % Training set process 25 | No_of_Files=length(dir(fullfile(TrainingSet_location{i},'*.jpg'))); 26 | k = dir(fullfile(TrainingSet_location{i},'*.jpg')); 27 | k = {k(~[k.isdir]).name}; 28 | No = No_Trainset+No_Testset; 29 | if (No > No_of_Files) 30 | No = No_of_Files; 31 | end 32 | for j=1:No 33 | tempImage = imread(horzcat(TrainingSet_location{i},filesep,k{j})); 34 | imgInfo = imfinfo(horzcat(TrainingSet_location{i},filesep,k{j})); 35 | 36 | % Image transformation 37 | if strcmp(imgInfo.ColorType,'grayscale') 38 | imageArray{j} = double(imresize(tempImage,[width height])); % array of images 39 | else 40 | imageArray{j} = double(imresize(rgb2gray(tempImage),[width height])); % array of images 41 | end 42 | end 43 | Train_Set_tmp = imageArray; 44 | imageArray = cell(No_Trainset, 1); 45 | 46 | % Training set 47 | for k=1:No_Trainset 48 | Train_Set = [Train_Set; Train_Set_tmp(k) ]; 49 | Train_Set_tmp{k} = {}; 50 | 51 | % Training set label 52 | train_label = [train_label; i ]; 53 | end 54 | Train_Set_tmp = Train_Set_tmp(~cellfun(@isempty, Train_Set_tmp)); 55 | 56 | % Test set 57 | for l=1:length(Train_Set_tmp) 58 | Test_Set = [Test_Set; Train_Set_tmp(l) ]; 59 | end 60 | end 61 | end 62 | 63 | -------------------------------------------------------------------------------- /Knn/gaborBeuthMod.m: -------------------------------------------------------------------------------- 1 | %Gabor filtering 2 | % 3 | % authors: beuth 4 | % modified: temi 5 | % 6 | % theta => orientation 7 | % psi => phase shift 8 | 9 | function RF = gaborBeuthMod(kernelSize, halfSDsq, theta, f, psi, gamma) 10 | 11 | %some terms for the gabor equation 12 | twopiF = 2 * pi *f; 13 | 14 | %calculate grids (calculation points) for RF 15 | a = floor(kernelSize/2); %half support upper boundary 16 | [X,Y] = meshgrid(-a:1:a, -a:1:a); 17 | XT1 = (X) .* cos(theta) + Y.*sin(theta); 18 | YT1 = Y.*cos(theta) - (X) .* sin(theta); 19 | 20 | % Receptive Field (RF) 21 | RF = exp( - halfSDsq*(XT1.*XT1+gamma^2*YT1.*YT1)) .* cos(twopiF*XT1 + psi); 22 | 23 | end -------------------------------------------------------------------------------- /Knn/knnClassify_with_Gabor_Filter.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | % Set constants 4 | f = 5.5/24; % Frequency 5 | halfSDsq = 3/( 2*pi*f*24 ); % Sigma 6 | gamma = 0.75; 7 | phases = 2; 8 | ori = 4; %orientation 9 | 10 | % Hard coded filter properties 11 | kernelSize = 11; % Windowsize 12 | 13 | % Please provide the Training set location & 'No_Testset' 14 | TrainingSet_add = 'F:\Matlab\new'; 15 | % TrainingSet_add = 'F:\101_ObjectCategories'; 16 | No_Testset = 50; 17 | No_Trainset = 15; 18 | 19 | [TrainingSet_location, Train_label_Names, noFiles] = DirRead(TrainingSet_add,'*.jpg'); 20 | 21 | % Process Training set & Test set 22 | [TrainingSet,TestSet,train_label] = SVM_datasets(TrainingSet_location,No_Trainset, No_Testset,140, 140); 23 | 24 | % Apply Gabour Filter to Training set & Testset images 25 | Training_Set = ImgGabor(TrainingSet, kernelSize, ori, phases, gamma, f, halfSDsq); 26 | TestSet = ImgGabor(TestSet, kernelSize, ori, phases, gamma, f, halfSDsq); 27 | 28 | class = knnclassify(TestSet, Training_Set, train_label) 29 | -------------------------------------------------------------------------------- /Knn/knnClassify_without_Gabor_Filter.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | % Training set location & 'No_Testset' 4 | TrainingSet_add = 'F:\Matlab\new'; 5 | No_Testset = 15; 6 | 7 | [TrainingSet_location, Train_label_Names, noFiles] = DirRead(TrainingSet_add,'*.jpg'); 8 | 9 | % Process Training set & Test set 10 | [TrainingSet,TestSet,train_label] = SVM_datasets(TrainingSet_location, No_Testset,100, 100); 11 | 12 | Training_Set=[]; 13 | 14 | for i=1:length(TrainingSet) 15 | Training_Set_tmp = reshape(TrainingSet{i},1, 100*100); 16 | Training_Set=[Training_Set;Training_Set_tmp]; 17 | end 18 | 19 | Test_Set=[]; 20 | for j=1:length(TestSet) 21 | Test_set_tmp = reshape(TestSet{j},1, 100*100); 22 | Test_Set=[Test_Set;Test_set_tmp]; 23 | end 24 | 25 | class = knnclassify(Test_Set, Training_Set, train_label) -------------------------------------------------------------------------------- /LDA/DirRead.m: -------------------------------------------------------------------------------- 1 | function [DirLoc, nameFolds, No_of_files, Name_of_files] = DirRead(Location, Extension ) 2 | 3 | % This function is specialy written for "SVM_datasets" function. 4 | % This function checks if there exists another subdirectory. 5 | % If there exists another dir then save the name and address 6 | %---------------------------------------------------------- 7 | % Inputs: Location 8 | % Location Path to the folder contaning the subfolder which has images 9 | % Extension : please use format like this '*.jpg' 10 | %----------------------------------------------------------- 11 | % Outputs: DirLoc, No_of_files, Name_of_folders 12 | % DirLoc Full address of the subdirectory - Array 13 | % No_of_files Number of files in the directory 14 | % nameFolds Name of Folders or Training level is named accordingly 15 | % incase if we are using it for my SVM_datasets specially. 16 | % Name_of_files : This will provide name of the files if there is no sub 17 | % directory 18 | % ---------------------------------------------------------- 19 | 20 | x=dir(Location); 21 | isub = [x(:).isdir]; 22 | nameFolds = {x(isub).name}'; 23 | nameFolds(ismember(nameFolds,{'.','..'})) = []; 24 | 25 | % If no subfolders then process all the files and save the names. 26 | list = dir(Location); 27 | isfile=~[list.isdir]; 28 | filenames={list(isfile).name}; 29 | Name_of_files = filenames.'; 30 | 31 | Dir_location=cell(length(nameFolds),1); 32 | NoFiles = zeros(length(nameFolds),1); 33 | 34 | for i=1:length(nameFolds) 35 | Dir_location{i} = horzcat(Location,filesep,nameFolds{i}); 36 | NoFiles(i) = length(dir(horzcat(Dir_location{i},filesep,Extension))); 37 | end 38 | if isempty(Dir_location) 39 | DirLoc = Location; 40 | No_of_files = length(dir(horzcat(Location,filesep,Extension))); 41 | else 42 | DirLoc = Dir_location; 43 | No_of_files = NoFiles; 44 | end 45 | end 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /LDA/ImgGabor.m: -------------------------------------------------------------------------------- 1 | function gaborProcessed = ImgGabor(ImgLocation,kernelSize,ori,phases,gamma,f,halfSDsq) 2 | 3 | if nargin < 3 4 | ori=4; %ori is a constant (orientation) 5 | end 6 | 7 | fprintf(1, '\nInitialising data structure\n'); 8 | gaborProcessed=zeros(length(ImgLocation),ori*phases*2*prod(size(ImgLocation{1})-(kernelSize-1))); 9 | 10 | 11 | fprintf(1, 'Now processing the images with Gabor filter\n'); 12 | output=''; 13 | for i=1:ori 14 | for j=1:phases 15 | %% Generate the Gabor function 16 | % Convert the orientation number into an angle (theta) 17 | theta = 0 + (i-1)*pi/ori; 18 | 19 | % Generate a matrix of size 11x11 (kernelSize), containing the Gabor weights 20 | RF = gaborBeuthMod(kernelSize, halfSDsq, theta, f, pi/phases * (j-1), gamma); 21 | 22 | % Normalizes the Gabor filter to a maximum value of 1 23 | RF = RF.*1/max([abs(max(max(RF))) abs(min(min(RF)))]); 24 | 25 | % Normalize the Gabor filter to zero mean 26 | RF = RF-sum(sum(RF))/kernelSize^2; 27 | 28 | %% Convolute each image 29 | for k = 1:length(ImgLocation) 30 | 31 | % Convolve the given image (IMAGE) using the Gabor filter (RF) 32 | E = convn(ImgLocation{k}, RF, 'valid'); 33 | 34 | % split the result into a positive and negative part, using only positive values 35 | E_p = E; 36 | E_p(E_p<0) = 0; 37 | E_n = -E; 38 | E_n(E_n<0) = 0; 39 | 40 | % Store the part results at the right position in the data vector 41 | lowerindex=(i-1)* phases*2*prod(size(E)) + (j-1)*2*prod(size(E)) + 1; 42 | upperindex=(i-1)* phases*2*prod(size(E)) + j*2*prod(size(E)); 43 | gaborProcessed(k,lowerindex:upperindex) = reshape([E_p;E_n],[],1); 44 | 45 | %Progress measurement 46 | outputOld=output; 47 | outputNr=sprintf('%g',floor(( k+(j-1)*length(ImgLocation)+(i-1)*phases*length(ImgLocation) ) * 100 / (ori*phases*length(ImgLocation)) )); 48 | output=[outputNr,'%%']; 49 | if strcmp(output,outputOld)==0 50 | for l=1:length(outputOld)-1 51 | fprintf('\b') 52 | end 53 | fprintf(output) 54 | end 55 | end 56 | end 57 | end 58 | end -------------------------------------------------------------------------------- /LDA/LDA_with_Gabor_Filter.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear all 3 | 4 | % Set constants 5 | f = 5.5/24; % Frequency 6 | halfSDsq = 3/( 2*pi*f*24 ); % Sigma 7 | gamma = 0.75; 8 | phases = 2; 9 | ori = 4; %orientation 10 | 11 | % Hard coded filter properties 12 | kernelSize = 11; % Windowsize 13 | 14 | % Please provide the Training set location & 'No_Testset' 15 | TrainingSet_add = 'F:\Matlab\new'; 16 | % TrainingSet_add = 'F:\101_ObjectCategories'; 17 | No_Testset = 50; 18 | No_Trainset = 15; 19 | 20 | [TrainingSet_location, Train_label_Names, noFiles] = DirRead(TrainingSet_add,'*.jpg'); 21 | 22 | % Process Training set & Test set 23 | [TrainingSet,TestSet,train_label] = SVM_datasets(TrainingSet_location,No_Trainset, No_Testset,140, 140); 24 | 25 | % Apply Gabour Filter to Training set & Testset images 26 | Training_Set = ImgGabor(TrainingSet, kernelSize, ori, phases, gamma, f, halfSDsq); 27 | TestSet = ImgGabor(TestSet, kernelSize, ori, phases, gamma, f, halfSDsq); 28 | 29 | %class = classify(TestSet,Training_Set,train_label,'linear'); 30 | class = classify(TestSet,Training_Set+10^-7*rand(size(Training_Set,1),size(Training_Set,2)),train_label,'linear'); -------------------------------------------------------------------------------- /LDA/LDA_without_Gabor_Filter.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | % Training set location & 'No_Testset' 4 | TrainingSet_add = 'F:\Matlab\new'; 5 | No_Testset = 15; 6 | 7 | [TrainingSet_location, Train_label_Names, noFiles] = DirRead(TrainingSet_add,'*.jpg'); 8 | 9 | % Process Training set & Test set 10 | [TrainingSet,TestSet,train_label] = SVM_datasets(TrainingSet_location, No_Testset,100, 100); 11 | 12 | Training_Set=[]; 13 | 14 | for i=1:length(TrainingSet) 15 | Training_Set_tmp = reshape(TrainingSet{i},1, 100*100); 16 | Training_Set=[Training_Set;Training_Set_tmp]; 17 | end 18 | 19 | Test_Set=[]; 20 | for j=1:length(TestSet) 21 | Test_set_tmp = reshape(TestSet{j},1, 100*100); 22 | Test_Set=[Test_Set;Test_set_tmp]; 23 | end 24 | 25 | class = classify(Test_Set,Training_Set,train_label,'linear') -------------------------------------------------------------------------------- /LDA/SVM_datasets.m: -------------------------------------------------------------------------------- 1 | 2 | function [Train_Set,Test_Set,train_label] = SVM_datasets(TrainingSet_location, No_Trainset, No_Testset,width, height) 3 | % This function will generate the Training set, random Test set, and 4 | % training class for multiSVM 5 | % Please use the output of my DirRead function as a input 6 | % http://www.mathworks.com/matlabcentral/fileexchange/40020-dir-read/content/DirRead.m 7 | 8 | % Inputs : TraininSet_location, No_Testset,width,height 9 | % TraininSet_location : Only o/p of function DirRead is accepted or an 10 | % array of locations 11 | % No_Testset : No of test set needed per class for svm train 12 | % No_Trainset : No of test set needed per class for svm train 13 | % width, height : Resize parameters 14 | % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 15 | % Outputs : Train_set, Test_set, train_label 16 | % Train_set : Training set ready for svm training 17 | % Test_set : Test set ready for svm classify 18 | % train_label : Training set class label 19 | % ---------------------------------------------------------- 20 | % 21 | % ---------------------------------------------------------- 22 | % This function is written by Om choudhary and this is free to use. 23 | % Email: choudharyom@hotmail.com 24 | 25 | Train_Set = []; 26 | Train_Set_tmp = []; 27 | Test_Set = []; 28 | imageArray = cell(No_Testset, 1); 29 | train_label = []; 30 | for i=1:length(TrainingSet_location) 31 | 32 | % Training set process 33 | No_of_Files=length(dir(fullfile(TrainingSet_location{i},'*.jpg'))); 34 | k = dir(fullfile(TrainingSet_location{i},'*.jpg')); 35 | k = {k(~[k.isdir]).name}; 36 | No = No_Trainset+No_Testset; 37 | if (No > No_of_Files) 38 | No = No_of_Files; 39 | end 40 | for j=1:No 41 | tempImage = imread(horzcat(TrainingSet_location{i},filesep,k{j})); 42 | imgInfo = imfinfo(horzcat(TrainingSet_location{i},filesep,k{j})); 43 | 44 | % Image transformation 45 | if strcmp(imgInfo.ColorType,'grayscale') 46 | imageArray{j} = double(imresize(tempImage,[width height])); % array of images 47 | else 48 | imageArray{j} = double(imresize(rgb2gray(tempImage),[width height])); % array of images 49 | end 50 | end 51 | Train_Set_tmp = imageArray; 52 | imageArray = cell(No_Trainset, 1); 53 | 54 | % Training set 55 | for k=1:No_Trainset 56 | Train_Set = [Train_Set; Train_Set_tmp(k) ]; 57 | Train_Set_tmp{k} = {}; 58 | 59 | % Training set label 60 | train_label = [train_label; i ]; 61 | end 62 | Train_Set_tmp = Train_Set_tmp(~cellfun(@isempty, Train_Set_tmp)); 63 | 64 | % Test set 65 | for l=1:length(Train_Set_tmp) 66 | Test_Set = [Test_Set; Train_Set_tmp(l) ]; 67 | end 68 | end 69 | end 70 | 71 | -------------------------------------------------------------------------------- /LDA/gaborBeuthMod.m: -------------------------------------------------------------------------------- 1 | %Gabor filtering 2 | % 3 | % authors: beuth 4 | % modified: temi 5 | % 6 | % theta => orientation 7 | % psi => phase shift 8 | 9 | function RF = gaborBeuthMod(kernelSize, halfSDsq, theta, f, psi, gamma) 10 | 11 | %some terms for the gabor equation 12 | twopiF = 2 * pi *f; 13 | 14 | %calculate grids (calculation points) for RF 15 | a = floor(kernelSize/2); %half support upper boundary 16 | [X,Y] = meshgrid(-a:1:a, -a:1:a); 17 | XT1 = (X) .* cos(theta) + Y.*sin(theta); 18 | YT1 = Y.*cos(theta) - (X) .* sin(theta); 19 | 20 | % Receptive Field (RF) 21 | RF = exp( - halfSDsq*(XT1.*XT1+gamma^2*YT1.*YT1)) .* cos(twopiF*XT1 + psi); 22 | 23 | end -------------------------------------------------------------------------------- /New folder/Version control.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/choudharyom/matlab-machine-learning/9d620ccbfa5fdd2e078fcd16d3a5d24339e7e1fb/New folder/Version control.rar -------------------------------------------------------------------------------- /SVM/DirRead.m: -------------------------------------------------------------------------------- 1 | function [DirLoc, nameFolds, No_of_files, Name_of_files] = DirRead(Location, Extension ) 2 | 3 | % This function is specialy written for "SVM_datasets" function. 4 | % This function checks if there exists another subdirectory. 5 | % If there exists another dir then save the name and address 6 | %---------------------------------------------------------- 7 | % Inputs: Location 8 | % Location Path to the folder contaning the subfolder which has images 9 | % Extension : please use format like this '*.jpg' 10 | %----------------------------------------------------------- 11 | % Outputs: DirLoc, No_of_files, Name_of_folders 12 | % DirLoc Full address of the subdirectory - Array 13 | % No_of_files Number of files in the directory 14 | % nameFolds Name of Folders or Training level is named accordingly 15 | % incase if we are using it for my SVM_datasets specially. 16 | % Name_of_files : This will provide name of the files if there is no sub 17 | % directory 18 | % ---------------------------------------------------------- 19 | 20 | x=dir(Location); 21 | isub = [x(:).isdir]; 22 | nameFolds = {x(isub).name}'; 23 | nameFolds(ismember(nameFolds,{'.','..'})) = []; 24 | 25 | % If no subfolders then process all the files and save the names. 26 | list = dir(Location); 27 | isfile=~[list.isdir]; 28 | filenames={list(isfile).name}; 29 | Name_of_files = filenames.'; 30 | 31 | Dir_location=cell(length(nameFolds),1); 32 | NoFiles = zeros(length(nameFolds),1); 33 | 34 | for i=1:length(nameFolds) 35 | Dir_location{i} = horzcat(Location,filesep,nameFolds{i}); 36 | NoFiles(i) = length(dir(horzcat(Dir_location{i},filesep,Extension))); 37 | end 38 | if isempty(Dir_location) 39 | DirLoc = Location; 40 | No_of_files = length(dir(horzcat(Location,filesep,Extension))); 41 | else 42 | DirLoc = Dir_location; 43 | No_of_files = NoFiles; 44 | end 45 | end 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /SVM/Filter.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear all 3 | 4 | % Set constants 5 | f = 5.5/24; % Frequency 6 | halfSDsq = 3/( 2*pi*f*24 ); % Sigma 7 | gamma = 0.75; 8 | phases = 2; 9 | ori = 4; %orientation 10 | 11 | % Hard coded filter properties 12 | kernelSize = 11; % Windowsize 13 | 14 | % Training set location & no of test image to use per class 15 | TrainingSet_add = 'F:\101_ObjectCategories'; 16 | No_Testset = 50; 17 | No_Trainset = 15; 18 | 19 | % Process the Training set location 20 | TrainingSet_location = DirRead(TrainingSet_add,'*.jpg'); 21 | 22 | % Prepare Training set & Test set 23 | [TrainingSet,TestSet,train_label] = SVM_datasets(TrainingSet_location,No_Trainset, No_Testset,140, 140); 24 | 25 | % Apply Gabour Filter to Training set & Testset images 26 | Training_Set = ImgGabor(TrainingSet, kernelSize, ori, phases, gamma, f, halfSDsq); 27 | TestSet = ImgGabor(TestSet, kernelSize, ori, phases, gamma, f, halfSDsq); 28 | 29 | % Train the classifier 30 | PredictedLabels = multisvm(TrainingSet,train_label,TestSet,'kernel_function','linear','boxconstraint',2,'kktviolationlevel',0.1,'kernelcachelimit',10000,'options',statset('MaxIter',1000000)); 31 | -------------------------------------------------------------------------------- /SVM/ImgGabor.m: -------------------------------------------------------------------------------- 1 | function gaborProcessed = ImgGabor(ImgLocation,kernelSize,ori,phases,gamma,f,halfSDsq) 2 | 3 | if nargin < 3 4 | ori=4; %ori is a constant (orientation) 5 | end 6 | 7 | fprintf(1, '\nInitialising data structure\n'); 8 | gaborProcessed=zeros(length(ImgLocation),ori*phases*2*prod(size(ImgLocation{1})-(kernelSize-1))); 9 | 10 | 11 | fprintf(1, 'Now processing the images with Gabor filter\n'); 12 | output=''; 13 | for i=1:ori 14 | for j=1:phases 15 | %% Generate the Gabor function 16 | % Convert the orientation number into an angle (theta) 17 | theta = 0 + (i-1)*pi/ori; 18 | 19 | % Generate a matrix of size 11x11 (kernelSize), containing the Gabor weights 20 | RF = gaborBeuthMod(kernelSize, halfSDsq, theta, f, pi/phases * (j-1), gamma); 21 | 22 | % Normalizes the Gabor filter to a maximum value of 1 23 | RF = RF.*1/max([abs(max(max(RF))) abs(min(min(RF)))]); 24 | 25 | % Normalize the Gabor filter to zero mean 26 | RF = RF-sum(sum(RF))/kernelSize^2; 27 | 28 | %% Convolute each image 29 | for k = 1:length(ImgLocation) 30 | 31 | % Convolve the given image (IMAGE) using the Gabor filter (RF) 32 | E = convn(ImgLocation{k}, RF, 'valid'); 33 | 34 | % split the result into a positive and negative part, using only positive values 35 | E_p = E; 36 | E_p(E_p<0) = 0; 37 | E_n = -E; 38 | E_n(E_n<0) = 0; 39 | 40 | % Store the part results at the right position in the data vector 41 | lowerindex=(i-1)* phases*2*prod(size(E)) + (j-1)*2*prod(size(E)) + 1; 42 | upperindex=(i-1)* phases*2*prod(size(E)) + j*2*prod(size(E)); 43 | gaborProcessed(k,lowerindex:upperindex) = reshape([E_p;E_n],[],1); 44 | 45 | %Progress measurement 46 | outputOld=output; 47 | outputNr=sprintf('%g',floor(( k+(j-1)*length(ImgLocation)+(i-1)*phases*length(ImgLocation) ) * 100 / (ori*phases*length(ImgLocation)) )); 48 | output=[outputNr,'%%']; 49 | if strcmp(output,outputOld)==0 50 | for l=1:length(outputOld)-1 51 | fprintf('\b') 52 | end 53 | fprintf(output) 54 | end 55 | end 56 | end 57 | end 58 | end -------------------------------------------------------------------------------- /SVM/SVM_datasets.m: -------------------------------------------------------------------------------- 1 | 2 | function [Train_Set,Test_Set,train_label] = SVM_datasets(TrainingSet_location, No_Trainset, No_Testset,width, height) 3 | % This function will generate the Training set, random Test set, and 4 | % training class for multiSVM 5 | % Inputs : TraininSet_location, No_Testset,width,height 6 | % TraininSet_location : Only o/p of function DirRead is accepted or an 7 | % array of locations 8 | % No_Testset : No of test set needed per class for svm train 9 | % width, height : Resize parameters 10 | % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 11 | % Outputs : Train_set, Test_set, train_label 12 | % Train_set : Training set ready for svm training 13 | % Test_set : Test set ready for svm classify 14 | % train_label : Training set class label 15 | % ---------------------------------------------------------- 16 | 17 | Train_Set = []; 18 | Train_Set_tmp = []; 19 | Test_Set = []; 20 | imageArray = cell(No_Testset, 1); 21 | train_label = []; 22 | for i=1:length(TrainingSet_location) 23 | 24 | % Training set process 25 | No_of_Files=length(dir(fullfile(TrainingSet_location{i},'*.jpg'))); 26 | k = dir(fullfile(TrainingSet_location{i},'*.jpg')); 27 | k = {k(~[k.isdir]).name}; 28 | No = No_Trainset+No_Testset; 29 | if (No > No_of_Files) 30 | No = No_of_Files; 31 | end 32 | for j=1:No 33 | tempImage = imread(horzcat(TrainingSet_location{i},filesep,k{j})); 34 | imgInfo = imfinfo(horzcat(TrainingSet_location{i},filesep,k{j})); 35 | 36 | % Image transformation 37 | if strcmp(imgInfo.ColorType,'grayscale') 38 | imageArray{j} = double(imresize(tempImage,[width height])); % array of images 39 | else 40 | imageArray{j} = double(imresize(rgb2gray(tempImage),[width height])); % array of images 41 | end 42 | end 43 | Train_Set_tmp = imageArray; 44 | imageArray = cell(No_Trainset, 1); 45 | 46 | % Training set 47 | for k=1:No_Trainset 48 | Train_Set = [Train_Set; Train_Set_tmp(k) ]; 49 | Train_Set_tmp{k} = {}; 50 | 51 | % Training set label 52 | train_label = [train_label; i ]; 53 | end 54 | Train_Set_tmp = Train_Set_tmp(~cellfun(@isempty, Train_Set_tmp)); 55 | 56 | % Test set 57 | for l=1:length(Train_Set_tmp) 58 | Test_Set = [Test_Set; Train_Set_tmp(l) ]; 59 | end 60 | end 61 | end 62 | 63 | -------------------------------------------------------------------------------- /SVM/gaborBeuthMod.m: -------------------------------------------------------------------------------- 1 | %Gabor filtering 2 | % 3 | % authors: beuth 4 | % modified: temi 5 | % 6 | % theta => orientation 7 | % psi => phase shift 8 | 9 | function RF = gaborBeuthMod(kernelSize, halfSDsq, theta, f, psi, gamma) 10 | 11 | %some terms for the gabor equation 12 | twopiF = 2 * pi *f; 13 | 14 | %calculate grids (calculation points) for RF 15 | a = floor(kernelSize/2); %half support upper boundary 16 | [X,Y] = meshgrid(-a:1:a, -a:1:a); 17 | XT1 = (X) .* cos(theta) + Y.*sin(theta); 18 | YT1 = Y.*cos(theta) - (X) .* sin(theta); 19 | 20 | % Receptive Field (RF) 21 | RF = exp( - halfSDsq*(XT1.*XT1+gamma^2*YT1.*YT1)) .* cos(twopiF*XT1 + psi); 22 | 23 | end -------------------------------------------------------------------------------- /SVM/multisvm.m: -------------------------------------------------------------------------------- 1 | function [result] = multisvm(TrainingSet,GroupTrain,TestSet, varargin) 2 | %Models a given training set with a corresponding group vector and 3 | %classifies a given test set using an SVM classifier according to a 4 | %one vs. all relation. 5 | % 6 | %This code was written by Cody Neuburger cneuburg@fau.edu 7 | %Florida Atlantic University, Florida USA 8 | %This code was adapted and cleaned from Anand Mishra's multisvm function 9 | %found at http://www.mathworks.com/matlabcentral/fileexchange/33170-multi-class-support-vector-machine/ 10 | 11 | u=unique(GroupTrain); 12 | numClasses=length(u); 13 | result = zeros(length(TestSet(:,1)),1); 14 | 15 | %build models 16 | for k=1:numClasses 17 | %Vectorized statement that binarizes Group 18 | %where 1 is the current class and 0 is all other classes 19 | G1vAll=(GroupTrain==u(k)); 20 | models(k) = svmtrain(TrainingSet,G1vAll,varargin{:}); 21 | end 22 | 23 | %classify test cases 24 | for j=1:size(TestSet,1) 25 | for k=1:numClasses 26 | if(svmclassify(models(k),TestSet(j,:))) 27 | break; 28 | end 29 | end 30 | result(j) = k; 31 | end -------------------------------------------------------------------------------- /SVM_datasets.m: -------------------------------------------------------------------------------- 1 | 2 | function [Train_Set,Test_Set,train_label] = SVM_datasets(TrainingSet_location, No_Trainset, No_Testset,width, height) 3 | % This function will generate the Training set, random Test set, and 4 | % training class for multiSVM 5 | % Please use the output of my DirRead function as a input 6 | % http://www.mathworks.com/matlabcentral/fileexchange/40020-dir-read/content/DirRead.m 7 | 8 | % Inputs : TraininSet_location, No_Testset,width,height 9 | % TraininSet_location : Only o/p of function DirRead is accepted or an 10 | % array of locations 11 | % No_Testset : No of test set needed per class for svm train 12 | % No_Trainset : No of test set needed per class for svm train 13 | % width, height : Resize parameters 14 | % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 15 | % Outputs : Train_set, Test_set, train_label 16 | % Train_set : Training set ready for svm training 17 | % Test_set : Test set ready for svm classify 18 | % train_label : Training set class label 19 | % ---------------------------------------------------------- 20 | % 21 | % ---------------------------------------------------------- 22 | % This function is written by Om choudhary and this is free to use. 23 | % Email: choudharyom@hotmail.com 24 | 25 | Train_Set = []; 26 | Train_Set_tmp = []; 27 | Test_Set = []; 28 | imageArray = cell(No_Testset, 1); 29 | train_label = []; 30 | for i=1:length(TrainingSet_location) 31 | 32 | % Training set process 33 | No_of_Files=length(dir(fullfile(TrainingSet_location{i},'*.jpg'))); 34 | k = dir(fullfile(TrainingSet_location{i},'*.jpg')); 35 | k = {k(~[k.isdir]).name}; 36 | No = No_Trainset+No_Testset; 37 | if (No > No_of_Files) 38 | No = No_of_Files; 39 | end 40 | for j=1:No 41 | tempImage = imread(horzcat(TrainingSet_location{i},filesep,k{j})); 42 | imgInfo = imfinfo(horzcat(TrainingSet_location{i},filesep,k{j})); 43 | 44 | % Image transformation 45 | if strcmp(imgInfo.ColorType,'grayscale') 46 | imageArray{j} = double(imresize(tempImage,[width height])); % array of images 47 | else 48 | imageArray{j} = double(imresize(rgb2gray(tempImage),[width height])); % array of images 49 | end 50 | end 51 | Train_Set_tmp = imageArray; 52 | imageArray = cell(No_Trainset, 1); 53 | 54 | % Training set 55 | for k=1:No_Trainset 56 | Train_Set = [Train_Set; Train_Set_tmp(k) ]; 57 | Train_Set_tmp{k} = {}; 58 | 59 | % Training set label 60 | train_label = [train_label; i ]; 61 | end 62 | Train_Set_tmp = Train_Set_tmp(~cellfun(@isempty, Train_Set_tmp)); 63 | 64 | % Test set 65 | for l=1:length(Train_Set_tmp) 66 | Test_Set = [Test_Set; Train_Set_tmp(l) ]; 67 | end 68 | end 69 | end 70 | 71 | -------------------------------------------------------------------------------- /gaborBeuthMod.m: -------------------------------------------------------------------------------- 1 | %Gabor filtering 2 | % 3 | % authors: beuth 4 | % modified: temi 5 | % 6 | % theta => orientation 7 | % psi => phase shift 8 | 9 | function RF = gaborBeuthMod(kernelSize, halfSDsq, theta, f, psi, gamma) 10 | 11 | %some terms for the gabor equation 12 | twopiF = 2 * pi *f; 13 | 14 | %calculate grids (calculation points) for RF 15 | a = floor(kernelSize/2); %half support upper boundary 16 | [X,Y] = meshgrid(-a:1:a, -a:1:a); 17 | XT1 = (X) .* cos(theta) + Y.*sin(theta); 18 | YT1 = Y.*cos(theta) - (X) .* sin(theta); 19 | 20 | % Receptive Field (RF) 21 | RF = exp( - halfSDsq*(XT1.*XT1+gamma^2*YT1.*YT1)) .* cos(twopiF*XT1 + psi); 22 | 23 | end -------------------------------------------------------------------------------- /multisvm.m: -------------------------------------------------------------------------------- 1 | function [result] = multisvm(TrainingSet,GroupTrain,TestSet, varargin) 2 | %Models a given training set with a corresponding group vector and 3 | %classifies a given test set using an SVM classifier according to a 4 | %one vs. all relation. 5 | % 6 | %This code was written by Cody Neuburger cneuburg@fau.edu 7 | %Florida Atlantic University, Florida USA 8 | %This code was adapted and cleaned from Anand Mishra's multisvm function 9 | %found at http://www.mathworks.com/matlabcentral/fileexchange/33170-multi-class-support-vector-machine/ 10 | 11 | u=unique(GroupTrain); 12 | numClasses=length(u); 13 | result = zeros(length(TestSet(:,1)),1); 14 | 15 | %build models 16 | for k=1:numClasses 17 | %Vectorized statement that binarizes Group 18 | %where 1 is the current class and 0 is all other classes 19 | G1vAll=(GroupTrain==u(k)); 20 | models(k) = svmtrain(TrainingSet,G1vAll,varargin{:}); 21 | end 22 | 23 | %classify test cases 24 | for j=1:size(TestSet,1) 25 | for k=1:numClasses 26 | if(svmclassify(models(k),TestSet(j,:))) 27 | break; 28 | end 29 | end 30 | result(j) = k; 31 | end -------------------------------------------------------------------------------- /test.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear all 3 | 4 | width =140;height=140; 5 | 6 | % Training set location & no of test image to use per class 7 | TrainingSet_add = 'F:\101_ObjectCategories'; 8 | No_Testset = 50; 9 | No_Trainset = 15; 10 | 11 | % Process the Training set location 12 | TrainingSet_location = DirRead(TrainingSet_add,'*.jpg'); 13 | 14 | 15 | Train_Set = []; 16 | Train_Set_tmp = []; 17 | Test_Set = []; 18 | imageArray = cell(No_Testset, 1); 19 | train_label = []; 20 | for i=1:length(TrainingSet_location) 21 | 22 | % Training set process 23 | No_of_Files=length(dir(fullfile(TrainingSet_location{i},'*.jpg'))); 24 | k = dir(fullfile(TrainingSet_location{i},'*.jpg')); 25 | k = {k(~[k.isdir]).name}; 26 | No = No_Trainset+No_Testset; 27 | if (No > No_of_Files) 28 | No = No_of_Files; 29 | end 30 | for j=1:No 31 | tempImage = imread(horzcat(TrainingSet_location{i},filesep,k{j})); 32 | imgInfo = imfinfo(horzcat(TrainingSet_location{i},filesep,k{j})); 33 | 34 | % Image transformation 35 | if strcmp(imgInfo.ColorType,'grayscale') 36 | imageArray{j} = double(imresize(tempImage,[width height])); % array of images 37 | else 38 | imageArray{j} = double(imresize(rgb2gray(tempImage),[width height])); % array of images 39 | end 40 | end 41 | Train_Set_tmp = imageArray; 42 | imageArray = cell(No_Trainset, 1); 43 | 44 | % Training set 45 | for k=1:No_Trainset 46 | Train_Set = [Train_Set; Train_Set_tmp(k) ]; 47 | Train_Set_tmp{k} = {}; 48 | 49 | % Training set label 50 | train_label = [train_label; i ]; 51 | end 52 | Train_Set_tmp = Train_Set_tmp(~cellfun(@isempty, Train_Set_tmp)); 53 | 54 | % Test set 55 | for l=1:length(Train_Set_tmp) 56 | Test_Set = [Test_Set; Train_Set_tmp(l) ]; 57 | end 58 | end 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /test1.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear all 3 | 4 | width =140;height=140; 5 | 6 | % Training set location & no of test image to use per class 7 | TrainingSet_add = 'F:\101_ObjectCategories'; 8 | No_Testset = 50; 9 | No_Trainset = 15; 10 | 11 | % Process the Training set location 12 | TrainingSet_location = DirRead(TrainingSet_add,'*.jpg'); 13 | 14 | 15 | Train_Set = []; 16 | Test_Set = []; 17 | imageArray = cell(No_Testset, 1); 18 | From = 1; 19 | upto = 0; 20 | for i=1:length(TrainingSet_location) 21 | 22 | % Training set process 23 | x=i 24 | No_of_Files=length(dir(fullfile(TrainingSet_location{i},'*.jpg'))) 25 | k = dir(fullfile(TrainingSet_location{i},'*.jpg')); 26 | k = {k(~[k.isdir]).name}; 27 | No = No_Trainset+No_Testset; 28 | if (No > No_of_Files) 29 | No = No_of_Files; 30 | end 31 | for j=1:No 32 | tempImage = imread(horzcat(TrainingSet_location{i},filesep,k{j})); 33 | imgInfo = imfinfo(horzcat(TrainingSet_location{i},filesep,k{j})); 34 | 35 | % Image transformation 36 | if strcmp(imgInfo.ColorType,'grayscale') 37 | imageArray{j} = double(imresize(tempImage,[width height])); % array of images 38 | else 39 | imageArray{j} = double(imresize(rgb2gray(tempImage),[width height])); % array of images 40 | end 41 | end 42 | Train_Set = [Train_Set; imageArray]; 43 | length(Train_Set); 44 | imageArray = cell(No_Trainset, 1); 45 | 46 | % SVM Train class Label 47 | upto = upto+j-No_Testset; 48 | train_label(From:upto,1) = i; 49 | From = upto+1; 50 | 51 | % Test set process 52 | Test_Set_temp = {}; 53 | %No_of_Files=length(dir(fullfile(TrainingSet_location{i},'*.jpg'))); 54 | 55 | if (No_Testset < No_of_Files) 56 | p=randperm(length(Train_Set),No_Testset); 57 | else 58 | p=randperm(No_of_Files-No_Trainset) 59 | end 60 | for k=1:length(p) 61 | z=p(k); 62 | Test_Set_temp{z} = Train_Set{z}; 63 | Train_Set{z} = {}; 64 | end 65 | Train_Set = Train_Set(~cellfun(@isempty, Train_Set)); 66 | Test_Set_temp = Test_Set_temp(~cellfun(@isempty, Test_Set_temp)); 67 | Test_Set = [Test_Set, Test_Set_temp]; 68 | length(Test_Set); 69 | T(i)=length(Train_Set) 70 | end 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /version 3,0/version 1.0/gaborBeuthMod.m: -------------------------------------------------------------------------------- 1 | %Gabor filtering 2 | % 3 | % authors: beuth 4 | % modified: temi 5 | % 6 | % theta => orientation 7 | % psi => phase shift 8 | 9 | function RF = gaborBeuthMod(kernelSize, halfSDsq, theta, f, psi, gamma) 10 | 11 | %some terms for the gabor equation 12 | twopiF = 2 * pi *f; 13 | 14 | %calculate grids (calculation points) for RF 15 | a = floor(kernelSize/2); %half support upper boundary 16 | [X,Y] = meshgrid(-a:1:a, -a:1:a); 17 | XT1 = (X) .* cos(theta) + Y.*sin(theta); 18 | YT1 = Y.*cos(theta) - (X) .* sin(theta); 19 | 20 | % Receptive Field (RF) 21 | RF = exp( - halfSDsq*(XT1.*XT1+gamma^2*YT1.*YT1)) .* cos(twopiF*XT1 + psi); 22 | 23 | end --------------------------------------------------------------------------------