1 #include "opencv2/highgui/highgui.hpp"
2 #include "opencv2/imgproc/imgproc.hpp"
14 #include "stickletrack.h"
17 #define BACKSECONDS 30
25 normalprefs normalPrefs;
27 static VideoCapture capture;
29 static bool stopvideo=false;
30 static bool leftbuttondown=false;
31 static Point2f startClickPos;
32 static int ilatetagintime=-1, iearlytagintime=-1;
33 static int startClickFrame;
34 static bool stoppedbefore;
35 static bool dooneframe=false;
36 static vector<tag> tags;
37 static int tagsselected=0;
38 static int nearestTags[2];
39 static vector<tag> **tagintime;
40 static int tagintimeidx=0;
41 static int moresleep=0;
42 static int gotoframe=0;
43 static int maxoutputfps;
44 static double rescalingfactor;
45 static ofstream tanjaLogFile;
49 Props.basedir = getenv("HOMEDRIVE");
50 Props.basedir += getenv("HOMEPATH");
52 Props.basedir = getenv("HOME");
55 Props.basedir += "/.stickletrack";
57 cout << "Using " << Props.basedir << " for data output." << endl;
61 cout << "Exitting ..." << endl;
64 _mkdir(Props.basedir.c_str());
65 _mkdir((Props.basedir+"/prefs").c_str());
67 mkdir( Props.basedir.c_str(), 0777 );
68 mkdir( (Props.basedir+"/prefs").c_str(), 0777 );
71 cout << "Saving preferences ..." << endl;
72 int scissorpointsize = Prefs.scissorpoints->size();
73 ofstream prefFile( (Props.basedir+"/prefs/"+Props.videohash+".prf").c_str(), ios::binary );
74 prefFile.write((char*)&Prefs, sizeof(Prefs));
75 prefFile.write((char*)&scissorpointsize, sizeof(int));
77 for (int ipoint = 0; ipoint < scissorpointsize; ipoint++)
78 prefFile.write( (char*)&(*Prefs.scissorpoints)[ipoint], sizeof(Point) );
83 void computeNormalizedPrefs() {
84 normalPrefs.contours_minshortaxis = Prefs.contours_minshortaxis * Props.diagonal / 1000.0;
85 normalPrefs.contours_maxshortaxis = Prefs.contours_maxshortaxis * Props.diagonal / 1000.0;
86 normalPrefs.contours_minlongaxis = Prefs.contours_minlongaxis * Props.diagonal / 500.0;
87 normalPrefs.contours_maxlongaxis = Prefs.contours_maxlongaxis * Props.diagonal / 500.0;
89 normalPrefs.contours_minarea = pow(Prefs.contours_minarea,2) * Props.width * Props.height / 1000000.0;
90 normalPrefs.contours_maxarea = pow(Prefs.contours_maxarea,2) * Props.width * Props.height / 1000000.0;
92 normalPrefs.contours_mincircum = Prefs.contours_mincircum * Props.diagonal / 500.0;
93 normalPrefs.contours_maxcircum = Prefs.contours_maxcircum * Props.diagonal / 500.0;
95 normalPrefs.contours_maxspeed = Prefs.contours_maxspeed * Props.diagonal / 100.0;
97 normalPrefs.contours_maxrot = 10 * Prefs.contours_maxrot;
99 normalPrefs.halfdecay = 10.0 * Prefs.halfdecay / Props.fps;
102 void trackbarCallbackUpdateNormPrefs (int trackpos, void *userdata) {
103 computeNormalizedPrefs();
106 void drawTimes(Mat& mContours) {
111 alles << framenum << " : " << fixed << (framenum)/(double)Props.fps << "s : +" << (double)moresleep/Props.fps << "ms";
113 string text = alles.str();
114 int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
116 double fontScale = Props.diagonal/1000.0;
120 Size textSize = getTextSize(text, fontFace,
121 fontScale, thickness, &baseline);
123 Point textOrg(10, 10 + textSize.height);
125 rectangle(mContours, Point(0, 0),
126 Point(Props.width, 20+textSize.height),
127 Scalar(0,0,255), -1);
129 putText(mContours, text, textOrg, fontFace, fontScale,
130 Scalar::all(255), thickness, 8);
133 void readCreatePrefs() {
134 ifstream prefFile( (Props.basedir+"/prefs/"+Props.videohash+".prf").c_str(), ios::binary );
135 if ( prefFile.is_open() ) {
136 int scissorpointsize;
138 prefFile.read((char*)&Prefs, sizeof(Prefs));
139 prefFile.read((char*)&scissorpointsize, sizeof(int));
141 Prefs.scissorpoints = new vector<Point>;
142 for (int ipoint = 0; ipoint < scissorpointsize; ipoint++) {
144 prefFile.read((char*)&tmpPoint, sizeof(Point));
145 Prefs.scissorpoints->push_back(tmpPoint);
150 cout << "Loaded saved preferences." << endl;
153 Prefs.scissorpoints = new vector<Point>;
157 void loadDefaultPrefs () {
158 Prefs.halfdecay = 100;
159 Prefs.forethreshold = 0;
160 Prefs.mincolor[0] = 0; Prefs.mincolor[1] = 0; Prefs.mincolor[2] = 0;
161 Prefs.maxcolor[0] = 255; Prefs.maxcolor[1] = 255; Prefs.maxcolor[2] = 255;
163 Prefs.contours_minshortaxis = 0;
164 Prefs.contours_maxshortaxis = 100;
165 Prefs.contours_minlongaxis = 0;
166 Prefs.contours_maxlongaxis = 100;
167 Prefs.contours_minarea = 0;
168 Prefs.contours_maxarea = 100;
169 Prefs.contours_mincircum = 0;
170 Prefs.contours_maxcircum = 100;
171 Prefs.contours_maxspeed = 100;
172 Prefs.contours_maxrot = 100;
175 unsigned long frameHash(const Mat& frame) {
176 unsigned long hash = 5381;
178 unsigned char *str = (unsigned char*)frame.data;
181 hash = ((hash << 5) + hash) + c;
187 int timeWarp(int x, int y) {
188 int horiMove = startClickPos.x - x;
189 int wannago = startClickFrame + 10*horiMove*Props.fps/Props.width;
191 int maxgo = min(ilatetagintime, Props.totframes);
192 int mingo = max(1, ilatetagintime - BACKSECONDS*Props.fps + 2);
196 else if (wannago < mingo)
202 int rotatingTime( int whereyouwant ) {
203 if ( whereyouwant < 0 )
204 return BACKSECONDS*Props.fps + whereyouwant;
209 void mouseTracking(int evt, int x, int y, int flags, void* param){
210 if ( stopvideo && evt == CV_EVENT_LBUTTONDBLCLK ) {
214 for (int itag = 0; itag < tags.size(); itag++) {
215 double dist = pow(tags[itag].x - x, 2) + pow(tags[itag].y - y, 2);
216 if ( dist < mindist || mindist == -1 ) {
217 nearestTags[tagsselected-1] = itag;
222 if (tagsselected == 2) {
223 if ( nearestTags[0] < tags.size() && nearestTags[0] >= 0
224 && nearestTags[1] < tags.size() && nearestTags[1] >= 0
225 && nearestTags[0] != nearestTags[1] ) {
229 tmp = tags[nearestTags[0]];
230 tags[nearestTags[0]] = tags[nearestTags[1]];
231 tags[nearestTags[1]] = tmp;
232 tags[nearestTags[1]].color = tags[nearestTags[0]].color;
233 tags[nearestTags[0]].color = tmp.color;
235 for (int itagintime = framenum-1; itagintime <= ilatetagintime; itagintime++) {
236 tmp = (*tagintime[ rotatingTime(tagintimeidx - (ilatetagintime-itagintime) - 1) ])[nearestTags[0]];
238 (*tagintime[ rotatingTime(tagintimeidx - (ilatetagintime-itagintime) - 1) ])[nearestTags[0]] =
239 (*tagintime[ rotatingTime(tagintimeidx - (ilatetagintime-itagintime) - 1) ])[nearestTags[1]];
241 (*tagintime[ rotatingTime(tagintimeidx - (ilatetagintime-itagintime) - 1) ])[nearestTags[1]] = tmp;
243 (*tagintime[ rotatingTime(tagintimeidx - (ilatetagintime-itagintime) - 1) ])[nearestTags[1]].color =
244 (*tagintime[ rotatingTime(tagintimeidx - (ilatetagintime-itagintime) - 1) ])[nearestTags[0]].color;
246 (*tagintime[ rotatingTime(tagintimeidx - (ilatetagintime-itagintime) - 1) ])[nearestTags[0]].color = tmp.color;
251 gotoframe = framenum;
256 if ( evt == CV_EVENT_RBUTTONUP ) {
257 stopvideo = (stopvideo+1)%2;
260 if (evt==CV_EVENT_LBUTTONDOWN) {
261 leftbuttondown = true;
262 startClickPos = Point2f(x,y);
263 startClickFrame = framenum;
266 stoppedbefore = true;
268 stoppedbefore = false;
273 if (evt==CV_EVENT_LBUTTONUP) {
274 leftbuttondown = false;
276 int vertiMove = startClickPos.y - y;
278 if ( ! stoppedbefore )
282 if ( leftbuttondown && startClickPos.x - x != 0 && ! (startClickPos.x > Props.width*5.0/6.0 && x > Props.width*5.0/6.0) ) {
283 gotoframe = timeWarp(x, y);
287 if ( leftbuttondown && startClickPos.y - y != 0) {
288 int vertiMove = startClickPos.y - y;
290 if ( startClickPos.x > Props.width*5.0/6.0 && x > Props.width*5.0/6.0 ) {
291 moresleep -= vertiMove*10000/Props.height;
295 drawTimes(tracking_getFrame());
300 void writeTanjaLog (int writeframenum, vector<tag> *tagstowrite) {
301 stringstream outline;
303 if ( writeframenum%(Props.fps/maxoutputfps) == 0 ) {
304 if ( tagstowrite->size() >= Prefs.manyfish ) {
305 outline << (double)writeframenum/Props.fps;
306 for ( int itag = 0; itag < Prefs.manyfish; itag++) {
307 outline << "," << (*tagstowrite)[itag].x << "," << (*tagstowrite)[itag].y << "," << (double)writeframenum/Props.fps-(*tagstowrite)[itag].lastseen;
310 tanjaLogFile.write( outline.str().c_str(), outline.str().size() );
311 tanjaLogFile.flush();
316 void openTanjaLog () {
317 stringstream tanjalogfilename;
320 _mkdir(Props.basedir.c_str());
321 _mkdir((Props.basedir+"/logs").c_str());
323 mkdir( Props.basedir.c_str(), 0777 );
324 mkdir( (Props.basedir+"/logs").c_str(), 0777 );
328 _mkdir(Props.basedir.c_str());
329 _mkdir((Props.basedir+"/logs/"+Props.videohash).c_str());
331 mkdir( Props.basedir.c_str(), 0777 );
332 mkdir( (Props.basedir+"/logs/"+Props.videohash).c_str(), 0777 );
335 tanjalogfilename << Props.basedir << "/logs/" << Props.videohash << "/" << time(0) << ".dat";
336 tanjaLogFile.open( tanjalogfilename.str().c_str() );
338 if ( ! tanjaLogFile.is_open() ) {
339 cerr << "Could not open tanjalogfile " << tanjalogfilename.str() << "!" << endl;
343 tanjaLogFile.write("# STICKLETRACK - TANJALOG\n", 26);
344 tanjaLogFile.write("# format: <time in s>,<tag_1 xpos>,<tag_1 ypos>,<tag_1 losttime>,...,<tag_n xpos>,<tag_n ypos>,<tag_n losttime>\n", 112);
346 tanjaLogFile.flush();
349 int process(VideoCapture& capture) {
352 bool pleaseExit = false;
354 namedWindow("original", CV_WINDOW_KEEPRATIO);
356 Mat frame, origframe, combinedmask;
358 Props.fps = capture.get(CV_CAP_PROP_FPS);
359 Props.width = capture.get(CV_CAP_PROP_FRAME_WIDTH)*rescalingfactor;
360 Props.height = capture.get(CV_CAP_PROP_FRAME_HEIGHT)*rescalingfactor;
361 Props.totframes = capture.get(CV_CAP_PROP_FRAME_COUNT);
362 Props.diagonal = sqrt( pow(Props.width, 2) + pow(Props.height, 2) );
364 Mat frameintime[BACKSECONDS*Props.fps];
366 tagintime = new vector<tag>*[BACKSECONDS*Props.fps];
368 for (int iback = 0; iback < BACKSECONDS*Props.fps; iback++)
369 tagintime[iback] = new vector<tag>;
376 stringstream tmphash;
377 tmphash << frameHash(frame);
378 Props.videohash = tmphash.str();
383 computeNormalizedPrefs();
390 cvSetMouseCallback("contours_picture", mouseTracking, 0);
392 capture.set(CV_CAP_PROP_POS_FRAMES, 0);
394 for (; !pleaseExit;) {
395 if ( !stopvideo || dooneframe ) {
396 framenum = gotoframe;
400 frametime = (framenum-1.0) / Props.fps;
402 if ( framenum <= ilatetagintime ) {
403 origframe = frameintime[ rotatingTime(tagintimeidx - (ilatetagintime-framenum) - 2) ];
404 masking_getCombinedMask(origframe, combinedmask);
407 for (int itag = 0; itag < tagintime[ rotatingTime( tagintimeidx - 2 - (ilatetagintime-framenum) ) ]->size(); itag++)
408 tags.push_back( (*tagintime[ rotatingTime(tagintimeidx - (ilatetagintime-framenum) - 2) ])[itag] );
410 tracking_locateTags (tags, combinedmask);
418 frame.convertTo(origframe, CV_32FC3);
419 resize(origframe, origframe, Size(0,0), rescalingfactor, rescalingfactor);
421 masking_getCombinedMask(origframe, combinedmask);
423 tracking_locateTags (tags, combinedmask);
425 tagintime[tagintimeidx]->clear();
426 for (int itag = 0; itag < tags.size(); itag++)
427 tagintime[tagintimeidx]->push_back(tags[itag]);
429 frameintime[tagintimeidx] = origframe;
431 ilatetagintime = framenum;
432 tagintimeidx = (tagintimeidx+1)%(BACKSECONDS*Props.fps);
434 if ( ilatetagintime >= BACKSECONDS*Props.fps-1 )
435 writeTanjaLog( framenum - (BACKSECONDS*Props.fps-1), tagintime[ tagintimeidx ] );
438 gotoframe = framenum + 1;
440 imshow("original", origframe/255.0);
443 drawTimes(tracking_getFrame());
445 if ( tagsselected == 1 ) {
446 circle( tracking_getFrame(), Point2f(tags[nearestTags[0]].x, tags[nearestTags[0]].y), Props.diagonal / 100.0, Scalar(0,0,255), -1, 8 );
449 tracking_showFrame();
454 key = (char)waitKey(5 + (double)moresleep/Props.fps);
456 key = (char)waitKey(5);
468 if ( ilatetagintime >= BACKSECONDS*Props.fps-1 ) {
469 int timeidx = (tagintimeidx+1)%(BACKSECONDS*Props.fps);
470 for ( int itime = 0; itime < BACKSECONDS*Props.fps-1; itime++ ) {
471 writeTanjaLog( framenum+1+itime - (BACKSECONDS*Props.fps-1), tagintime[ timeidx ] );
472 timeidx = (timeidx+1)%(BACKSECONDS*Props.fps);
476 for ( int itime = 0; itime < ilatetagintime+1; itime++ )
477 writeTanjaLog( itime, tagintime[ itime ] );
480 tanjaLogFile.close();
484 cout << "Bye bye." << endl;
489 int main(int ac, char** av) {
491 cout << "Usage: stickletrack <maxoutputfps> <rescalingfactor> <videofilename>" << endl;
495 maxoutputfps = atoi( av[1] );
496 rescalingfactor = atof( av[2] );
498 capture.open( av[3] );
502 if (!capture.isOpened()) {
503 cerr << "Failed to open a video device or video file!\n" << endl;
507 cout << "Exit with q if you want to save your data!!!" << endl;
509 return process(capture);