Ask Your Question

Revision history [back]

Features2D example

Hi,

I start writting an example about feature2d and matching. I have got some questions. First questions Is it a good program call in right order, graphical result , dynamicCast and ...? Second questions about create method I use ORB::create and BRISK::create. Is it possible to write something like create("ORB"). Third questions matches are saved in file. On screen I can see

0       237     0       808.622
1       208     0       676.574
2       220     0       558.299
3       15      0       297.963

in file I have got

<Matches>
  0 237 0 1145710537 1 208 0 1143547076 2 220 0 1141609254 3 15 0

I don't understand. have you got same results?

thanks for your help

#include <opencv2/features2d.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <vector>
#include <iostream>

using namespace std;
using namespace cv;


int main(void)
    {
    vector<string> typeAlgoMatch = { "BruteForce",  "BruteForce-Hamming(2)" };
    vector<string> typeDesc = { "BRISK", "ORB" };

    string  dataFolder("../data/");
    vector<string> fileName={"basketball1.png","basketball2.png"};
    Mat img1 = imread(dataFolder+fileName[0], IMREAD_GRAYSCALE);
    Mat img2 = imread(dataFolder+fileName[1], IMREAD_GRAYSCALE);


    Ptr<Feature2D> b;
    vector<KeyPoint> keyImg1;       /*<! keypoint  for img1 */
    vector<KeyPoint> keyImg2;       /*<! keypoint  for img2 */
    Mat descImg1, descImg2;         /*<! Descriptor for img1 and img2 */
    Mat result;
    Ptr<DescriptorMatcher> descriptorMatcher;
    vector<DMatch> matches; /*<! Match */


    vector<string>::iterator itDesc;
    for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++)
        {
        vector<string>::iterator itMatcher;
        if (*itDesc == "ORB")
            {
            b = ORB::create();
            }
        else if (*itDesc == "BRISK")
            {
            b = BRISK::create();
            }

        b->detect(img1, keyImg1, Mat());
// Only 30 keyPoints with strong response values are keep. It's only used  to  have a nice drawing 
        Mat index;
        Mat tab(keyImg1.size(),1,CV_32F);
        for (int i = 0; i<keyImg1.size();i++)
            tab.at<float>(i,0)= keyImg1[i].response;
        sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_DESCENDING);
        vector<KeyPoint> keyFilterImg1;
        for (int i=0;i<30;i++)
            keyFilterImg1.push_back(keyImg1[index.at<int>(i,0)]);
        b->compute(img1, keyFilterImg1, descImg1);


        b->detectAndCompute(img2, Mat(), keyImg2, descImg2);
        for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++)
            {

            descriptorMatcher = DescriptorMatcher::create(*itMatcher);
            descriptorMatcher.dynamicCast<cv::DescriptorMatcher>()->match(descImg1, descImg2, matches, Mat());

            drawMatches(img1, keyFilterImg1, img2, keyImg2, matches, result);
            namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE);
            imshow(*itDesc + ": " + *itMatcher, result);
            FileStorage fs(dataFolder+*itDesc+"_"+*itMatcher+"_"+fileName[0]+"_"+fileName[1]+".xml", FileStorage::WRITE);
            fs<<"Matches"<<matches;

            vector<DMatch>::iterator it;

            cout << "Index \tIndex \tindex \tdistance\n";
            cout << "in img1\tin img2\timage\t\n";
            for (it = matches.begin(); it != matches.end(); it++)
                cout << it->queryIdx << "\t" << it->trainIdx << "\t" << it->imgIdx << "\t" << it->distance<<"\n";
            waitKey();
            }
        }
    return 0;
    }

Features2D example

Hi,

I start writting an example about feature2d and matching. I have got some questions. First questions Is it a good program call in right order, graphical result , dynamicCast and ...? Second questions about create method I use ORB::create and BRISK::create. Is it possible to write something like create("ORB"). Third questions matches are saved in file. On screen I can see

