2 #include "stickletrack.h"
6 static Mat background, foregroundmask, colormask, mScissors, scissorsmask;
9 void mouseScissors(int evt, int x, int y, int flags, void* param){
10 if (evt==CV_EVENT_LBUTTONDOWN) {
11 Prefs.scissorpoints->push_back( Point(x, y) );
14 else if (evt==CV_EVENT_RBUTTONDOWN) {
18 for ( int ipoint = 0; ipoint < Prefs.scissorpoints->size(); ipoint++ ) {
19 double dist = pow( x - (*Prefs.scissorpoints)[ipoint].x, 2 ) + pow( y - (*Prefs.scissorpoints)[ipoint].y, 2 );
21 if ( dist < nearestdist || nearestid == -1 ) {
26 if ( nearestid != -1 )
27 Prefs.scissorpoints->erase( Prefs.scissorpoints->begin() + nearestid );
32 background = Mat::zeros(Props.height, Props.width, CV_32FC3);
34 namedWindow("stickletrack_masking_background", CV_WINDOW_KEEPRATIO);
35 namedWindow("stickletrack_masking_backgroundmask", CV_WINDOW_KEEPRATIO);
36 namedWindow("stickletrack_masking_colormask", CV_WINDOW_KEEPRATIO);
37 namedWindow("stickletrack_masking_scissorsmask", CV_WINDOW_KEEPRATIO);
39 createTrackbar("Decay", "stickletrack_masking_background", &Prefs.halfdecay, 100, &trackbarCallbackUpdateNormPrefs, 0);
40 createTrackbar("Threshold", "stickletrack_masking_backgroundmask", &Prefs.forethreshold, 255, &trackbarCallbackUpdateNormPrefs, 0);
42 createTrackbar("min H", "stickletrack_masking_colormask", &Prefs.mincolor[0], 255, NULL, 0);
43 createTrackbar("min S", "stickletrack_masking_colormask", &Prefs.mincolor[1], 255, NULL, 0);
44 createTrackbar("min V", "stickletrack_masking_colormask", &Prefs.mincolor[2], 255, NULL, 0);
45 createTrackbar("max H", "stickletrack_masking_colormask", &Prefs.maxcolor[0], 255, NULL, 0);
46 createTrackbar("max S", "stickletrack_masking_colormask", &Prefs.maxcolor[1], 255, NULL, 0);
47 createTrackbar("max V", "stickletrack_masking_colormask", &Prefs.maxcolor[2], 255, NULL, 0);
49 cvSetMouseCallback("stickletrack_masking_scissorsmask", mouseScissors, 0);
52 void computeBackground (const Mat& origframe, Mat& background, double& Z, const double& decayfac) {
53 background = ( exp(-decayfac) * background * Z + origframe ) / ( exp(-decayfac) * Z + 1 );
54 Z = exp(-decayfac) * Z + 1;
56 if ( ! isWindowClosed("stickletrack_masking_background") )
57 imshow("stickletrack_masking_background", background/255.0);
60 void computeForegroundMask (const Mat& origframe, const Mat& background, Mat& foregroundMask) {
61 absdiff(origframe, background, foregroundMask);
62 cvtColor(foregroundMask, foregroundMask, CV_RGB2GRAY);
63 threshold(foregroundMask, foregroundMask, Prefs.forethreshold, 255, CV_THRESH_BINARY);
64 foregroundMask.convertTo(foregroundMask, CV_8UC3);
66 if ( ! isWindowClosed("stickletrack_masking_backgroundmask") )
67 imshow("stickletrack_masking_backgroundmask", foregroundmask);
70 void computeColorMask (const Mat& origframe, Mat& colormask) {
71 origframe.convertTo(colormask, CV_8UC3);
72 cvtColor(colormask, colormask, CV_RGB2HSV);
73 inRange(colormask, Scalar(Prefs.mincolor[0],Prefs.mincolor[1],Prefs.mincolor[2]), Scalar(Prefs.maxcolor[0], Prefs.maxcolor[1], Prefs.maxcolor[2]), colormask);
74 colormask.convertTo(colormask, CV_8UC3);
76 if ( ! isWindowClosed("stickletrack_masking_colormask") )
77 imshow("stickletrack_masking_colormask", colormask);
80 void computeScissors (const Mat& origframe, Mat& mScissors) {
81 origframe.convertTo(mScissors, CV_8UC3);
82 for (int ipoint = 0; ipoint < Prefs.scissorpoints->size(); ipoint++) {
83 circle(mScissors, (*Prefs.scissorpoints)[ipoint], 5, Scalar(0,0,255), -1, 8);
84 line(mScissors, (*Prefs.scissorpoints)[ipoint], (*Prefs.scissorpoints)[ (ipoint+1)%Prefs.scissorpoints->size() ], Scalar(0,0,255), 1, 8);
88 void computeScissorsMask (Mat& scissorsmask) {
89 if ( Prefs.scissorpoints->size() >= 3) {
90 const Point* elementPoints[1] = { &((*Prefs.scissorpoints)[0]) };
91 int numberOfPoints = (int)Prefs.scissorpoints->size();
92 scissorsmask = Mat::zeros(Props.height, Props.width, CV_8U);
93 fillPoly (scissorsmask, elementPoints, &numberOfPoints, 1, Scalar (255, 255, 255), 8);
96 scissorsmask = Mat::ones(Props.height, Props.width, CV_8U);
98 //if ( ! windowclosed_sissors )
99 if ( ! isWindowClosed("stickletrack_masking_scissorsmask") )
100 imshow("stickletrack_masking_scissorsmask", mScissors);
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);
112 computeScissors(origframe, mScissors);
113 computeScissorsMask(scissorsmask);
115 bitwise_and(foregroundmask, colormask, combinedmask);
116 bitwise_and(combinedmask, scissorsmask, combinedmask);