Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Since OpenCV 3.0 "official release" is not out yet, the doc isn't that much available, so asking questions is a thing to do (well, it is what I have done).

Here is an example of knn match, which I think is another name for KNearest neighbours.

            // Number of neighbours
            int k = 2; 
            // Vector of DMatch containing the filtered results, only the best matches
            vector<DMatch> good_matches;
            // Vector containing a vector of DMatch (which contain the "k" nearest matches)
            vector<vector< DMatch >> matches;
            // Declaration of the matcher "3.0 style"
            Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");

            // Computation of the "k" nearest matches
            matcher -> knnMatch( objectDescriptors, sceneDescriptors, matches, k );


            /// Ratio test from Lowe-SIFT. It helps select the most relevant matches by doing a
            // ratio between each best and second best matches 

            if (matches[0].size() > 1)
            {

                for (int i = 0; i < matches.size(); ++i)
                {
                    const double ratio = 0.8; // As in Lowe's paper; can be tuned
                    if (matches[i][0].distance < ratio * matches[i][1].distance)
                    {
                        good_matches.push_back(matches[i][0]);
                    }
                }
            }

So at the end, you have a vector of DMatch indicating which matches are relevant according to a criterion, here, Lowe's ratio test. You may then use other techniques to continue your detection.

It assumes that prior to its execution, you have computed, for example, BRISK keypoint-descriptors for an object (model) and a scene (the picture on which you try to find the object). If you need more info on keypoint-descriptors, there are wide range of question on this site. If it is still obscure after, ask another question and people will gladly provide help :)

+++ OK, it appears that I misunderstood the OP's question and answered something that is true but not right on topic... So understand this is an answer about feature detector, the machine learning as the OP intended! +++

Since OpenCV 3.0 "official release" is not out yet, the doc isn't that much available, so asking questions is a thing to do (well, it is what I have done).

Here is an example of knn match, which I think is another name for KNearest neighbours.

            // Number of neighbours
            int k = 2; 
            // Vector of DMatch containing the filtered results, only the best matches
            vector<DMatch> good_matches;
            // Vector containing a vector of DMatch (which contain the "k" nearest matches)
            vector<vector< DMatch >> matches;
            // Declaration of the matcher "3.0 style"
            Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");

            // Computation of the "k" nearest matches
            matcher -> knnMatch( objectDescriptors, sceneDescriptors, matches, k );


            /// Ratio test from Lowe-SIFT. It helps select the most relevant matches by doing a
            // ratio between each best and second best matches 

            if (matches[0].size() > 1)
            {

                for (int i = 0; i < matches.size(); ++i)
                {
                    const double ratio = 0.8; // As in Lowe's paper; can be tuned
                    if (matches[i][0].distance < ratio * matches[i][1].distance)
                    {
                        good_matches.push_back(matches[i][0]);
                    }
                }
            }

So at the end, you have a vector of DMatch indicating which matches are relevant according to a criterion, here, Lowe's ratio test. You may then use other techniques to continue your detection.

It assumes that prior to its execution, you have computed, for example, BRISK keypoint-descriptors for an object (model) and a scene (the picture on which you try to find the object). If you need more info on keypoint-descriptors, there are wide range of question on this site. If it is still obscure after, ask another question and people will gladly provide help :)

+++ OK, it appears that I misunderstood the OP's question and answered something that is true but not right on topic... So understand this is an answer about feature detector, the detectors, not on machine learning as the OP intended! Thanks for people in comments for pointing out ;) +++

Since OpenCV 3.0 "official release" is not out yet, the doc isn't that much available, so asking questions is a thing to do (well, it is what I have done).

Here is an example of knn match, which I think is another name for KNearest neighbours.

            // Number of neighbours
            int k = 2; 
            // Vector of DMatch containing the filtered results, only the best matches
            vector<DMatch> good_matches;
            // Vector containing a vector of DMatch (which contain the "k" nearest matches)
            vector<vector< DMatch >> matches;
            // Declaration of the matcher "3.0 style"
            Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming");

            // Computation of the "k" nearest matches
            matcher -> knnMatch( objectDescriptors, sceneDescriptors, matches, k );


            /// Ratio test from Lowe-SIFT. It helps select the most relevant matches by doing a
            // ratio between each best and second best matches 

            if (matches[0].size() > 1)
            {

                for (int i = 0; i < matches.size(); ++i)
                {
                    const double ratio = 0.8; // As in Lowe's paper; can be tuned
                    if (matches[i][0].distance < ratio * matches[i][1].distance)
                    {
                        good_matches.push_back(matches[i][0]);
                    }
                }
            }

So at the end, you have a vector of DMatch indicating which matches are relevant according to a criterion, here, Lowe's ratio test. You may then use other techniques to continue your detection.

It assumes that prior to its execution, you have computed, for example, BRISK keypoint-descriptors for an object (model) and a scene (the picture on which you try to find the object). If you need more info on keypoint-descriptors, there are wide range of question on this site. If it is still obscure after, ask another question and people will gladly provide help :)