|
Here I will use OpenCV on the bitmap loaded on step one.
In the CVB right click on "MyIplClass" and then on "Add member function", let the type be "void" and in "function declaration" write "ProcessIpl()".
I will do all the image processing inside this function.
I'll use the CVSnake on the bitmap and display the result
In the function "ProcessIpl" add the code below:
- - - Start cut'n paste - - -
     IplImage *IplTmp1,*IplTmp2,*IplTmp3;
     IplTmp1 = cvCreateImage( cvSize(IMAGE_WIDTH,IMAGE_HEIGHT), IPL_DEPTH_8U, 1 );
     IplTmp2 = cvCreateImage( cvSize(IMAGE_WIDTH,IMAGE_HEIGHT), IPL_DEPTH_8U, 1 );
     IplTmp3 = cvCreateImage( cvSize(IMAGE_WIDTH,IMAGE_HEIGHT), IPL_DEPTH_8U, 1 );
     int ThressValue = 90;
     float alpha = 3;
     float beta = 5;
     float gamma = 2;
     CvSize win;
     CvTermCriteria criteria;
     int jumpPoint;
     CvPoint *WholePointArray;
     CvPoint *PointArray;
     const int NUMBER_OF_SNAKE_POINTS= 50;
     PointArray = (CvPoint *)malloc(NUMBER_OF_SNAKE_POINTS * sizeof(CvPoint));
     cvCopyImage( m_Ipl,IplTmp3); //IplTmp3 is the image to draw temp results to
     cvCopyImage( m_Ipl, IplTmp1);//(src,dst) copy to Working ipl image (IplTmp1 and IplTmp2 is working containers)
     // Make a average filtering
     cvSmooth(IplTmp1,IplTmp2,CV_BLUR,31,15);
     // iplBlur( IplTmp1, IplTmp2, 31, 31, 15, 15); //Don't use IPL
     //Do a threshold
     cvThreshold(IplTmp2,IplTmp1,ThressValue,255,CV_THRESH_BINARY);
     //iplThreshold(IplTmp2,IplTmp1,ThressValue); // DistImg is thressholded image (IplTmp1)//Don't use IPL
     // expand the thressholded image of ones -smoothing the edge.
     //And move start position of snake out since there are no ballon force
     cvDilate( IplTmp1, IplTmp2, NULL, 3);
     //Find the contours
     CvMemStorage *storage;
     CvSeq* contour = NULL;
     storage = cvCreateMemStorage (0);
     cvFindContours( IplTmp2,storage, &contour,sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE );
     //Run through the found coutours
     while( contour != NULL )
     {
          if ( contour ->total >= NUMBER_OF_SNAKE_POINTS)
          {
               cvSmooth(m_Ipl,IplTmp2,CV_BLUR,7,3);
               //iplBlur( m_Ipl, IplTmp2, 7, 7, 3, 3); // Put blured image in TempImg//Don't use IPL
               WholePointArray = (CvPoint *)malloc(contour->total * sizeof(CvPoint));
               cvCvtSeqToArray(contour, WholePointArray, CV_WHOLE_SEQ); //Copy the contour to a array
               // Number of jumps between the desired points (downsample only!)
               jumpPoint = (int)(contour->total / NUMBER_OF_SNAKE_POINTS);
               for (int i=0; i < NUMBER_OF_SNAKE_POINTS; i++)
               {
                    PointArray[i].x = WholePointArray[ (int)( i*jumpPoint)].x;
                    PointArray[i].y = WholePointArray[ (int)( i*jumpPoint)].y;
               }
               // Now for the snake
               criteria.maxIter = 100; // Do max N iterations
               criteria.epsilon = 1; // If only N points is moved then terminate
               criteria.type = CV_TERMCRIT_EPS|CV_TERMCRIT_ITER;
               win.width = 21; // search for energi minimizing in this area around snake points
               win.height = 21; // Be sure that width and heigth is uneven
               // Iterate snake
               cvSnakeImage(IplTmp2, PointArray, NUMBER_OF_SNAKE_POINTS, &alpha, &beta, &gamma, CV_VALUE, win, criteria,1);
               // Draw snake on image
               int n = NUMBER_OF_SNAKE_POINTS;
               cvPolyLine( IplTmp3, &PointArray, &n, 1,1, 255, 3,8 );
               free(WholePointArray);
          }
          //Get next contour
          contour = contour->h_next;
     }
     //Clean up
     free (contour);
     free(PointArray);
     cvReleaseMemStorage(&storage);
     //Save result
     cvCopyImage( IplTmp3, m_Ipl);//(src,dst)
     // Clean up
     cvReleaseImage(&IplTmp1);
     cvReleaseImage(&IplTmp2);
     cvReleaseImage(&IplTmp3);
- - - End cut'n paste - - -
Then go to the OnProcess() through the CVB and add following line just after the line "Img->LoadBMP(filename);"
- - - Start cut'n paste - - -
Img->ProcessIpl();
- - - End cut'n paste - - -
Thats it hit F5 to build and run, when the "Process" button is pressed the ring appears with a superimposed "snake".
Press on the Start to go to start of tutorial.
Press on the Back to go to start of tutorial.
Press on the Next to load a tiff file into the ipl structure.
^ back to top ^
Last updated:
May 3 - 2004: My counter from freewebcounter.com created popups, so a new counter is used.
June 18 - 2003 Removed dependency of the IPL library.
May 5 - 2003.
|