从这一节开始学习OpenCV并使用它实现PTAM 的另类版本:Feature Tracking and Synchronous Scene Generation with a Single Camera主要思想还是parallel,两个线程tracking和mapping并行运行。这里我将mapping简化为generation,细节以后会做具体介绍,今天开始记录我的研究历程。
上一节中我们选择Orb算法因为它速度快,性能佳,性价比极高。这一节第一部分我们首先使用Orb算法对两幅静态图像进行角点检测然后使用BF(Brute-Force)匹配并画出匹配点。第二部分我们从摄像头里得到的每一帧进行角点检测并作BF匹配。(我使用的OpenCV版本是2.4.10,关于OpenCV配置,感谢浅墨大神的入门教程)
第一部分:
Orb算法里简单的实现了检测和匹配。因为我们要进行的实时匹配,所以每两帧图相间的差别很小,镜头或物体在极短时间内属于小运动,位移差别不大,这正好为我们提供了过滤错误匹配的方法。如下面代码里我们说明若匹配点对中的点相隔距离大于30,我们即认为匹配错误。
#include <core/core.hpp>
#include <highgui/highgui.hpp>
#include <features2d/features2d.hpp>
#include <iostream>
#include <windows.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat img_1 = imread("C:\\Users\\柴\\Pictures\\Logitech Webcam\\Picture 9.jpg");
Mat img_2 = imread("C:\\Users\\柴\\Pictures\\Logitech Webcam\\Picture 10.jpg");
namedWindow("Matches");
DWORD t1,t2;
t1 = GetTickCount();
vector<KeyPoint> keypoints_1,keypoints_2;
ORB orb;
orb.detect(img_1, keypoints_1);
orb.detect(img_2, keypoints_2);
Mat descriptors_1, descriptors_2;
orb.compute(img_1, keypoints_1, descriptors_1);
orb.compute(img_2, keypoints_2, descriptors_2);
BFMatcher matcher(NORM_HAMMING);
vector<Mat> descriptors;
descriptors.push_back(descriptors_1);
matcher.add(descriptors);
vector<vector<DMatch>> matches,goodmatches;
DMatch bestMatch,betterMatch;
vector<DMatch> bestMatches;
matcher.knnMatch(descriptors_2,matches,2);
int n=0;
Point p1,p2;
for (int i=0; i<(int)matches.size(); i++)
{
bestMatch = matches[i][0];
betterMatch = matches[i][1];
p1 = keypoints_1[bestMatch.trainIdx].pt;
p2 = keypoints_2[bestMatch.queryIdx].pt;
double distance = sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.