0       237     0       808.622
1       208     0       676.574
2       220     0       558.299
3       15      0       297.963

in file I have got

<Matches>
  0 237 0 1145710537 1 208 0 1143547076 2 220 0 1141609254 3 15 0

I don't understand. have you got same results?

thanks for your help

#include <opencv2/features2d.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <vector>
#include <iostream>

using namespace std;
using namespace cv;


int main(void)
    {
    vector<string> typeAlgoMatch = { "BruteForce",  "BruteForce-Hamming", "BruteForce-Hamming(2)" };
    vector<string> typeDesc = { "BRISK", "ORB" "AKAZE","ORB" ,"BRISK" };

    string  dataFolder("../data/");
    vector<string> fileName={"basketball1.png","basketball2.png"};
fileName = { "basketball1.png", "basketball2.png" };
    Mat img1 = imread(dataFolder+fileName[0], IMREAD_GRAYSCALE);
    Mat img2 = imread(dataFolder+fileName[1], IMREAD_GRAYSCALE);


    Ptr<Feature2D> b;


    vector<string>::iterator itDesc;
    for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++)
        {
        Ptr<DescriptorMatcher> descriptorMatcher;
        vector<DMatch> matches; /*<! Match */
        vector<KeyPoint> keyImg1;       /*<! keypoint  for img1 */
     vector<KeyPoint> keyImg2;       /*<! keypoint  for img2 */
     Mat descImg1, descImg2;         /*<! Descriptor for img1 and img2 */
    Mat result;
    Ptr<DescriptorMatcher> descriptorMatcher;
    vector<DMatch> matches; /*<! Match */


    vector<string>::iterator itDesc;
    for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++)
itMatcher = typeAlgoMatch.end();
        if (*itDesc == "AKAZE")
            {
        vector<string>::iterator itMatcher;
    b = AKAZE::create();
            }
        if (*itDesc == "ORB")
            {
            b = ORB::create();
            }
        else if (*itDesc == "BRISK")
            {
            b = BRISK::create();
            }
         try
            {
            b->detect(img1, keyImg1, Mat());
// Only 30 keyPoints with strong response values are keep. It's only used  to  have a nice drawing 
        Mat index;
        Mat tab(keyImg1.size(),1,CV_32F);
        for (int i = 0; i<keyImg1.size();i++)
            tab.at<float>(i,0)= keyImg1[i].response;
        sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_DESCENDING);
        vector<KeyPoint> keyFilterImg1;
        for (int i=0;i<30;i++)
            keyFilterImg1.push_back(keyImg1[index.at<int>(i,0)]);
         b->compute(img1, keyFilterImg1, keyImg1, descImg1);
          b->detectAndCompute(img2, Mat(), keyImg2, descImg2);
Mat(),keyImg2, descImg2,false);


            for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++)
             {

             descriptorMatcher = DescriptorMatcher::create(*itMatcher);
            descriptorMatcher.dynamicCast<cv::DescriptorMatcher>()->match(descImg1,     descriptorMatcher->match(descImg1, descImg2, matches, Mat());
                 Mat index;
                Mat tab(matches.size(), 1, CV_32F);
                for (int i = 0; i<matches.size(); i++)
                    tab.at<float>(i, 0) = matches[i].distance;
                sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING);
                vector<DMatch> bestMatches; /*<! Match */

                for (int i = 0; i<30; i++)
                    bestMatches.push_back(matches[index.at<int>(i, 0)]);

                Mat result;
                drawMatches(img1, keyFilterImg1, keyImg1, img2, keyImg2, matches, bestMatches, result);
             namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE);
             imshow(*itDesc + ": " + *itMatcher, result);
             FileStorage fs(dataFolder+*itDesc+"_"+*itMatcher+"_"+fileName[0]+"_"+fileName[1]+".xml", FileStorage::WRITE);
             fs<<"Matches"<<matches;

             vector<DMatch>::iterator it;

             cout << "Index \tIndex \tindex \tdistance\n";
             cout << "in img1\tin img2\timage\t\n";
             for (it = matches.begin(); it != matches.end(); it++)
                 cout << it->queryIdx << "\t" << it->trainIdx << "\t" << it->imgIdx << "\t" << it->distance<<"\n";
             waitKey();
                }
            }
        catch (Exception& e)
            {
            cout << "Feature : " << *itDesc << "\n";
            if (itMatcher != typeAlgoMatch.end())
                cout << "Matcher : " << *itMatcher << "\n";
            cout<<e.msg<<endl;
            }
        }
    return 0;
    }

