// API function usage example of Bayesian classifier in opencv # include "stdafx. H "# include" CV. H "# include" highgui. H "# include" cxcore. H "# include" opencv. HPP "# include" iostream "using namespace CV; using namespace STD; // training sample set with 10 sample feature vector dimensions of 12, double inputarr [10] [13] = {1, 0.708333, 0.320755,-0.105023,-0.419847,-,-1, -0.225806, 0.583333,-1, 0.333333,-1, 0.603774,-0.358779, 1,-0.483871, 0.166667,-1,-, 0,-, 1, -0.333333,-0.433962,-0.383562,-1,-1, 0.0687023,-1,-0.903226,-1,-1,-1, 0.458333, 0.358491, -0.374429,-1,-1,-0.480916, 1,-0.935484, 0,-0.333333,-1, 0.875,-1,-0.333333,-0.509434,-0.347032, -0.236641,-0.935484, 1,-0.333333,-1,-0.5,-1, 0.509434, 0.767123,-0.0534351,-,-1,-1, -1,-0.870968,-1,-0.125, 0.333333, 1, 0.320755,-0.406393,-0.0839695, 0.806452, 0.333333, 1,-, 0,-, 1, 0.25, 0.698113,-0.484018,-0.0839695,-0.612903, 0.333333, 1,-0.291667, 0,-0.132075, 1, 0.237443, 0.51145,-1,-0.612903, 0, 0.333333, 1, 0.416667,-0.0566038, 0.283105, 0.267176,-0.290323,-1 }; // feature vector double testarr [] = {0.25, 0.226415,-0.506849,-0.374046,-1,-1, 0.83871,-1,-,-0, -1}; int _ tmain (INT argc, _ tchar * argv []) {mat traindata (10, 12, cv_32fc1 ); // construct the feature vector for (INT I = 0; I <10; I ++) {for (Int J = 0; j <12; j ++) of the training sample) {traindata. at <float> (I, j) = inputarr [I] [J + 1] ;}} mat trainresponse (10, 1, cv_32fc1 ); // construct the category label of the training sample for (INT I = 0; I <10; I ++) {trainresponse. at <float> (I, 0) = inputarr [I] [0];} cvnormalbayesclassifier NBC; bool trainflag = NBC. train (traindata, trainresponse); // performs Bayesian Classifier Training If (trainflag) {cout <"train over... "<Endl; NBC. save ("D:/normalbayes.txt");} else {cout <"Train error... "<Endl; System (" pause "); exit (-1);} cvnormalbayesclassifier testnbc; testnbc. load ("D:/normalbayes.txt"); MAT testsample (1, 12, cv_32fc1); // build the test sample for (INT I = 0; I <12; I ++) {testsample. at <float> (0, I) = testarr [I];} float flag = testnbc. predict (testsample); // test cout <"flag =" <flag <Endl; System ("pause"); Return 0 ;}