2 #include "stickletrack.h"
4 static Mat background, foregroundmask, colormask, mScissors, scissorsmask;
7 void mouseScissors(int evt, int x, int y, int flags, void* param){
8 if (evt==CV_EVENT_LBUTTONDOWN) {
9 Prefs.scissorpoints->push_back( Point(x, y) );
12 else if (evt==CV_EVENT_RBUTTONDOWN) {
16 for ( int ipoint = 0; ipoint < Prefs.scissorpoints->size(); ipoint++ ) {
17 double dist = pow( x - (*Prefs.scissorpoints)[ipoint].x, 2 ) + pow( y - (*Prefs.scissorpoints)[ipoint].y, 2 );
19 if ( dist < nearestdist || nearestid == -1 ) {
24 if ( nearestid != -1 )
25 Prefs.scissorpoints->erase( Prefs.scissorpoints->begin() + nearestid );
30 background = Mat::zeros(Props.height, Props.width, CV_32FC3);
32 namedWindow("background", CV_WINDOW_KEEPRATIO);
33 namedWindow("foregroundmask", CV_WINDOW_KEEPRATIO);
34 namedWindow("colormask", CV_WINDOW_KEEPRATIO);
35 namedWindow("scissorsmask", CV_WINDOW_KEEPRATIO);
37 createTrackbar("Decay", "background", &Prefs.halfdecay, 100, &trackbarCallbackUpdateNormPrefs, 0);
38 createTrackbar("Threshold", "foregroundmask", &Prefs.forethreshold, 255, &trackbarCallbackUpdateNormPrefs, 0);
40 createTrackbar("min H", "colormask", &Prefs.mincolor[0], 255, NULL, 0);
41 createTrackbar("min S", "colormask", &Prefs.mincolor[1], 255, NULL, 0);
42 createTrackbar("min V", "colormask", &Prefs.mincolor[2], 255, NULL, 0);
43 createTrackbar("max H", "colormask", &Prefs.maxcolor[0], 255, NULL, 0);
44 createTrackbar("max S", "colormask", &Prefs.maxcolor[1], 255, NULL, 0);
45 createTrackbar("max V", "colormask", &Prefs.maxcolor[2], 255, NULL, 0);
47 cvSetMouseCallback("scissorsmask", mouseScissors, 0);
50 void computeBackground (const Mat& origframe, Mat& background, double& Z, const double& decayfac) {
51 background = ( exp(-decayfac) * background * Z + origframe ) / ( exp(-decayfac) * Z + 1 );
52 Z = exp(-decayfac) * Z + 1;
54 imshow("background", background/255.0);
57 void computeForegroundMask (const Mat& origframe, const Mat& background, Mat& foregroundMask) {
58 absdiff(origframe, background, foregroundMask);
59 cvtColor(foregroundMask, foregroundMask, CV_RGB2GRAY);
60 threshold(foregroundMask, foregroundMask, Prefs.forethreshold, 255, CV_THRESH_BINARY);
61 foregroundMask.convertTo(foregroundMask, CV_8UC3);
63 imshow("foregroundmask", foregroundmask);
66 void computeColorMask (const Mat& origframe, Mat& colormask) {
67 origframe.convertTo(colormask, CV_8UC3);
68 cvtColor(colormask, colormask, CV_RGB2HSV);
69 inRange(colormask, Scalar(Prefs.mincolor[0],Prefs.mincolor[1],Prefs.mincolor[2]), Scalar(Prefs.maxcolor[0], Prefs.maxcolor[1], Prefs.maxcolor[2]), colormask);
70 colormask.convertTo(colormask, CV_8UC3);
72 imshow("colormask", colormask);
75 void computeScissors (const Mat& origframe, Mat& mScissors) {
76 origframe.convertTo(mScissors, CV_8UC3);
77 for (int ipoint = 0; ipoint < Prefs.scissorpoints->size(); ipoint++) {
78 circle(mScissors, (*Prefs.scissorpoints)[ipoint], 5, Scalar(0,0,255), -1, 8);
79 line(mScissors, (*Prefs.scissorpoints)[ipoint], (*Prefs.scissorpoints)[ (ipoint+1)%Prefs.scissorpoints->size() ], Scalar(0,0,255), 1, 8);
83 void computeScissorsMask (Mat& scissorsmask) {
84 if ( Prefs.scissorpoints->size() >= 3) {
85 const Point* elementPoints[1] = { &((*Prefs.scissorpoints)[0]) };
86 int numberOfPoints = (int)Prefs.scissorpoints->size();
87 scissorsmask = Mat::zeros(Props.height, Props.width, CV_8U);
88 fillPoly (scissorsmask, elementPoints, &numberOfPoints, 1, Scalar (255, 255, 255), 8);
91 scissorsmask = Mat::ones(Props.height, Props.width, CV_8U);
93 imshow("scissorsmask", mScissors);
96 void masking_getCombinedMask(const Mat& origframe, Mat& combinedmask) {
97 double decayfac = (log(2) / normalPrefs.halfdecay) / Props.fps;
99 computeBackground(origframe, background, Z, decayfac);
100 computeForegroundMask(origframe, background, foregroundmask );
102 computeColorMask(origframe, colormask);
105 computeScissors(origframe, mScissors);
106 computeScissorsMask(scissorsmask);
108 bitwise_and(foregroundmask, colormask, combinedmask);
109 bitwise_and(combinedmask, scissorsmask, combinedmask);