From: Michal Vokac Date: Mon, 22 Nov 2010 00:21:13 +0000 (+0100) Subject: Some minor changes, working on (Canny, Hough) line detection in frame. X-Git-Url: http://rtime.felk.cvut.cz/gitweb/eurobot/public.git/commitdiff_plain/c0594b225a211be89128e267267b7c4e29d9e595 Some minor changes, working on (Canny, Hough) line detection in frame. --- diff --git a/src/camera/barcam/barcam.cxx b/src/camera/barcam/barcam.cxx index c45b84c7..4b249ff8 100644 --- a/src/camera/barcam/barcam.cxx +++ b/src/camera/barcam/barcam.cxx @@ -75,16 +75,17 @@ extern "C" { // highgui windows names #define WINDOW_ORIG "BARCAM original scene" #define WINDOW_ROI "BARCAM set region of interest" +#define WINDOW_PROD "BARCAM recognition product" // size of graphical windows #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 // size of desired region of interest (crop camera view) -#define ROI_LU_X 0 +#define ROI_LU_X WINDOW_WIDTH/2 - 40 #define ROI_LU_Y 0 -#define ROI_RL_X WINDOW_WIDTH -#define ROI_RL_Y WINDOW_HEIGHT +#define ROI_RL_X WINDOW_WIDTH/2 + 40 +#define ROI_RL_Y WINDOW_HEIGHT - 20 // values of keys #define KEY_ESC 0x1B @@ -113,10 +114,6 @@ extern "C" { #define KEY_8 0x38 -// default threshold and saturation -#define SATURATION 1.0 -#define THRESHOLD_SHIFT -25 //automatic threshold correction - // filename pattern of snapshots (PPC does not support png) #ifdef WITH_GUI #define SNAPSHOTFILENAME "snapshot%02d.png" @@ -124,9 +121,6 @@ extern "C" { #define SNAPSHOTFILENAME "snapshot%02d.pnm" #endif /* WITH_GUI */ -//number of results to select the most supported one from -#define DECISION_BOX_SIZE 3 - /******************************************************************************/ /************************** function declarations *****************************/ @@ -136,7 +130,6 @@ extern "C" { int saveFrame(const CvArr *img); void drawPauseText(IplImage *img); void selectROI(CvCapture* capture, struct roi *roi); -void cropROI(IplImage* frame, struct roi *roi); /******************** multimode computational functions ***********************/ int countThreshold(const IplImage *frame); @@ -218,7 +211,8 @@ int saveFrame(const CvArr *img) #ifdef WITH_GUI void drawPauseText(IplImage *img) { - cvPutText(img, "PAUSE", cvPoint(250,400), &fontLarge, cvScalar(0,0,255)); + cvPutText(img, "PAUSE", cvPoint(img->width/2 - 40,img->height - 50), + &fontLarge, cvScalar(0,0,255)); cvShowImage(WINDOW_ORIG, img); } #else /* PPC */ @@ -259,10 +253,10 @@ void selectROI(CvCapture* capture, struct roi *roi) case KEY_R: case KEY_r: // reset ROI to full frame - roi->lu.x = ROI_LU_X; - roi->lu.y = ROI_LU_Y; - roi->rl.x = ROI_RL_X; - roi->rl.y = ROI_RL_Y; + roi->lu.x = 0; + roi->lu.y = 0; + roi->rl.x = WINDOW_WIDTH; + roi->rl.y = WINDOW_HEIGHT; break; case KEY_1: @@ -306,14 +300,16 @@ void selectROI(CvCapture* capture, struct roi *roi) break; } - frame = cvQueryFrame(capture); - - cvPutText(frame, "Select ROI", cvPoint(240,400), &fontLarge, cvScalar(0,0,255)); + fprintf(stderr, "ROI step: %d left: %d up: %d right: %d down: %d\n", + step, roi->lu.x, roi->lu.y, roi->rl.x, roi->rl.y); step = (step < 0 ? 0 : step); - fprintf(stderr, "ROI step: %d left: %d up: %d right: %d down: %d\n", - step, roi->lu.x, roi->lu.y, roi->rl.x, roi->rl.y); + frame = cvQueryFrame(capture); + + cvPutText(frame, "Select ROI", cvPoint(frame->width/2 -60, + frame->height - 50), + &fontLarge, cvScalar(0,0,255)); cvRectangle(frame, roi->lu, roi->rl, cvScalar(0,0,255), 2, 8, 0); @@ -323,44 +319,7 @@ void selectROI(CvCapture* capture, struct roi *roi) } #else /* PPC */ -void setRegionOfInterrest(IplImage *img) {} -#endif /* WITH_GUI */ - -/******************************************************************************/ - -/** Sets selected ROI to the frame. - * Warning - the frame must be accesst from ROI coordinates now! - * @param frame Pointer to the frame. - * @param roi Pointer to ROI structure */ -void cropROI(IplImage* frame, struct roi *roi) -{ - cvSetImageROI(frame, cvRect(roi->lu.x, roi->lu.y, roi->rl.x - roi->lu.x, - roi->rl.y - roi->lu.y) ); - -} - -/******************************************************************************/ - -/** Normalizes the matrix values and displays them in window specified by name. - * @param floatMat Pointer to matrix data to display. - * @param windowName Name of window in which to display the image. */ -#ifdef WITH_GUI -void displayFloatMat(const CvMat *floatMat, const char *windowName) -{ - double min = 0.0, max = 0.0; - CvMat *normalized = cvCloneMat(floatMat); - - //find extremes of floatMat - cvMinMaxLoc(floatMat, &min, &max); - //normalize - cvConvertScale(floatMat, normalized, 1/(max-min), -(min/(max-min))); - cvShowImage(windowName, normalized); - - cvReleaseMat(&normalized); -} -#else /* PPC */ - -void displayFloatMat(const CvMat *floatMat, const char *windowName) {} +void selectROI(CvCapture *capture, struct roi *roi) {} #endif /* WITH_GUI */ /******************************************************************************/ @@ -380,10 +339,62 @@ int countThreshold(const IplImage *frame) /** Returns an ordinary number of recognized configuration. * @param frame Float representation of frame. * @return */ -int recognize(IplImage *frame) { +int recognize(IplImage* roi_frame) { + int i,j; + /* say you want to set some pixels in the ROI to 0 */ + for (i = 0; i < roi_frame->height/2; i++) { + for (j = 0; j < roi_frame->width/2; j++) { + ((uchar*)(roi_frame->imageData + i * roi_frame->widthStep))[j*3] = 0; + ((uchar*)(roi_frame->imageData + i * roi_frame->widthStep))[j*3+1] = 0; + ((uchar*)(roi_frame->imageData + i * roi_frame->widthStep))[j*3+2] = 0; + } + } + +// IplImage* dst = cvCreateImage( cvGetSize(roi_frame), roi_frame->depth, 1 ); +// IplImage* color_dst = cvCreateImage( cvGetSize(roi_frame), roi_frame->depth, roi_frame->nChannels); +// +// CvMemStorage* storage = cvCreateMemStorage(0); +// CvSeq* lines = 0; +// + +// +// cvCanny(roi_frame, dst, 50, 200, 3 ); +// +// cvCvtColor( dst, color_dst, CV_GRAY2BGR ); +// +// lines = cvHoughLines2( dst, +// storage, +// CV_HOUGH_STANDARD, +// 1, +// CV_PI/180, +// 100, +// 0, +// 0 ); +// +// for( i = 0; i < MIN(lines->total,100); i++ ) +// { +// float* line = (float*)cvGetSeqElem(lines,i); +// float rho = line[0]; +// float theta = line[1]; +// CvPoint pt1, pt2; +// double a = cos(theta), b = sin(theta); +// double x0 = a*rho, y0 = b*rho; +// pt1.x = cvRound(x0 + 1000*(-b)); +// pt1.y = cvRound(y0 + 1000*(a)); +// pt2.x = cvRound(x0 - 1000*(-b)); +// pt2.y = cvRound(y0 - 1000*(a)); +// cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, 8 ); +// } +// +// cvNamedWindow( "Hough", 1 ); +// cvShowImage( "Hough", color_dst ); +// +// cvWaitKey(0); + + return 0; } @@ -443,8 +454,9 @@ int modeImage(char *imageFilename) { #ifdef WITH_GUI int modeVideo(CvCapture* capture, struct roi *roi) { - IplImage* frame = NULL; - + IplImage *frame = NULL; + IplImage *roi_frame = NULL; + while (1) { // wait 10ms for an event switch(cvWaitKey(10) & 0xFF) { @@ -460,13 +472,13 @@ int modeVideo(CvCapture* capture, struct roi *roi) // pause - switch to wait mode case KEY_P: case KEY_p: - drawPauseText(frame); + drawPauseText(roi_frame); return MODE_WAIT; // save frame to file case KEY_S: case KEY_s: - saveFrame(frame); + saveFrame(roi_frame); break; case KEY_O: @@ -485,9 +497,14 @@ int modeVideo(CvCapture* capture, struct roi *roi) continue; } - cropROI(frame, roi); + cvSetImageROI(frame, cvRect(roi->lu.x, roi->lu.y, roi->rl.x - roi->lu.x, + roi->rl.y - roi->lu.y)); + + roi_frame = cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels); + + cvCopy(frame, roi_frame, NULL); - cvShowImage(WINDOW_ORIG, frame); + cvShowImage(WINDOW_ORIG, roi_frame); } return MODE_QUIT; @@ -508,7 +525,7 @@ int modeVideo(CvCapture* capture) { * @param frame Last camera frame. * @return MODE_RECOGNIZE or mode to switch to. */ #ifdef WITH_GUI -int recognizeMode_processKeys(IplImage *frame) +int recognizeMode_processKeys(IplImage *roi_frame) { // wait 10ms for an event switch (cvWaitKey(10) & 0xFF) { @@ -524,15 +541,9 @@ int recognizeMode_processKeys(IplImage *frame) // pause - wait mode case KEY_P: case KEY_p: - drawPauseText(frame); + drawPauseText(roi_frame); camera_control_on = 0; return MODE_WAIT; - - // save frame to file - case KEY_S: - case KEY_s: - saveFrame(frame); - break; } return MODE_RECOGNIZE; } @@ -546,6 +557,27 @@ int recognizeMode_processKeys(IplImage *frame) /******************************************************************************/ +/** Displays analytic frames on screen (on PC), or saves (input) frame to file + * (on PPC in debug mode). + * @param frame Input frame from camera. + * @param thrFrame Thresholded frame. + * @param fMat Float representation of frame. + * @param cfgSide Index of current side configuration. + * @param cfgCenter Index of current center configuration. */ +#ifdef WITH_GUI +void displayFrames(IplImage *roi_frame) { + cvShowImage(WINDOW_PROD, roi_frame); + +} + +#else /* PPC */ + +void displayFrames(IplImage *frame, IplImage **thrFrame, CvMat *fMat, + int cfgSide, int cfgCenter) { /* nothing */ } +#endif /* WITH_GUI */ + +/******************************************************************************/ + /** Implements the camera recognition of corns configurations. * @param capture Pointer to a camera capture. * @return Code of mode to switch to. */ @@ -557,7 +589,7 @@ int modeRecognize(CvCapture* capture, struct roi *roi) while (camera_control_on) { - if ((ret = recognizeMode_processKeys(frame)) != MODE_RECOGNIZE) + if ((ret = recognizeMode_processKeys(roi_frame)) != MODE_RECOGNIZE) return ret; //get one frame from camera (do not release!) @@ -572,10 +604,19 @@ int modeRecognize(CvCapture* capture, struct roi *roi) //ORTEPublicationSend(orte.publication_camera_result); // } - cropROI(frame, roi); + cvSetImageROI(frame, cvRect(roi->lu.x, roi->lu.y, roi->rl.x - roi->lu.x, + roi->rl.y - roi->lu.y)); + + roi_frame = cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels); + + cvCopy(frame, roi_frame, NULL); - //TODO call some image processing (do something useful with the image, FFT atc.) - recognize(frame); + //TODO call image processing (do something useful with the image, FFT atc.) + ret = recognize(roi_frame); + +#ifdef WITH_GUI + cvShowImage(WINDOW_PROD, roi_frame); +#endif /* WITH_GUI */ // publish results // orte.camera_result.side = sideConfig; @@ -655,38 +696,20 @@ void initGUI(void) { cvInitFont(&fontLarge, CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0, 2); } #else /* PPC */ - void initGUI(void) {} #endif /* WITH_GUI */ /******************************************************************************/ -/** Sets visibility of 3 analytic windows (threshold, mask and product). - * @param visible Use true to display windows, false to hide. */ -#ifdef WITH_GUI -void setAnalyticWindowsVisible(bool visible) -{ - if(visible) { - cvNamedWindow(WINDOW_ORIG, CV_WINDOW_AUTOSIZE); - } else { - cvDestroyWindow(WINDOW_ORIG); - } -} -#else /* PPC */ - -void setAnalyticWindowsVisible(bool visible) {} -#endif /* WITH_GUI */ - -/******************************************************************************/ - /** Destroys highgui windows. */ #ifdef WITH_GUI void destroyGUI(void) { cvDestroyWindow(WINDOW_ORIG); + cvDestroyWindow(WINDOW_PROD); + cvDestroyWindow(WINDOW_ROI); } #else /* PPC */ - void destroyGUI(void) {} #endif /* WITH_GUI */ @@ -730,13 +753,11 @@ int modeManager(int defaultMode, char *paramC) switch(mode) { case MODE_VIDEO: - setAnalyticWindowsVisible(false); mode = modeVideo(capture, &roi); lastMode = MODE_VIDEO; break; case MODE_RECOGNIZE: - setAnalyticWindowsVisible(true); mode = modeRecognize(capture, &roi); lastMode = MODE_RECOGNIZE; break; @@ -746,7 +767,6 @@ int modeManager(int defaultMode, char *paramC) break; case MODE_IMAGE: - setAnalyticWindowsVisible(true); mode = modeImage(paramC); lastMode = MODE_IMAGE; break; @@ -759,7 +779,7 @@ int modeManager(int defaultMode, char *paramC) _modeManager_end: cvReleaseCapture(&capture); - destroyGUI(); + //destroyGUI(); return 0; }