cv::Mat trainData;
cv::Mat ClassData;
// ... push_back trainData and ClassData, list features as row sample; set type 0-9 for example as a class value
// train
cv::Ptr<cv::ml::RTrees> rtrees = cv::ml::RTrees::create();
rtrees->setMaxDepth(4);
rtrees->setMinSampleCount(2);
rtrees->setRegressionAccuracy(0.f);
rtrees->setUseSurrogates(false);
rtrees->setMaxCategories(16);
rtrees->setPriors(cv::Mat());
rtrees->setCalculateVarImportance(false);
rtrees->setActiveVarCount(1);
rtrees->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER, 5, 0));
cv::Ptr<cv::ml::TrainData> tData = cv::ml::TrainData::create(trainData, cv::ml::ROW_SAMPLE, ClassData);
rtrees->train(tData);
rtrees->save("trainingFile.xml");
// test
cv::Ptr<cv::ml::RTrees> prtrees2 = cv::ml::RTrees::load<cv::ml::RTrees>("trainingFile.xml");
cv::Mat testMat;
// ... row sample
float response = prtrees2->predict(testMat);
// ... compare the testMat with Class, to know the correct rate