X-Git-Url: http://git.treefish.org/~alex/stickletrack.git/blobdiff_plain/67cf895ef6d35267c1f0b9681729881397ecee2f..6ef674a0c32b5ef389a97b80486c56be6b75b6e5:/masking.cpp diff --git a/masking.cpp b/masking.cpp index d76a8b4..cad10f1 100644 --- a/masking.cpp +++ b/masking.cpp @@ -1,12 +1,16 @@ #include "masking.h" #include "stickletrack.h" +#include + static Mat background, foregroundmask, colormask, mScissors, scissorsmask; static double Z = 1; +static bool recomputeScissors = true; void mouseScissors(int evt, int x, int y, int flags, void* param){ if (evt==CV_EVENT_LBUTTONDOWN) { Prefs.scissorpoints->push_back( Point(x, y) ); + recomputeScissors = true; } else if (evt==CV_EVENT_RBUTTONDOWN) { @@ -21,37 +25,40 @@ void mouseScissors(int evt, int x, int y, int flags, void* param){ nearestid = ipoint; } } - if ( nearestid != -1 ) + if ( nearestid != -1 ) { Prefs.scissorpoints->erase( Prefs.scissorpoints->begin() + nearestid ); + recomputeScissors = true; + } } } 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 +67,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 +77,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) { @@ -89,8 +98,6 @@ void computeScissorsMask (Mat& scissorsmask) { } else scissorsmask = Mat::ones(Props.height, Props.width, CV_8U); - - imshow("scissorsmask", mScissors); } void masking_getCombinedMask(const Mat& origframe, Mat& combinedmask) { @@ -101,10 +108,16 @@ void masking_getCombinedMask(const Mat& origframe, Mat& combinedmask) { computeColorMask(origframe, colormask); - computeScissors(origframe, mScissors); - computeScissorsMask(scissorsmask); + if ( recomputeScissors ) { + computeScissorsMask(scissorsmask); + recomputeScissors = false; + } + + if ( ! isWindowClosed("stickletrack_masking_scissorsmask") ) + imshow("stickletrack_masking_scissorsmask", mScissors); + bitwise_and(foregroundmask, colormask, combinedmask); bitwise_and(combinedmask, scissorsmask, combinedmask); }