comparison c/feature-based-tracking.cpp @ 122:654f1c748644

work on displaying matched features
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 15 Aug 2011 18:37:14 -0400
parents c4d4b5b93add
children df3bdd8e50ba
comparison
equal deleted inserted replaced
121:c4d4b5b93add 122:654f1c748644
1 #include "../include/Feature.hpp" 1 #include "Feature.hpp"
2 #include "utils.hpp"
2 3
3 #include "opencv2/highgui/highgui.hpp" 4 #include "opencv2/highgui/highgui.hpp"
4 //#include "opencv2/imgproc/imgproc.hpp" 5 //#include "opencv2/imgproc/imgproc.hpp"
5 #include "opencv2/features2d/features2d.hpp" 6 #include "opencv2/features2d/features2d.hpp"
6 7
18 int main(int argc, char *argv[]) { 19 int main(int argc, char *argv[]) {
19 20
20 BriefDescriptorExtractor brief(32); 21 BriefDescriptorExtractor brief(32);
21 22
22 VideoCapture capture; 23 VideoCapture capture;
23 capture.open(atoi(argv[1])); 24
25 Mat frame, display;
26
27 if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) // if no parameter or number parameter
28 capture.open(argc == 2 ? argv[1][0] - '0' : 0);
29 else if( argc >= 2 )
30 {
31 capture.open(argv[1]);
32 if( capture.isOpened() )
33 cout << "Video " << argv[1] <<
34 ": width=" << capture.get(CV_CAP_PROP_FRAME_WIDTH) <<
35 ", height=" << capture.get(CV_CAP_PROP_FRAME_HEIGHT) <<
36 ", nframes=" << capture.get(CV_CAP_PROP_FRAME_COUNT) << endl;
37 if( argc > 2 && isdigit(argv[2][0]) ) // could be used to reach first frame, dumping library messages to log file (2> /tmp/log.txt)
38 {
39 int pos;
40 sscanf(argv[2], "%d", &pos);
41 cout << "seeking to frame #" << pos << endl;
42 //cap.set(CV_CAP_PROP_POS_FRAMES, pos);
43 for (int i=0; i<pos; i++)
44 capture >> frame;
45 }
46 }
47
48 // capture.open(atoi(argv[1]));
24 if (!capture.isOpened()) 49 if (!capture.isOpened())
25 { 50 {
26 //help(argv); 51 //help(argv);
27 cout << "capture device " << atoi(argv[1]) << " failed to open!" << endl; 52 cout << "capture device " << argv[1] << " failed to open!" << endl;
28 return 1; 53 return 1;
29 } 54 }
30 55
31 Mat frame;
32
33 vector<DMatch> matches; 56 vector<DMatch> matches;
34 57
35 BruteForceMatcher<Hamming> desc_matcher; 58 BruteForceMatcher<Hamming> desc_matcher;
36 59
37 vector<Point2f> train_pts, query_pts; 60 vector<Point2f> train_pts, query_pts;
42 65
43 Mat train_desc, query_desc; 66 Mat train_desc, query_desc;
44 const int DESIRED_FTRS = 500; 67 const int DESIRED_FTRS = 500;
45 GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4); 68 GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4);
46 69
70 int frameNum = 0;
47 for (;;) 71 for (;;)
48 { 72 {
73 frameNum+=2;
49 capture >> frame; 74 capture >> frame;
75 cout << capture.get(CV_CAP_PROP_POS_FRAMES) << endl;
50 if (frame.empty()) 76 if (frame.empty())
51 break; 77 break;
52 78
53 cvtColor(frame, gray, CV_RGB2GRAY); 79 cvtColor(frame, gray, CV_RGB2GRAY);
54 80
55 detector.detect(gray, query_kpts); //Find interest points 81 detector.detect(gray, query_kpts); //Find interest points
56 82
57 brief.compute(gray, query_kpts, query_desc); //Compute brief descriptors at each keypoint location 83 brief.compute(gray, query_kpts, query_desc); //Compute brief descriptors at each keypoint location
58 84
59 // find how keypoints descriptions are matched to previous ones (in train kpts probably) 85 // find how keypoints descriptions are matched to previous ones (in train kpts probably)
60 86 display = frame.clone();
61 imshow("frame", frame); 87 if (!train_kpts.empty())
62 char key = (char)waitKey(2); 88 {
89 //vector<KeyPoint> test_kpts;
90 //warpKeypoints(H_prev.inv(), query_kpts, test_kpts);
91 //Mat mask = windowedMatchingMask(test_kpts, train_kpts, 25, 25);
92 desc_matcher.match(query_desc, train_desc, matches);
93 drawMatches(frame, train_kpts, frame, query_kpts, matches, display);//, Scalar::all(-1), Scalar::all(-1), vector<vector<char> >(), DrawMatchesFlags::DRAW_OVER_OUTIMG);
94 } // TODO do something like the match relative of the sample
95
96 imshow("frame", display);
97 train_kpts = query_kpts;
98 int key = waitKey(0);
99 if (::interruptionKey(key))
100 break;
63 } 101 }
64 102
65 Feature f; 103 Feature f;
66 104
67 return 0; 105 return 0;