Mario
Mario

Reputation: 1469

labeling data in SVM opencv c++

I'm trying to implement SVM in opencv for features that I have extracted features by using SIFT. I have extracted features for 2 different objects (each object has features of 10 different images which in total I got more than 3000 features for one object) and I put those features in one file (yaml file)..

My problem is: I don't know how to label them? so I need to label these two files (as I said each file is the type of yaml and it contains matrix 3260*128 and the second yaml file for the second object is 3349*128)...

So please help me to show me how to label these files in order to train them later on... I'm using openCV c++.. by the way, the openCV code for SVM is based on LIBSVM

Thank you in advanced

Upvotes: 1

Views: 4290

Answers (2)

cxyzs7
cxyzs7

Reputation: 1227

Assume you get your matrix correctly, and each row represents one sample, what you can do is similar to what lakesh suggested:

Cv::Mat anger, disgust;
// Load the data into anger and disgust
...
// Make sure anger.cols == disgust.cols 
// Combine your features from different classes into one big matrix
int numPostives = anger.rows, numNegatives = disgust.rows;
int numSamples = numPostives+numNegatives;
int featureSize = anger.cols;
cv::Mat data(numSamples, featureSize, CV_32FC1) // Assume your anger matrix is in float type
cv::Mat positiveData = data.rowRange(0, numPostives);
cv::Mat negativeData = data.rowRange(numPostives, numSamples);
anger.copyTo(positiveData);
disgust.copyTo(negativeData);
// Create label matrix according to the big feature matrix
cv::Mat labels(numSamples, 1, CV_32SC1);
labels.rowRange(0, numPositives).setTo(cv::Scalar_<int>(1));
labels.rowRange(numPositives, numSamples).setTo(cv::Scalar_<int>(-1));
// Finally, train your model
cv::SVM model;
model.train(data, labels, cv::Mat(), cv::Mat(), cv::SVMParams());

Hope this helps.

Upvotes: 4

lakshmen
lakshmen

Reputation: 29074

Labeling is easy. Just label one of the classes/objects as 1 and the other as -1.

                  case 'Anger'
                     CVTrainLabel = [CVTrainLabel;1];
                     Hist = UniformLBP2(I1);
                     CVTrainVec = [CVTrainVec;Hist];
                     continue;
                 case 'Disgust'
                    CVTrainLabel = [CVTrainLabel;-1];
                     Hist = UniformLBP2(I1);
                     CVTrainVec = [CVTrainVec;Hist];

Upvotes: 0

Related Questions