X-Git-Url: http://git.treefish.org/~alex/stickletrack.git/blobdiff_plain/e81c69eea555724e25ea911c274a1fa996363d4c..6ef674a0c32b5ef389a97b80486c56be6b75b6e5:/stickletrack.cpp diff --git a/stickletrack.cpp b/stickletrack.cpp index c5c8504..85deb6d 100755 --- a/stickletrack.cpp +++ b/stickletrack.cpp @@ -19,8 +19,6 @@ #include "stickletrack.h" #include "tracking.h" -#define BACKSECONDS 30 - double frametime = 0; int framenum = 0; @@ -45,10 +43,17 @@ static vector **tagintime; static int tagintimeidx=0; static int moresleep=0; static int gotoframe=0; -static int maxoutputfps; -static double rescalingfactor; static ofstream tanjaLogFile; +struct comargs { + int backBufferSize; + char *videoFileName; + int maxOutputFPS; + double rescalingFactor; +}; + +static comargs comArgs; + void genBaseDir() { #if defined(_WIN32) Props.basedir = getenv("HOMEDRIVE"); @@ -201,7 +206,7 @@ int timeWarp(int x, int y) { int wannago = startClickFrame + 10*horiMove*Props.fps/Props.width; int maxgo = min(ilatetagintime, Props.totframes); - int mingo = max(1, ilatetagintime - BACKSECONDS*Props.fps + 2); + int mingo = max(1, ilatetagintime - comArgs.backBufferSize*Props.fps + 2); if (wannago > maxgo) return maxgo; @@ -213,7 +218,7 @@ int timeWarp(int x, int y) { int rotatingTime( int whereyouwant ) { if ( whereyouwant < 0 ) - return BACKSECONDS*Props.fps + whereyouwant; + return comArgs.backBufferSize*Props.fps + whereyouwant; else return whereyouwant; } @@ -312,7 +317,7 @@ void mouseTracking(int evt, int x, int y, int flags, void* param){ void writeTanjaLog (int writeframenum, vector *tagstowrite) { stringstream outline; - if ( writeframenum%(Props.fps/maxoutputfps) == 0 ) { + if ( writeframenum%(Props.fps/comArgs.maxOutputFPS) == 0 ) { if ( tagstowrite->size() >= Prefs.manyfish ) { outline << (double)writeframenum/Props.fps; for ( int itag = 0; itag < Prefs.manyfish; itag++) { @@ -368,16 +373,25 @@ int process(VideoCapture& capture) { Mat frame, origframe, combinedmask; Props.fps = capture.get(CV_CAP_PROP_FPS); - Props.width = capture.get(CV_CAP_PROP_FRAME_WIDTH)*rescalingfactor; - Props.height = capture.get(CV_CAP_PROP_FRAME_HEIGHT)*rescalingfactor; + Props.width = capture.get(CV_CAP_PROP_FRAME_WIDTH)*comArgs.rescalingFactor; + Props.height = capture.get(CV_CAP_PROP_FRAME_HEIGHT)*comArgs.rescalingFactor; Props.totframes = capture.get(CV_CAP_PROP_FRAME_COUNT); Props.diagonal = sqrt( pow(Props.width, 2) + pow(Props.height, 2) ); - Mat frameintime[BACKSECONDS*Props.fps]; + if ( Props.width == 0 || Props.height == 0 || Props.fps == 0 || Props.totframes == 0 ) { + cerr << "Something got wrong while reading video-file info!" << endl; + cerr << "Width: " << Props.width << endl; + cerr << "Height: " << Props.height << endl; + cerr << "FPS: " << Props.fps << endl; + cerr << "Total frames: " << Props.totframes << endl; + exit(1); + } + + Mat frameintime[comArgs.backBufferSize*Props.fps]; - tagintime = new vector*[BACKSECONDS*Props.fps]; + tagintime = new vector*[comArgs.backBufferSize*Props.fps]; - for (int iback = 0; iback < BACKSECONDS*Props.fps; iback++) + for (int iback = 0; iback < comArgs.backBufferSize*Props.fps; iback++) tagintime[iback] = new vector; capture >> frame; @@ -426,7 +440,7 @@ int process(VideoCapture& capture) { break; frame.convertTo(origframe, CV_32FC3); - resize(origframe, origframe, Size(0,0), rescalingfactor, rescalingfactor); + resize(origframe, origframe, Size(0,0), comArgs.rescalingFactor, comArgs.rescalingFactor); masking_getCombinedMask(origframe, combinedmask); @@ -439,10 +453,10 @@ int process(VideoCapture& capture) { frameintime[tagintimeidx] = origframe; ilatetagintime = framenum; - tagintimeidx = (tagintimeidx+1)%(BACKSECONDS*Props.fps); + tagintimeidx = (tagintimeidx+1)%(comArgs.backBufferSize*Props.fps); - if ( ilatetagintime >= BACKSECONDS*Props.fps-1 ) - writeTanjaLog( framenum - (BACKSECONDS*Props.fps-1), tagintime[ tagintimeidx ] ); + if ( ilatetagintime >= comArgs.backBufferSize*Props.fps-1 ) + writeTanjaLog( framenum - (comArgs.backBufferSize*Props.fps-1), tagintime[ tagintimeidx ] ); } gotoframe = framenum + 1; @@ -477,11 +491,11 @@ int process(VideoCapture& capture) { } - if ( ilatetagintime >= BACKSECONDS*Props.fps-1 ) { - int timeidx = (tagintimeidx+1)%(BACKSECONDS*Props.fps); - for ( int itime = 0; itime < BACKSECONDS*Props.fps-1; itime++ ) { - writeTanjaLog( framenum+1+itime - (BACKSECONDS*Props.fps-1), tagintime[ timeidx ] ); - timeidx = (timeidx+1)%(BACKSECONDS*Props.fps); + if ( ilatetagintime >= comArgs.backBufferSize*Props.fps-1 ) { + int timeidx = (tagintimeidx+1)%(comArgs.backBufferSize*Props.fps); + for ( int itime = 0; itime < comArgs.backBufferSize*Props.fps-1; itime++ ) { + writeTanjaLog( framenum+1+itime - (comArgs.backBufferSize*Props.fps-1), tagintime[ timeidx ] ); + timeidx = (timeidx+1)%(comArgs.backBufferSize*Props.fps); } } else { @@ -498,20 +512,50 @@ int process(VideoCapture& capture) { return 0; } -int main(int ac, char** av) { - if ( ac != 4 ) { - cout << "Usage: stickletrack " << endl; +void showUsage() { + cout << "Usage: stickletrack [OPTIONS]... " << endl; + cout << endl << "Available options:" << endl; + cout << "-x " << "\t" << "rescale video by a factor " << endl; + cout << "-o " << "\t" << "write to log maximally times per second" << endl; + cout << "-b " << "\t" << "buffer seconds for going back in time" << endl; +} + +bool parseComArgs (int ac, char** av) { + if ( ac < 2 || ac%2 ) { + showUsage(); exit(0); } - maxoutputfps = atoi( av[1] ); - rescalingfactor = atof( av[2] ); + comArgs.videoFileName = NULL; + comArgs.rescalingFactor = 1.0; + comArgs.maxOutputFPS = 25; + comArgs.backBufferSize = 10; + + for (int i = 1; i < ac-1; i++) + if (i + 1 != ac) + if (av[i] == "-x") { + comArgs.rescalingFactor = atof(av[i + 1]); + } else if (av[i] == "-o") { + comArgs.maxOutputFPS = atoi(av[i + 1]); + } else if (av[i] == "-b") { + comArgs.backBufferSize = atoi(av[i + 1]); + } else { + cout << "Unknown command line parameters!\n"; + showUsage(); + exit(0); + } + + comArgs.videoFileName = av[ac-1]; +} + +int main(int ac, char** av) { + parseComArgs(ac, av); - capture.open( av[3] ); + capture.open( comArgs.videoFileName ); genBaseDir(); - if (!capture.isOpened()) { + if (!capture.isOpened()) { cerr << "Failed to open a video device or video file!\n" << endl; return 1; }