Features2D example

Hi,

I start writting an example about feature2d and matching. I have got some questions. First questions Is it a good program call in right order, graphical result , dynamicCast and ...? Second questions about create method I use ORB::create and BRISK::create. Is it possible to write something like create("ORB"). Third questions matches are saved in file. On screen I can see

0       237     0       808.622
1       208     0       676.574
2       220     0       558.299
3       15      0       297.963

in file I have got

<Matches>
  0 237 0 1145710537 1 208 0 1143547076 2 220 0 1141609254 3 15 0

I don't understand. have you got same results?

thanks for your help

#include <opencv2/features2d.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <vector>
#include <iostream>

using namespace std;
using namespace cv;


int main(void)
 {
    vector<string> typeAlgoMatch = { "BruteForce", "BruteForce-Hamming", "BruteForce-Hamming(2)" };
    vector<string> typeDesc = { "AKAZE","ORB" ,"BRISK" };

    string vector<String> typeAlgoMatch;
    typeAlgoMatch.push_back("BruteForce");
    typeAlgoMatch.push_back("BruteForce-Hamming");
    typeAlgoMatch.push_back("BruteForce-Hamming(2)");

    vector<String> typeDesc;
    typeDesc.push_back("AKAZE");
    typeDesc.push_back("ORB");
    typeDesc.push_back("BRISK");

    String  dataFolder("../data/");
    vector<string> fileName = { "basketball1.png", "basketball2.png" };
vector<String> fileName;
    fileName.push_back("basketball1.png");
    fileName.push_back("basketball2.png");

    Mat img1 = imread(dataFolder+fileName[0], IMREAD_GRAYSCALE);
    Mat img2 = imread(dataFolder+fileName[1], IMREAD_GRAYSCALE);

     Ptr<Feature2D> b;


    vector<string>::iterator     vector<String>::iterator itDesc;
// Descriptor loop
    for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++)
        {
itDesc++){
        Ptr<DescriptorMatcher> descriptorMatcher;
        vector<DMatch> matches;  /*<! Match */
between img and img2*/
        vector<KeyPoint> keyImg1;       /*<! keypoint  for img1 */
        vector<KeyPoint> keyImg2;       /*<! keypoint  for img2 */
        Mat descImg1, descImg2;         /*<! Descriptor for img1 and img2 */
        vector<string>::iterator vector<String>::iterator itMatcher = typeAlgoMatch.end();
        if (*itDesc == "AKAZE")
            {
"AKAZE"){
            b = AKAZE::create();
         }
        if (*itDesc == "ORB")
            {
"ORB"){
            b = ORB::create();
         }
        else if (*itDesc == "BRISK")
            {
"BRISK"){
            b = BRISK::create();
         }
        try
            try {
            b->detect(img1, keyImg1, Mat());
            b->compute(img1, keyImg1, descImg1);
            b->detectAndCompute(img2, Mat(),keyImg2, descImg2,false);
  // Match method loop
             for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++)
                {

itMatcher++){
                descriptorMatcher = DescriptorMatcher::create(*itMatcher);
                descriptorMatcher->match(descImg1, descImg2, matches, Mat());
 // Keep best matches only to have a nice drawing drawing
                Mat index;
                Mat tab(matches.size(), 1, CV_32F);
                for (int i = 0; i<matches.size(); i++)
                    tab.at<float>(i, 0) = matches[i].distance;
                sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING);
                vector<DMatch> bestMatches; /*<! Match best match */

                for (int i = 0; i<30; i++)
                    bestMatches.push_back(matches[index.at<int>(i, 0)]);

                Mat result;
                drawMatches(img1, keyImg1, img2, keyImg2, bestMatches, result);
                namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE);
                imshow(*itDesc + ": " + *itMatcher, result);
                FileStorage fs(dataFolder+*itDesc+"_"+*itMatcher+"_"+fileName[0]+"_"+fileName[1]+".xml", FileStorage::WRITE);
                fs<<"Matches"<<matches;

                vector<DMatch>::iterator it;

                cout << "Index \tIndex \tindex \tdistance\n";
                cout << "in img1\tin img2\timage\t\n";
                for (it = matches.begin(); it != matches.end(); it++)
                    cout << it->queryIdx << "\t" << it->trainIdx << "\t" << it->imgIdx << "\t" << it->distance<<"\n";
                waitKey();
             }
         }
        catch (Exception& e)
            {
e){
            cout << "Feature : " << *itDesc << "\n";
            if (itMatcher != typeAlgoMatch.end())
                cout << "Matcher : " << *itMatcher << "\n";
            cout<<e.msg<<endl;
         }
     }
    return 0;
 }

