Created
September 8, 2015 09:56
-
-
Save DaHoC/0878b1e768056a10a685 to your computer and use it in GitHub Desktop.
Simple demo program showing how to use boosting in openCV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Simple demo program showing how to use boosted training in openCV | |
* File: opencv_2class_boosting_example.cpp | |
* Author: Jan Hendriks, [email protected], http://www.janhendriks.de | |
* | |
* Created on 18. August 2011, 13:55 | |
*/ | |
#include <cstdlib> | |
#include "opencv/cv.h" | |
#include "opencv/ml.h" | |
using namespace std; | |
using namespace cv; | |
int main(int argc, char** argv) { | |
const int ntestsamples = 11; // ntestsamples > 10, otherwise boosting does not work? | |
CvMat* featureVectorSamples = cvCreateMat(ntestsamples, 2, CV_32F); | |
{ | |
CvMat mat; | |
cvGetRows(featureVectorSamples, &mat, 0, 1); | |
cvSet(&mat, cvRealScalar(1)); | |
} | |
{ | |
CvMat mat; | |
cvGetRows(featureVectorSamples, &mat, 1, ntestsamples); | |
cvSet(&mat, cvRealScalar(0)); | |
} | |
int var_count = featureVectorSamples->cols; // number of single features=variables | |
int nsamples_all = featureVectorSamples->rows; // number of samples=feature vectors | |
CvMat* classLabelResponses = cvCreateMat(nsamples_all, 1, CV_32S); | |
{ | |
CvMat mat; | |
cvGetRows(classLabelResponses, &mat, 0, 1); | |
cvSet(&mat, cvRealScalar(1)); | |
} | |
{ | |
CvMat mat; | |
cvGetRows(classLabelResponses, &mat, 1, nsamples_all); | |
cvSet(&mat, cvRealScalar(-1)); | |
} | |
printf("data dim: %d rows, %d columns - responses dim: %d rows, %d columns\n", featureVectorSamples->rows, featureVectorSamples->cols, classLabelResponses->rows, classLabelResponses->cols); | |
CvMat* var_type = cvCreateMat(var_count + 1, 1, CV_8U); | |
var_type->data.ptr[var_count] = CV_VAR_CATEGORICAL; // = cvSetReal1D( var_type, var_count, CV_VAR_CATEGORICAL ); | |
printf("var_type dim: %d rows, %d columns\n", var_type->rows, var_type->cols); | |
/* | |
// create sample_idx | |
CvMat* sample_idx = cvCreateMat(nsamples_all, 1, CV_32SC1); // CV_8UC1 ); // CV_32SC1 | |
{ | |
CvMat mat; | |
cvGetRows(sample_idx, &mat, 0, nsamples_all); | |
cvSet(&mat, cvRealScalar(1)); | |
} | |
CvMat* var_idx = cvCreateMat(1, var_count, CV_8UC1); // CV_32SC1); // CV_8UC1); | |
{ | |
CvMat mat; | |
cvGetCols(var_idx, &mat, 0, var_count); | |
cvSet(&mat, cvRealScalar(1)); | |
} | |
for (int i = 0; i < sample_idx->rows; ++i) { | |
sample_idx->data.ptr[i] = 1; // consider every sample | |
} | |
*/ | |
CvBoost boost; // (featureVectorSamples, CV_ROW_SAMPLE, classLabelResponses, var_idx, sample_idx, var_type, NULL, CvBoostParams(CvBoost::DISCRETE, 100, 0.95, 5, false, 0)); // *this->parameters);; | |
printf("Training ... "); | |
boost.train(featureVectorSamples, CV_ROW_SAMPLE, classLabelResponses, 0, 0, var_type, 0, CvBoostParams(CvBoost::REAL, 100, 0.95, 5, false, 0)); | |
// boost.train(featureVectorSamples, CV_ROW_SAMPLE, classLabelResponses, var_idx, sample_idx, var_type, NULL, CvBoostParams(CvBoost::REAL, 100, 0.95, 5, false, 0)); // *this->parameters); | |
printf("done!\n"); | |
// boost.save("./boosttest.xml", "boost"); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment