points_classifier.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #include "opencv2/core.hpp"
  2. #include "opencv2/imgproc.hpp"
  3. #include "opencv2/ml.hpp"
  4. #include "opencv2/highgui.hpp"
  5. #include <stdio.h>
  6. using namespace std;
  7. using namespace cv;
  8. using namespace cv::ml;
  9. const Scalar WHITE_COLOR = Scalar(255,255,255);
  10. const string winName = "points";
  11. const int testStep = 5;
  12. Mat img, imgDst;
  13. RNG rng;
  14. vector<Point> trainedPoints;
  15. vector<int> trainedPointsMarkers;
  16. const int MAX_CLASSES = 2;
  17. vector<Vec3b> classColors(MAX_CLASSES);
  18. int currentClass = 0;
  19. vector<int> classCounters(MAX_CLASSES);
  20. #define _NBC_ 1 // normal Bayessian classifier
  21. #define _KNN_ 1 // k nearest neighbors classifier
  22. #define _SVM_ 1 // support vectors machine
  23. #define _DT_ 1 // decision tree
  24. #define _BT_ 1 // ADA Boost
  25. #define _GBT_ 0 // gradient boosted trees
  26. #define _RF_ 1 // random forest
  27. #define _ANN_ 1 // artificial neural networks
  28. #define _EM_ 1 // expectation-maximization
  29. static void on_mouse( int event, int x, int y, int /*flags*/, void* )
  30. {
  31. if( img.empty() )
  32. return;
  33. int updateFlag = 0;
  34. if( event == EVENT_LBUTTONUP )
  35. {
  36. trainedPoints.push_back( Point(x,y) );
  37. trainedPointsMarkers.push_back( currentClass );
  38. classCounters[currentClass]++;
  39. updateFlag = true;
  40. }
  41. //draw
  42. if( updateFlag )
  43. {
  44. img = Scalar::all(0);
  45. // draw points
  46. for( size_t i = 0; i < trainedPoints.size(); i++ )
  47. {
  48. Vec3b c = classColors[trainedPointsMarkers[i]];
  49. circle( img, trainedPoints[i], 5, Scalar(c), -1 );
  50. }
  51. imshow( winName, img );
  52. }
  53. }
  54. static Mat prepare_train_samples(const vector<Point>& pts)
  55. {
  56. Mat samples;
  57. Mat(pts).reshape(1, (int)pts.size()).convertTo(samples, CV_32F);
  58. return samples;
  59. }
  60. static Ptr<TrainData> prepare_train_data()
  61. {
  62. Mat samples = prepare_train_samples(trainedPoints);
  63. return TrainData::create(samples, ROW_SAMPLE, Mat(trainedPointsMarkers));
  64. }
  65. static void predict_and_paint(const Ptr<StatModel>& model, Mat& dst)
  66. {
  67. Mat testSample( 1, 2, CV_32FC1 );
  68. for( int y = 0; y < img.rows; y += testStep )
  69. {
  70. for( int x = 0; x < img.cols; x += testStep )
  71. {
  72. testSample.at<float>(0) = (float)x;
  73. testSample.at<float>(1) = (float)y;
  74. int response = (int)model->predict( testSample );
  75. dst.at<Vec3b>(y, x) = classColors[response];
  76. }
  77. }
  78. }
  79. #if _NBC_
  80. static void find_decision_boundary_NBC()
  81. {
  82. // learn classifier
  83. Ptr<NormalBayesClassifier> normalBayesClassifier = StatModel::train<NormalBayesClassifier>(prepare_train_data());
  84. predict_and_paint(normalBayesClassifier, imgDst);
  85. }
  86. #endif
  87. #if _KNN_
  88. static void find_decision_boundary_KNN( int K )
  89. {
  90. Ptr<KNearest> knn = KNearest::create();
  91. knn->setDefaultK(K);
  92. knn->setIsClassifier(true);
  93. knn->train(prepare_train_data());
  94. predict_and_paint(knn, imgDst);
  95. }
  96. #endif
  97. #if _SVM_
  98. static void find_decision_boundary_SVM( double C )
  99. {
  100. Ptr<SVM> svm = SVM::create();
  101. svm->setType(SVM::C_SVC);
  102. svm->setKernel(SVM::POLY); //SVM::LINEAR;
  103. svm->setDegree(0.5);
  104. svm->setGamma(1);
  105. svm->setCoef0(1);
  106. svm->setNu(0.5);
  107. svm->setP(0);
  108. svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS, 1000, 0.01));
  109. svm->setC(C);
  110. svm->train(prepare_train_data());
  111. predict_and_paint(svm, imgDst);
  112. Mat sv = svm->getSupportVectors();
  113. for( int i = 0; i < sv.rows; i++ )
  114. {
  115. const float* supportVector = sv.ptr<float>(i);
  116. circle( imgDst, Point(saturate_cast<int>(supportVector[0]),saturate_cast<int>(supportVector[1])), 5, Scalar(255,255,255), -1 );
  117. }
  118. }
  119. #endif
  120. #if _DT_
  121. static void find_decision_boundary_DT()
  122. {
  123. Ptr<DTrees> dtree = DTrees::create();
  124. dtree->setMaxDepth(8);
  125. dtree->setMinSampleCount(2);
  126. dtree->setUseSurrogates(false);
  127. dtree->setCVFolds(0); // the number of cross-validation folds
  128. dtree->setUse1SERule(false);
  129. dtree->setTruncatePrunedTree(false);
  130. dtree->train(prepare_train_data());
  131. predict_and_paint(dtree, imgDst);
  132. }
  133. #endif
  134. #if _BT_
  135. static void find_decision_boundary_BT()
  136. {
  137. Ptr<Boost> boost = Boost::create();
  138. boost->setBoostType(Boost::DISCRETE);
  139. boost->setWeakCount(100);
  140. boost->setWeightTrimRate(0.95);
  141. boost->setMaxDepth(2);
  142. boost->setUseSurrogates(false);
  143. boost->setPriors(Mat());
  144. boost->train(prepare_train_data());
  145. predict_and_paint(boost, imgDst);
  146. }
  147. #endif
  148. #if _GBT_
  149. static void find_decision_boundary_GBT()
  150. {
  151. GBTrees::Params params( GBTrees::DEVIANCE_LOSS, // loss_function_type
  152. 100, // weak_count
  153. 0.1f, // shrinkage
  154. 1.0f, // subsample_portion
  155. 2, // max_depth
  156. false // use_surrogates )
  157. );
  158. Ptr<GBTrees> gbtrees = StatModel::train<GBTrees>(prepare_train_data(), params);
  159. predict_and_paint(gbtrees, imgDst);
  160. }
  161. #endif
  162. #if _RF_
  163. static void find_decision_boundary_RF()
  164. {
  165. Ptr<RTrees> rtrees = RTrees::create();
  166. rtrees->setMaxDepth(4);
  167. rtrees->setMinSampleCount(2);
  168. rtrees->setRegressionAccuracy(0.f);
  169. rtrees->setUseSurrogates(false);
  170. rtrees->setMaxCategories(16);
  171. rtrees->setPriors(Mat());
  172. rtrees->setCalculateVarImportance(false);
  173. rtrees->setActiveVarCount(1);
  174. rtrees->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 5, 0));
  175. rtrees->train(prepare_train_data());
  176. predict_and_paint(rtrees, imgDst);
  177. }
  178. #endif
  179. #if _ANN_
  180. static void find_decision_boundary_ANN( const Mat& layer_sizes )
  181. {
  182. Mat trainClasses = Mat::zeros( (int)trainedPoints.size(), (int)classColors.size(), CV_32FC1 );
  183. for( int i = 0; i < trainClasses.rows; i++ )
  184. {
  185. trainClasses.at<float>(i, trainedPointsMarkers[i]) = 1.f;
  186. }
  187. Mat samples = prepare_train_samples(trainedPoints);
  188. Ptr<TrainData> tdata = TrainData::create(samples, ROW_SAMPLE, trainClasses);
  189. Ptr<ANN_MLP> ann = ANN_MLP::create();
  190. ann->setLayerSizes(layer_sizes);
  191. ann->setActivationFunction(ANN_MLP::SIGMOID_SYM, 1, 1);
  192. ann->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS, 300, FLT_EPSILON));
  193. ann->setTrainMethod(ANN_MLP::BACKPROP, 0.001);
  194. ann->train(tdata);
  195. predict_and_paint(ann, imgDst);
  196. }
  197. #endif
  198. #if _EM_
  199. static void find_decision_boundary_EM()
  200. {
  201. img.copyTo( imgDst );
  202. Mat samples = prepare_train_samples(trainedPoints);
  203. int i, j, nmodels = (int)classColors.size();
  204. vector<Ptr<EM> > em_models(nmodels);
  205. Mat modelSamples;
  206. for( i = 0; i < nmodels; i++ )
  207. {
  208. const int componentCount = 3;
  209. modelSamples.release();
  210. for( j = 0; j < samples.rows; j++ )
  211. {
  212. if( trainedPointsMarkers[j] == i )
  213. modelSamples.push_back(samples.row(j));
  214. }
  215. // learn models
  216. if( !modelSamples.empty() )
  217. {
  218. Ptr<EM> em = EM::create();
  219. em->setClustersNumber(componentCount);
  220. em->setCovarianceMatrixType(EM::COV_MAT_DIAGONAL);
  221. em->trainEM(modelSamples, noArray(), noArray(), noArray());
  222. em_models[i] = em;
  223. }
  224. }
  225. // classify coordinate plane points using the bayes classifier, i.e.
  226. // y(x) = arg max_i=1_modelsCount likelihoods_i(x)
  227. Mat testSample(1, 2, CV_32FC1 );
  228. Mat logLikelihoods(1, nmodels, CV_64FC1, Scalar(-DBL_MAX));
  229. for( int y = 0; y < img.rows; y += testStep )
  230. {
  231. for( int x = 0; x < img.cols; x += testStep )
  232. {
  233. testSample.at<float>(0) = (float)x;
  234. testSample.at<float>(1) = (float)y;
  235. for( i = 0; i < nmodels; i++ )
  236. {
  237. if( !em_models[i].empty() )
  238. logLikelihoods.at<double>(i) = em_models[i]->predict2(testSample, noArray())[0];
  239. }
  240. Point maxLoc;
  241. minMaxLoc(logLikelihoods, 0, 0, 0, &maxLoc);
  242. imgDst.at<Vec3b>(y, x) = classColors[maxLoc.x];
  243. }
  244. }
  245. }
  246. #endif
  247. int main()
  248. {
  249. cout << "Use:" << endl
  250. << " key '0' .. '1' - switch to class #n" << endl
  251. << " left mouse button - to add new point;" << endl
  252. << " key 'r' - to run the ML model;" << endl
  253. << " key 'i' - to init (clear) the data." << endl << endl;
  254. cv::namedWindow( "points", 1 );
  255. img.create( 480, 640, CV_8UC3 );
  256. imgDst.create( 480, 640, CV_8UC3 );
  257. imshow( "points", img );
  258. setMouseCallback( "points", on_mouse );
  259. classColors[0] = Vec3b(0, 255, 0);
  260. classColors[1] = Vec3b(0, 0, 255);
  261. for(;;)
  262. {
  263. char key = (char)waitKey();
  264. if( key == 27 ) break;
  265. if( key == 'i' ) // init
  266. {
  267. img = Scalar::all(0);
  268. trainedPoints.clear();
  269. trainedPointsMarkers.clear();
  270. classCounters.assign(MAX_CLASSES, 0);
  271. imshow( winName, img );
  272. }
  273. if( key == '0' || key == '1' )
  274. {
  275. currentClass = key - '0';
  276. }
  277. if( key == 'r' ) // run
  278. {
  279. double minVal = 0;
  280. minMaxLoc(classCounters, &minVal, 0, 0, 0);
  281. if( minVal == 0 )
  282. {
  283. printf("each class should have at least 1 point\n");
  284. continue;
  285. }
  286. img.copyTo( imgDst );
  287. #if _NBC_
  288. find_decision_boundary_NBC();
  289. imshow( "NormalBayesClassifier", imgDst );
  290. #endif
  291. #if _KNN_
  292. find_decision_boundary_KNN( 3 );
  293. imshow( "kNN", imgDst );
  294. find_decision_boundary_KNN( 15 );
  295. imshow( "kNN2", imgDst );
  296. #endif
  297. #if _SVM_
  298. //(1)-(2)separable and not sets
  299. find_decision_boundary_SVM( 1 );
  300. imshow( "classificationSVM1", imgDst );
  301. find_decision_boundary_SVM( 10 );
  302. imshow( "classificationSVM2", imgDst );
  303. #endif
  304. #if _DT_
  305. find_decision_boundary_DT();
  306. imshow( "DT", imgDst );
  307. #endif
  308. #if _BT_
  309. find_decision_boundary_BT();
  310. imshow( "BT", imgDst);
  311. #endif
  312. #if _GBT_
  313. find_decision_boundary_GBT();
  314. imshow( "GBT", imgDst);
  315. #endif
  316. #if _RF_
  317. find_decision_boundary_RF();
  318. imshow( "RF", imgDst);
  319. #endif
  320. #if _ANN_
  321. Mat layer_sizes1( 1, 3, CV_32SC1 );
  322. layer_sizes1.at<int>(0) = 2;
  323. layer_sizes1.at<int>(1) = 5;
  324. layer_sizes1.at<int>(2) = (int)classColors.size();
  325. find_decision_boundary_ANN( layer_sizes1 );
  326. imshow( "ANN", imgDst );
  327. #endif
  328. #if _EM_
  329. find_decision_boundary_EM();
  330. imshow( "EM", imgDst );
  331. #endif
  332. }
  333. }
  334. return 0;
  335. }