Add Java and Python code for AKAZE local features matching tutorial. Fix incorrect uses of Mat.mul() in Java code.
Uniform Lowe's ratio test in the code.
This commit is contained in:
@@ -6,11 +6,12 @@
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
const float inlier_threshold = 2.5f; // Distance threshold to identify inliers
|
||||
const float inlier_threshold = 2.5f; // Distance threshold to identify inliers with homography check
|
||||
const float nn_match_ratio = 0.8f; // Nearest neighbor matching ratio
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
//! [load]
|
||||
CommandLineParser parser(argc, argv,
|
||||
"{@img1 | ../data/graf1.png | input image 1}"
|
||||
"{@img2 | ../data/graf3.png | input image 2}"
|
||||
@@ -21,20 +22,25 @@ int main(int argc, char* argv[])
|
||||
Mat homography;
|
||||
FileStorage fs(parser.get<String>("@homography"), FileStorage::READ);
|
||||
fs.getFirstTopLevelNode() >> homography;
|
||||
//! [load]
|
||||
|
||||
//! [AKAZE]
|
||||
vector<KeyPoint> kpts1, kpts2;
|
||||
Mat desc1, desc2;
|
||||
|
||||
Ptr<AKAZE> akaze = AKAZE::create();
|
||||
akaze->detectAndCompute(img1, noArray(), kpts1, desc1);
|
||||
akaze->detectAndCompute(img2, noArray(), kpts2, desc2);
|
||||
//! [AKAZE]
|
||||
|
||||
//! [2-nn matching]
|
||||
BFMatcher matcher(NORM_HAMMING);
|
||||
vector< vector<DMatch> > nn_matches;
|
||||
matcher.knnMatch(desc1, desc2, nn_matches, 2);
|
||||
//! [2-nn matching]
|
||||
|
||||
vector<KeyPoint> matched1, matched2, inliers1, inliers2;
|
||||
vector<DMatch> good_matches;
|
||||
//! [ratio test filtering]
|
||||
vector<KeyPoint> matched1, matched2;
|
||||
for(size_t i = 0; i < nn_matches.size(); i++) {
|
||||
DMatch first = nn_matches[i][0];
|
||||
float dist1 = nn_matches[i][0].distance;
|
||||
@@ -45,8 +51,12 @@ int main(int argc, char* argv[])
|
||||
matched2.push_back(kpts2[first.trainIdx]);
|
||||
}
|
||||
}
|
||||
//! [ratio test filtering]
|
||||
|
||||
for(unsigned i = 0; i < matched1.size(); i++) {
|
||||
//! [homography check]
|
||||
vector<DMatch> good_matches;
|
||||
vector<KeyPoint> inliers1, inliers2;
|
||||
for(size_t i = 0; i < matched1.size(); i++) {
|
||||
Mat col = Mat::ones(3, 1, CV_64F);
|
||||
col.at<double>(0) = matched1[i].pt.x;
|
||||
col.at<double>(1) = matched1[i].pt.y;
|
||||
@@ -63,12 +73,14 @@ int main(int argc, char* argv[])
|
||||
good_matches.push_back(DMatch(new_i, new_i, 0));
|
||||
}
|
||||
}
|
||||
//! [homography check]
|
||||
|
||||
//! [draw final matches]
|
||||
Mat res;
|
||||
drawMatches(img1, inliers1, img2, inliers2, good_matches, res);
|
||||
imwrite("akaze_result.png", res);
|
||||
|
||||
double inlier_ratio = inliers1.size() * 1.0 / matched1.size();
|
||||
double inlier_ratio = inliers1.size() / (double) matched1.size();
|
||||
cout << "A-KAZE Matching Results" << endl;
|
||||
cout << "*******************************" << endl;
|
||||
cout << "# Keypoints 1: \t" << kpts1.size() << endl;
|
||||
@@ -80,6 +92,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
imshow("result", res);
|
||||
waitKey();
|
||||
//! [draw final matches]
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+1
-1
@@ -46,7 +46,7 @@ int main( int argc, char* argv[] )
|
||||
std::vector<DMatch> good_matches;
|
||||
for (size_t i = 0; i < knn_matches.size(); i++)
|
||||
{
|
||||
if (knn_matches[i].size() > 1 && knn_matches[i][0].distance / knn_matches[i][1].distance <= ratio_thresh)
|
||||
if (knn_matches[i][0].distance < ratio_thresh * knn_matches[i][1].distance)
|
||||
{
|
||||
good_matches.push_back(knn_matches[i][0]);
|
||||
}
|
||||
|
||||
+1
-1
@@ -48,7 +48,7 @@ int main( int argc, char* argv[] )
|
||||
std::vector<DMatch> good_matches;
|
||||
for (size_t i = 0; i < knn_matches.size(); i++)
|
||||
{
|
||||
if (knn_matches[i].size() > 1 && knn_matches[i][0].distance / knn_matches[i][1].distance <= ratio_thresh)
|
||||
if (knn_matches[i][0].distance < ratio_thresh * knn_matches[i][1].distance)
|
||||
{
|
||||
good_matches.push_back(knn_matches[i][0]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user