Mercurial > hg > nsaunier > traffic-intelligence
comparison c/feature-based-tracking.cpp @ 128:536510f60854
new features generated as needed
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 17 Aug 2011 02:44:28 -0400 |
| parents | d19d6e63dd77 |
| children | 2a6e7a9a5c53 |
comparison
equal
deleted
inserted
replaced
| 127:d19d6e63dd77 | 128:536510f60854 |
|---|---|
| 70 params.derivLambda = 0.5; | 70 params.derivLambda = 0.5; |
| 71 Size window = Size(params.windowSize, params.windowSize); | 71 Size window = Size(params.windowSize, params.windowSize); |
| 72 | 72 |
| 73 BruteForceMatcher<Hamming> descMatcher; | 73 BruteForceMatcher<Hamming> descMatcher; |
| 74 vector<DMatch> matches; | 74 vector<DMatch> matches; |
| 75 Size videoSize; | |
| 75 | 76 |
| 76 if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) // if no parameter or number parameter | 77 if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) // if no parameter or number parameter |
| 77 capture.open(argc == 2 ? argv[1][0] - '0' : 0); | 78 capture.open(argc == 2 ? argv[1][0] - '0' : 0); |
| 78 else if( argc >= 2 ) | 79 else if( argc >= 2 ) |
| 79 { | 80 { |
| 80 capture.open(argv[1]); | 81 capture.open(argv[1]); |
| 81 if( capture.isOpened() ) | 82 if( capture.isOpened() ) |
| 83 videoSize = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)); | |
| 82 cout << "Video " << argv[1] << | 84 cout << "Video " << argv[1] << |
| 83 ": width=" << capture.get(CV_CAP_PROP_FRAME_WIDTH) << | 85 ": width=" << videoSize.width << |
| 84 ", height=" << capture.get(CV_CAP_PROP_FRAME_HEIGHT) << | 86 ", height=" << videoSize.height << |
| 85 ", nframes=" << capture.get(CV_CAP_PROP_FRAME_COUNT) << endl; | 87 ", nframes=" << capture.get(CV_CAP_PROP_FRAME_COUNT) << endl; |
| 86 if( argc > 2 && isdigit(argv[2][0]) ) // could be used to reach first frame, dumping library messages to log file (2> /tmp/log.txt) | 88 if( argc > 2 && isdigit(argv[2][0]) ) // could be used to reach first frame, dumping library messages to log file (2> /tmp/log.txt) |
| 87 { | 89 { |
| 88 sscanf(argv[2], "%d", ¶ms.frame1); | 90 sscanf(argv[2], "%d", ¶ms.frame1); |
| 89 cout << "seeking to frame #" << params.frame1 << endl; | 91 cout << "seeking to frame #" << params.frame1 << endl; |
| 104 vector<KeyPoint> prevKpts, currKpts; | 106 vector<KeyPoint> prevKpts, currKpts; |
| 105 vector<Point2f> prevPts, currPts; | 107 vector<Point2f> prevPts, currPts; |
| 106 vector<uchar> status; | 108 vector<uchar> status; |
| 107 vector<float> errors; | 109 vector<float> errors; |
| 108 Mat prevDesc, currDesc; | 110 Mat prevDesc, currDesc; |
| 111 | |
| 109 | 112 |
| 110 // TODO structure de donnee paires pointeur trajectory, numero de keypoint | 113 // TODO structure de donnee paires pointeur trajectory, numero de keypoint |
| 111 int key = '?'; | 114 int key = '?'; |
| 112 for (int frameNum = 0; ((params.frame1+frameNum < params.nFrames) || (params.nFrames < 0)) && !::interruptionKey(key); frameNum++) { | 115 for (int frameNum = params.frame1; ((params.frame1+frameNum < params.nFrames) || (params.nFrames < 0)) && !::interruptionKey(key); frameNum++) { |
| 113 capture >> frame; | 116 capture >> frame; |
| 114 cout << capture.get(CV_CAP_PROP_POS_FRAMES) << endl; | 117 cout << capture.get(CV_CAP_PROP_POS_FRAMES) << " " << prevPts.size() << endl; |
| 115 while (frame.empty()) | 118 while (frame.empty()) |
| 116 capture >> frame;//break; | 119 capture >> frame;//break; |
| 117 | 120 |
| 118 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); | 121 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); |
| 119 | 122 |
| 120 detector.detect(currentFrameBW, currKpts); | 123 if (!prevPts.empty()) { |
| 121 //cout << currKpts.size() << " kpts" << endl; | 124 //::keyPoints2Points(prevKpts, prevPts); |
| 122 | |
| 123 brief.compute(currentFrameBW, currKpts, currDesc); //Compute brief descriptors at each keypoint location | |
| 124 | |
| 125 if (!prevKpts.empty()) { | |
| 126 ::keyPoints2Points(prevKpts, prevPts); | |
| 127 currPts.clear(); | 125 currPts.clear(); |
| 128 calcOpticalFlowPyrLK(previousFrameBW, currentFrameBW, prevPts, currPts, status, errors, window, params.pyramidLevel, TermCriteria(3 /*static_cast<int>(TermCriteria::COUNT)+static_cast<int>(TermCriteria::EPS)*/, params.maxNumberTrackingIterations, params.minTrackingError), params.derivLambda, 0); // OPTFLOW_USE_INITIAL_FLOW | 126 calcOpticalFlowPyrLK(previousFrameBW, currentFrameBW, prevPts, currPts, status, errors, window, params.pyramidLevel, TermCriteria(3 /*static_cast<int>(TermCriteria::COUNT)+static_cast<int>(TermCriteria::EPS)*/, params.maxNumberTrackingIterations, params.minTrackingError), params.derivLambda, 0); // OPTFLOW_USE_INITIAL_FLOW |
| 127 | |
| 129 drawOpticalFlow(prevPts, currPts, status, frame); | 128 drawOpticalFlow(prevPts, currPts, status, frame); |
| 130 | 129 |
| 130 vector<Point2f> trackedPts; | |
| 131 for (unsigned int i=0; i<status.size(); i++) | |
| 132 if (status[i]) | |
| 133 trackedPts.push_back(currPts[i]); | |
| 134 currPts = trackedPts; | |
| 135 | |
| 131 // cout << matches.size() << " matches" << endl; | 136 // cout << matches.size() << " matches" << endl; |
| 132 // descMatcher.match(currDesc, prevDesc, matches); | 137 // descMatcher.match(currDesc, prevDesc, matches); |
| 133 // cout << matches.size() << " matches" << endl; | 138 // cout << matches.size() << " matches" << endl; |
| 134 drawMatchesRelative(prevKpts, currKpts, matches, frame); | 139 //drawMatchesRelative(prevKpts, currKpts, matches, frame); |
| 135 //drawMatches(frame, prevKpts, frame, currKpts, matches, display);//, Scalar::all(-1), Scalar::all(-1), vector<vector<char> >(), DrawMatchesFlags::DRAW_OVER_OUTIMG); | |
| 136 } | 140 } |
| 137 | 141 |
| 142 // adding new features, using mask around existing feature positions | |
| 143 Mat featureMask = Mat::ones(videoSize, CV_8UC1); | |
| 144 for (unsigned int n=0;n<currPts.size(); n++) | |
| 145 for (int j=MAX(0, currPts[n].x-params.minFeatureDistanceKLT); j<MIN(videoSize.width, currPts[n].x+params.minFeatureDistanceKLT+1); j++) | |
| 146 for (int i=MAX(0, currPts[n].y-params.minFeatureDistanceKLT); i<MIN(videoSize.height, currPts[n].y+params.minFeatureDistanceKLT+1); i++) | |
| 147 featureMask.at<uchar>(i,j)=0; | |
| 148 detector.detect(currentFrameBW, currKpts, featureMask); | |
| 149 ::keyPoints2Points(currKpts, currPts, false); | |
| 150 | |
| 151 //brief.compute(currentFrameBW, currKpts, currDesc); //Compute brief descriptors at each keypoint location | |
| 152 | |
| 138 imshow("frame", frame); | 153 imshow("frame", frame); |
| 154 imshow("mask", featureMask*256); | |
| 139 previousFrameBW = currentFrameBW.clone(); | 155 previousFrameBW = currentFrameBW.clone(); |
| 140 prevKpts = currKpts; | 156 prevPts = currPts; |
| 141 currDesc.copyTo(prevDesc); | 157 //prevKpts = currKpts; |
| 158 //currDesc.copyTo(prevDesc); | |
| 142 key = waitKey(2); | 159 key = waitKey(2); |
| 143 } | 160 } |
| 144 | 161 |
| 145 return 0; | 162 return 0; |
| 146 } | 163 } |
