Deltanullnull
Deltanullnull

Reputation: 23

OpenCV Random Decision Forest: How to get posterior probability

I did research on multiple websites, but I couldn't find any solution. Here's the problem:

I am implementing a pixel-wise classification using RTrees from OpenCV. I need the posterior probability for each class. I tried to get it via cv::ml::StatModel::predict(), but the output matrix only contains the predicted value. Is there another way to get the posterior probability from RTrees?

PS: I'm still quite new to Machine Learning, so please forgive me my lack of knowledge ^^"

Upvotes: 1

Views: 908

Answers (1)

Ardavel
Ardavel

Reputation: 1515

Instead of using cv::ml::StatModel::predict, you could refer to the cv::ml::RTrees::getVotes member function. This way, in case of classification, you get the number of trees which voted for each class for given sample. By dividing these numbers of votes by the forest size you get an approximation of posterior probabilities.

The getVotes function should be called instead of predict like this:

cv::Mat samples = [one or multiple samples (their feature vectors)]
cv::Mat votes;
classifier.getVotes(sample, votes, 0);
// provide 0 here unless you would like to manipulate with RTrees flags

What you should be aware of is that the votes matrix is going to have one more row than the number of samples. In this first row there are your classes enumerated (in ascending order if I remember well from the OpenCV source code).

The answer is up to date as of the 3.4.1 version of OpenCV.

Upvotes: 2

Related Questions