Modify the SVM in opencv and output the Top N matching results.

Source: Internet
Author: User

Recently, I want to use SVM to output the Top N matching results. After reading SVM and predict in opencv, we can only output top 1.

Three predict functions are available. One of them uses CV: parallel_for, which should be parallel optimization and cannot be found in the relevant explanatory documents.

Last selected

Float cvsvm: predict (const float * row_sample, int row_len, bool returndfval) const

Returndfval does not work if it is not a one-class problem.

In fact, there is no choice, because the other few are based on this ....

The original Code contains the following content, which is the maximum output value. Sort and then output the nth value.

For (I = 1, K = 0; I <class_count; I ++)
{
If (vote [I]> vote [k])
K = I;
}
Result = returndfval & class_count = 2? (Float) sum: (float) (class_labels-> data. I [k]);
}
Else
Cv_error (cv_stsbadarg, "internal error: Unknown SVM type ,"
"The SVM structure is probably successfully upted ");

Return result;
}

If you do not change the source code, you need to train (N * N-1)/Two SVM, then set returndfval to true, and compare the output results by yourself.

The last modification is as follows:

/*
For (I = 1, K = 0; I <class_count; I ++)
{
If (vote [I]> vote [k])
K = I;
}
Result = returndfval & class_count = 2? (Float) sum: (float) (class_labels-> data. I [k]);
*/
/* Modify the code so that the nth number can be output as a candidate result */
Float xx = 0;
CV: mat vote2 = CV: MAT (class_count, 1, cv_32fc1, vote );
CV: mat vote3;
CV: sortidx (vote2, vote3, cv_sort_every_column + cv_sort_descending );
K = vote3.at <int> (n-1, 0 );
Result = returndfval & class_count = 2? (Float) sum: (float) (class_labels-> data. I [k]);
The results are to be verified.

After changing the HPP and CPP, check whether the HPP has been referenced. If it is different from the referenced one, it will be a tragedy. Then I met it. I thought about it right away when I had a good rest.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.