From a41ac8fd407db38f88ff2f6e5b0ad7b7e9fe3015 Mon Sep 17 00:00:00 2001 From: GilLevi Date: Wed, 13 May 2015 21:20:28 +0300 Subject: [PATCH 1/5] added sample code for LATCH --- .../tutorial_code/xfeatures2D/LATCH_match.cpp | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp diff --git a/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp b/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp new file mode 100644 index 0000000000..1967c7a26a --- /dev/null +++ b/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include + +// If you find this code useful, please add a reference to the following paper in your work: +// Gil Levi and Tal Hassner, "LATCH: Learned Arrangements of Three Patch Codes", arXiv preprint arXiv:1501.03719, 15 Jan. 2015 + +using namespace std; +using namespace cv; + +const float inlier_threshold = 2.5f; // Distance threshold to identify inliers +const float nn_match_ratio = 0.8f; // Nearest neighbor matching ratio + +int main(void) +{ + Mat img1 = imread("../data/graf1.png", IMREAD_GRAYSCALE); + Mat img2 = imread("../data/graf3.png", IMREAD_GRAYSCALE); + + + Mat homography; + FileStorage fs("../data/H1to3p.xml", FileStorage::READ); + + fs.getFirstTopLevelNode() >> homography; + + vector kpts1, kpts2; + Mat desc1, desc2; + + Ptr orb_detector = cv::ORB::create(10000); + + Ptr latch = xfeatures2d::LATCHDescriptorExtractor::create(); + + + orb_detector->detect(img1, kpts1); + latch->compute(img1, kpts1, desc1); + + orb_detector->detect(img2, kpts2); + latch->compute(img2, kpts2, desc2); + + BFMatcher matcher(NORM_HAMMING); + vector< vector > nn_matches; + matcher.knnMatch(desc1, desc2, nn_matches, 2); + + vector matched1, matched2, inliers1, inliers2; + vector good_matches; + for (size_t i = 0; i < nn_matches.size(); i++) { + DMatch first = nn_matches[i][0]; + float dist1 = nn_matches[i][0].distance; + float dist2 = nn_matches[i][1].distance; + + if (dist1 < nn_match_ratio * dist2) { + matched1.push_back(kpts1[first.queryIdx]); + matched2.push_back(kpts2[first.trainIdx]); + } + } + + for (unsigned i = 0; i < matched1.size(); i++) { + Mat col = Mat::ones(3, 1, CV_64F); + col.at(0) = matched1[i].pt.x; + col.at(1) = matched1[i].pt.y; + + col = homography * col; + col /= col.at(2); + double dist = sqrt(pow(col.at(0) - matched2[i].pt.x, 2) + + pow(col.at(1) - matched2[i].pt.y, 2)); + + if (dist < inlier_threshold) { + int new_i = static_cast(inliers1.size()); + inliers1.push_back(matched1[i]); + inliers2.push_back(matched2[i]); + good_matches.push_back(DMatch(new_i, new_i, 0)); + } + } + + Mat res; + drawMatches(img1, inliers1, img2, inliers2, good_matches, res); + imwrite("../../samples/data/latch_res.png", res); + + double inlier_ratio = inliers1.size() * 1.0 / matched1.size(); + cout << "LATCH Matching Results" << endl; + cout << "*******************************" << endl; + cout << "# Keypoints 1: \t" << kpts1.size() << endl; + cout << "# Keypoints 2: \t" << kpts2.size() << endl; + cout << "# Matches: \t" << matched1.size() << endl; + cout << "# Inliers: \t" << inliers1.size() << endl; + cout << "# Inliers Ratio: \t" << inlier_ratio << endl; + cout << endl; + return 0; +} From f85eb23c3baf9a4019a71e5a6c9599617fd7948e Mon Sep 17 00:00:00 2001 From: GilLevi Date: Sat, 16 May 2015 21:15:09 +0300 Subject: [PATCH 2/5] sample for LATCH --- samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp b/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp index 1967c7a26a..08ee6c1e20 100644 --- a/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp +++ b/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp @@ -19,7 +19,7 @@ int main(void) Mat img1 = imread("../data/graf1.png", IMREAD_GRAYSCALE); Mat img2 = imread("../data/graf3.png", IMREAD_GRAYSCALE); - + Mat homography; FileStorage fs("../data/H1to3p.xml", FileStorage::READ); @@ -77,6 +77,7 @@ int main(void) Mat res; drawMatches(img1, inliers1, img2, inliers2, good_matches, res); imwrite("../../samples/data/latch_res.png", res); + double inlier_ratio = inliers1.size() * 1.0 / matched1.size(); cout << "LATCH Matching Results" << endl; From 6613519c2e26fdf0c2f59b61293028165cb950c7 Mon Sep 17 00:00:00 2001 From: GilLevi Date: Sat, 16 May 2015 22:37:38 +0300 Subject: [PATCH 3/5] added sample for LATCH --- .../tutorial_code/xfeatures2D/LATCH_match.cpp | 104 +++++++++--------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp b/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp index 08ee6c1e20..29f44b2f31 100644 --- a/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp +++ b/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp @@ -18,75 +18,75 @@ int main(void) { Mat img1 = imread("../data/graf1.png", IMREAD_GRAYSCALE); Mat img2 = imread("../data/graf3.png", IMREAD_GRAYSCALE); - - Mat homography; + + Mat homography; FileStorage fs("../data/H1to3p.xml", FileStorage::READ); - - fs.getFirstTopLevelNode() >> homography; + + fs.getFirstTopLevelNode() >> homography; vector kpts1, kpts2; Mat desc1, desc2; - Ptr orb_detector = cv::ORB::create(10000); + Ptr orb_detector = cv::ORB::create(10000); - Ptr latch = xfeatures2d::LATCHDescriptorExtractor::create(); + Ptr latch = xfeatures2d::LATCHDescriptorExtractor::create(); - orb_detector->detect(img1, kpts1); - latch->compute(img1, kpts1, desc1); + orb_detector->detect(img1, kpts1); + latch->compute(img1, kpts1, desc1); - orb_detector->detect(img2, kpts2); - latch->compute(img2, kpts2, desc2); + orb_detector->detect(img2, kpts2); + latch->compute(img2, kpts2, desc2); - BFMatcher matcher(NORM_HAMMING); - vector< vector > nn_matches; - matcher.knnMatch(desc1, desc2, nn_matches, 2); + BFMatcher matcher(NORM_HAMMING); + vector< vector > nn_matches; + matcher.knnMatch(desc1, desc2, nn_matches, 2); - vector matched1, matched2, inliers1, inliers2; - vector good_matches; - for (size_t i = 0; i < nn_matches.size(); i++) { - DMatch first = nn_matches[i][0]; - float dist1 = nn_matches[i][0].distance; - float dist2 = nn_matches[i][1].distance; + vector matched1, matched2, inliers1, inliers2; + vector good_matches; + for (size_t i = 0; i < nn_matches.size(); i++) { + DMatch first = nn_matches[i][0]; + float dist1 = nn_matches[i][0].distance; + float dist2 = nn_matches[i][1].distance; - if (dist1 < nn_match_ratio * dist2) { - matched1.push_back(kpts1[first.queryIdx]); - matched2.push_back(kpts2[first.trainIdx]); - } - } + if (dist1 < nn_match_ratio * dist2) { + matched1.push_back(kpts1[first.queryIdx]); + matched2.push_back(kpts2[first.trainIdx]); + } + } - for (unsigned i = 0; i < matched1.size(); i++) { - Mat col = Mat::ones(3, 1, CV_64F); - col.at(0) = matched1[i].pt.x; - col.at(1) = matched1[i].pt.y; + for (unsigned i = 0; i < matched1.size(); i++) { + Mat col = Mat::ones(3, 1, CV_64F); + col.at(0) = matched1[i].pt.x; + col.at(1) = matched1[i].pt.y; - col = homography * col; - col /= col.at(2); - double dist = sqrt(pow(col.at(0) - matched2[i].pt.x, 2) + - pow(col.at(1) - matched2[i].pt.y, 2)); + col = homography * col; + col /= col.at(2); + double dist = sqrt(pow(col.at(0) - matched2[i].pt.x, 2) + + pow(col.at(1) - matched2[i].pt.y, 2)); - if (dist < inlier_threshold) { - int new_i = static_cast(inliers1.size()); - inliers1.push_back(matched1[i]); - inliers2.push_back(matched2[i]); - good_matches.push_back(DMatch(new_i, new_i, 0)); - } - } + if (dist < inlier_threshold) { + int new_i = static_cast(inliers1.size()); + inliers1.push_back(matched1[i]); + inliers2.push_back(matched2[i]); + good_matches.push_back(DMatch(new_i, new_i, 0)); + } + } - Mat res; - drawMatches(img1, inliers1, img2, inliers2, good_matches, res); - imwrite("../../samples/data/latch_res.png", res); - + Mat res; + drawMatches(img1, inliers1, img2, inliers2, good_matches, res); + imwrite("../../samples/data/latch_res.png", res); - double inlier_ratio = inliers1.size() * 1.0 / matched1.size(); - cout << "LATCH Matching Results" << endl; - cout << "*******************************" << endl; - cout << "# Keypoints 1: \t" << kpts1.size() << endl; - cout << "# Keypoints 2: \t" << kpts2.size() << endl; - cout << "# Matches: \t" << matched1.size() << endl; - cout << "# Inliers: \t" << inliers1.size() << endl; - cout << "# Inliers Ratio: \t" << inlier_ratio << endl; - cout << endl; + + double inlier_ratio = inliers1.size() * 1.0 / matched1.size(); + cout << "LATCH Matching Results" << endl; + cout << "*******************************" << endl; + cout << "# Keypoints 1: \t" << kpts1.size() << endl; + cout << "# Keypoints 2: \t" << kpts2.size() << endl; + cout << "# Matches: \t" << matched1.size() << endl; + cout << "# Inliers: \t" << inliers1.size() << endl; + cout << "# Inliers Ratio: \t" << inlier_ratio << endl; + cout << endl; return 0; } From 05a45ef6dc447b9a8d45de677be8074b451522ac Mon Sep 17 00:00:00 2001 From: GilLevi Date: Thu, 21 May 2015 00:04:24 +0300 Subject: [PATCH 4/5] renamed LATCH --- samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp b/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp index 29f44b2f31..b1886744dd 100644 --- a/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp +++ b/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp @@ -22,7 +22,7 @@ int main(void) Mat homography; FileStorage fs("../data/H1to3p.xml", FileStorage::READ); - + fs.getFirstTopLevelNode() >> homography; vector kpts1, kpts2; @@ -30,7 +30,7 @@ int main(void) Ptr orb_detector = cv::ORB::create(10000); - Ptr latch = xfeatures2d::LATCHDescriptorExtractor::create(); + Ptr latch = xfeatures2d::LATCH::create(); orb_detector->detect(img1, kpts1); From a4e5b09ea44147386f5ed155c941b6178d2dfbbd Mon Sep 17 00:00:00 2001 From: GilLevi Date: Mon, 25 May 2015 01:28:05 +0300 Subject: [PATCH 5/5] fixed whitespace issue --- samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp b/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp index b1886744dd..a9413b871b 100644 --- a/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp +++ b/samples/cpp/tutorial_code/xfeatures2D/LATCH_match.cpp @@ -22,7 +22,7 @@ int main(void) Mat homography; FileStorage fs("../data/H1to3p.xml", FileStorage::READ); - + fs.getFirstTopLevelNode() >> homography; vector kpts1, kpts2;