Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

to train an SVM with opencv3, you'd go like this:

// preprocess data:
vector<Mat> fromBOW = ...;
Mat data, labels; // initially empty
for (size_t i=0; i<fromBOW.size(); i++) {
      Mat m;
      fromBOW.convertTo(m, CV_32F); // we need float features
      data.push_back(m.reshape(1,1)); // add as a single, flat row vec.

      int label = 17; // ??? how do you get your labels ????
      labels.push_back(label);
}

then we can train the SVM:

Ptr<ml::SVM> svm = ml::SVM::create();
svm->setKernel(ml::SVM::LINEAR);
bool itWorked = svm->train(data, ml::ROW_DATA, labels);

later, we can predict on bow features:

Mat bowfeature = ...
Mat m;
bowfeature .convertTo(m, CV_32F); // we need float features
int predicted = svm->predict(m.reshape(1,1)); // single, flat row vec.

to train an SVM with opencv3, you'd go like this:

// preprocess data:
vector<Mat> fromBOW = ...;
Mat data, labels; // initially empty
for (size_t i=0; i<fromBOW.size(); i++) {
      Mat m;
      fromBOW.convertTo(m, CV_32F); // we need float features
      data.push_back(m.reshape(1,1)); // add as a single, flat row vec.

      int label = 17; // ??? how do you get your labels ????
      labels.push_back(label);
}

then we can train the SVM:

Ptr<ml::SVM> svm = ml::SVM::create();
svm->setKernel(ml::SVM::LINEAR);
bool itWorked = svm->train(data, ml::ROW_DATA, ml::ROW_SAMPLE, labels);

later, we can predict on bow features:

Mat bowfeature = ...
Mat m;
bowfeature .convertTo(m, CV_32F); // we need float features
int predicted = svm->predict(m.reshape(1,1)); // single, flat row vec.