The RTrees API seems to have changed across different versions. The RTrees 2.4.1 documentation says that it supports both regression and classification, though I don't see how it's possible to do so.
I want to use RTrees as a binary classifier in OpenCV 3.1, though the documentation doesn't say anything about it and RTrees::isClassifier() returns false.
m_pTrees->setMaxDepth(20);
m_pTrees->setMinSampleCount(10);
cv::TermCriteria criteria(cv::TermCriteria::EPS, 0, 0);
m_pTrees->setTermCriteria(criteria);
m_pTrees->setCalculateVarImportance(false);
m_pTrees->setRegressionAccuracy(0);
// I assumed setting categories makes it a classifier.
m_pTrees->setMaxCategories(2);
// Always returns a float (that looks like the average of votes).
// I expected a single 0 or 1 (since max categories is 2).
m_pTrees->predict(sample);
EDIT: I've done some more legwork and looked into the OpenCV source code. RTrees
creates a hidden implementation of DTReesImplForRTrees
object which extends the DTreesImpl
class. This class manages the _isClassifier
member variable and sets it according to the response type of the TrainData given to train()
.
From tree.cpp in OpenCV source code
_isClassifier = data->getResponseType() == VAR_CATEGORICAL;
At the moment, I don't see any method of configuring the TrainData object to return this. Perhaps it's because my training classes are stored as floats instead of integers? If I remember correctly, the data type was required to be CV_32F, but maybe I made an error somewhere.