Mercurial > hg > nsaunier > traffic-intelligence
comparison c/feature-based-tracking.cpp @ 227:b7612c6d5702
cleaned the code
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 27 Jun 2012 09:52:06 -0400 |
| parents | d4d3b1e8a9f1 |
| children | 23da16442433 |
comparison
equal
deleted
inserted
replaced
| 226:91197f6a03fe | 227:b7612c6d5702 |
|---|---|
| 75 float minTotalFeatureDisplacement = params.nDisplacements*params.minFeatureDisplacement; | 75 float minTotalFeatureDisplacement = params.nDisplacements*params.minFeatureDisplacement; |
| 76 Size window = Size(params.windowSize, params.windowSize); | 76 Size window = Size(params.windowSize, params.windowSize); |
| 77 | 77 |
| 78 // BruteForceMatcher<Hamming> descMatcher; | 78 // BruteForceMatcher<Hamming> descMatcher; |
| 79 // vector<DMatch> matches; | 79 // vector<DMatch> matches; |
| 80 | |
| 81 VideoCapture capture; | |
| 80 Size videoSize; | 82 Size videoSize; |
| 81 | 83 int nFrames = -1; |
| 82 // if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) // if no parameter or number parameter | |
| 83 // capture.open(argc == 2 ? argv[1][0] - '0' : 0); | |
| 84 // else if( argc >= 2 ) | |
| 85 // { | |
| 86 // capture.open(argv[1]); | |
| 87 // if( capture.isOpened() ) | |
| 88 // videoSize = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)); | |
| 89 // cout << "Video " << argv[1] << | |
| 90 // ": width=" << videoSize.width << | |
| 91 // ", height=" << videoSize.height << | |
| 92 // ", nframes=" << capture.get(CV_CAP_PROP_FRAME_COUNT) << endl; | |
| 93 // if( argc > 2 && isdigit(argv[2][0]) ) // could be used to reach first frame, dumping library messages to log file (2> /tmp/log.txt) | |
| 94 // { | |
| 95 // sscanf(argv[2], "%d", ¶ms.frame1); | |
| 96 // cout << "seeking to frame #" << params.frame1 << endl; | |
| 97 // //cap.set(CV_CAP_PROP_POS_FRAMES, pos); | |
| 98 // for (int i=0; i<params.frame1; i++) | |
| 99 // capture >> frame; | |
| 100 // } | |
| 101 // } | |
| 102 | |
| 103 VideoCapture capture; | |
| 104 capture.open(params.videoFilename); | 84 capture.open(params.videoFilename); |
| 105 if(capture.isOpened()) { | 85 if(capture.isOpened()) { |
| 106 videoSize = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)); | 86 videoSize = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT)); |
| 87 nFrames = capture.get(CV_CAP_PROP_FRAME_COUNT); | |
| 107 cout << "Video " << params.videoFilename << | 88 cout << "Video " << params.videoFilename << |
| 108 ": width=" << videoSize.width << | 89 ": width=" << videoSize.width << |
| 109 ", height=" << videoSize.height << | 90 ", height=" << videoSize.height << |
| 110 ", nframes=" << capture.get(CV_CAP_PROP_FRAME_COUNT) << endl; | 91 ", nframes=" << nFrames << endl; |
| 111 } else { | 92 } else { |
| 112 cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl; | 93 cout << "Video filename " << params.videoFilename << " could not be opened. Exiting." << endl; |
| 113 exit(0); | 94 exit(0); |
| 114 } | 95 } |
| 115 // if (!capture.isOpened()) | 96 // if (!capture.isOpened()) |
| 143 hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); | 124 hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); |
| 144 | 125 |
| 145 int key = '?'; | 126 int key = '?'; |
| 146 unsigned int savedFeatureId=0; | 127 unsigned int savedFeatureId=0; |
| 147 Mat frame, currentFrameBW, previousFrameBW; | 128 Mat frame, currentFrameBW, previousFrameBW; |
| 148 for (int frameNum = params.frame1; ((frameNum-params.frame1 < params.nFrames) || (params.nFrames < 0)) && !::interruptionKey(key); frameNum++) { | 129 |
| 130 unsigned int lastFrameNum = nFrames; | |
| 131 if (params.nFrames >= 0) | |
| 132 lastFrameNum = min(params.frame1+params.nFrames, nFrames); | |
| 133 | |
| 134 capture.set(CV_CAP_PROP_POS_FRAMES, params.frame1); | |
| 135 for (unsigned int frameNum = params.frame1; (frameNum < lastFrameNum) && !::interruptionKey(key); frameNum++) { | |
| 149 capture >> frame; | 136 capture >> frame; |
| 150 cout << frameNum << " " << capture.get(CV_CAP_PROP_POS_FRAMES) << " " << prevPts.size() << endl; | 137 if (frameNum%50 ==0) |
| 151 int emptyFrameNum = 0; | 138 cout << "frame " << frameNum << endl; |
| 152 while (frame.empty()) { | 139 //capture.get(CV_CAP_PROP_POS_FRAMES) << " " << prevPts.size() << endl; |
| 153 cerr << "empty frame " << emptyFrameNum << " " << capture.get(CV_CAP_PROP_POS_FRAMES)<< endl; | 140 |
| 154 capture >> frame;//break; | 141 // int emptyFrameNum = 0; |
| 155 emptyFrameNum++; | 142 // while (frame.empty()) { |
| 156 if (emptyFrameNum>=3000) | 143 // cerr << "empty frame " << emptyFrameNum << " " << capture.get(CV_CAP_PROP_POS_FRAMES)<< endl; |
| 157 exit(0); | 144 // capture >> frame;//break; |
| 158 } | 145 // emptyFrameNum++; |
| 146 // if (emptyFrameNum>=3000) | |
| 147 // exit(0); | |
| 148 // } | |
| 159 | 149 |
| 160 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); | 150 cvtColor(frame, currentFrameBW, CV_RGB2GRAY); |
| 161 | 151 |
| 162 // "normal" feature detectors: detect features here | 152 // "normal" feature detectors: detect features here |
| 163 // detector.detect(currentFrameBW, currKpts); // see video_homography c++ sample | 153 // detector.detect(currentFrameBW, currKpts); // see video_homography c++ sample |
| 279 cout << "problem with trajectory length " << success << endl; | 269 cout << "problem with trajectory length " << success << endl; |
| 280 exit(0); | 270 exit(0); |
| 281 } | 271 } |
| 282 cout << "Longest trajectory: " << maxTrajectoryLength << endl; | 272 cout << "Longest trajectory: " << maxTrajectoryLength << endl; |
| 283 | 273 |
| 284 // alternative: read and load features in batches directly select * from positions where trajectory_id in select trajectory_id from positions where frame_number <100 and frame_number > 50 group by trajectory_id | |
| 285 int queryIntervalLength = 10; | |
| 286 | |
| 287 FeatureGraph featureGraph(params.mmConnectionDistance, params.mmSegmentationDistance, params.minFeatureTime, params.minNFeaturesPerGroup); | 274 FeatureGraph featureGraph(params.mmConnectionDistance, params.mmSegmentationDistance, params.minFeatureTime, params.minNFeaturesPerGroup); |
| 288 | 275 |
| 289 // main loop | 276 // main loop |
| 290 int frameNum; | 277 unsigned int frameNum; |
| 291 unsigned int firstFrameNum, lastFrameNum; | 278 unsigned int firstFrameNum = -1, lastFrameNum = -1; |
| 292 trajectoryDB->firstLastInstants(firstFrameNum, lastFrameNum); | 279 trajectoryDB->firstLastInstants(firstFrameNum, lastFrameNum); |
| 293 firstFrameNum = MAX(firstFrameNum, params.frame1); | 280 firstFrameNum = MAX(firstFrameNum, params.frame1); |
| 294 if (params.nFrames>0) | 281 if (params.nFrames>0) |
| 295 lastFrameNum = MIN(lastFrameNum,params.frame1+params.nFrames); | 282 lastFrameNum = MIN(lastFrameNum,params.frame1+params.nFrames); |
| 296 for (frameNum = firstFrameNum; frameNum<lastFrameNum; frameNum ++) { | 283 for (frameNum = firstFrameNum; frameNum<lastFrameNum; frameNum ++) { |
