const int testStep = 5;
-Mat img, img_dst;
+Mat img, imgDst;
RNG rng;
vector<Point> trainedPoints;
vector<int> trainedPointsMarkers;
vector<Scalar> classColors;
-#define KNN 0
-#define SVM 0
-#define DT 1
-#define RF 0
-#define ANN 0
-#define GMM 0
+#define NBC 0 // normal Bayessian classifier
+#define KNN 0 // k nearest neighbors classifier
+#define SVM 0 // support vectors machine
+#define DT 1 // decision tree
+#define BT 0 // ADA Boost
+#define GBT 1 // gradient boosted trees
+#define RF 0 // random forest
+#define ERT 0 // extremely randomized trees
+#define ANN 0 // artificial neural networks
+#define EM 0 // expectation-maximization
void on_mouse( int event, int x, int y, int /*flags*/, void* )
{
}
else if( event == CV_EVENT_RBUTTONUP )
{
- classColors.push_back( Scalar((uchar)rng(256), (uchar)rng(256), (uchar)rng(256)) );
- updateFlag = true;
+#if BT
+ if( classColors.size() < 2 )
+ {
+#endif
+ classColors.push_back( Scalar((uchar)rng(256), (uchar)rng(256), (uchar)rng(256)) );
+ updateFlag = true;
+#if BT
+ }
+ else
+ cout << "New class can not be added, because CvBoost can only be used for 2-class classification" << endl;
+#endif
+
}
//draw
samples.convertTo( samples, CV_32FC1 );
}
+#if NBC
+void find_decision_boundary_NBC()
+{
+ img.copyTo( imgDst );
+
+ Mat trainSamples, trainClasses;
+ prepare_train_data( trainSamples, trainClasses );
+
+ // learn classifier
+ CvNormalBayesClassifier normalBayesClassifier( trainSamples, trainClasses );
+
+ Mat testSample( 1, 2, CV_32FC1 );
+ for( int y = 0; y < img.rows; y += testStep )
+ {
+ for( int x = 0; x < img.cols; x += testStep )
+ {
+ testSample.at<float>(0) = (float)x;
+ testSample.at<float>(1) = (float)y;
+
+ int response = (int)normalBayesClassifier.predict( testSample );
+ circle( imgDst, Point(x,y), 1, classColors[response] );
+ }
+ }
+}
+#endif
+
+
#if KNN
void find_decision_boundary_KNN( int K )
{
- img.copyTo( img_dst );
+ img.copyTo( imgDst );
Mat trainSamples, trainClasses;
prepare_train_data( trainSamples, trainClasses );
testSample.at<float>(1) = (float)y;
int response = (int)knnClassifier.find_nearest( testSample, K );
- circle( img_dst, Point(x,y), 1, classColors[response] );
+ circle( imgDst, Point(x,y), 1, classColors[response] );
}
}
}
#if SVM
void find_decision_boundary_SVM( CvSVMParams params )
{
- img.copyTo( img_dst );
+ img.copyTo( imgDst );
Mat trainSamples, trainClasses;
prepare_train_data( trainSamples, trainClasses );
testSample.at<float>(1) = (float)y;
int response = (int)svmClassifier.predict( testSample );
- circle( img_dst, Point(x,y), 2, classColors[response], 1 );
+ circle( imgDst, Point(x,y), 2, classColors[response], 1 );
}
}
for( int i = 0; i < svmClassifier.get_support_vector_count(); i++ )
{
const float* supportVector = svmClassifier.get_support_vector(i);
- circle( img_dst, Point(supportVector[0],supportVector[1]), 5, CV_RGB(255,255,255), -1 );
+ circle( imgDst, Point(supportVector[0],supportVector[1]), 5, CV_RGB(255,255,255), -1 );
}
}
#if DT
void find_decision_boundary_DT()
{
- img.copyTo( img_dst );
+ img.copyTo( imgDst );
Mat trainSamples, trainClasses;
prepare_train_data( trainSamples, trainClasses );
testSample.at<float>(1) = (float)y;
int response = (int)dtree.predict( testSample )->value;
- circle( img_dst, Point(x,y), 2, classColors[response], 1 );
+ circle( imgDst, Point(x,y), 2, classColors[response], 1 );
+ }
+ }
+}
+#endif
+
+#if BT
+void find_decision_boundary_BT()
+{
+ img.copyTo( imgDst );
+
+ Mat trainSamples, trainClasses;
+ prepare_train_data( trainSamples, trainClasses );
+
+ // learn classifier
+ CvBoost boost;
+
+ Mat var_types( 1, trainSamples.cols + 1, CV_8UC1, Scalar(CV_VAR_ORDERED) );
+ var_types.at<uchar>( trainSamples.cols ) = CV_VAR_CATEGORICAL;
+
+ CvBoostParams params( CvBoost::DISCRETE, // boost_type
+ 100, // weak_count
+ 0.95, // weight_trim_rate
+ 2, // max_depth
+ false, //use_surrogates
+ 0 // priors
+ );
+
+ boost.train( trainSamples, CV_ROW_SAMPLE, trainClasses, Mat(), Mat(), var_types, Mat(), params );
+
+ Mat testSample(1, 2, CV_32FC1 );
+ for( int y = 0; y < img.rows; y += testStep )
+ {
+ for( int x = 0; x < img.cols; x += testStep )
+ {
+ testSample.at<float>(0) = (float)x;
+ testSample.at<float>(1) = (float)y;
+
+ int response = (int)boost.predict( testSample );
+ circle( imgDst, Point(x,y), 2, classColors[response], 1 );
+ }
+ }
+}
+
+#endif
+
+#if GBT
+void find_decision_boundary_GBT()
+{
+ img.copyTo( imgDst );
+
+ Mat trainSamples, trainClasses;
+ prepare_train_data( trainSamples, trainClasses );
+
+ // learn classifier
+ CvGBTrees gbtrees;
+
+ Mat var_types( 1, trainSamples.cols + 1, CV_8UC1, Scalar(CV_VAR_ORDERED) );
+ var_types.at<uchar>( trainSamples.cols ) = CV_VAR_CATEGORICAL;
+
+ CvGBTreesParams params( CvGBTrees::SQUARED_LOSS, // loss_function_type
+ 100, // weak_count
+ 0.05f, // shrinkage
+ 0.6f, // subsample_portion
+ 2, // max_depth
+ true // use_surrogates )
+ );
+
+ gbtrees.train( trainSamples, CV_ROW_SAMPLE, trainClasses, Mat(), Mat(), var_types, Mat(), params );
+
+ Mat testSample(1, 2, CV_32FC1 );
+ for( int y = 0; y < img.rows; y += testStep )
+ {
+ for( int x = 0; x < img.cols; x += testStep )
+ {
+ testSample.at<float>(0) = (float)x;
+ testSample.at<float>(1) = (float)y;
+
+ int response = (int)gbtrees.predict( testSample );
+ circle( imgDst, Point(x,y), 2, classColors[response], 1 );
}
}
}
+
#endif
#if RF
void find_decision_boundary_RF()
{
- img.copyTo( img_dst );
+ img.copyTo( imgDst );
Mat trainSamples, trainClasses;
prepare_train_data( trainSamples, trainClasses );
testSample.at<float>(1) = (float)y;
int response = (int)rtrees.predict( testSample );
- circle( img_dst, Point(x,y), 2, classColors[response], 1 );
+ circle( imgDst, Point(x,y), 2, classColors[response], 1 );
}
}
}
#endif
+#if ERT
+void find_decision_boundary_ERT()
+{
+ img.copyTo( imgDst );
+
+ Mat trainSamples, trainClasses;
+ prepare_train_data( trainSamples, trainClasses );
+
+ // learn classifier
+ CvERTrees ertrees;
+
+ Mat var_types( 1, trainSamples.cols + 1, CV_8UC1, Scalar(CV_VAR_ORDERED) );
+ var_types.at<uchar>( trainSamples.cols ) = CV_VAR_CATEGORICAL;
+
+ CvRTParams params( 4, // max_depth,
+ 2, // min_sample_count,
+ 0.f, // regression_accuracy,
+ false, // use_surrogates,
+ 16, // max_categories,
+ 0, // priors,
+ false, // calc_var_importance,
+ 1, // nactive_vars,
+ 5, // max_num_of_trees_in_the_forest,
+ 0, // forest_accuracy,
+ CV_TERMCRIT_ITER // termcrit_type
+ );
+
+ ertrees.train( trainSamples, CV_ROW_SAMPLE, trainClasses, Mat(), Mat(), var_types, Mat(), params );
+
+ Mat testSample(1, 2, CV_32FC1 );
+ for( int y = 0; y < img.rows; y += testStep )
+ {
+ for( int x = 0; x < img.cols; x += testStep )
+ {
+ testSample.at<float>(0) = (float)x;
+ testSample.at<float>(1) = (float)y;
+
+ int response = (int)ertrees.predict( testSample );
+ circle( imgDst, Point(x,y), 2, classColors[response], 1 );
+ }
+ }
+}
+#endif
+
#if ANN
void find_decision_boundary_ANN( const Mat& layer_sizes )
{
- img.copyTo( img_dst );
+ img.copyTo( imgDst );
Mat trainSamples, trainClasses;
prepare_train_data( trainSamples, trainClasses );
ann.predict( testSample, outputs );
Point maxLoc;
minMaxLoc( outputs, 0, 0, 0, &maxLoc );
- circle( img_dst, Point(x,y), 2, classColors[maxLoc.x], 1 );
+ circle( imgDst, Point(x,y), 2, classColors[maxLoc.x], 1 );
}
}
}
#endif
-#if GMM
-void find_decision_boundary_GMM()
+#if EM
+void find_decision_boundary_EM()
{
- img.copyTo( img_dst );
+ img.copyTo( imgDst );
Mat trainSamples, trainClasses;
prepare_train_data( trainSamples, trainClasses );
testSample.at<float>(1) = (float)y;
int response = (int)em.predict( testSample );
- circle( img_dst, Point(x,y), 2, classColors[response], 1 );
+ circle( imgDst, Point(x,y), 2, classColors[response], 1 );
}
}
}
int main()
{
+ cout << "Use:" << endl
+ << " right mouse button - to add new class;" << endl
+ << " left mouse button - to add new point;" << endl
+ << " key 'r' - to run the ML model;" << endl
+ << " key 'i' - to init (clear) the data." << endl << endl;
+
cv::namedWindow( "points", 1 );
img.create( 480, 640, CV_8UC3 );
- img_dst.create( 480, 640, CV_8UC3 );
+ imgDst.create( 480, 640, CV_8UC3 );
imshow( "points", img );
cvSetMouseCallback( "points", on_mouse );
if( key == 'r' ) // run
{
+#if NBC
+ find_decision_boundary_NBC();
+ cvNamedWindow( "NormalBayesClassifier", WINDOW_AUTOSIZE );
+ imshow( "NormalBayesClassifier", imgDst );
+#endif
#if KNN
int K = 3;
find_decision_boundary_KNN( K );
namedWindow( "kNN", WINDOW_AUTOSIZE );
- imshow( "kNN", img_dst );
+ imshow( "kNN", imgDst );
K = 15;
find_decision_boundary_KNN( K );
namedWindow( "kNN2", WINDOW_AUTOSIZE );
- imshow( "kNN2", img_dst );
+ imshow( "kNN2", imgDst );
#endif
#if SVM
find_decision_boundary_SVM( params );
namedWindow( "classificationSVM1", WINDOW_AUTOSIZE );
- imshow( "classificationSVM1", img_dst );
+ imshow( "classificationSVM1", imgDst );
params.C = 10;
find_decision_boundary_SVM( params );
cvNamedWindow( "classificationSVM2", WINDOW_AUTOSIZE );
- imshow( "classificationSVM2", img_dst );
+ imshow( "classificationSVM2", imgDst );
#endif
#if DT
find_decision_boundary_DT();
namedWindow( "DT", 1 );
- imshow( "DT", img_dst );
+ imshow( "DT", imgDst );
+#endif
+
+#if BT
+ find_decision_boundary_BT();
+ namedWindow( "BT", 1 );
+ imshow( "BT", imgDst);
+#endif
+
+#if GBT
+ find_decision_boundary_GBT();
+ namedWindow( "GBT", 1 );
+ imshow( "GBT", imgDst);
#endif
#if RF
find_decision_boundary_RF();
namedWindow( "RF", 1 );
- imshow( "RF", img_dst);
+ imshow( "RF", imgDst);
+#endif
+
+#if ERT
+ find_decision_boundary_ERT();
+ namedWindow( "ERT", 1 );
+ imshow( "ERT", imgDst);
#endif
#if ANN
layer_sizes1.at<int>(2) = classColors.size();
find_decision_boundary_ANN( layer_sizes1 );
namedWindow( "ANN", WINDOW_AUTOSIZE );
- imshow( "ANN", img_dst );
+ imshow( "ANN", imgDst );
#endif
-#if GMM
- find_decision_boundary_GMM();
- namedWindow( "GMM", WINDOW_AUTOSIZE );
- imshow( "GMM", img_dst );
+#if EM
+ find_decision_boundary_EM();
+ namedWindow( "EM", WINDOW_AUTOSIZE );
+ imshow( "EM", imgDst );
#endif
}
}