本文整理汇总了C++中CvRTrees::get_tree方法的典型用法代码示例。如果您正苦于以下问题:C++ CvRTrees::get_tree方法的具体用法?C++ CvRTrees::get_tree怎么用?C++ CvRTrees::get_tree使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CvRTrees
的用法示例。
在下文中一共展示了CvRTrees::get_tree方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: mexFunction
/* Examines the values at each leaf node in order to see what the distribution of data
we put in is doing */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
ASSERT_NUM_RHS_ARGS_EQUALS(1);
const mxArray* forest_ptr = prhs[0];
ASSERT_IS_POINTER(forest_ptr);
CvRTrees *forest = (CvRTrees *) unpack_pointer(forest_ptr);
// We are going to return a cell array with one cell per tree, so need this number
int num_trees = forest->get_tree_count();
mexPrintf("Loaded forest of %d trees, retrieving leave node values.\n", num_trees);
mxArray *output_cell_array = mxCreateCellMatrix(1, num_trees);
ASSERT_NON_NULL(output_cell_array);
for (unsigned int t = 0; t < num_trees; t++) {
mxArray* tree_struct = mxCreateStructArray(num_dims, dims, tree_num_fields, tree_field_names);
ASSERT_NON_NULL(tree_struct);
mxSetCell(output_cell_array, t, make_matlab_tree_struct(forest->get_tree(t)));
}
plhs[0] = output_cell_array;
}
示例2: main
int main(int argc, char** argv)
{
// std::cout<<FLT_EPSILON<<std::endl;
cv::Mat training_data, training_labels,testing_data, testing_labels;
training_data = read_rgbd_data_cv(argv[1],NUMBER_OF_TRAINING_SAMPLES);
training_labels = read_rgbd_data_cv(argv[2], NUMBER_OF_TRAINING_SAMPLES);
testing_data = read_rgbd_data_cv(argv[3],NUMBER_OF_TESTING_SAMPLES);
testing_labels = read_rgbd_data_cv(argv[4], NUMBER_OF_TESTING_SAMPLES);
printf("dataset specs: %d samples with %d features\n", training_data.rows, training_data.cols);
// define all the attributes as numerical
// alternatives are CV_VAR_CATEGORICAL or CV_VAR_ORDERED(=CV_VAR_NUMERICAL)
// that can be assigned on a per attribute basis
cv::Mat var_type = cv::Mat(training_data.cols + 1, 1, CV_8U );
var_type.setTo(cv::Scalar(CV_VAR_NUMERICAL) ); // all inputs are numerical
var_type.at<uchar>(training_data.cols, 0) = CV_VAR_CATEGORICAL; // the labels are categorical
/********************************步骤1:定义初始化Random Trees的参数******************************/
float priors[] = {1,1,1,1,1}; // weights of each classification for classes
CvRTParams params = CvRTParams(25, // max depth
50, // min sample count
0, // regression accuracy: N/A here
false, // compute surrogate split, no missing data
15, // max number of categories (use sub-optimal algorithm for larger numbers)
priors, // the array of priors
false, // calculate variable importance
20, // number of variables randomly selected at node and used to find the best split(s).
NUMBER_OF_TREES, // max number of trees in the forest
0.01f, // forrest accuracy
CV_TERMCRIT_ITER | CV_TERMCRIT_EPS // termination cirteria
);
/****************************步骤2:训练 Random Decision Forest(RDF)分类器*********************/
// printf( "\nUsing training database: %s\n\n", argv[1]);
CvRTrees* rtree = new CvRTrees;
rtree->train(training_data, CV_ROW_SAMPLE, training_labels,
cv::Mat(), cv::Mat(), var_type, cv::Mat(), params);
// perform classifier testing and report results
cv::Mat test_sample, train_sample;
int correct_class = 0;
int wrong_class = 0;
int result;
int label;
int false_positives [NUMBER_OF_CLASSES] = {0,0,0,0,0};
int false_negatives [NUMBER_OF_CLASSES] = {0,0,0,0,0};
CvDTreeNode* leaf_nodes [training_data.rows];
for (int tsample = 0; tsample < training_data.rows; tsample++)
{
train_sample = training_data.row(tsample);
CvForestTree* tree = rtree->get_tree(1);
CvDTreeNode* leaf_node = tree->predict(train_sample, cv::Mat());
leaf_nodes[tsample] = leaf_node;
}
// printf( "\nUsing testing database: %s\n\n", argv[2]);
for (int tsample = 0; tsample < testing_data.rows; tsample++)
{
// extract a row from the testing matrix
test_sample = testing_data.row(tsample);
// train on the testing data:
// test_sample = training_data.row(tsample);
/********************************步骤3:预测*********************************************/
result = (int) rtree->predict(test_sample, cv::Mat());
label = (int) testing_labels.at<float>(tsample, 0);
printf("Testing Sample %i -> class result (digit %d) - label (digit %d)\n", tsample, result, label);
// get the leaf nodes of the first tree in the forest
/*CvForestTree* tree = rtree->get_tree(0);
std::list<const CvDTreeNode*> leaf_list;
leaf_list = get_leaf_node( tree );
printf("Number of Leaf nodes: %ld\n", leaf_list.size());*/
// if the prediction and the (true) testing classification are the same
// (N.B. openCV uses a floating point decision tree implementation!)
if (fabs(result - label)
>= FLT_EPSILON)
{
// if they differ more than floating point error => wrong class
wrong_class++;
false_positives[(int) result]++;
false_negatives[(int) testing_labels.at<float>(tsample, 0)]++;
}
else
{
// otherwise correct
correct_class++;
}
}
printf( // "\nResults on the testing database: %s\n"
//.........这里部分代码省略.........