Object Detection using SVM

asked 2015-12-08 22:59:12 -0500

kaushikmit gravatar image

I am new to SVM. I used to do object detection using HAAR Cascading. Now I am trying to implement SVM for object detection. I searched online and got to know about the basics. I wanted to use libsvm while coding for c++. I am getting lots of problems. Can anyone please explain step by step process of using it for object detection. BTW I looked into opencv documentation of svm. But I am not able to do anything further.

Also I got this code for training my SVM and saving it into an xml file. Now I want a code which can take this xml and detect objects in test cases.

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cv.h>
#include <highgui.h>
#include <cvaux.h>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;

int main ( int argc, char** argv )
    cout << "OpenCV Training SVM Automatic Number Plate Recognition\n";
    cout << "\n";

    char* path_Plates;
    char* path_NoPlates;
    int numPlates;
    int numNoPlates;
    int imageWidth=150;
    int imageHeight=150;

    //Check if user specify image to process
        numPlates= 11;
        numNoPlates= 90 ;
        path_Plates= "/home/kaushik/opencv_work/Manas6/Pics/Positive_Images/";
        path_NoPlates= "/home/kaushik/opencv_work/Manas6/Pics/Negative_Images/i";

        cout << "Usage:\n" << argv[0] << " <num Plate Files> <num Non Plate Files> <path to plate folder files> <path to non plate files> \n";
        return 0;

    Mat classes;//(numPlates+numNoPlates, 1, CV_32FC1);
    Mat trainingData;//(numPlates+numNoPlates, imageWidth*imageHeight, CV_32FC1 );

    Mat trainingImages;
    vector<int> trainingLabels;

    for(int i=1; i<= numPlates; i++)

        stringstream ss(stringstream::in | stringstream::out);

            const char* a = ss.str().c_str();
            Mat img = imread(ss.str(), CV_LOAD_IMAGE_UNCHANGED);
            img= img.clone().reshape(1, 1);
        catch(Exception e){;}

    for(int i=0; i< numNoPlates; i++)
        stringstream ss(stringstream::in | stringstream::out);
        ss << path_NoPlates<<i << ".jpg";
            const char* a = ss.str().c_str();
            Mat img=imread(ss.str(), 0);
            img= img.clone().reshape(1, 1);
        catch(Exception e){;}

    //trainingData = trainingData.reshape(1,trainingData.rows);
    trainingData.convertTo(trainingData, CV_32FC1);

    FileStorage fs("SVM.xml", FileStorage::WRITE);
    fs << "TrainingData" << trainingData;
    fs << "classes" << classes;

    return 0;

Any help would be greatly appreciated.

Also I would love to have suggestions on how to implement libsvm for object detection.

edit retag flag offensive close merge delete


  1. Do not use .h headers from OpenCV
  2. SVM is just a classifier, so you need to implement the cascade algorithm: using a sliding window on the input image, try to classify each with your SVM classifier and keep the Rects that are classified as positives. Re-do it on different scales. Then do a groupRectangles on the vector you found. But this is going to be slow. The idea of cascades is presented here.
  3. More, your SVM is based on the pixels values, so I am pretty sure your SVM is not going to be very accurate
thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-12-09 07:01:23 -0500 )edit

@thdrksdfthmn be careful, don't mixed-up cascade classifiers with sliding-window algorithm. They're 2 different things. You'll more than probably use a sliding-window mechanism (dense multi-scale scanning of windows), but not necessarily use a cascade of classifiers in it (in fact, I've hardly seen SVM in a cascade setting).

LorenaGdL gravatar imageLorenaGdL ( 2015-12-09 10:03:43 -0500 )edit

Yes, sorry, I meant detectMultiScale... or the algorithm of detection.

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-12-10 02:41:27 -0500 )edit