First of all, let me start by saying that I am a student and I am working as a student assistant at Technische Universität Chemnitz presently. The project which was handed over to me was on object recognition & development of a working model. During this project I encountered that very few students actually know how to do image processing and most of all there is no place to find a good tutorial for beginners who do not want to go by theoretical knowledge and would want to get their hands dirty with MATLAB programming. So, my blog is targeted for those students who wants to work in this field and unfortunately are not able to find any relevant information on machine learning algorithms and programming with MATLab anywhere on Internet.
Image can be processed in plenty of ways and one of them which I will present to you is on machine learning algorithms which I will be using on MATLAB. I will keep my language as basic as possible for beginners to understand, no offense to professionals as we all were in a learning phase in our life.
My tutorial will follow a very basic structure as follows:
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.
Just for beginners,
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 Faces and Airplanes, 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 30 images divided in two class and two labels will be provided to them. Code is very basic to be understood. Hope it helps. The program goes as follows:
Prepatory steps:
Training set - Create a folder with 15 "Faces" images and 15 "airplanes" images, this will be our dataset.
Test set - Create another folder with random face and airplanes images, this will be our testset, basically we have to understand here is that if you use training set images as test set images then you will get 100% recognition performance.
--------------------------------------------------------------------------------------------------------
clc
clear all
% Load Datasets
Dataset = 'absolute path of the folder';
Testset = 'absolute path of the folder';
% we need to process the images first.
% Convert your images into grayscale
% Resize the 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 30 images and we divided it into two label groups here.
train_label = zeros(size(30,1),1);
train_label(1:15,1) = 1; % 1 = Airplanes
train_label(16:30,1) = 2; % 2 = Faces
% 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. you can also try to give the same dataset and testset location and you will achieve 100% recognition. This is because the same image is being classified which you are using to train you svm.
In my next tutorial, I will explain you how to use multisvm and re-evaluate the recognition performance of scholarly articles. I will try to explain you how you can repeat the recognition performance of "Robust Object Recognition with Cortex-Like Mechanisms" by Thomas Serre, Lior Wolf, Stanley Bileschi, Maximilian Riesenhuber, and Tomaso Poggio, Member, IEEE
Image can be processed in plenty of ways and one of them which I will present to you is on machine learning algorithms which I will be using on MATLAB. I will keep my language as basic as possible for beginners to understand, no offense to professionals as we all were in a learning phase in our life.
My tutorial will follow a very basic structure as follows:
Obtaining the Image Datasets - ( I will be using Caltech101 dataset )
Separate Training set and Test set images.
Creating Lables for SVM train to distinct class.
Training SVM
Classify Test set images.
Just for beginners,
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 Faces and Airplanes, 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 30 images divided in two class and two labels will be provided to them. Code is very basic to be understood. Hope it helps. The program goes as follows:
Prepatory steps:
Training set - Create a folder with 15 "Faces" images and 15 "airplanes" images, this will be our dataset.
Test set - Create another folder with random face and airplanes images, this will be our testset, basically we have to understand here is that if you use training set images as test set images then you will get 100% recognition performance.
--------------------------------------------------------------------------------------------------------
clc
clear all
% Load Datasets
Dataset = 'absolute path of the folder';
Testset = 'absolute path of the folder';
% we need to process the images first.
% Convert your images into grayscale
% Resize the 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 30 images and we divided it into two label groups here.
train_label = zeros(size(30,1),1);
train_label(1:15,1) = 1; % 1 = Airplanes
train_label(16:30,1) = 2; % 2 = Faces
% 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. you can also try to give the same dataset and testset location and you will achieve 100% recognition. This is because the same image is being classified which you are using to train you svm.
In my next tutorial, I will explain you how to use multisvm and re-evaluate the recognition performance of scholarly articles. I will try to explain you how you can repeat the recognition performance of "Robust Object Recognition with Cortex-Like Mechanisms" by Thomas Serre, Lior Wolf, Stanley Bileschi, Maximilian Riesenhuber, and Tomaso Poggio, Member, IEEE
thanks a lot.. you just solved my problem ! :)
ReplyDeletep.s: we badly need more image processing blogs
and tutorials like this !
how it works? please tell me
DeleteThis may be the oldest surviving educational blog and will celebrate this year. It’s a blog covering resources for the best robotics training in Noida, I think its a very good option to us for robotics .
ReplyDeletewhy do I always get the error message like this?
ReplyDelete"Error using svmclassify (line 53)
The first input should be a struct generated by SVMTRAIN.
Error in impsvm (line 77)
Group = svmclassify(SVMStruct, Test_Set);"
Is that means something wrong in svmclassify?
Thanks a lot......
ReplyDeletei need ur help for coding of image classification using svm.....how to load or train it...plss help me n reply soon
ReplyDeleteplease , i need to learn coding for image classification (svm) , can u help me
Deletehow to load and train image? pl help me
ReplyDeleteplease help me i need image classification algorithm
ReplyDeleteSir where can find your tutorial regarding multi class svm
ReplyDeleteThank u very much Sir.
ReplyDeleteplease , i need to learn coding for image classification (svm) , can u help me
ReplyDeleteHi, I'm currently work for image classification using svm.
ReplyDeleteI was stuck after feature extraction.
i have no idea how to make my dataset for training.
Can someone help me on this ??
Regards
JK
Error in ASD (line 76)
ReplyDeleteSVMStruct = svmtrain(Training_Set , train_label, 'kernel_function', 'linear');
i got error Training and Y contain same no rows means : Training_Set and train_label
me too
Deletei got the same error.please help.
ReplyDeleteSVMStruct = svmtrain(Training_Set , train_label, 'kernel_function', 'linear');
ReplyDeleteY and TRAINING must have the same number of rows.
I am facing an error which says Y and TRAINING do not have same number of rows. How can I solve this error?
DeleteAre you solved this problem ?? , i have the same error :(
Deletedid you already solve the problem..kindly help me..thanks in advance
Deleteyou need to have only 30 images in total with 15 each of the group label
Deleteplease , i need to learn coding for image classification (svm) , can u help me how to write a code in Matlab i am doing leaf identification if u know please help me the coding part
ReplyDeletesrinathaec@gmail.com
srinathakn.ece@citech.edu.in
sir i need your help in road image classification ......
ReplyDeleteumarnasir910@gmail.com
thank you sir. i understood the logic in SVM classification. i tried with few data sets.. it's working...
ReplyDeletewould you please tell me how it works
DeleteDid you used any feature here to classify....if,then can you mention please what feature and which potion u have written that code....Thank you
ReplyDeletewhyyyy ?
ReplyDeleteError using svmtrain (line 253)
Y and TRAINING must have the same number of rows.
Error in Untitled6 (line 76)
SVMStruct = svmtrain(Training_Set , train_label, 'kernel_function', 'linear');
need help plzzzz
DeleteThis comment has been removed by the author.
Deletehi Huda, You have to make trainingSet folder and testSet folder with same images number. In this example, Author make trainingSet include 30 images (15 faces + 15 airplanes), testSet is similar ( random 15 face and 15 air).
Deletei tested on 2 folders which contained 30 images and it works correctly
Deletehow files will be placed in a folder to rum this code
DeleteError using svmtrain (line 253)
DeleteY and TRAINING must have the same number of rows.
Error in sv (line 73)
SVMStruct = svmtrain(Training_Set , train_label, 'kernel_function', 'linear');
i have also got the same error pls help me
now svm completed run.. there is no output image.. only retrieve values only.. correct?
DeleteHow you rectified this error?
Deleteplease help me...i get the same error
Deletealready place 30 picture into folders but still get the same error..can u explain more details for me...
i would like to work with a dataset which contains different type of images (.jpg, .png ...) so i simply change '*.jpg' by '*.*' like this
ReplyDeletek = dir(fullfile(Dataset,'*.*'));
k = {k(~[k.isdir]).name};
for j=1:length(k)
tempImage = imread(horzcat(Dataset,filesep,k{j}));
imgInfo = imfinfo(horzcat(Dataset,filesep,k{j}));
but i got this error
=>>>Error using imread (line 362)
Unable to determine the file format.
Error in SVM2 (line 21)
tempImage = imread(horzcat(Dataset,filesep,k{j}));
can you help me!
Sir please anyone help me.
ReplyDeleteIf no matching is found in the image i mean if no airplane or face then result should be zero how to achieve this
in this program, there is no error.. but it return only values.. is it correct or it display any image?
ReplyDeleteHello, i want to use SVM algorithm to classify sounds (I have multiple classes).
ReplyDeleteHow can i procced ?
SVMStruct = svmtrain(Training_Set , train_label, 'kernel_function', 'linear');
ReplyDeleteY and TRAINING must have the same number of rows........
are you solved this problem??
plezz help
Deletejust make sure in folder u make contains 30 image each other
Deletei still get the same error,,,please help me
DeleteHi I am new to Matlab. I have to do project using caltech 101 for image classification. I copied the matlab code above and have run it but got errors. Can you plz guide me. Your efforts are highly appreciated.
ReplyDeletevery helpful code
ReplyDeletewell I have a doubt regarding SVM.
ReplyDeleteI have 2 class of images.All images are of size 10*10 (assume). and I have 100 images.
I am giving 60 images as input. so input contains (60*10*10) input neurons,trained the machine. now I am having 40 images to classify. but my SVM contains 600 input neurons with trained weights between input and hidden layer. BUT i have only 400 inputs for testing. what will be the input for others 200 input neurons. please answer me.
After the execution of the code I do not find any answer. Pls help me
ReplyDeleteLook into the variable Group in work space
DeleteI face the same problem and checked the workspace.I do not understand that.would you tell me how output look like?
DeleteThanks alot
ReplyDeleteCode works fine thanks
ReplyDeleteMatlab is the best technology at Learning New things. Appreciable blog on Matlab Course
ReplyDeleteAmigo, talvez tu trabajas con rostros en 3D?, Necesito realizar un proyecto en este tema de reconocimiento facial, en especial para trabajar con Gabor y SVM. Mi correo es jose.cadena@utc.edu.ec. Escribeme porfa
ReplyDeleteUsually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man,Keep it up.
ReplyDeleteMatlab Training in Noida
Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man,Keep it up.
ReplyDeleteMatlab Training in Noida
Thank you so much for sharing your knowledge with us.
ReplyDeleteBest MATLAB Training Institute in Jaipur
Great article and helpful Information and Thanks for sharing this article and also visit we are leading the Best MATLAB Training in Jodhpur, web development & designing, Python, java, android, iPhone, PHP training institute in jodhpur
ReplyDeleteCode Runs without any error! But What is the output look like? Can anyone help?
ReplyDeleteCan I use this code for classifying the Images into Original image and Encrypted image? If not someone please help me out with that.
ReplyDeleteThanks a lot.
Very nice article,
ReplyDeletehttps://www.indiatvnetwork.in/
Semantic Segmentation Matlab allows you to divide images into several parts instead of two parts. Moreover, it every pixel in an image, resulting in an image that is segmented by class.
ReplyDeleteThanks for shearing very nice and exceptional idea about this Image processing . I hope you will share about Clipping Path & Image Editing Service related post .
ReplyDeleteReally a awesome blog for the freshers. Thanks for posting the information.
ReplyDeleteMachine Learning Training in Delhi
sir kindly help me how to do image fusion using SVM
ReplyDeleteSir please help me with fruit grading
ReplyDeleteHello,
ReplyDeleteI am trying to replicate whatever you have on the page but I come up with the following when I run the ::::
SVMStruct = svmtrain(Training_Set , train_label, 'kernel_function', 'linear');
Unrecognized function or variable 'svmtrain'.
I am using matlab 2020 and this has been removed and replaced with fitcsvm().
When I try to use this command I am getting the following errors:::
SVMStruct = fitcsvm(Training_Set , train_label, 'kernel_function', 'linear');
Error using classreg.learning.FitTemplate/fillIfNeeded (line 666)
kernel_function is not a valid parameter name.
Error in classreg.learning.FitTemplate.make (line 125)
temp = fillIfNeeded(temp,type);
Error in ClassificationSVM.template (line 235)
temp = classreg.learning.FitTemplate.make('SVM','type','classification',varargin{:});
Error in ClassificationSVM.fit (line 239)
temp = ClassificationSVM.template(varargin{:});
Error in fitcsvm (line 343)
obj = ClassificationSVM.fit(X,Y,RemainingArgs{:});
Many thanks
Silly me....Apologies, I just realised that in the new function is given as 'KernelFunction'....that's why the error...now it runs fine...no errors...
DeleteThis comment has been removed by the author.
ReplyDeletethanks a lot.. you just solved my problem. If you want to read similar technology article/news then visit us, we are technology/news/smartphone company. Visit us: https://techmie.com/
ReplyDeleteSir please help me with fruit grading.
ReplyDeleteplease share the code.
Great Content. Thanks for sharing this valuable information. It will be useful for knowledge seekers.
ReplyDeleteMatlab Training In Chennai