1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/xfeatures2d/nonfree.hpp> #include <opencv2/features2d/features2d.hpp> using namespace cv; using namespace std;
int main( ) { Mat trainImage = imread("1.jpg"), trainImage_gray; imshow("src",trainImage); cvtColor(trainImage, trainImage_gray, COLOR_BGR2GRAY);
vector<KeyPoint> train_keyPoint; Mat trainDescriptor; Ptr<xfeatures2d::SURF> surf = xfeatures2d::SURF::create(80); surf->detectAndCompute(trainImage_gray, Mat(),train_keyPoint,trainDescriptor);
FlannBasedMatcher matcher; vector<Mat> train_desc_collection(1, trainDescriptor); matcher.add(train_desc_collection); matcher.train();
VideoCapture cap("tifa.mp4"); unsigned int frameCount = 0;
while(char(waitKey(1)) != 'q') { int64 time0 = getTickCount(); Mat testImage, testImage_gray; cap >> testImage; if(testImage.empty()) continue;
cvtColor(testImage, testImage_gray, COLOR_BGR2GRAY);
vector<KeyPoint> test_keyPoint; Mat testDescriptor; surf->detectAndCompute(testImage_gray, Mat(),test_keyPoint,testDescriptor);
vector<vector<DMatch> > matches; matcher.knnMatch(testDescriptor, matches, 2);
vector<DMatch> goodMatches; for(unsigned int i = 0; i < matches.size(); i++) { if(matches[i][0].distance < 0.6 * matches[i][1].distance) goodMatches.push_back(matches[i][0]); }
Mat dstImage; drawMatches(testImage, test_keyPoint, trainImage, train_keyPoint, goodMatches, dstImage); imshow("matched", dstImage);
cout << "当前帧率为:" << getTickFrequency() / (getTickCount() - time0) << endl; }
return 0; }
|