#include "opencv2/opencv.hpp" using namespace std; using namespace cv; const int MAX_FEATURES = 500; const int MAX_MOVEMENT = 100; int move_test(Mat& oframe, Mat& frame) { // Select features for optical flow vector<Point2f> ofeatures; goodFeaturesToTrack(oframe, ofeatures, MAX_FEATURES, 0.1, 0.2 ); // Parameters for LK vector<Point2f> new_features; vector<uchar> status; vector<float> err; TermCriteria criteria(TermCriteria::COUNT | TermCriteria::EPS, 20, 0.03); Size window(10,10); int max_level = 3; int flags = 0; double min_eigT = 0.004; // Lucas-Kanade method calcOpticalFlowPyrLK(oframe, frame, ofeatures, new_features, status, err, window, max_level, criteria, flags, min_eigT ); double max_move = 0; double movement = 0; for(int i=0; i<ofeatures.size(); i++) { Point pointA (ofeatures[i].x, ofeatures[i].y); Point pointB (new_features[i].x, new_features[i].y); movement = norm(pointA-pointB); if(movement > max_move) max_move = movement; } return max_move > MAX_MOVEMENT; } int main(int argc, char *argv[]) { int i = 0; Mat frame; Mat oframe; if (argc != 2) { cout << "USAGE: <cmd> <file_in>\n"; return -1; } VideoCapture vid(argv[1]); if (!vid.isOpened()) { cout << "Video corrupt\n"; return -1; } int fps = (int)vid.get(CV_CAP_PROP_FPS); i++; if(!vid.read(oframe)) return 1; cvtColor(oframe, oframe, COLOR_BGR2GRAY); while (1) { if (!vid.read(frame)) break; i++; cvtColor(frame,frame,COLOR_BGR2GRAY); if(move_test(oframe, frame)) cout << i/fps << "\n"; oframe = frame; } return 0; }