Clear Filters
Clear Filters

Classification of images uisng SVM classifier

3 views (last 30 days)
Can anyone help me in the code of image classification using SVM classifier.....
I have done image pre-processing, segmentation and feature extraction.... I even know that i have to form a matrix of these features extracted and input it to the SVM classifier....
However I am not able to understand how to input these features to SVM classifier...and how to use classify images using SVM...can anyone help me in this......

Answers (1)

Sai Pavan
Sai Pavan on 28 May 2024
Hello Preeti,
I understand that you want to classify images using SVM classifier after performing pre-processing, segmentation and feature extraction from the images.
To perform image classification using an SVM classifier, we need to ensure that the features extracted from the images are organized into a matrix where each row represents an image and each column represents a feature. Additionally, we need a vector that contains the labels for each image. Please refer to the below workflow to perform image classification using SVM:
  • Prepare the data in the form of features as a matrix of extracted features, where each row corresponds to an image, and labels as a column vector of labels for each image.
  • Split the data into training and testing sets with "cvpartition" function as it enables us to evaluate the performance of SVM model on unseen data.
  • Train the SVM classifier for multi-class classification using "fitcecoc" function. If it is a binary classification problem, we can use "fitcsvm" function instead.
  • Predict the labels of the testing set using the trained model using the "predict" function.
  • Evaluate the performance of the SVM classifier by comparing the predicted labels with the actual labels with the help of performance metrics like accuracy and confusion matrix.
Please refer to the below code snippet that illustrates the above workflow:
% features - A matrix of size (nImages x nFeatures)
% labels - A column vector of size (nImages x 1)
cv = cvpartition(labels, 'HoldOut', 0.2); % Hold out 20% of the data for testing
trainIdx = cv.training;
testIdx = cv.test;
XTrain = features(trainIdx,:);
YTrain = labels(trainIdx);
XTest = features(testIdx,:);
YTest = labels(testIdx);
svmModel = fitcecoc(XTrain, YTrain); % For multi-class classification
YPred = predict(svmModel, XTest);
accuracy = sum(YPred == YTest) / length(YTest);
fprintf('Accuracy: %.2f%%\n', accuracy * 100);
confMat = confusionmat(YTest, YPred);
confchart(confMat);
Please refer to the below documentation to learn more about:
Hope it helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!