#include "stickletrack.h"
#include "tracking.h"
-#define BACKSECONDS 30
-
double frametime = 0;
int framenum = 0;
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");
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;
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,
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;
int rotatingTime( int whereyouwant ) {
if ( whereyouwant < 0 )
- return BACKSECONDS*Props.fps + whereyouwant;
+ return comArgs.backBufferSize*Props.fps + whereyouwant;
else
return whereyouwant;
}
void writeTanjaLog (int writeframenum, vector<tag> *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++) {
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);
+ }
- tagintime = new vector<tag>*[BACKSECONDS*Props.fps];
+ Mat frameintime[comArgs.backBufferSize*Props.fps];
- for (int iback = 0; iback < BACKSECONDS*Props.fps; iback++)
+ tagintime = new vector<tag>*[comArgs.backBufferSize*Props.fps];
+
+ for (int iback = 0; iback < comArgs.backBufferSize*Props.fps; iback++)
tagintime[iback] = new vector<tag>;
capture >> frame;
openTanjaLog();
masking_init();
- tracking_init();
-
- cvSetMouseCallback("contours_picture", mouseTracking, 0);
+ tracking_init(&mouseTracking);
capture.set(CV_CAP_PROP_POS_FRAMES, 0);
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);
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());
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;
}
- 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 {
return 0;
}
-int main(int ac, char** av) {
- if ( ac != 4 ) {
- cout << "Usage: stickletrack <maxoutputfps> <rescalingfactor> <videofilename>" << endl;
+void showUsage() {
+ cout << "Usage: stickletrack [OPTIONS]... <path to videofile>" << endl;
+ cout << endl << "Available options:" << endl;
+ cout << "-x <rescalingfactor>" << "\t" << "rescale video by a factor <rescalingfactor>" << endl;
+ cout << "-o <maxfps>" << "\t" << "write to log maximally <maxfps> times per second" << endl;
+ cout << "-b <bsize>" << "\t" << "buffer <bsize> 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];
+}
- capture.open( av[3] );
+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);
}