]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/camera/rozkuk/rozkuk.cxx
rozkuk - OpenCV based corns camera recognition program
[eurobot/public.git] / src / camera / rozkuk / rozkuk.cxx
1 /*******************************************************************************
2  * 
3  * rozkuk - OpenCV based corns camera recognition program
4  *
5  * Created for Eurobot 2010 competition.
6  *
7  * Petr Kubizňák (kubiznak.petr@gmail.com), 2010
8  * 
9  ******************************************************************************/
10
11 extern "C" {
12 #include <inttypes.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <semaphore.h>
16 #include <getopt.h>
17
18 #include <orte.h>
19 //#include "roboorte.h"
20
21 //#include "rozporte.h"
22 }
23
24 #include <cv.h>
25 #include <highgui.h>
26
27 #include "clr2float.h"
28
29 /******************************************************************************/
30
31 // modes definitions
32 #define MODE_QUIT                                       0x01
33 #define MODE_REALTIME                   0x02
34 #define MODE_RECOGNIZE          0x04
35
36 // mask of all modes / errors
37 #define MASK_MODES                              0x07
38 #define MASK_ERRORS                             ~MASK_MODES
39
40 // default mode in standard session
41 #define START_MODE                              MODE_RECOGNIZE
42
43 #ifdef ROZKUK_DEBUG                             /************ DEBUG SESSION ONLY ************/
44
45 // default mode in debug session
46 #undef START_MODE
47 #define START_MODE                              MODE_REALTIME
48
49 // highgui windows names
50 #define WINDOW_ORIG                             "ROZKUK original scene"
51 #define WINDOW_THRES                    "ROZKUK threshold"
52
53 // values of keys
54 #define KEY_ESC                                         0x1B
55 #define KEY_SPACE                                       0x20
56 #define KEY_P                                                   0x50
57 #define KEY_R                                                   0x52
58 #define KEY_p                                                   0x70
59 #define KEY_r                                                   0x72
60
61 // number of frames passed in rozkuk mode before refresh
62 #define PASS_FRAMES                             0
63
64 #endif                                                                          /*----------- DEBUG SESSION ONLY -----------*/
65
66 #define THRESHOLD                                       50
67
68
69 #ifdef ROZKUK_DEBUG                             /************ DEBUG SESSION ONLY ************/
70
71 /******************************************************************************/
72
73 CvFont font;                                            //text font
74
75 /******************************************************************************/
76
77 /** Writes text "pause" in the CAMERA window. */
78 void drawPauseText(IplImage *img) {
79         cvPutText(img,"pause", cvPoint(300,440), &font, cvScalar(255,255,255));
80         cvShowImage(WINDOW_ORIG, img);
81 }
82
83 /******************************************************************************/
84
85 /** Displays a real video in a window (only for DEBUG session).
86  * @param capture Pointer to a camera capture.
87  * @return Code of mode to switch to. */
88 int modeRealtime(CvCapture* capture) {
89   IplImage* frame = NULL;                                                                                                                               // frame from camera
90   int pause=0;                                                                                                                                                                  // set pause=1 to pause the video
91   
92         while(1) {
93                 /* keyboard events handeling */
94     switch(cvWaitKey(10) & 0xFF) {                                                                                      // wait 10ms for an event
95     // stop capturing
96     case KEY_ESC:
97         return MODE_QUIT;
98     // switch to recognize mode
99     case KEY_R:
100     case KEY_r:
101         return MODE_RECOGNIZE;
102     // (un)pause
103     case KEY_P:
104     case KEY_p:
105         pause = !pause;
106         drawPauseText(frame);
107         break;
108     }
109
110     frame = cvQueryFrame(capture);                                                                                      // Get one frame
111     if(!frame) {
112       perror("NULL frame");
113       continue;
114     }
115                 
116     if(!pause) cvShowImage(WINDOW_ORIG, frame);                                 // show image (! Do not release the frame !)    
117         }
118         return MODE_QUIT;
119 }
120 #endif                                                                          /*----------- DEBUG SESSION ONLY -----------*/
121
122 /******************************************************************************/
123
124 /** Implements the camera recognition of corns.
125  * In DEBUG session the results are also displayed on screen.
126  * @param capture Pointer to a camera capture.
127  * @return Code of mode to switch to. */
128 int modeRecognize(CvCapture* capture) {
129         IplImage *frame = NULL;                                                                                                                         // frame from camera
130   IplImage *thresFrame = NULL;                                                                                                  // thresholded frame
131   float *floatMap = NULL;                                                                                                                               // float <-1,1> image of the frame
132 #ifdef ROZKUK_DEBUG                             /************ DEBUG SESSION ONLY ************/
133   int framesPassed = PASS_FRAMES;                                                                                               // number of passed frames before refresh
134         int pause=0;                                                                                                                                                                    // set pause=1 to pause processing
135 #endif                                                                          /*----------- DEBUG SESSION ONLY -----------*/
136
137         while(1) {
138 #ifdef ROZKUK_DEBUG                             /************ DEBUG SESSION ONLY ************/
139                 /* keyboard events handeling */
140     switch(cvWaitKey(10) & 0xFF) {                                                                                      // wait 10ms for an event
141     // stop capturing
142     case KEY_ESC:
143         return MODE_QUIT;
144     // skip the frame
145     case KEY_SPACE:
146         framesPassed = PASS_FRAMES;
147         break;
148     // switch to recognize mode
149     case KEY_R:
150     case KEY_r:
151         return MODE_REALTIME;
152     // (un)pause
153     case KEY_P:
154     case KEY_p:
155         pause = !pause;
156         drawPauseText(frame);
157         break;
158     }
159 #endif                                                                          /*----------- DEBUG SESSION ONLY -----------*/
160
161                 // Get one frame
162 #ifdef ROZKUK_DEBUG                             /************ DEBUG SESSION ONLY ************/
163                 if(pause) cvQueryFrame(capture);
164                 else
165 #endif                                                                          /*----------- DEBUG SESSION ONLY -----------*/
166                         frame = cvQueryFrame(capture);
167     if(!frame) {
168       perror("NULL frame");
169       continue;
170     }
171     
172 #ifdef ROZKUK_DEBUG                             /************ DEBUG SESSION ONLY ************/
173                 if(pause) continue;
174
175     // transform colorful image to its float <-1,1> representation
176     cvReleaseImage(&thresFrame);
177         thresFrame = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
178 #endif                                                                          /*----------- DEBUG SESSION ONLY -----------*/
179         clr2float(frame, &floatMap, THRESHOLD, thresFrame);     // get float representation (in DEBUG session, thresholded image is stored to thresFrame)
180
181 #ifdef ROZKUK_DEBUG                             /************ DEBUG SESSION ONLY ************/
182                 if(framesPassed++ > PASS_FRAMES) {
183             framesPassed=0;
184             cvShowImage(WINDOW_ORIG, frame);                                                                    // show image (! Do not release the frame !)
185         cvShowImage(WINDOW_THRES, thresFrame);
186                 }
187 #endif                                                                          /*----------- DEBUG SESSION ONLY -----------*/
188
189         }
190         return MODE_QUIT;
191 }
192
193 /******************************************************************************/
194
195 /** Switches between modes of the program.
196  * @param defaultMode The first mode to run. */
197 int modeManager(int defaultMode) {
198         int mode=defaultMode;
199   CvCapture* capture;
200   
201         // connect to a camera
202   while(!(capture=cvCaptureFromCAM(CV_CAP_ANY))) {
203           perror("NULL capture");
204     usleep(500*1000);
205   }  
206   
207 #ifdef ROZKUK_DEBUG                             /************ DEBUG SESSION ONLY ************/
208         // create gui windows and init font
209   cvNamedWindow(WINDOW_ORIG, CV_WINDOW_AUTOSIZE);                               // real camera video / one frame viewer
210         cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0, 1);             //init text font
211 #endif                                                                          /*----------- DEBUG SESSION ONLY -----------*/
212
213   while(!(mode & MODE_QUIT)) {
214         switch(mode & MASK_MODES) {
215
216 #ifdef ROZKUK_DEBUG                             /************ DEBUG SESSION ONLY ************/
217         case MODE_REALTIME:
218                 cvDestroyWindow(WINDOW_THRES);
219                 mode = modeRealtime(capture);
220                 break;
221 #endif                                                                          /*----------- DEBUG SESSION ONLY -----------*/
222
223         case MODE_RECOGNIZE:
224 #ifdef ROZKUK_DEBUG                             /************ DEBUG SESSION ONLY ************/
225                 cvNamedWindow(WINDOW_THRES, CV_WINDOW_AUTOSIZE);
226                 cvMoveWindow(WINDOW_THRES, cvQueryFrame(capture)->width, 0);
227 #endif                                                                          /*----------- DEBUG SESSION ONLY -----------*/
228                 mode = modeRecognize(capture);
229                 break;
230
231         default:
232                 goto end;                       // jump out of the loop
233         }
234   }
235
236 end:
237   cvReleaseCapture(&capture);
238 #ifdef ROZKUK_DEBUG                             /************ DEBUG SESSION ONLY ************/
239   cvDestroyWindow(WINDOW_ORIG);
240   cvDestroyWindow(WINDOW_THRES);
241 #endif                                                                          /*----------- DEBUG SESSION ONLY -----------*/
242   return mode & MASK_ERRORS;
243 }
244
245 /******************************************************************************/
246
247 int main(int argc, char *argv[]) {
248 /*  while (orte_init()) {
249     perror("orte_init");
250     usleep(500*1000);
251   }
252 */
253         return modeManager(START_MODE);
254 }
255