1 /*******************************************************************************
3 * rozkuk - OpenCV based corns camera recognition program
5 * Created for Eurobot 2010 competition.
7 * Petr Kubizňák (kubiznak.petr@gmail.com), 2010
9 ******************************************************************************/
15 #include <semaphore.h>
19 //#include "roboorte.h"
21 //#include "rozporte.h"
27 #include "clr2float.h"
29 /******************************************************************************/
32 #define MODE_QUIT 0x01
33 #define MODE_REALTIME 0x02
34 #define MODE_RECOGNIZE 0x04
36 // mask of all modes / errors
37 #define MASK_MODES 0x07
38 #define MASK_ERRORS ~MASK_MODES
40 // default mode in standard session
41 #define START_MODE MODE_RECOGNIZE
43 #ifdef ROZKUK_DEBUG /************ DEBUG SESSION ONLY ************/
45 // default mode in debug session
47 #define START_MODE MODE_REALTIME
49 // highgui windows names
50 #define WINDOW_ORIG "ROZKUK original scene"
51 #define WINDOW_THRES "ROZKUK threshold"
55 #define KEY_SPACE 0x20
61 // number of frames passed in rozkuk mode before refresh
64 #endif /*----------- DEBUG SESSION ONLY -----------*/
69 #ifdef ROZKUK_DEBUG /************ DEBUG SESSION ONLY ************/
71 /******************************************************************************/
73 CvFont font; //text font
75 /******************************************************************************/
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);
83 /******************************************************************************/
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
93 /* keyboard events handeling */
94 switch(cvWaitKey(10) & 0xFF) { // wait 10ms for an event
98 // switch to recognize mode
101 return MODE_RECOGNIZE;
106 drawPauseText(frame);
110 frame = cvQueryFrame(capture); // Get one frame
112 perror("NULL frame");
116 if(!pause) cvShowImage(WINDOW_ORIG, frame); // show image (! Do not release the frame !)
120 #endif /*----------- DEBUG SESSION ONLY -----------*/
122 /******************************************************************************/
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 -----------*/
138 #ifdef ROZKUK_DEBUG /************ DEBUG SESSION ONLY ************/
139 /* keyboard events handeling */
140 switch(cvWaitKey(10) & 0xFF) { // wait 10ms for an event
146 framesPassed = PASS_FRAMES;
148 // switch to recognize mode
151 return MODE_REALTIME;
156 drawPauseText(frame);
159 #endif /*----------- DEBUG SESSION ONLY -----------*/
162 #ifdef ROZKUK_DEBUG /************ DEBUG SESSION ONLY ************/
163 if(pause) cvQueryFrame(capture);
165 #endif /*----------- DEBUG SESSION ONLY -----------*/
166 frame = cvQueryFrame(capture);
168 perror("NULL frame");
172 #ifdef ROZKUK_DEBUG /************ DEBUG SESSION ONLY ************/
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)
181 #ifdef ROZKUK_DEBUG /************ DEBUG SESSION ONLY ************/
182 if(framesPassed++ > PASS_FRAMES) {
184 cvShowImage(WINDOW_ORIG, frame); // show image (! Do not release the frame !)
185 cvShowImage(WINDOW_THRES, thresFrame);
187 #endif /*----------- DEBUG SESSION ONLY -----------*/
193 /******************************************************************************/
195 /** Switches between modes of the program.
196 * @param defaultMode The first mode to run. */
197 int modeManager(int defaultMode) {
198 int mode=defaultMode;
201 // connect to a camera
202 while(!(capture=cvCaptureFromCAM(CV_CAP_ANY))) {
203 perror("NULL capture");
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 -----------*/
213 while(!(mode & MODE_QUIT)) {
214 switch(mode & MASK_MODES) {
216 #ifdef ROZKUK_DEBUG /************ DEBUG SESSION ONLY ************/
218 cvDestroyWindow(WINDOW_THRES);
219 mode = modeRealtime(capture);
221 #endif /*----------- DEBUG SESSION ONLY -----------*/
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);
232 goto end; // jump out of the loop
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;
245 /******************************************************************************/
247 int main(int argc, char *argv[]) {
248 /* while (orte_init()) {
253 return modeManager(START_MODE);