Features2D example

Hi,

I start writting an example about feature2d and matching. I have got some questions. First questions Is it a good program call in right order, graphical result , dynamicCast and ...? Second questions about create method I use ORB::create and BRISK::create. Is it possible to write something like create("ORB"). Third questions matches are saved in file. On screen I can see

0       237     0       808.622
1       208     0       676.574
2       220     0       558.299
3       15      0       297.963

in file I have got

<Matches>
  0 237 0 1145710537 1 208 0 1143547076 2 220 0 1141609254 3 15 0

I don't understand. have you got same results?

thanks for your help

#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>

using namespace std;
using namespace cv;


int main(void)
{
    vector<String> typeAlgoMatch;
    typeAlgoMatch.push_back("BruteForce");
    typeAlgoMatch.push_back("BruteForce-Hamming");
    typeAlgoMatch.push_back("BruteForce-Hamming(2)");

    vector<String> typeDesc;
    typeDesc.push_back("AKAZE");
    typeDesc.push_back("ORB");
    typeDesc.push_back("BRISK");

    String  dataFolder("../data/");
    vector<String> fileName;
    fileName.push_back("basketball1.png");
    fileName.push_back("basketball2.png");

    Mat img1 = imread(dataFolder+fileName[0], IMREAD_GRAYSCALE);
    Mat img2 = imread(dataFolder+fileName[1], IMREAD_GRAYSCALE);

    Ptr<Feature2D> b;

    vector<String>::iterator itDesc;
// Descriptor loop
    for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++){
        Ptr<DescriptorMatcher> descriptorMatcher;
        vector<DMatch> matches;         /*<! Match between img and img2*/
        vector<KeyPoint> keyImg1;       /*<! keypoint  for img1 */
        vector<KeyPoint> keyImg2;       /*<! keypoint  for img2 */
        Mat descImg1, descImg2;         /*<! Descriptor for img1 and img2 */
        vector<String>::iterator itMatcher = typeAlgoMatch.end();
        if (*itDesc == "AKAZE"){
            b = AKAZE::create();
        }
        if (*itDesc == "ORB"){
            b = ORB::create();
        }
        else if (*itDesc == "BRISK"){
            b = BRISK::create();
        }
        try {
            b->detect(img1, keyImg1, Mat());
            b->compute(img1, keyImg1, descImg1);
            b->detectAndCompute(img2, Mat(),keyImg2, descImg2,false);
 // Match method loop
             for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++){
                descriptorMatcher = DescriptorMatcher::create(*itMatcher);
                descriptorMatcher->match(descImg1, descImg2, matches, Mat());
 // Keep best matches only to have a nice drawing drawing
                 Mat index;
                Mat tab(matches.size(), 1, CV_32F);
                for (int i = 0; i<matches.size(); i++)
                    tab.at<float>(i, 0) = matches[i].distance;
                sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING);
                vector<DMatch> bestMatches; /*<! best match */

                for (int i = 0; i<30; i++)
                    bestMatches.push_back(matches[index.at<int>(i, 0)]);

                Mat result;
                drawMatches(img1, keyImg1, img2, keyImg2, bestMatches, result);
                namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE);
                imshow(*itDesc + ": " + *itMatcher, result);
                FileStorage fs(dataFolder+*itDesc+"_"+*itMatcher+"_"+fileName[0]+"_"+fileName[1]+".xml", fs(*itDesc+"_"+*itMatcher+"_"+fileName[0]+"_"+fileName[1]+".xml", FileStorage::WRITE);
                fs<<"Matches"<<matches;

                vector<DMatch>::iterator it;

                cout << "Index \tIndex \tindex \tdistance\n";
                cout << "in img1\tin img2\timage\t\n";
                for (it = matches.begin(); it != matches.end(); it++)
                    cout << it->queryIdx << "\t" << it->trainIdx << "\t" << it->imgIdx << "\t" << it->distance<<"\n";
                waitKey();
            }
        }
        catch (Exception& e){
            cout << "Feature : " << *itDesc << "\n";
            if (itMatcher != typeAlgoMatch.end())
                cout << "Matcher : " << *itMatcher << "\n";
            cout<<e.msg<<endl;
        }
    }
    return 0;
}

