Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

to train opencv's ml classes(they're all very similar there), you need a single Mat with the train features (each on a row of its own), and a Mat with one class label for each feature, those 2 Mat's go into a cv::TrainData container later.

TrainData
      features             labels
         image1              1
         image2              0
         image3              2
...

again, all features need to have the same size (needs some cropping/resize / procrustes), for detection you would use (-1,1) labels, for recognition e.g. person-id's

here's some (fairly pseudo) code:

Mat feats, labels; // start empty

for each image {
    Mat f = image.reshape(1,1); // flatten to a single row
    f.convertTo(f, CV_32F);     // ml needs float data
    feats.push_back(f);         // append at bottom

    labels.push_back(person_id); // an integer, this is, what you get back in the prediction
}

Ptr<ml::DTree> model = ml::DTree::create();
model->train(ml::TrainData::create(feats, ml::ROW_SAMPLE, labels));

for the prediction later, you'd treat your data in the same way:

Mat f = image.reshape(1,1); // flatten to a single row
f.convertTo(f, CV_32F);    // ml needs float data
prediction = model->predict(f);

to train opencv's ml classes(they're all very similar there), you need a single Mat with the train features (each on a row of its own), and a Mat with one class label for each feature, those 2 Mat's go into a cv::TrainData container later.

TrainData
      features             labels
         image1              1
         image2              0
         image3              2
...

again, all features need to have the same size (needs some cropping/resize / procrustes), for detection you would use (-1,1) labels, for recognition e.g. person-id's

here's some (fairly pseudo) code:

Mat feats, labels; // start empty

for each image {
    Mat f = image.reshape(1,1); // flatten to a single row
    f.convertTo(f, CV_32F);     // ml needs float data
    feats.push_back(f);         // append at bottom

    labels.push_back(person_id); // an integer, this is, what you get back in the prediction
}

Ptr<ml::DTree> model = ml::DTree::create();
model->train(ml::TrainData::create(feats, ml::ROW_SAMPLE, labels));

for the prediction later, you'd treat your data in the same way:

Mat f = image.reshape(1,1); // flatten to a single row
f.convertTo(f, CV_32F);    // ml needs float data
prediction = model->predict(f);

sidenote: this is just to get you started with the ml setup. you'll soon find, that a dtree might not be the optimal approach (SVM, maybe ?), that using e.g. HOG or LBPH features instead of images should be considered, etc.

to train opencv's ml classes(they're all very similar there), you need a single Mat with the train features (each on a row of its own), and a Mat with one class label for each feature, those 2 Mat's go into a cv::TrainData container later.

TrainData
      features             labels
         image1              1
         image2              0
         image3              2
...

again, all features need to have the same size (needs some cropping/resize / procrustes), for detection a 2-class problem you would use (-1,1) labels, for recognition e.g. person-id's

here's some (fairly pseudo) code:

Mat feats, labels; // start empty

for each image {
    Mat f = image.reshape(1,1); // flatten to a single row
    f.convertTo(f, CV_32F);     // ml needs float data
    feats.push_back(f);         // append at bottom

    labels.push_back(person_id); // an integer, this is, what you get back in the prediction
}

Ptr<ml::DTree> model = ml::DTree::create();
model->train(ml::TrainData::create(feats, ml::ROW_SAMPLE, labels));

for the prediction later, you'd treat your data in the same way:

Mat f = image.reshape(1,1); // flatten to a single row
f.convertTo(f, CV_32F);    // ml needs float data
prediction = model->predict(f);

sidenote: this is just to get you started with the ml setup. you'll soon find, that a dtree might not be the optimal approach (SVM, maybe ?), that using e.g. HOG or LBPH features instead of images should be considered, etc.