opencv/samples/cpp/video_homography.cpp

235 lines
6.4 KiB
C++
Raw Normal View History

/*
2010-12-21 19:37:08 +08:00
* video_homography.cpp
*
* Created on: Oct 18, 2010
* Author: erublee
*/
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/features2d/features2d.hpp"
#include <iostream>
#include <list>
#include <vector>
using namespace std;
using namespace cv;
2012-06-08 01:21:29 +08:00
static void help(char **av)
2010-12-01 09:59:39 +08:00
{
2010-12-21 19:37:08 +08:00
cout << "\nThis program demonstrated the use of features2d with the Fast corner detector and brief descriptors\n"
<< "to track planar objects by computing their homography from the key (training) image to the query (test) image\n\n" << endl;
2010-12-01 09:59:39 +08:00
cout << "usage: " << av[0] << " <video device number>\n" << endl;
cout << "The following keys do stuff:" << endl;
cout << " t : grabs a reference frame to match against" << endl;
cout << " l : makes the reference frame new every frame" << endl;
cout << " q or escape: quit" << endl;
}
namespace
{
2010-12-21 19:37:08 +08:00
void drawMatchesRelative(const vector<KeyPoint>& train, const vector<KeyPoint>& query,
std::vector<cv::DMatch>& matches, Mat& img, const vector<unsigned char>& mask = vector<
unsigned char> ())
{
2010-12-21 19:37:08 +08:00
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;
2010-12-21 19:37:08 +08:00
cv::line(img, pt_new, pt_old, Scalar(125, 255, 125), 1);
cv::circle(img, pt_new, 2, Scalar(255, 0, 125), 1);
2010-12-21 19:37:08 +08:00
}
}
}
2010-12-21 19:37:08 +08:00
//Takes a descriptor and turns it into an xy point
void keypoints2points(const vector<KeyPoint>& in, vector<Point2f>& out)
{
out.clear();
out.reserve(in.size());
for (size_t i = 0; i < in.size(); ++i)
{
out.push_back(in[i].pt);
}
}
2010-12-21 19:37:08 +08:00
//Takes an xy point and appends that to a keypoint structure
void points2keypoints(const vector<Point2f>& in, vector<KeyPoint>& out)
{
out.clear();
out.reserve(in.size());
for (size_t i = 0; i < in.size(); ++i)
{
out.push_back(KeyPoint(in[i], 1));
}
}
2010-12-21 19:37:08 +08:00
//Uses computed homography H to warp original input points to new planar position
void warpKeypoints(const Mat& H, const vector<KeyPoint>& in, vector<KeyPoint>& out)
{
vector<Point2f> pts;
keypoints2points(in, pts);
vector<Point2f> pts_w(pts.size());
Mat m_pts_w(pts_w);
perspectiveTransform(Mat(pts), m_pts_w, H);
points2keypoints(pts_w, out);
}
2010-12-21 19:37:08 +08:00
//Converts matching indices to xy points
void matches2points(const vector<KeyPoint>& train, const vector<KeyPoint>& query,
const std::vector<cv::DMatch>& matches, std::vector<cv::Point2f>& pts_train,
std::vector<Point2f>& pts_query)
{
2010-12-21 19:37:08 +08:00
pts_train.clear();
pts_query.clear();
pts_train.reserve(matches.size());
pts_query.reserve(matches.size());
2010-12-21 19:37:08 +08:00
size_t i = 0;
2010-12-21 19:37:08 +08:00
for (; i < matches.size(); i++)
{
2010-12-21 19:37:08 +08:00
const DMatch & dmatch = matches[i];
2010-12-21 19:37:08 +08:00
pts_query.push_back(query[dmatch.queryIdx].pt);
pts_train.push_back(train[dmatch.trainIdx].pt);
2010-12-21 19:37:08 +08:00
}
2010-12-21 19:37:08 +08:00
}
2010-12-21 19:37:08 +08:00
void resetH(Mat&H)
{
H = Mat::eye(3, 3, CV_32FC1);
}
}
2010-12-01 09:59:39 +08:00
int main(int ac, char ** av)
{
2010-12-21 19:37:08 +08:00
if (ac != 2)
{
help(av);
return 1;
}
2010-12-21 19:37:08 +08:00
BriefDescriptorExtractor brief(32);
2010-12-21 19:37:08 +08:00
VideoCapture capture;
capture.open(atoi(av[1]));
if (!capture.isOpened())
{
help(av);
cout << "capture device " << atoi(av[1]) << " failed to open!" << endl;
return 1;
}
2010-12-21 19:37:08 +08:00
cout << "following keys do stuff:" << endl;
cout << "t : grabs a reference frame to match against" << endl;
cout << "l : makes the reference frame new every frame" << endl;
cout << "q or escape: quit" << endl;
2010-12-21 19:37:08 +08:00
Mat frame;
2010-12-21 19:37:08 +08:00
vector<DMatch> matches;
BFMatcher desc_matcher(brief.defaultNorm());
2010-12-21 19:37:08 +08:00
vector<Point2f> train_pts, query_pts;
vector<KeyPoint> train_kpts, query_kpts;
vector<unsigned char> match_mask;
2010-12-21 19:37:08 +08:00
Mat gray;
2010-12-21 19:37:08 +08:00
bool ref_live = true;
2010-12-21 19:37:08 +08:00
Mat train_desc, query_desc;
const int DESIRED_FTRS = 500;
2013-08-13 22:49:29 +08:00
GridAdaptedFeatureDetector detector(makePtr<FastFeatureDetector>(10, true), DESIRED_FTRS, 4, 4);
2010-12-21 19:37:08 +08:00
Mat H_prev = Mat::eye(3, 3, CV_32FC1);
for (;;)
{
2010-12-21 19:37:08 +08:00
capture >> frame;
if (frame.empty())
break;
cvtColor(frame, gray, COLOR_RGB2GRAY);
2010-12-21 19:37:08 +08:00
detector.detect(gray, query_kpts); //Find interest points
2010-12-21 19:37:08 +08:00
brief.compute(gray, query_kpts, query_desc); //Compute brief descriptors at each keypoint location
2010-12-21 19:37:08 +08:00
if (!train_kpts.empty())
{
2010-12-21 19:37:08 +08:00
vector<KeyPoint> test_kpts;
warpKeypoints(H_prev.inv(), query_kpts, test_kpts);
Mat mask = windowedMatchingMask(test_kpts, train_kpts, 25, 25);
desc_matcher.match(query_desc, train_desc, matches, mask);
drawKeypoints(frame, test_kpts, frame, Scalar(255, 0, 0), DrawMatchesFlags::DRAW_OVER_OUTIMG);
matches2points(train_kpts, query_kpts, matches, train_pts, query_pts);
if (matches.size() > 5)
{
2011-04-18 23:14:32 +08:00
Mat H = findHomography(train_pts, query_pts, RANSAC, 4, match_mask);
2010-12-21 19:37:08 +08:00
if (countNonZero(Mat(match_mask)) > 15)
{
H_prev = H;
}
else
resetH(H_prev);
drawMatchesRelative(train_kpts, query_kpts, matches, frame, match_mask);
}
else
resetH(H_prev);
}
else
2010-12-21 19:37:08 +08:00
{
H_prev = Mat::eye(3, 3, CV_32FC1);
Mat out;
drawKeypoints(gray, query_kpts, out);
frame = out;
}
2010-12-21 19:37:08 +08:00
imshow("frame", frame);
2010-12-21 19:37:08 +08:00
if (ref_live)
{
train_kpts = query_kpts;
query_desc.copyTo(train_desc);
}
char key = (char)waitKey(2);
switch (key)
{
case 'l':
ref_live = true;
resetH(H_prev);
break;
case 't':
ref_live = false;
train_kpts = query_kpts;
query_desc.copyTo(train_desc);
resetH(H_prev);
break;
case 27:
case 'q':
return 0;
break;
}
}
2010-12-21 19:37:08 +08:00
return 0;
}