X-Git-Url: http://git.treefish.org/~alex/stickletrack.git/blobdiff_plain/c22f55516cf5df96e2a97056f42c66f71b774d9a..6ef674a0c32b5ef389a97b80486c56be6b75b6e5:/stickletrack.cpp?ds=inline diff --git a/stickletrack.cpp b/stickletrack.cpp index e90f030..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,23 +43,37 @@ 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"); Props.basedir += getenv("HOMEPATH"); - Props.basedir += "\.stickletrack"; #else Props.basedir = getenv("HOME"); - Props.basedir += "/.stickletrack"; #endif + Props.basedir += "/.stickletrack"; + cout << "Using " << Props.basedir << " for data output." << endl; } +bool isWindowClosed (const char* name) { + if ( cvGetWindowHandle(name) == NULL ) + return true; + else + return false; +} + void beforeExit() { cout << "Exitting ..." << endl; @@ -116,19 +128,19 @@ void drawTimes(Mat& mContours) { alles << framenum << " : " << fixed << (framenum)/(double)Props.fps << "s : +" << (double)moresleep/Props.fps << "ms"; string text = alles.str(); - int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX; + int fontFace = FONT_HERSHEY_DUPLEX; - double fontScale = Props.diagonal/1000.0; - int thickness = 3; + double fontScale = Props.width / 600.0; + int thickness = Props.width / 250.0; int baseline=0; Size textSize = getTextSize(text, fontFace, fontScale, thickness, &baseline); - Point textOrg(10, 10 + textSize.height); + Point textOrg(Props.width*0.01, Props.width*0.01 + textSize.height); rectangle(mContours, Point(0, 0), - Point(Props.width, 20+textSize.height), + Point(Props.width, Props.width*0.02 + textSize.height), Scalar(0,0,255), -1); putText(mContours, text, textOrg, fontFace, fontScale, @@ -194,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; @@ -206,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; } @@ -305,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++) { @@ -356,21 +368,30 @@ int process(VideoCapture& capture) { char filename[200]; bool pleaseExit = false; - namedWindow("original", CV_WINDOW_KEEPRATIO); + namedWindow("stickletrack_original", CV_WINDOW_KEEPRATIO); 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; @@ -390,9 +411,7 @@ int process(VideoCapture& capture) { openTanjaLog(); masking_init(); - tracking_init(); - - cvSetMouseCallback("contours_picture", mouseTracking, 0); + tracking_init(&mouseTracking); capture.set(CV_CAP_PROP_POS_FRAMES, 0); @@ -421,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); @@ -434,15 +453,16 @@ 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; - imshow("original", origframe/255.0); + if ( ! isWindowClosed("stickletrack_original") ) + imshow("stickletrack_original", origframe/255.0); } drawTimes(tracking_getFrame()); @@ -451,7 +471,8 @@ int process(VideoCapture& capture) { circle( tracking_getFrame(), Point2f(tags[nearestTags[0]].x, tags[nearestTags[0]].y), Props.diagonal / 100.0, Scalar(0,0,255), -1, 8 ); } - tracking_showFrame(); + if ( tracking_showFrame() ) + pleaseExit = true; char key; @@ -470,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 { @@ -491,25 +512,53 @@ 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); + } - capture.open( av[3] ); + comArgs.videoFileName = av[ac-1]; +} + +int main(int ac, char** av) { + parseComArgs(ac, av); + + 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; } - - cout << "Exit with q if you want to save your data!!!" << endl; return process(capture); }