Features2D example

Hi,

I start writting an example about feature2d and matching. I have got some questions. First questions Is it a good program call in right order, graphical result , dynamicCast and ...? Second questions about create method I use ORB::create and BRISK::create. Is it possible to write something like create("ORB"). Third questions matches are saved in file. On screen I can see

0       237     0       808.622
1       208     0       676.574
2       220     0       558.299
3       15      0       297.963

in file I have got

<Matches>
  0 237 0 1145710537 1 208 0 1143547076 2 220 0 1141609254 3 15 0

I don't understand. have you got same results?

thanks for your help

#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>

using namespace std;
using namespace cv;


int main(void)
{
    vector<String> typeAlgoMatch;
    typeAlgoMatch.push_back("BruteForce");
    typeAlgoMatch.push_back("BruteForce-Hamming");
    typeAlgoMatch.push_back("BruteForce-Hamming(2)");

    vector<String> typeDesc;
    typeDesc.push_back("AKAZE");
    typeDesc.push_back("ORB");
    typeDesc.push_back("BRISK");

    String  dataFolder("../data/");
    vector<String> fileName;
    fileName.push_back("basketball1.png");
    fileName.push_back("basketball2.png");

    Mat img1 = imread(dataFolder+fileName[0], IMREAD_GRAYSCALE);
    Mat img2 = imread(dataFolder+fileName[1], IMREAD_GRAYSCALE);

    Ptr<Feature2D> b;

    vector<String>::iterator itDesc;
// Descriptor loop
    for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++){
        Ptr<DescriptorMatcher> descriptorMatcher;
        vector<DMatch> matches;         /*<! Match between img and img2*/
        vector<KeyPoint> keyImg1;       /*<! keypoint  for img1 */
        vector<KeyPoint> keyImg2;       /*<! keypoint  for img2 */
        Mat descImg1, descImg2;         /*<! Descriptor for img1 and img2 */
        vector<String>::iterator itMatcher = typeAlgoMatch.end();
        if (*itDesc == "AKAZE"){
            b = AKAZE::create();
        }
        else if (*itDesc == "ORB"){
            b = ORB::create();
        }
        else if (*itDesc == "BRISK"){
            b = BRISK::create();
        }
        try {
            b->detect(img1, keyImg1, Mat());
            b->compute(img1, keyImg1, descImg1);
            b->detectAndCompute(img2, Mat(),keyImg2, descImg2,false);
 // Match method loop
             for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++){
                descriptorMatcher = DescriptorMatcher::create(*itMatcher);
                descriptorMatcher->match(descImg1, descImg2, matches, Mat());
 // Keep best matches only to have a nice drawing 
                Mat index;
                Mat tab(matches.size(), 1, CV_32F);
                for (int i = 0; i<matches.size(); i++)
                    tab.at<float>(i, 0) = matches[i].distance;
                sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING);
                vector<DMatch> bestMatches; /*<! best match */

                for (int i = 0; i<30; i++)
                    bestMatches.push_back(matches[index.at<int>(i, 0)]);

                Mat result;
                drawMatches(img1, keyImg1, img2, keyImg2, bestMatches, result);
                namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE);
                imshow(*itDesc + ": " + *itMatcher, result);
                FileStorage fs(*itDesc+"_"+*itMatcher+"_"+fileName[0]+"_"+fileName[1]+".xml", FileStorage::WRITE);
                fs<<"Matches"<<matches;

                vector<DMatch>::iterator it;

                cout << "Index \tIndex \tindex \tdistance\n";
                cout << "in img1\tin img2\timage\t\n";
                for (it = matches.begin(); it != matches.end(); it++)
                    cout << it->queryIdx << "\t" << it->trainIdx << "\t" << it->imgIdx << "\t" << it->distance<<"\n";
                waitKey();
            }
        }
        catch (Exception& e){
            cout << "Feature : " << *itDesc << "\n";
            if (itMatcher != typeAlgoMatch.end())
                cout << "Matcher : " << *itMatcher << "\n";
            cout<<e.msg<<endl;
        }
    }
    return 0;
}

