int maxGap(vector<int> A, int n) { int _min = INT_MAX, _max = INT_MIN; for (int i = 0; i < n; ++i) { _min = min(_min, A[i]); _max = max(_max, A[i]); } vector<bool> hasNum(n + 1, false); vector<int> minBucket(n + 1), maxBucket(n + 1); for (int i = 0; i < n; ++i) { int idx = getBucketIdx(A[i], _min, _max, n); minBucket[idx] = hasNum[idx] ? min(minBucket[idx], A[i]) : A[i]; maxBucket[idx] = hasNum[idx] ? max(maxBucket[idx], A[i]) : A[i]; hasNum[idx] = true; } int i = 0; int last; int ret = INT_MIN; while (i <= n) { if (hasNum[i]) { last = i++; break; } } while (i <= n) { if (hasNum[i]) { ret = max(minBucket[i] - maxBucket[last], ret); last = i; } ++i; } return ret; }
void OpticalFlow::computeOpticalFlow(Mat& cf){ Mat current_frame; cvtColor(cf, current_frame, COLOR_BGR2GRAY); if( points[0].empty() ){ buildPointGrid(current_frame); } if (buckets.empty()){ buildBuckets(6, 30.0, 10); cout <<"first bucket angle :" << buckets[0].angle_max << endl; } if( !previous_frame.empty() ){ vector<double> distance(points[0].size()), angle(points[0].size()); vector<uchar> status; vector<float> err; calcOpticalFlowPyrLK(previous_frame, current_frame, points[0], points[1], status, err, winSize, 3, termcrit, 0, 0.01); for( int i = 0; i < points[1].size(); i++ ){ if( !status[i] ) continue; distance[i] = computeDistance(points[0][i], points[1][i]); angle[i] = computeAngle(points[0][i], points[1][i]); status[i] = assignBucket(distance[i], angle[i]); } Bucket max_bucket = maxBucket(); cout << max_bucket.getCount() << endl; for( int i = 0; i < points[1].size(); i++ ){ if( !status[i]) continue; if(!max_bucket.inBucket(distance[i], angle[i])){ drawFlow(points[0][i], points[1][i], false, cf); } } //debug if ( debug_ ){ imshow(windowName, previous_frame); } } // update for next frame previous_frame = current_frame; // current frame becomes previous frame. }