From 5915e4c7ee03fdeb3268283159d0a205f8d8d1d1 Mon Sep 17 00:00:00 2001 From: Ethan Rublee Date: Sun, 14 Nov 2010 06:28:41 +0000 Subject: [PATCH] adding samples for brief and the cout << cv::Mat functions. --- samples/cpp/brief_match_test.cpp | 118 +++++++++++++++++ samples/cpp/cvout_sample.cpp | 34 +++++ samples/cpp/video_homography.cpp | 214 +++++++++++++++++++++++++++++++ 3 files changed, 366 insertions(+) create mode 100644 samples/cpp/brief_match_test.cpp create mode 100644 samples/cpp/cvout_sample.cpp create mode 100644 samples/cpp/video_homography.cpp diff --git a/samples/cpp/brief_match_test.cpp b/samples/cpp/brief_match_test.cpp new file mode 100644 index 0000000000..81f517d433 --- /dev/null +++ b/samples/cpp/brief_match_test.cpp @@ -0,0 +1,118 @@ +/* + * matching_test.cpp + * + * Created on: Oct 17, 2010 + * Author: ethan + */ +#include +#include +#include + +using namespace cv; + +using std::cout; +using std::cerr; +using std::endl; +using std::vector; + +void matches2points(const vector& matches, const vector& kpts_train, + const vector& kpts_query, vector& pts_train, vector& pts_query) +{ + pts_train.clear(); + pts_query.clear(); + pts_train.reserve(matches.size()); + pts_query.reserve(matches.size()); + for (size_t i = 0; i < matches.size(); i++) + { + const DMatch& match = matches[i]; + pts_query.push_back(kpts_query[match.queryIdx].pt); + pts_train.push_back(kpts_train[match.trainIdx].pt); + } + +} + +float match(const vector& kpts_train, const vector& kpts_query, DescriptorMatcher& matcher, + const Mat& train, const Mat& query, vector& matches) +{ + + float t = (double)getTickCount(); + matcher.match(query, train, matches); + return ((double)getTickCount() - t) / getTickFrequency(); +} + +int main(int ac, char ** av) +{ + if (ac != 3) + { + cerr << "usage: " << av[0] << " im1.jpg im2.jpg" << endl; + return 1; + } + string im1_name, im2_name; + im1_name = av[1]; + im2_name = av[2]; + + Mat im1 = imread(im1_name, CV_LOAD_IMAGE_GRAYSCALE); + Mat im2 = imread(im2_name, CV_LOAD_IMAGE_GRAYSCALE); + + if (im1.empty() || im2.empty()) + { + cerr << "could not open one of the images..." << endl; + return 1; + } + + double t = (double)getTickCount(); + + FastFeatureDetector detector(50); + BriefDescriptorExtractor extractor(32); + + vector kpts_1, kpts_2; + detector.detect(im1, kpts_1); + detector.detect(im2, kpts_2); + + t = ((double)getTickCount() - t) / getTickFrequency(); + + cout << "found " << kpts_1.size() << " keypoints in " << im1_name << endl << "fount " << kpts_2.size() + << " keypoints in " << im2_name << endl << "took " << t << " seconds." << endl; + + Mat desc_1, desc_2; + + cout << "computing descriptors..." << endl; + + t = (double)getTickCount(); + + extractor.compute(im1, kpts_1, desc_1); + extractor.compute(im2, kpts_2, desc_2); + + t = ((double)getTickCount() - t) / getTickFrequency(); + + cout << "done computing descriptors... took " << t << " seconds" << endl; + + cout << "matching with BruteForceMatcher" << endl; + BruteForceMatcher matcher; + vector matches_lut; + float lut_time = match(kpts_1, kpts_2, matcher, desc_1, desc_2, matches_lut); + cout << "done BruteForceMatcher matching. took " << lut_time << " seconds" << endl; + + cout << "matching with BruteForceMatcher" << endl; + BruteForceMatcher matcher_popcount; + vector matches_popcount; + float pop_time = match(kpts_1, kpts_2, matcher_popcount, desc_1, desc_2, matches_popcount); + cout << "done BruteForceMatcher matching. took " << pop_time << " seconds" << endl; + + vector mpts_1, mpts_2; + matches2points(matches_popcount, kpts_1, kpts_2, mpts_1, mpts_2); + vector outlier_mask; + Mat H = findHomography(Mat(mpts_2), Mat(mpts_1), outlier_mask, RANSAC, 1); + + Mat outimg; + drawMatches(im2, kpts_2, im1, kpts_1, matches_popcount, outimg, Scalar::all(-1), Scalar::all(-1), + reinterpret_cast&> (outlier_mask)); + imshow("matches - popcount - outliers removed", outimg); + + Mat warped; + warpPerspective(im2, warped, H, im1.size()); + imshow("warped", warped); + imshow("diff", im1 - warped); + waitKey(); + return 0; +} diff --git a/samples/cpp/cvout_sample.cpp b/samples/cpp/cvout_sample.cpp new file mode 100644 index 0000000000..86e703459b --- /dev/null +++ b/samples/cpp/cvout_sample.cpp @@ -0,0 +1,34 @@ +#include "opencv2/core/core.hpp" + +using namespace std; +using namespace cv; + +int main() +{ + Mat i = Mat::eye(4, 4, CV_32F); + cout << "i = " << i << ";" << endl; + + Mat r = Mat(10, 10, CV_8UC1); + randu(r, Scalar(0), Scalar(255)); + + cout << "r = " << r << ";" << endl; + + Point2f p(5, 1); + cout << "p = " << p << ";" << endl; + + Point3f p3f(2, 6, 7); + cout << "p3f = " << p3f << ";" << endl; + + vector points(20); + for (size_t i = 0; i < points.size(); ++i) + { + points[i] = Point2f(i * 5, i % 7); + } + cout << "points = " << points << ";" << endl; + + cout << "#csv" << endl; + + writeCSV(cout, r); + + return 1; +} diff --git a/samples/cpp/video_homography.cpp b/samples/cpp/video_homography.cpp new file mode 100644 index 0000000000..7324a517c2 --- /dev/null +++ b/samples/cpp/video_homography.cpp @@ -0,0 +1,214 @@ +/* + * video_homography.cpp + * + * Created on: Oct 18, 2010 + * Author: erublee + */ + +#include +#include +#include +#include + +using namespace std; +using namespace cv; + +namespace +{ +void drawMatchesRelative(const vector& train, const vector& query, + std::vector& matches, Mat& img, const vector& mask = vector< + unsigned char> ()) +{ + for (int i = 0; i < (int)matches.size(); i++) + { + if (mask.empty() || mask[i]) + { + Point2f pt_new = query[matches[i].queryIdx].pt; + Point2f pt_old = train[matches[i].trainIdx].pt; + Point2f dist = pt_new - pt_old; + + cv::line(img, pt_new, pt_old, Scalar(125, 255, 125), 1); + cv::circle(img, pt_new, 2, Scalar(255, 0, 125), 1); + + } + } +} + +void keypoints2points(const vector& in, vector& out) +{ + out.clear(); + out.reserve(in.size()); + for (size_t i = 0; i < in.size(); ++i) + { + out.push_back(in[i].pt); + } +} + +void points2keypoints(const vector& in, vector& out) +{ + out.clear(); + out.reserve(in.size()); + for (size_t i = 0; i < in.size(); ++i) + { + out.push_back(KeyPoint(in[i], 1)); + } +} + +void warpKeypoints(const Mat& H, const vector& in, vector& out) +{ + vector pts; + keypoints2points(in, pts); + vector pts_w(pts.size()); + Mat m_pts_w(pts_w); + perspectiveTransform(Mat(pts), m_pts_w, H); + points2keypoints(pts_w, out); +} + +void matches2points(const vector& train, const vector& query, + const std::vector& matches, std::vector& pts_train, + std::vector& pts_query) +{ + + pts_train.clear(); + pts_query.clear(); + pts_train.reserve(matches.size()); + pts_query.reserve(matches.size()); + + size_t i = 0; + + for (; i < matches.size(); i++) + { + + const DMatch & dmatch = matches[i]; + + pts_query.push_back(query[dmatch.queryIdx].pt); + pts_train.push_back(train[dmatch.trainIdx].pt); + + } + +} + +void resetH(Mat&H) +{ + H = Mat::eye(3, 3, CV_32FC1); +} +} +int main(int ac, char ** av) +{ + + if (ac != 2) + { + cout << "usage: " << av[0] << "