Features2D example

Hi,

I start writting an example about feature2d and matching. I have got some questions. First questions Is it a good program call in right order, graphical result , dynamicCast and ...? Second questions about create method I use ORB::create and BRISK::create. Is it possible to write something like create("ORB"). Third questions matches are saved in file. On screen I can see

0       237     0       808.622
1       208     0       676.574
2       220     0       558.299
3       15      0       297.963

in file I have got

<Matches>
  0 237 0 1145710537 1 208 0 1143547076 2 220 0 1141609254 3 15 0

I don't understand. have you got same results?

thanks for your help

#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>

using namespace std;
using namespace cv;


int main(void)
{
    vector<String> typeAlgoMatch;
    typeAlgoMatch.push_back("BruteForce");
    typeAlgoMatch.push_back("BruteForce-Hamming");
    typeAlgoMatch.push_back("BruteForce-Hamming(2)");

    vector<String> typeDesc;
    typeDesc.push_back("AKAZE");
    typeDesc.push_back("ORB");
    typeDesc.push_back("BRISK");

    String  dataFolder("../data/");
    vector<String> fileName;
    fileName.push_back("basketball1.png");
    fileName.push_back("basketball2.png");

    Mat img1 = imread(dataFolder+fileName[0], IMREAD_GRAYSCALE);
    Mat img2 = imread(dataFolder+fileName[1], IMREAD_GRAYSCALE);

    Ptr<Feature2D> b;

    vector<String>::iterator itDesc;
// Descriptor loop
    for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++){
        Ptr<DescriptorMatcher> descriptorMatcher;
        vector<DMatch> matches;         /*<! Match between img and img2*/
        vector<KeyPoint> keyImg1;       /*<! keypoint  for img1 */
        vector<KeyPoint> keyImg2;       /*<! keypoint  for img2 */
        Mat descImg1, descImg2;         /*<! Descriptor for img1 and img2 */
        vector<String>::iterator itMatcher = typeAlgoMatch.end();
        if (*itDesc == "AKAZE"){
            b = AKAZE::create();
        }
        else if (*itDesc == "ORB"){
            b = ORB::create();
        }
        else if (*itDesc == "BRISK"){
            b = BRISK::create();
        }
        try {
            b->detect(img1, keyImg1, Mat());
            b->compute(img1, keyImg1, descImg1);
            b->detectAndCompute(img2, Mat(),keyImg2, descImg2,false);
 // Match method loop
             for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++){
                descriptorMatcher = DescriptorMatcher::create(*itMatcher);
                descriptorMatcher->match(descImg1, descImg2, matches, Mat());
 // Keep best matches only to have a nice drawing 
                Mat index;
                Mat tab(matches.size(), 1, CV_32F);
                for (int i = 0; i<matches.size(); i++)
                    tab.at<float>(i, 0) = matches[i].distance;
                sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING);
                vector<DMatch> bestMatches; /*<! best match */

                for (int i = 0; i<30; i++)
                    bestMatches.push_back(matches[index.at<int>(i, 0)]);

                Mat result;
                drawMatches(img1, keyImg1, img2, keyImg2, bestMatches, result);
                namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE);
                imshow(*itDesc + ": " + *itMatcher, result);
                FileStorage fs(*itDesc+"_"+*itMatcher+"_"+fileName[0]+"_"+fileName[1]+".xml", FileStorage::WRITE);
                fs<<"Matches"<<matches;

                vector<DMatch>::iterator it;

             cout << "Index \tIndex \tindex \tdistance\n";
             cout << "in img1\tin img2\timage\t\n";
    img2\n";
            double cumSumDist2=0;
            for (it = matches.begin(); bestMatches.begin(); it != matches.end(); bestMatches.end(); it++)
                {
                cout << it->queryIdx << "\t" <<  it->trainIdx << "\t" << it->imgIdx << "\t" << it->distance<<"\n";
     <<  << it->distance << "\n";
                Point2d p=keyImg1[it->queryIdx].pt-keyImg2[it->trainIdx].pt;
                cumSumDist2=p.x*p.x+p.y*p.y;
                }
            desMethCmp.push_back(cumSumDist2);
            waitKey();
         }
     }
     catch (Exception& e){
         cout << "Feature : " << *itDesc << "\n";
         if (itMatcher != typeAlgoMatch.end())
             cout << "Matcher : " << *itMatcher << "\n";
         cout<<e.msg<<endl;
     }
}
int i=0;
cout << "\n\t";
for (vector<String>::iterator itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++)
    cout<<*itMatcher<<"\t";
