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;
}