1 // Background average sample code done with averages and done with codebooks
2 // (adapted from the OpenCV book sample)
4 // NOTE: To get the keyboard to work, you *have* to have one of the video windows be active
5 // and NOT the consule window.
7 // Gary Bradski Oct 3, 2008.
9 /* *************** License:**************************
11 Right to use this code in any way you want without warrenty, support or any guarentee of it working.
13 BOOK: It would be nice if you cited it:
14 Learning OpenCV: Computer Vision with the OpenCV Library
15 by Gary Bradski and Adrian Kaehler
16 Published by O'Reilly Media, October 3, 2008
19 http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134
20 Or: http://oreilly.com/catalog/9780596516130/
21 ISBN-10: 0596516134 or: ISBN-13: 978-0596516130
22 ************************************************** */
31 //VARIABLES for CODEBOOK METHOD:
32 CvBGCodeBookModel* model = 0;
33 const int NCHANNELS = 3;
34 bool ch[NCHANNELS]={true,true,true}; // This sets what channels should be adjusted for background bounds
38 printf("\nLearn background and find foreground using simple average and average difference learning method:\n"
39 "\nUSAGE:\nbgfg_codebook [--nframes=300] [movie filename, else from camera]\n"
40 "***Keep the focus on the video windows, NOT the consol***\n\n"
41 "INTERACTIVE PARAMETERS:\n"
42 "\tESC,q,Q - quit the program\n"
43 "\th - print this help\n"
44 "\tp - pause toggle\n"
46 "\tr - run mode (single step off)\n"
47 "=== AVG PARAMS ===\n"
48 "\t- - bump high threshold UP by 0.25\n"
49 "\t= - bump high threshold DOWN by 0.25\n"
50 "\t[ - bump low threshold UP by 0.25\n"
51 "\t] - bump low threshold DOWN by 0.25\n"
52 "=== CODEBOOK PARAMS ===\n"
53 "\ty,u,v- only adjust channel 0(y) or 1(u) or 2(v) respectively\n"
54 "\ta - adjust all 3 channels at once\n"
55 "\tb - adjust both 2 and 3 at once\n"
56 "\ti,o - bump upper threshold up,down by 1\n"
57 "\tk,l - bump lower threshold up,down by 1\n"
58 "\tSPACE - reset the model\n"
63 //USAGE: ch9_background startFrameCollection# endFrameCollection# [movie filename, else from camera]
64 //If from AVI, then optionally add HighAvg, LowAvg, HighCB_Y LowCB_Y HighCB_U LowCB_U HighCB_V LowCB_V
66 int main(int argc, char** argv)
68 const char* filename = 0;
69 IplImage* rawImage = 0, *yuvImage = 0; //yuvImage is for codebook method
70 IplImage *ImaskCodeBook = 0,*ImaskCodeBookCC = 0;
71 CvCapture* capture = 0;
73 int c, n, nframes = 0;
74 int nframesToLearnBG = 300;
76 model = cvCreateBGCodeBookModel();
78 //Set color thresholds to default values
80 model->modMin[1] = model->modMin[2] = 3;
81 model->modMax[0] = 10;
82 model->modMax[1] = model->modMax[2] = 10;
83 model->cbBounds[0] = model->cbBounds[1] = model->cbBounds[2] = 10;
86 bool singlestep = false;
88 for( n = 1; n < argc; n++ )
90 static const char* nframesOpt = "--nframes=";
91 if( strncmp(argv[n], nframesOpt, strlen(nframesOpt))==0 )
93 if( sscanf(argv[n] + strlen(nframesOpt), "%d", &nframesToLearnBG) == 0 )
105 printf("Capture from camera\n");
106 capture = cvCaptureFromCAM( 0 );
110 printf("Capture from file %s\n",filename);
111 capture = cvCreateFileCapture( filename );
116 printf( "Can not initialize video capturing\n\n" );
121 //MAIN PROCESSING LOOP:
126 rawImage = cvQueryFrame( capture );
135 if( nframes == 1 && rawImage )
137 // CODEBOOK METHOD ALLOCATION
138 yuvImage = cvCloneImage(rawImage);
139 ImaskCodeBook = cvCreateImage( cvGetSize(rawImage), IPL_DEPTH_8U, 1 );
140 ImaskCodeBookCC = cvCreateImage( cvGetSize(rawImage), IPL_DEPTH_8U, 1 );
141 cvSet(ImaskCodeBook,cvScalar(255));
143 cvNamedWindow( "Raw", 1 );
144 cvNamedWindow( "ForegroundCodeBook",1);
145 cvNamedWindow( "CodeBook_ConnectComp",1);
148 // If we've got an rawImage and are good to go:
151 cvCvtColor( rawImage, yuvImage, CV_BGR2YCrCb );//YUV For codebook method
152 //This is where we build our background model
153 if( !pause && nframes-1 < nframesToLearnBG )
154 cvBGCodeBookUpdate( model, yuvImage );
156 if( nframes-1 == nframesToLearnBG )
157 cvBGCodeBookClearStale( model, model->t/2 );
159 //Find the foreground if any
160 if( nframes-1 >= nframesToLearnBG )
162 // Find foreground by codebook method
163 cvBGCodeBookDiff( model, yuvImage, ImaskCodeBook );
164 // This part just to visualize bounding boxes and centers if desired
165 cvCopy(ImaskCodeBook,ImaskCodeBookCC);
166 cvSegmentFGMask( ImaskCodeBookCC );
169 cvShowImage( "Raw", rawImage );
170 cvShowImage( "ForegroundCodeBook",ImaskCodeBook);
171 cvShowImage( "CodeBook_ConnectComp",ImaskCodeBookCC);
175 c = cvWaitKey(10)&0xFF;
177 // End processing on ESC, q or Q
178 if(c == 27 || c == 'q')
180 //Else check for user input
190 singlestep = !singlestep;
198 cvBGCodeBookClearStale( model, 0 );
207 ch[0] = c == 'y' || c == '0' || c == 'a' || c == '3';
208 ch[1] = c == 'u' || c == '1' || c == 'a' || c == '3' || c == 'b';
209 ch[2] = c == 'v' || c == '2' || c == 'a' || c == '3' || c == 'b';
210 printf("CodeBook YUV Channels active: %d, %d, %d\n", ch[0], ch[1], ch[2] );
212 case 'i': //modify max classification bounds (max bound goes higher)
213 case 'o': //modify max classification bounds (max bound goes lower)
214 case 'k': //modify min classification bounds (min bound goes lower)
215 case 'l': //modify min classification bounds (min bound goes higher)
217 uchar* ptr = c == 'i' || c == 'o' ? model->modMax : model->modMin;
218 for(n=0; n<NCHANNELS; n++)
222 int v = ptr[n] + (c == 'i' || c == 'l' ? 1 : -1);
223 ptr[n] = CV_CAST_8U(v);
225 printf("%d,", ptr[n]);
227 printf(" CodeBook %s Side\n", c == 'i' || c == 'o' ? "High" : "Low" );
233 cvReleaseCapture( &capture );
234 cvDestroyWindow( "Raw" );
235 cvDestroyWindow( "ForegroundCodeBook");
236 cvDestroyWindow( "CodeBook_ConnectComp");