number of inliers (i. e. matches that fit in the given homography).
3 #include <opencv2/opencv.hpp>
10 const float inlier_threshold = 2.5f;
11 const float nn_match_ratio = 0.8f;
19 FileStorage fs(
"../data/H1to3p.xml", FileStorage::READ);
20 fs.getFirstTopLevelNode() >> homography;
22 vector<KeyPoint> kpts1, kpts2;
30 vector< vector<DMatch> > nn_matches;
31 matcher.knnMatch(desc1, desc2, nn_matches, 2);
33 vector<KeyPoint> matched1, matched2, inliers1, inliers2;
34 vector<DMatch> good_matches;
35 for(
size_t i = 0; i < nn_matches.size(); i++) {
36 DMatch first = nn_matches[
i][0];
37 float dist1 = nn_matches[
i][0].distance;
38 float dist2 = nn_matches[
i][1].distance;
40 if(dist1 < nn_match_ratio * dist2) {
41 matched1.push_back(kpts1[first.
queryIdx]);
42 matched2.push_back(kpts2[first.
trainIdx]);
46 for(
unsigned i = 0; i < matched1.size(); i++) {
48 col.
at<
double>(0) = matched1[i].pt.x;
49 col.
at<
double>(1) = matched1[
i].pt.y;
51 col = homography * col;
52 col /= col.
at<
double>(2);
53 double dist =
sqrt(
pow(col.at<
double>(0) - matched2[i].pt.x, 2) +
54 pow(col.at<
double>(1) - matched2[i].pt.y, 2));
56 if(dist < inlier_threshold) {
57 int new_i =
static_cast<int>(inliers1.size());
58 inliers1.push_back(matched1[i]);
59 inliers2.push_back(matched2[i]);
60 good_matches.push_back(
DMatch(new_i, new_i, 0));
65 drawMatches(img1, inliers1, img2, inliers2, good_matches, res);
68 double inlier_ratio = inliers1.size() * 1.0 / matched1.size();
69 cout <<
"A-KAZE Matching Results" << endl;
70 cout <<
"*******************************" << endl;
71 cout <<
"# Keypoints 1: \t" << kpts1.size() << endl;
72 cout <<
"# Keypoints 2: \t" << kpts2.size() << endl;
73 cout <<
"# Matches: \t" << matched1.size() << endl;
74 cout <<
"# Inliers: \t" << inliers1.size() << endl;
75 cout <<
"# Inliers Ratio: \t" << inlier_ratio << endl;
bool imwrite(const String &filename, InputArray img, const std::vector< int > ¶ms=std::vector< int >())
Saves an image to a specified file.
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
void drawMatches(InputArray img1, const std::vector< KeyPoint > &keypoints1, InputArray img2, const std::vector< KeyPoint > &keypoints2, const std::vector< DMatch > &matches1to2, InputOutputArray outImg, const Scalar &matchColor=Scalar::all(-1), const Scalar &singlePointColor=Scalar::all(-1), const std::vector< char > &matchesMask=std::vector< char >(), int flags=DrawMatchesFlags::DEFAULT)
Draws the found matches of keypoints from two images.
#define CV_64F
Definition: cvdef.h:107
Definition: imgcodecs.hpp:64
int queryIdx
Definition: types.hpp:735
void sqrt(InputArray src, OutputArray dst)
Calculates a square root of array elements.
Brute-force descriptor matcher.
Definition: features2d.hpp:913
InputOutputArray noArray()
XML/YAML file storage class that encapsulates all the information necessary for writing or reading da...
Definition: persistence.hpp:298
Template class for smart pointers with shared ownership.
Definition: cvstd.hpp:283
virtual void detectAndCompute(InputArray image, InputArray mask, std::vector< KeyPoint > &keypoints, OutputArray descriptors, bool useProvidedKeypoints=false)
for i
Definition: modelConvert.m:63
void pow(InputArray src, double power, OutputArray dst)
Raises every array element to a power.
int trainIdx
Definition: types.hpp:736
int main(int argc, const char *argv[])
Definition: facerec_demo.cpp:67
n-dimensional dense array class
Definition: mat.hpp:726
_Tp & at(int i0=0)
Returns a reference to the specified array element.
Class for matching keypoint descriptors.
Definition: types.hpp:728