24,855
社区成员
发帖
与我相关
我的任务
分享
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <iostream>
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
int main()
{
system("color 5F");
Mat trainImage = imread("D:\\O\\cst\\07.jpg");
imshow("原始图", trainImage);
Mat trainImage_gray;
cvtColor(trainImage, trainImage_gray, CV_BGR2GRAY);
vector<KeyPoint> train_keyPoint;
Mat trainDescription;
SiftFeatureDetector featureDetetor;
featureDetetor.detect(trainImage_gray, train_keyPoint);
SiftDescriptorExtractor featureExtractor;
featureDetetor.compute(trainImage_gray, train_keyPoint, trainDescription);
BFMatcher matcher;
vector<Mat> train_desc_collectin(1, trainDescription);
matcher.add(train_desc_collectin);
matcher.train();
VideoCapture cap(0);
unsigned int frameCount = 0;
while (char(waitKey(1)) != 'q')
{
double time0 = getTickCount();
Mat captureImage, captureImage_gray;
cap >> captureImage;
if (captureImage.empty())
continue;
cvtColor(captureImage, captureImage_gray, CV_BGR2GRAY);
vector<KeyPoint> test_keyPoint;
Mat testDescriptor;
featureDetetor.detect(captureImage_gray, test_keyPoint);
featureExtractor.compute(captureImage_gray, test_keyPoint, testDescriptor);
vector<vector<DMatch>> matches;
matcher.knnMatch(testDescriptor, matches, 2);
vector<DMatch> goodMatches;
for (double k = 0; k < matches.size(); k++)
{
if (matches[k][0].distance < 0.6 * matches[k][1].distance)
goodMatches.push_back(matches[k][0]);
}
Mat dstImage;
drawMatches(captureImage, test_keyPoint, trainImage, train_keyPoint, goodMatches, dstImage);
imshow("匹配窗口", dstImage);
cout << "当前帧率为:" << getTickFrequency() / (getTickCount() - time0) << endl;
}
return 0;
}