Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Random forests CvRTrees

Hi all,

I am experimenting with the random forests class of openCV (CvRTrees) and I am encountering some troubles. In my example code, I simply create training data with two variables and twenty samples, 10 belonging to the first class and 10 belonging to the second class - so the classification task should be very easy.

The training works but when calling the get_train_error() function, the program crashes each time in CvDTreeTrainData::get_ord_var_data.

Did I discover a bug or did I - as a newbie - used the random forests in a wrong way? Did you encounter similar problems?

Thanks a lot in advance, Stefan

int numSamples = 10;
int numFeatures = 2;

cv::Mat trainingData(numSamples*2,numFeatures, CV_32FC1);
cv::Mat trainingClassification(numSamples*2,1, CV_32FC1);


for (int i=0; i<numSamples*2; i+=2)
{
    trainingData.at<float>(i,0) = 1.0f;
    trainingData.at<float>(i,1) = 0.0f;
    trainingClassification.at<float>(i,0) = 1.0f;

    trainingData.at<float>(i+1,0) = 0.0f;
    trainingData.at<float>(i+1,1) = 1.0f;
    trainingClassification.at<float>(i+1,0) = 0.0f;
}

cv::Mat var_type = cv::Mat(numFeatures+1 , 1, CV_8U );
var_type.setTo(cv::Scalar(CV_VAR_NUMERICAL) );
var_type.at<uchar>(numFeatures, 0) = CV_VAR_CATEGORICAL;


CvRTParams params;
CvRTrees rtree;
rtree.train(trainingData, CV_ROW_SAMPLE, trainingClassification,
            cv::Mat(), cv::Mat(), var_type, cv::Mat(), params);

std::cout << rtree.get_train_error() << std::endl;