Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OK, let me give you some insights and useful links, but the answer remains the same: you can't get SVM multiclass probability with a pure combination of OpenCV functions and minimum effort.

OpenCV SVM (based on LibSVM) implements multi-class classification in the form of one-vs-one problems, i.e. each class is compared with each other class, so there is N(N-1)/2 decision functions in an N-class problem. Each comparison has a winner class, and such class receives one vote to be the final best match (so yeah, you obviously get the best match). This is coded in svm.cpp, lines 1886-1906:

                    for( i = dfi = 0; i < class_count; i++ )
                    {
                        for( j = i+1; j < class_count; j++, dfi++ )
                        {
                            const DecisionFunc& df = svm->decision_func[dfi];
                            sum = -df.rho;
                            int sv_count = svm->getSVCount(dfi);
                            const double* alpha = &svm->df_alpha[df.ofs];
                            const int* sv_index = &svm->df_index[df.ofs];
                            for( k = 0; k < sv_count; k++ )
                                sum += alpha[k]*buffer[sv_index[k]];

                            vote[sum > 0 ? i : j]++;
                        }
                    }

                    for( i = 1, k = 0; i < class_count; i++ )
                    {
                        if( vote[i] > vote[k] )
                            k = i;
                    }

Regarding probabilities, they're not so easy to compute. In fact, even for 2-class classification problems, no probability is outputted in OpenCV. Instead, we can just get the distance of the sample to the SVM separating hyperplane, which gives a rough indicator of the correctness of the prediction (the larger the distance, the larger the confidence). There are several methods to compute probabilities, and you can read about them in the LibSVM manual, Section 8.

The getDecisionFunction() indeed gives you all necesarry information to predict yourself the output value, but in any case you would have to implement a probability estimator, which I doubt is your goal here.