cout << "\n";
for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++){
    cout << *itDesc << "\t";
    for (vector<String>::iterator itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++, i++)
            cout << desMethCmp[i]<<"\t";
    cout<<"\n";
    }
    return 0;
}

Features2D example

Hi,

I start writting an example about feature2d and matching. I have got some questions. First questions Is it a good program call in right order, graphical result , dynamicCast and ...? Second questions about create method I use ORB::create and BRISK::create. Is it possible to write something like create("ORB"). Third questions matches are saved in file. On screen I can see

0       237     0       808.622
1       208     0       676.574
2       220     0       558.299
3       15      0       297.963

in file I have got

<Matches>
  0 237 0 1145710537 1 208 0 1143547076 2 220 0 1141609254 3 15 0

I don't understand. have you got same results?

thanks for your help

#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>

using namespace std;
using namespace cv;


int main(void)
{
    vector<String> typeAlgoMatch;
    typeAlgoMatch.push_back("BruteForce");
    typeAlgoMatch.push_back("BruteForce-Hamming");
    typeAlgoMatch.push_back("BruteForce-Hamming(2)");

    vector<String> typeDesc;
    typeDesc.push_back("AKAZE");
    typeDesc.push_back("ORB");
    typeDesc.push_back("BRISK");

    String  dataFolder("../data/");
    vector<String> fileName;
    fileName.push_back("basketball1.png");
    fileName.push_back("basketball2.png");

    Mat img1 = imread(dataFolder+fileName[0], IMREAD_GRAYSCALE);
    Mat img2 = imread(dataFolder+fileName[1], IMREAD_GRAYSCALE);

    Ptr<Feature2D> b;

    vector<String>::iterator itDesc;
// Descriptor loop
    for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++){
        Ptr<DescriptorMatcher> descriptorMatcher;
        vector<DMatch> matches;         /*<! Match between img and img2*/
        vector<KeyPoint> keyImg1;       /*<! keypoint  for img1 */
        vector<KeyPoint> keyImg2;       /*<! keypoint  for img2 */
        Mat descImg1, descImg2;         /*<! Descriptor for img1 and img2 */
        vector<String>::iterator itMatcher = typeAlgoMatch.end();
        if (*itDesc == "AKAZE"){
            b = AKAZE::create();
        }
        else if (*itDesc == "ORB"){
            b = ORB::create();
        }
        else if (*itDesc == "BRISK"){
            b = BRISK::create();
        }
        try {
            b->detect(img1, keyImg1, Mat());
            b->compute(img1, keyImg1, descImg1);
            b->detectAndCompute(img2, Mat(),keyImg2, descImg2,false);
 // Match method loop
             for (itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++){
                descriptorMatcher = DescriptorMatcher::create(*itMatcher);
                descriptorMatcher->match(descImg1, descImg2, matches, Mat());
 // Keep best matches only to have a nice drawing 
                Mat index;
                Mat tab(matches.size(), 1, CV_32F);
                for (int i = 0; i<matches.size(); i++)
                    tab.at<float>(i, 0) = matches[i].distance;
                sortIdx(tab, index, SORT_EVERY_COLUMN + SORT_ASCENDING);
                vector<DMatch> bestMatches; /*<! best match */

                for (int i = 0; i<30; i++)
                    bestMatches.push_back(matches[index.at<int>(i, 0)]);

                Mat result;
                drawMatches(img1, keyImg1, img2, keyImg2, bestMatches, result);
                namedWindow(*itDesc+": "+*itMatcher, WINDOW_AUTOSIZE);
                imshow(*itDesc + ": " + *itMatcher, result);
                FileStorage fs(*itDesc+"_"+*itMatcher+"_"+fileName[0]+"_"+fileName[1]+".xml", FileStorage::WRITE);
                fs<<"Matches"<<matches;

                vector<DMatch>::iterator it;

            cout << "Index \tIndex \tdistance\n";
            cout << "in img1\tin img2\n";
            double cumSumDist2=0;
            for (it = bestMatches.begin(); it != bestMatches.end(); it++)
                {
                cout << it->queryIdx << "\t" <<  it->trainIdx << "\t"  <<  <<  it->distance << "\n";
                Point2d p=keyImg1[it->queryIdx].pt-keyImg2[it->trainIdx].pt;
                cumSumDist2=p.x*p.x+p.y*p.y;
                }
            desMethCmp.push_back(cumSumDist2);
            waitKey();
        }
    }
    catch (Exception& e){
        cout << "Feature : " << *itDesc << "\n";
        if (itMatcher != typeAlgoMatch.end())
            cout << "Matcher : " << *itMatcher << "\n";
        cout<<e.msg<<endl;
    }
}
int i=0;
cout << "\n\t";
for (vector<String>::iterator itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++)
    cout<<*itMatcher<<"\t";
cout << "\n";
for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++){
    cout << *itDesc << "\t";
    for (vector<String>::iterator itMatcher = typeAlgoMatch.begin(); itMatcher != typeAlgoMatch.end(); itMatcher++, i++)
            cout << desMethCmp[i]<<"\t";
    cout<<"\n";
    }
    return 0;
}