2 #include "stickletrack.h"
6 static Mat background, foregroundmask, colormask, mScissors, scissorsmask;
8 static bool recomputeScissors = true;
10 void mouseScissors(int evt, int x, int y, int flags, void* param){
11 if (evt==CV_EVENT_LBUTTONDOWN) {
12 Prefs.scissorpoints->push_back( Point(x, y) );
13 recomputeScissors = true;
16 else if (evt==CV_EVENT_RBUTTONDOWN) {
20 for ( int ipoint = 0; ipoint < Prefs.scissorpoints->size(); ipoint++ ) {
21 double dist = pow( x - (*Prefs.scissorpoints)[ipoint].x, 2 ) + pow( y - (*Prefs.scissorpoints)[ipoint].y, 2 );
23 if ( dist < nearestdist || nearestid == -1 ) {
28 if ( nearestid != -1 ) {
29 Prefs.scissorpoints->erase( Prefs.scissorpoints->begin() + nearestid );
30 recomputeScissors = true;
36 background = Mat::zeros(Props.height, Props.width, CV_32FC3);
38 namedWindow("stickletrack_masking_background", CV_WINDOW_KEEPRATIO);
39 namedWindow("stickletrack_masking_backgroundmask", CV_WINDOW_KEEPRATIO);
40 namedWindow("stickletrack_masking_colormask", CV_WINDOW_KEEPRATIO);
41 namedWindow("stickletrack_masking_scissorsmask", CV_WINDOW_KEEPRATIO);
43 createTrackbar("Decay", "stickletrack_masking_background", &Prefs.halfdecay, 100, &trackbarCallbackUpdateNormPrefs, 0);
44 createTrackbar("Threshold", "stickletrack_masking_backgroundmask", &Prefs.forethreshold, 255, &trackbarCallbackUpdateNormPrefs, 0);
46 createTrackbar("min H", "stickletrack_masking_colormask", &Prefs.mincolor[0], 255, NULL, 0);
47 createTrackbar("min S", "stickletrack_masking_colormask", &Prefs.mincolor[1], 255, NULL, 0);
48 createTrackbar("min V", "stickletrack_masking_colormask", &Prefs.mincolor[2], 255, NULL, 0);
49 createTrackbar("max H", "stickletrack_masking_colormask", &Prefs.maxcolor[0], 255, NULL, 0);
50 createTrackbar("max S", "stickletrack_masking_colormask", &Prefs.maxcolor[1], 255, NULL, 0);
51 createTrackbar("max V", "stickletrack_masking_colormask", &Prefs.maxcolor[2], 255, NULL, 0);
53 cvSetMouseCallback("stickletrack_masking_scissorsmask", mouseScissors, 0);
56 void computeBackground (const Mat& origframe, Mat& background, double& Z, const double& decayfac) {
57 background = ( exp(-decayfac) * background * Z + origframe ) / ( exp(-decayfac) * Z + 1 );
58 Z = exp(-decayfac) * Z + 1;
60 if ( ! isWindowClosed("stickletrack_masking_background") )
61 imshow("stickletrack_masking_background", background/255.0);
64 void computeForegroundMask (const Mat& origframe, const Mat& background, Mat& foregroundMask) {
65 absdiff(origframe, background, foregroundMask);
66 cvtColor(foregroundMask, foregroundMask, CV_RGB2GRAY);
67 threshold(foregroundMask, foregroundMask, Prefs.forethreshold, 255, CV_THRESH_BINARY);
68 foregroundMask.convertTo(foregroundMask, CV_8UC3);
70 if ( ! isWindowClosed("stickletrack_masking_backgroundmask") )
71 imshow("stickletrack_masking_backgroundmask", foregroundmask);
74 void computeColorMask (const Mat& origframe, Mat& colormask) {
75 origframe.convertTo(colormask, CV_8UC3);
76 cvtColor(colormask, colormask, CV_RGB2HSV);
77 inRange(colormask, Scalar(Prefs.mincolor[0],Prefs.mincolor[1],Prefs.mincolor[2]), Scalar(Prefs.maxcolor[0], Prefs.maxcolor[1], Prefs.maxcolor[2]), colormask);
78 colormask.convertTo(colormask, CV_8UC3);
80 if ( ! isWindowClosed("stickletrack_masking_colormask") )
81 imshow("stickletrack_masking_colormask", colormask);
84 void computeScissors (const Mat& origframe, Mat& mScissors) {
85 origframe.convertTo(mScissors, CV_8UC3);
86 for (int ipoint = 0; ipoint < Prefs.scissorpoints->size(); ipoint++) {
87 circle(mScissors, (*Prefs.scissorpoints)[ipoint], 5, Scalar(0,0,255), -1, 8);
88 line(mScissors, (*Prefs.scissorpoints)[ipoint], (*Prefs.scissorpoints)[ (ipoint+1)%Prefs.scissorpoints->size() ], Scalar(0,0,255), 1, 8);
92 void computeScissorsMask (Mat& scissorsmask) {
93 if ( Prefs.scissorpoints->size() >= 3) {
94 const Point* elementPoints[1] = { &((*Prefs.scissorpoints)[0]) };
95 int numberOfPoints = (int)Prefs.scissorpoints->size();
96 scissorsmask = Mat::zeros(Props.height, Props.width, CV_8U);
97 fillPoly (scissorsmask, elementPoints, &numberOfPoints, 1, Scalar (255, 255, 255), 8);
100 scissorsmask = Mat::ones(Props.height, Props.width, CV_8U);
103 void masking_getCombinedMask(const Mat& origframe, Mat& combinedmask) {
104 double decayfac = (log(2) / normalPrefs.halfdecay) / Props.fps;
106 computeBackground(origframe, background, Z, decayfac);
107 computeForegroundMask(origframe, background, foregroundmask );
109 computeColorMask(origframe, colormask);
111 computeScissors(origframe, mScissors);
113 if ( recomputeScissors ) {
114 computeScissorsMask(scissorsmask);
115 recomputeScissors = false;
118 if ( ! isWindowClosed("stickletrack_masking_scissorsmask") )
119 imshow("stickletrack_masking_scissorsmask", mScissors);
121 bitwise_and(foregroundmask, colormask, combinedmask);
122 bitwise_and(combinedmask, scissorsmask, combinedmask);