00001 #include <stdio.h>
00002 #ifndef WIN32
00003 #include <unistd.h>
00004 #endif
00005
00006 #ifdef WIN32
00007 #define _USE_MATH_DEFINES
00008 #endif
00009 #include <math.h>
00010
00011 #include <cv.h>
00012 #include <highgui.h>
00013
00014 #include "../Common/sharedmem.h"
00015
00016 #ifdef WIN32
00017 #include "../Common/Linux4Win.h"
00018 #endif
00019
00020 #include "define.h"
00021
00022
00023 #define MAP_WINDOW "ceil map"
00024
00025
00026
00027 static void on_mouse(int event, int x, int y, int flags, void* param);
00028
00029
00030 static robot_status *rstat;
00031
00032 int main (int argc, char **argv )
00033 {
00034 #ifdef BIG_WINDOW
00035 double scale = 3.0;
00036 #endif
00037 int key = 0;
00038 IplImage *map_img;
00039 IplImage *output_img;
00040 IplImage *capture_img;
00041 IplImage *capture_tmp;
00042 #ifdef SAVE_TRACK
00043 char fname[256];
00044 unsigned int count = 0;
00045 #endif
00046 CvRect roi;
00047
00048
00049 rstat = (robot_status *)InitSharedMem(SHM_KEY, sizeof(robot_status));
00050
00051
00052 map_img = cvLoadImage (MAP_SHOW, CV_LOAD_IMAGE_COLOR );
00053 if (!map_img)
00054 {
00055 fprintf(stderr, "%s: LOAD ERROR\n", MAP_SHOW);
00056 return 1;
00057 }
00058
00059
00060 printf("mapsize: x=%3d y=%3d\n",map_img->width,map_img->height);
00061
00062
00063 output_img = cvCreateImage( cvSize(map_img->width, map_img->height), IPL_DEPTH_8U, 3);
00064 capture_img = cvCreateImage( cvSize(TEMPLATE_SIZE, TEMPLATE_SIZE), IPL_DEPTH_8U, 1);
00065 capture_tmp = cvCreateImage( cvSize(TEMPLATE_SIZE, TEMPLATE_SIZE), IPL_DEPTH_8U, 3);
00066
00067 #ifdef BIG_WINDOW
00068 IplImage *big_img = cvCreateImage (cvSize ((int)(output_img->width*scale),(int)(output_img->height*scale)), output_img->depth, output_img->nChannels);
00069 cvNamedWindow (BIG_WINDOW, CV_WINDOW_AUTOSIZE);
00070 #endif
00071
00072 cvNamedWindow (MAP_WINDOW, CV_WINDOW_AUTOSIZE);
00073 cvNamedWindow("CameraWindow", 0);
00074 cvSetMouseCallback(MAP_WINDOW, on_mouse, 0);
00075
00076 while(1)
00077 {
00078 cvCopy (map_img, output_img,NULL);
00079
00080 memcpy (capture_img->imageData, rstat->buf, sizeof(unsigned char)*TEMPLATE_SIZE*TEMPLATE_SIZE);
00081
00082
00083 cvConvertImage( capture_img, capture_tmp, 0);
00084
00085
00086 roi = cvRect (0, 0, capture_img->width, capture_img->height);
00087 cvSetImageROI (output_img, roi);
00088 cvCopy (capture_tmp, output_img, 0);
00089 cvResetImageROI(output_img);
00090
00091 cvRectangle ( output_img, cvPoint(roi.x,roi.y), cvPoint(roi.x+roi.width,roi.y+roi.height), CV_RGB(255,0,0), 1, 4, 0 );
00092
00093 #ifdef _DEBUG
00094 printf("%d, %d, %f ( %d )\n", rstat->x, rstat->y, rstat->r, rstat->s);
00095 #endif
00096
00097
00098
00099 cvCircle ( map_img,
00100 cvPoint( (int)(rstat->x+TEMPLATE_SIZE/2),
00101 (int)(rstat->y+TEMPLATE_SIZE/2) ),
00102 1, CV_RGB(255,0,0), -1, 4, 0 );
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 cvLine ( output_img,
00118 cvPoint( (int)(rstat->x+TEMPLATE_SIZE/2),
00119 (int)(rstat->y+TEMPLATE_SIZE/2) ),
00120 cvPoint( (int)(rstat->x+TEMPLATE_SIZE/2+sin(rstat->r)*10),
00121 (int)(rstat->y+TEMPLATE_SIZE/2+cos(rstat->r)*10) ),
00122 CV_RGB(255,140,0), 1, CV_AA, 0 );
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 cvLine ( output_img,
00143 cvPoint( (int)(rstat->x + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(cos(-rstat->r) - sin(-rstat->r)) ),
00144 (int)(rstat->y + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(sin(-rstat->r) + cos(-rstat->r)) ) ),
00145 cvPoint( (int)(rstat->x + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(-cos(-rstat->r) - sin(-rstat->r)) ),
00146 (int)(rstat->y + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(-sin(-rstat->r) + cos(-rstat->r)) ) ),
00147 CV_RGB(255,255,0), 1, CV_AA, 0 );
00148 cvLine ( output_img,
00149 cvPoint( (int)(rstat->x + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(cos(-rstat->r) - sin(-rstat->r)) ),
00150 (int)(rstat->y + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(sin(-rstat->r) + cos(-rstat->r)) ) ),
00151 cvPoint( (int)(rstat->x + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(cos(-rstat->r) + sin(-rstat->r)) ),
00152 (int)(rstat->y + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(sin(-rstat->r) - cos(-rstat->r)) ) ),
00153 CV_RGB(255,255,0), 1, CV_AA, 0 );
00154 cvLine ( output_img,
00155 cvPoint( (int)(rstat->x + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(cos(-rstat->r) + sin(-rstat->r)) ),
00156 (int)(rstat->y + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(sin(-rstat->r) - cos(-rstat->r)) ) ),
00157 cvPoint( (int)(rstat->x + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(-cos(-rstat->r) + sin(-rstat->r)) ),
00158 (int)(rstat->y + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(-sin(-rstat->r) - cos(-rstat->r)) ) ),
00159 CV_RGB(255,255,0), 1, CV_AA, 0 );
00160 cvLine ( output_img,
00161 cvPoint( (int)(rstat->x + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(-cos(-rstat->r) - sin(-rstat->r)) ),
00162 (int)(rstat->y + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(-sin(-rstat->r) + cos(-rstat->r)) ) ),
00163 cvPoint( (int)(rstat->x + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(-cos(-rstat->r) + sin(-rstat->r)) ),
00164 (int)(rstat->y + TEMPLATE_SIZE/2 + TEMPLATE_SIZE/2*(-sin(-rstat->r) - cos(-rstat->r)) ) ),
00165 CV_RGB(255,255,0), 1, CV_AA, 0 );
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 cvShowImage(MAP_WINDOW, output_img);
00223
00224 cvShowImage("CameraWindow",capture_img);
00225
00226 #ifdef BIG_WINDOW
00227
00228 cvResize (output_img, big_img, CV_INTER_NN);
00229 cvShowImage(BIG_WINDOW, big_img);
00230 #endif
00231
00232
00233 #ifdef SAVE_TRACK
00234 sprintf( fname, SAVE_TRACK"map_%05d.png", count++ );
00235
00236 #endif
00237
00238
00239
00240
00241 key = cvWaitKey(50) & 0xff;
00242 switch (key)
00243 {
00244 case 0x1b:
00245 goto exit;
00246 case 'q':
00247 goto exit;
00248 case 'r':
00249 map_img = cvLoadImage (MAP_SHOW, CV_LOAD_IMAGE_COLOR );
00250 printf("Map reset!");
00251 break;
00252 }
00253 }
00254
00255 exit:;
00256
00257
00258 #ifdef BIG_WINDOW
00259 cvDestroyWindow (BIG_WINDOW);
00260 #endif
00261 cvDestroyWindow (MAP_WINDOW);
00262
00263 return 0;
00264 }
00265
00266
00267 static CvPoint lbuttondown_pt;
00268
00269 void on_mouse(int event, int x, int y, int flags, void* param)
00270 {
00271
00272
00273
00274
00275 switch (event)
00276 {
00277 case CV_EVENT_LBUTTONDOWN:
00278 lbuttondown_pt.x = x;
00279 lbuttondown_pt.y = y;
00280 rstat->x = x-TEMPLATE_SIZE/2;
00281 rstat->y = y-TEMPLATE_SIZE/2;
00282 break;
00283 case CV_EVENT_LBUTTONUP:
00284 rstat->r = atan2(x-lbuttondown_pt.x,y-lbuttondown_pt.y);
00285 printf("rstat->(x,y,r)=(%4d,%4d, %3.3f )\n", lbuttondown_pt.x-TEMPLATE_SIZE/2, lbuttondown_pt.y-TEMPLATE_SIZE/2, rstat->r );
00286 break;
00287 }
00288 }