Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

assuming (for simplicity), that your features are the 8 histogram bins from the freeman chain in your last question, you could try like this:

// 1. we need to setup the traindata (a Mat of N rows, and 8 cols, each feature on a row) 
// and trainlabels (a Mat with N rows, and 1 col):
Mat trainData(N,8,CV_32F);
Mat trainLabels(N,1,CV_32S);

for (int r=0; r<N; r++) {
    for (int c=0; c<8; c++) {
        trainData.at<float>(r,c) = one_histogram[c]; // ??? no idea, what you have, exactly.
    }
    trainLabels.at<int>(r,0) = a_label; // ??? a number from 0 to num_classes
}

// 2. now we can setup the SVM, and train it:
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
svm->train(trainData, ROW_SAMPLE, trainLabels);

// 3. now to the prediction:
Mat testData(1,8,CV_32F);
for (int c=0; c<8; c++) {
    testData.at<float>(r,c) = one_histogram[c]; // ??? same as above
}

int predicted_label = (int)svm->predict(testData);

note, that for machine learning, all features need to have exactly the same size ! (so, if you want to do that on raw coords, you need to re-sample those, apply procrustes, or similar)

assuming (for simplicity), simplicity, now), that your features are the 8 histogram bins from the freeman chain in your last question, you could try like this:

// 1. we need to setup the traindata (a Mat of N rows, and 8 cols, each feature on a row) 
// and trainlabels (a Mat with N rows, and 1 col):
Mat trainData(N,8,CV_32F);
Mat trainLabels(N,1,CV_32S);

for (int r=0; r<N; r++) {
    for (int c=0; c<8; c++) {
        trainData.at<float>(r,c) = one_histogram[c]; // ??? no idea, what you have, exactly.
exactly
    }
    trainLabels.at<int>(r,0) = a_label; // ??? a number from 0 to num_classes
}

// 2. now we can setup the SVM, and train it:
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
svm->train(trainData, ROW_SAMPLE, trainLabels);

// 3. now to the prediction:
Mat testData(1,8,CV_32F);
for (int c=0; c<8; c++) {
    testData.at<float>(r,c) = one_histogram[c]; // ??? same as above
}

int predicted_label = (int)svm->predict(testData);

good luck !

note, that for machine learning, all features need to have exactly the same size ! (so, if you want to do that on raw coords, you need to re-sample those, apply procrustes, or similar)

assuming (for simplicity, now), that your features are the 8 histogram bins from the freeman chain in your last question, you could try like this:

// 1. we need to setup the traindata (a Mat of N rows, and 8 cols, each feature on a row) 
// and trainlabels (a Mat with N rows, and 1 col):
Mat trainData(N,8,CV_32F);
Mat trainLabels(N,1,CV_32S);

for (int r=0; r<N; r++) {
    for (int c=0; c<8; c++) {
        trainData.at<float>(r,c) = one_histogram[c]; // ??? no idea, what you have, exactly
    }
    trainLabels.at<int>(r,0) = a_label; // ??? a number from 0 to num_classes
}

// 2. now we can setup the SVM, and train it:
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->train(trainData, ROW_SAMPLE, trainLabels);

// 3. now to the prediction:
prediction (on a single sample):
Mat testData(1,8,CV_32F);
for (int c=0; c<8; c++) {
    testData.at<float>(r,c) testData.at<float>(0,c) = one_histogram[c]; // ??? same as above
}

int predicted_label = (int)svm->predict(testData);

good luck !