From ab5ee4c78e1fa591a4d8d9af812f83a9e81ef6d7 Mon Sep 17 00:00:00 2001 From: Alex Schmidt Date: Wed, 9 Oct 2013 11:49:57 +0200 Subject: [PATCH 1/1] windows stay closed and stickletrack exits properly if tracking screen is closes. --- masking.cpp | 47 +++++++++++++++++++++++++++-------------------- stickletrack.cpp | 17 ++++++++++++----- stickletrack.h | 1 + tracking.cpp | 39 ++++++++++++++++++++++----------------- tracking.h | 2 +- 5 files changed, 63 insertions(+), 43 deletions(-) diff --git a/masking.cpp b/masking.cpp index d76a8b4..b90153e 100644 --- a/masking.cpp +++ b/masking.cpp @@ -1,6 +1,8 @@ #include "masking.h" #include "stickletrack.h" +#include + static Mat background, foregroundmask, colormask, mScissors, scissorsmask; static double Z = 1; @@ -29,29 +31,30 @@ void mouseScissors(int evt, int x, int y, int flags, void* param){ void masking_init() { background = Mat::zeros(Props.height, Props.width, CV_32FC3); - namedWindow("background", CV_WINDOW_KEEPRATIO); - namedWindow("foregroundmask", CV_WINDOW_KEEPRATIO); - namedWindow("colormask", CV_WINDOW_KEEPRATIO); - namedWindow("scissorsmask", CV_WINDOW_KEEPRATIO); - - createTrackbar("Decay", "background", &Prefs.halfdecay, 100, &trackbarCallbackUpdateNormPrefs, 0); - createTrackbar("Threshold", "foregroundmask", &Prefs.forethreshold, 255, &trackbarCallbackUpdateNormPrefs, 0); - - createTrackbar("min H", "colormask", &Prefs.mincolor[0], 255, NULL, 0); - createTrackbar("min S", "colormask", &Prefs.mincolor[1], 255, NULL, 0); - createTrackbar("min V", "colormask", &Prefs.mincolor[2], 255, NULL, 0); - createTrackbar("max H", "colormask", &Prefs.maxcolor[0], 255, NULL, 0); - createTrackbar("max S", "colormask", &Prefs.maxcolor[1], 255, NULL, 0); - createTrackbar("max V", "colormask", &Prefs.maxcolor[2], 255, NULL, 0); - - cvSetMouseCallback("scissorsmask", mouseScissors, 0); + namedWindow("stickletrack_masking_background", CV_WINDOW_KEEPRATIO); + namedWindow("stickletrack_masking_backgroundmask", CV_WINDOW_KEEPRATIO); + namedWindow("stickletrack_masking_colormask", CV_WINDOW_KEEPRATIO); + namedWindow("stickletrack_masking_scissorsmask", CV_WINDOW_KEEPRATIO); + + createTrackbar("Decay", "stickletrack_masking_background", &Prefs.halfdecay, 100, &trackbarCallbackUpdateNormPrefs, 0); + createTrackbar("Threshold", "stickletrack_masking_backgroundmask", &Prefs.forethreshold, 255, &trackbarCallbackUpdateNormPrefs, 0); + + createTrackbar("min H", "stickletrack_masking_colormask", &Prefs.mincolor[0], 255, NULL, 0); + createTrackbar("min S", "stickletrack_masking_colormask", &Prefs.mincolor[1], 255, NULL, 0); + createTrackbar("min V", "stickletrack_masking_colormask", &Prefs.mincolor[2], 255, NULL, 0); + createTrackbar("max H", "stickletrack_masking_colormask", &Prefs.maxcolor[0], 255, NULL, 0); + createTrackbar("max S", "stickletrack_masking_colormask", &Prefs.maxcolor[1], 255, NULL, 0); + createTrackbar("max V", "stickletrack_masking_colormask", &Prefs.maxcolor[2], 255, NULL, 0); + + cvSetMouseCallback("stickletrack_masking_scissorsmask", mouseScissors, 0); } void computeBackground (const Mat& origframe, Mat& background, double& Z, const double& decayfac) { background = ( exp(-decayfac) * background * Z + origframe ) / ( exp(-decayfac) * Z + 1 ); Z = exp(-decayfac) * Z + 1; - imshow("background", background/255.0); + if ( ! isWindowClosed("stickletrack_masking_background") ) + imshow("stickletrack_masking_background", background/255.0); } void computeForegroundMask (const Mat& origframe, const Mat& background, Mat& foregroundMask) { @@ -60,7 +63,8 @@ void computeForegroundMask (const Mat& origframe, const Mat& background, Mat& fo threshold(foregroundMask, foregroundMask, Prefs.forethreshold, 255, CV_THRESH_BINARY); foregroundMask.convertTo(foregroundMask, CV_8UC3); - imshow("foregroundmask", foregroundmask); + if ( ! isWindowClosed("stickletrack_masking_backgroundmask") ) + imshow("stickletrack_masking_backgroundmask", foregroundmask); } void computeColorMask (const Mat& origframe, Mat& colormask) { @@ -69,7 +73,8 @@ void computeColorMask (const Mat& origframe, Mat& colormask) { inRange(colormask, Scalar(Prefs.mincolor[0],Prefs.mincolor[1],Prefs.mincolor[2]), Scalar(Prefs.maxcolor[0], Prefs.maxcolor[1], Prefs.maxcolor[2]), colormask); colormask.convertTo(colormask, CV_8UC3); - imshow("colormask", colormask); + if ( ! isWindowClosed("stickletrack_masking_colormask") ) + imshow("stickletrack_masking_colormask", colormask); } void computeScissors (const Mat& origframe, Mat& mScissors) { @@ -90,7 +95,9 @@ void computeScissorsMask (Mat& scissorsmask) { else scissorsmask = Mat::ones(Props.height, Props.width, CV_8U); - imshow("scissorsmask", mScissors); + //if ( ! windowclosed_sissors ) + if ( ! isWindowClosed("stickletrack_masking_scissorsmask") ) + imshow("stickletrack_masking_scissorsmask", mScissors); } void masking_getCombinedMask(const Mat& origframe, Mat& combinedmask) { diff --git a/stickletrack.cpp b/stickletrack.cpp index 0db07ce..da812df 100755 --- a/stickletrack.cpp +++ b/stickletrack.cpp @@ -62,6 +62,13 @@ void genBaseDir() { 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; @@ -356,7 +363,7 @@ 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; @@ -442,7 +449,8 @@ int process(VideoCapture& capture) { gotoframe = framenum + 1; - imshow("original", origframe/255.0); + if ( ! isWindowClosed("stickletrack_original") ) + imshow("stickletrack_original", origframe/255.0); } drawTimes(tracking_getFrame()); @@ -451,7 +459,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; @@ -508,8 +517,6 @@ int main(int ac, char** av) { 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); } diff --git a/stickletrack.h b/stickletrack.h index 51715f0..5ad39f8 100644 --- a/stickletrack.h +++ b/stickletrack.h @@ -68,3 +68,4 @@ extern int wannabeframenum; extern normalprefs normalPrefs; void trackbarCallbackUpdateNormPrefs (int trackpos, void *userdata); +bool isWindowClosed (const char* name); diff --git a/tracking.cpp b/tracking.cpp index 4e401f8..88c9512 100644 --- a/tracking.cpp +++ b/tracking.cpp @@ -52,8 +52,13 @@ Mat& tracking_getFrame() { return mContours; } -void tracking_showFrame() { - imshow("contours_picture", mContours); +int tracking_showFrame() { + if ( ! isWindowClosed("stickletrack_tracking_screen") ) { + imshow("stickletrack_tracking_screen", mContours); + return 0; + } + else + return 1; } bool tracking_isEnabled () { @@ -61,21 +66,21 @@ bool tracking_isEnabled () { } void tracking_init() { - namedWindow("contours", CV_WINDOW_KEEPRATIO); - namedWindow("contours_picture", CV_WINDOW_KEEPRATIO); - - createTrackbar("Enable", "contours", &enablecontours, 1, NULL, 0); - createTrackbar("Manyfish", "contours", &Prefs.manyfish, 10, NULL, 0); - createTrackbar("min area", "contours", &Prefs.contours_minarea, 100, &trackbarCallbackUpdateNormPrefs, 0); - createTrackbar("max area", "contours", &Prefs.contours_maxarea, 100, &trackbarCallbackUpdateNormPrefs, 0); - createTrackbar("min circum", "contours", &Prefs.contours_mincircum, 100, &trackbarCallbackUpdateNormPrefs, 0); - createTrackbar("max circum", "contours", &Prefs.contours_maxcircum, 100, &trackbarCallbackUpdateNormPrefs, 0); - createTrackbar("min shortaxis", "contours", &Prefs.contours_minshortaxis, 100, &trackbarCallbackUpdateNormPrefs, 0); - createTrackbar("max shortaxis", "contours", &Prefs.contours_maxshortaxis, 100, &trackbarCallbackUpdateNormPrefs, 0); - createTrackbar("min longaxis", "contours", &Prefs.contours_minlongaxis, 100, &trackbarCallbackUpdateNormPrefs, 0); - createTrackbar("max longaxis", "contours", &Prefs.contours_maxlongaxis, 100, &trackbarCallbackUpdateNormPrefs, 0); - createTrackbar("max speed", "contours", &Prefs.contours_maxspeed, 100, &trackbarCallbackUpdateNormPrefs, 0); - createTrackbar("max rotation speed", "contours", &Prefs.contours_maxrot, 100, &trackbarCallbackUpdateNormPrefs, 0); + namedWindow("stickletrack_tracking_prefs", CV_WINDOW_KEEPRATIO); + namedWindow("stickletrack_tracking_screen", CV_WINDOW_KEEPRATIO); + + createTrackbar("Enable", "stickletrack_tracking_prefs", &enablecontours, 1, NULL, 0); + createTrackbar("Manyfish", "stickletrack_tracking_prefs", &Prefs.manyfish, 10, NULL, 0); + createTrackbar("min area", "stickletrack_tracking_prefs", &Prefs.contours_minarea, 100, &trackbarCallbackUpdateNormPrefs, 0); + createTrackbar("max area", "stickletrack_tracking_prefs", &Prefs.contours_maxarea, 100, &trackbarCallbackUpdateNormPrefs, 0); + createTrackbar("min circum", "stickletrack_tracking_prefs", &Prefs.contours_mincircum, 100, &trackbarCallbackUpdateNormPrefs, 0); + createTrackbar("max circum", "stickletrack_tracking_prefs", &Prefs.contours_maxcircum, 100, &trackbarCallbackUpdateNormPrefs, 0); + createTrackbar("min shortaxis", "stickletrack_tracking_prefs", &Prefs.contours_minshortaxis, 100, &trackbarCallbackUpdateNormPrefs, 0); + createTrackbar("max shortaxis", "stickletrack_tracking_prefs", &Prefs.contours_maxshortaxis, 100, &trackbarCallbackUpdateNormPrefs, 0); + createTrackbar("min longaxis", "stickletrack_tracking_prefs", &Prefs.contours_minlongaxis, 100, &trackbarCallbackUpdateNormPrefs, 0); + createTrackbar("max longaxis", "stickletrack_tracking_prefs", &Prefs.contours_maxlongaxis, 100, &trackbarCallbackUpdateNormPrefs, 0); + createTrackbar("max speed", "stickletrack_tracking_prefs", &Prefs.contours_maxspeed, 100, &trackbarCallbackUpdateNormPrefs, 0); + createTrackbar("max rotation speed", "stickletrack_tracking_prefs", &Prefs.contours_maxrot, 100, &trackbarCallbackUpdateNormPrefs, 0); mContours = Mat::zeros(Props.height, Props.width, CV_8UC3); } diff --git a/tracking.h b/tracking.h index c7cda7b..7911da0 100644 --- a/tracking.h +++ b/tracking.h @@ -22,5 +22,5 @@ struct tag { void tracking_init(); void tracking_locateTags(vector& tags, Mat combinedmask_contour); Mat& tracking_getFrame(); -void tracking_showFrame(); +int tracking_showFrame(); bool tracking_isEnabled(); -- 2.39.5