From 5d7940def631cd172844bb73ed91eeb6639f647a Mon Sep 17 00:00:00 2001 From: Michal Vokac Date: Mon, 22 Nov 2010 19:17:05 +0100 Subject: [PATCH] New, better definition of the ROI structure. Now defined like a CvRect, which is implicit parameter for cvSetImageROI(). Prolem with memory owerflow solved - do not use cvCreateImage() in cycles! --- src/camera/barcam/barcam.cxx | 288 +++++++++++++++++------------------ 1 file changed, 137 insertions(+), 151 deletions(-) diff --git a/src/camera/barcam/barcam.cxx b/src/camera/barcam/barcam.cxx index 4b249ff8..089e0c11 100644 --- a/src/camera/barcam/barcam.cxx +++ b/src/camera/barcam/barcam.cxx @@ -49,6 +49,7 @@ #include #include #include +#include #include #include @@ -74,18 +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 WINDOW_WIDTH/2 - 40 -#define ROI_LU_Y 0 -#define ROI_RL_X WINDOW_WIDTH/2 + 40 -#define ROI_RL_Y WINDOW_HEIGHT - 20 +//TODO tune size of region of interest (crop camera view) +#define ROI_X 0 +#define ROI_Y 0 +#define ROI_WIDTH WINDOW_WIDTH +#define ROI_HEIGHT WINDOW_HEIGHT // values of keys #define KEY_ESC 0x1B @@ -128,8 +128,7 @@ extern "C" { /********************** multimode graphical functions *************************/ int saveFrame(const CvArr *img); -void drawPauseText(IplImage *img); -void selectROI(CvCapture* capture, struct roi *roi); +void selectROI(CvCapture* capture, CvRect *roi); /******************** multimode computational functions ***********************/ int countThreshold(const IplImage *frame); @@ -139,13 +138,12 @@ int recognize(const CvMat *frame); int modeImage(char *imageFilename); /********************************* MODE_VIDEO *********************************/ -int modeVideo(CvCapture* capture, struct roi *roi); +int modeVideo(CvCapture* capture, CvRect *roi); /****************************** MODE_RECOGNIZE ********************************/ int recognizeMode_processKeys(IplImage *frame); -void displayFrames(IplImage *frame, IplImage **thrFrame, CvMat *fMat, - int cfgSide, int cfgCenter); -int modeRecognize(CvCapture* capture, struct roi *roi); +void displayFrames(IplImage *frame); +int modeRecognize(CvCapture* capture, CvRect *roi); /********************************* MODE_WAIT **********************************/ int waitMode_processKeys(int previousMode); @@ -176,11 +174,6 @@ bool camera_control_on = false; CvFont fontLarge; -struct roi { - CvPoint lu; - CvPoint rl; -}; - /******************************************************************************/ /********************** multimode graphical functions *************************/ @@ -204,30 +197,13 @@ int saveFrame(const CvArr *img) return cvSaveImage(filename, img); } -/******************************************************************************/ - -/** Writes text "pause" in the CAMERA window. - * @param img Frame to display with PAUSE text. */ -#ifdef WITH_GUI -void drawPauseText(IplImage *img) -{ - cvPutText(img, "PAUSE", cvPoint(img->width/2 - 40,img->height - 50), - &fontLarge, cvScalar(0,0,255)); - cvShowImage(WINDOW_ORIG, img); -} -#else /* PPC */ - -void drawPauseText(IplImage *img) {} -#endif /* WITH_GUI */ - - /******************************************************************************/ /** Setting region of interest in the ROI window. * @param capture Pointer to a camera capture. - * @param roi Pointer to ROI structure */ + * @param roi Pointer to ROI rectangle. */ #ifdef WITH_GUI -void selectROI(CvCapture* capture, struct roi *roi) +void selectROI(CvCapture* capture, CvRect *roi) { IplImage* frame = NULL; int step = 10; @@ -237,7 +213,6 @@ void selectROI(CvCapture* capture, struct roi *roi) switch(cvWaitKey(10) & 0xFF) { // exit ROI setting case KEY_ESC: - cvDestroyWindow(WINDOW_ROI); return; case KEY_PLUS: @@ -253,73 +228,78 @@ void selectROI(CvCapture* capture, struct roi *roi) case KEY_R: case KEY_r: // reset ROI to full frame - roi->lu.x = 0; - roi->lu.y = 0; - roi->rl.x = WINDOW_WIDTH; - roi->rl.y = WINDOW_HEIGHT; + roi->x = 0; + roi->y = 0; + roi->width = WINDOW_WIDTH; + roi->height = WINDOW_HEIGHT; break; case KEY_1: // increase left border - roi->lu.x -= step; + roi->x -= step; + roi->width += step; break; case KEY_2: // decrease left border - roi->lu.x += step; + roi->x += step; + roi->width -= step; break; case KEY_3: // increase upper border - roi->lu.y -= step; + roi->y -= step; + roi->height += step; break; case KEY_4: // decrease upper border - roi->lu.y += step; + roi->y += step; + roi->height -= step; break; case KEY_5: // decrease lower border - roi->rl.y -= step; + roi->height -= step; break; case KEY_6: // increase lower border - roi->rl.y += step; + roi->height += step; break; case KEY_7: // decrease right border - roi->rl.x -= step; + roi->width -= step; break; case KEY_8: // increase right border - roi->rl.x += step; + roi->width += step; break; } - 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 x: %d x: %d width: %d height: %d\n", + step, roi->x, roi->y, roi->width, roi->height); + 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); - - cvShowImage(WINDOW_ROI, frame); + cvRectangle(frame, cvPoint(roi->x, roi->y), cvPoint(roi->width + roi->x, + roi->height + roi->y), + cvScalar(0,0,255), 2, 8, 0); + cvShowImage(WINDOW_ORIG, frame); } } #else /* PPC */ -void selectROI(CvCapture *capture, struct roi *roi) {} +void selectROI(CvCapture *capture, CvRect *roi) {} #endif /* WITH_GUI */ /******************************************************************************/ @@ -337,64 +317,79 @@ int countThreshold(const IplImage *frame) /******************************************************************************/ /** Returns an ordinary number of recognized configuration. - * @param frame Float representation of frame. + * @param frame Frame to be processed. * @return */ -int recognize(IplImage* roi_frame) { +int recognize(IplImage* fft) { - int i,j; + int i,j,k; + + //IplImage* fft = NULL; + + //cvCvtColor( roi_frame, fft, CV_BGR2GRAY ); - /* 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; + // example of accessing image data (invert the image) + for(i = 0; i < fft->height; i++) { + for(j = 0; j < fft->width; j++) { + for(k = 0; k < fft->nChannels; k++) { + fft->imageData[i*fft->widthStep + j*fft->nChannels + k] = + 255 - fft->imageData[i*fft->widthStep + j*fft->nChannels + k]; } - } - -// IplImage* dst = cvCreateImage( cvGetSize(roi_frame), roi_frame->depth, 1 ); -// IplImage* color_dst = cvCreateImage( cvGetSize(roi_frame), roi_frame->depth, roi_frame->nChannels); -// + } + } + + // cvDFT(fft, fft, CV_DXT_FORWARD, fft->height); + // CvMemStorage* storage = cvCreateMemStorage(0); // CvSeq* lines = 0; // - +// +// cvCanny(dst, dst, 50, 200, 3 ); // -// 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 ); +// cvCvtColor( dst, color_dst, CV_GRAY2BGR ); +// +// if (0) { +// lines = cvHoughLines2( dst, +// storage, +// CV_HOUGH_STANDARD, +// 1, +// CV_PI/180, +// 100, +// 0, +// 0 ); // -// cvWaitKey(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 ); +// } +// } else { +// +// lines = cvHoughLines2( dst, +// storage, +// CV_HOUGH_PROBABILISTIC, +// 1, +// CV_PI/180, +// 80, +// 30, +// 10 ); +// for( i = 0; i < lines->total; i++ ) +// { +// CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i); +// cvLine( color_dst, line[0], line[1], CV_RGB(255,0,0), 3, 8 ); +// } +// } +// +// cvClearMemStorage(storage); - return 0; } @@ -450,13 +445,13 @@ int modeImage(char *imageFilename) { /** Displays just a real video in a window (only on PC). * @param capture Pointer to a camera capture. + * @param roi Pointer to ROI rectangle. * @return Code of mode to switch to. */ #ifdef WITH_GUI -int modeVideo(CvCapture* capture, struct roi *roi) +int modeVideo(CvCapture* capture, CvRect *roi) { IplImage *frame = NULL; - IplImage *roi_frame = NULL; - + while (1) { // wait 10ms for an event switch(cvWaitKey(10) & 0xFF) { @@ -472,19 +467,20 @@ int modeVideo(CvCapture* capture, struct roi *roi) // pause - switch to wait mode case KEY_P: case KEY_p: - drawPauseText(roi_frame); return MODE_WAIT; // save frame to file case KEY_S: case KEY_s: - saveFrame(roi_frame); + saveFrame(frame); break; + // display ROI select window case KEY_O: case KEY_o: cvResetImageROI(frame); selectROI(capture, roi); + cvSetImageROI(frame, *roi); break; } @@ -497,21 +493,14 @@ int modeVideo(CvCapture* capture, struct roi *roi) continue; } - 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, roi_frame); + cvShowImage(WINDOW_ORIG, frame); } return MODE_QUIT; } #else /* PPC */ -int modeVideo(CvCapture* capture) { +int modeVideo(CvCapture* capture, CvRect *roi) { fprintf(stderr, "Video mode is unsupported on PPC.\nTerminating.\n"); return MODE_QUIT; } @@ -525,7 +514,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 *roi_frame) +int recognizeMode_processKeys(IplImage *frame) { // wait 10ms for an event switch (cvWaitKey(10) & 0xFF) { @@ -541,9 +530,14 @@ int recognizeMode_processKeys(IplImage *roi_frame) // pause - wait mode case KEY_P: case KEY_p: - 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; } @@ -558,35 +552,32 @@ 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. */ + * @param window_name Name of the window to show frame in. + */ #ifdef WITH_GUI -void displayFrames(IplImage *roi_frame) { - cvShowImage(WINDOW_PROD, roi_frame); +void displayFrames(const char* window_name, IplImage *frame) { + cvShowImage(window_name, frame); } #else /* PPC */ -void displayFrames(IplImage *frame, IplImage **thrFrame, CvMat *fMat, - int cfgSide, int cfgCenter) { /* nothing */ } +void displayFrames(const char* window_name, IplImage *frame) { /* nothing */ } #endif /* WITH_GUI */ /******************************************************************************/ /** Implements the camera recognition of corns configurations. * @param capture Pointer to a camera capture. + * @param roi Pointer to ROI rectangle. * @return Code of mode to switch to. */ -int modeRecognize(CvCapture* capture, struct roi *roi) +int modeRecognize(CvCapture* capture, CvRect *roi) { int ret; IplImage *frame = NULL; - IplImage *roi_frame = NULL; - + IplImage *roi_frame = cvCreateImage(cvSize(roi->width, roi->height), 8, 3); + while (camera_control_on) { if ((ret = recognizeMode_processKeys(roi_frame)) != MODE_RECOGNIZE) @@ -604,20 +595,16 @@ int modeRecognize(CvCapture* capture, struct roi *roi) //ORTEPublicationSend(orte.publication_camera_result); // } - 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); - + cvSetImageROI(frame, *roi); + + cvCopy(frame, roi_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 */ - + + displayFrames(WINDOW_ORIG, frame); + displayFrames(WINDOW_PROD, roi_frame); + // publish results // orte.camera_result.side = sideConfig; // orte.camera_result.center = centerConfig; @@ -707,7 +694,6 @@ void destroyGUI(void) { cvDestroyWindow(WINDOW_ORIG); cvDestroyWindow(WINDOW_PROD); - cvDestroyWindow(WINDOW_ROI); } #else /* PPC */ void destroyGUI(void) {} @@ -724,12 +710,12 @@ int modeManager(int defaultMode, char *paramC) int mode = defaultMode; int lastMode = MODE_RECOGNIZE; CvCapture* capture = NULL; - struct roi roi; - - roi.lu.x = ROI_LU_X; - roi.lu.y = ROI_LU_Y; - roi.rl.x = ROI_RL_X; - roi.rl.y = ROI_RL_Y; + CvRect roi; + + roi.x = ROI_X; + roi.y = ROI_Y; + roi.width = ROI_WIDTH; + roi.height = ROI_HEIGHT; if (defaultMode == MODE_IMAGE) { fprintf(stderr, "barcam started in image mode\n"); -- 2.39.2