Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

so, let's try a first iteration of an "answer" here...

for any kind of opencv machine learning, we'll need a single Mat, where each feature is on a flat row

assuming, you got something like this:

struct superpixel {
      float L,a,b;
      float depth;
      float x,y;
};
vector<superpixel> sp;

we need to repack it into a single cv::Mat, with each superpixel on a row (also, process it a bit): Mat spmat(const superpixel &s, const Size &size, const Point &person_center) { Mat_<float> row(1,5); row << s.L/255, sp.a/255, s.b/255, s.depth/255, (s.x-person_center.x)/size.width, (s.y-person_center.y)/size.height; return row; }

Mat trainData, trainLabels;
for (size_t i=0; i<sp.size(); i++) {
        Mat row = spmat(sp[i], image.size(), person_center);
        trainData.push_back(row);
        int classId = ???? (an int in [0..15]) again, it needs manually labelled train data, see comments above !
        trainLabels.push_back(classId);
}

then we can setup the SVM, and train it:

Ptr<ml::SVM> svm = ml::SVM::create();
// optionally change params, like a LINEAR kernel
svm->train(trainData, 0, trainLabels);

then, later, you can classify your test superpixels:

Mat row =  = spmat(sp[i], image.size(), person_center);
int classId = (int)svm->predict(row);

so, let's try a first iteration of an "answer" here...

for any kind of opencv machine learning, we'll need a single Mat, Mat for the train data, where each feature is on a flat row

assuming, you got something like this:

struct superpixel {
      float L,a,b;
      float depth;
      float x,y;
};
vector<superpixel> sp;

we need to repack it into a single cv::Mat, with each superpixel on a row (also, process it a bit): bit):

Mat spmat(const superpixel &s, const Size &size, const Point &person_center) {
         Mat_<float> row(1,5);
         row << s.L/255, sp.a/255, s.b/255, s.depth/255, 
               (s.x-person_center.x)/size.width, 
               (s.y-person_center.y)/size.height;
         return row;
    }

}

Mat trainData, trainLabels;
for (size_t i=0; i<sp.size(); i++) {
        Mat row = spmat(sp[i], image.size(), person_center);
        trainData.push_back(row);
        int classId = ???? (an int in [0..15]) again, it needs manually labelled train data, see comments above !
        trainLabels.push_back(classId);
}

then we can setup the SVM, and train it:

Ptr<ml::SVM> svm = ml::SVM::create();
// optionally change params, like a LINEAR kernel
svm->train(trainData, 0, trainLabels);

then, later, you can classify your test superpixels:

Mat row =  = spmat(sp[i], image.size(), person_center);
int classId = (int)svm->predict(row);

so, let's try a first iteration of an "answer" here...

for any kind of opencv machine learning, we'll need a single Mat for the train data, where each feature is on a flat row

assuming, you got something like this:

struct superpixel {
      float L,a,b;
      float depth;
      float x,y;
};
vector<superpixel> sp;

we need to repack it into a single cv::Mat, with each superpixel on a row (also, process it a bit):

Mat spmat(const superpixel &s, const Size &size, const Point &person_center) {
        Mat_<float> row(1,5);
        row << s.L/255, sp.a/255, s.b/255, s.depth/255, 
              (s.x-person_center.x)/size.width, 
              (s.y-person_center.y)/size.height;
        return row;
}

Mat trainData, trainLabels;
for (size_t i=0; i<sp.size(); i++) {
        Mat row = spmat(sp[i], image.size(), person_center);
        trainData.push_back(row);
        int classId = ???? (an int in [0..15]) 
        // again, it needs manually labelled train data, see comments above !
        trainLabels.push_back(classId);
}

then we can setup the SVM, and train it:

Ptr<ml::SVM> svm = ml::SVM::create();
// optionally change params, like a LINEAR kernel
svm->train(trainData, 0, trainLabels);

then, later, you can classify your test superpixels:

Mat row =  = spmat(sp[i], image.size(), person_center);
int classId = (int)svm->predict(row);