コード例 #1
0
    CvImage& copy( const CvImage& another )
    {
        if( !imageData || width != another.width || height != another.height )
        {
            cvReleaseData( this );
            cvResetImageROI( this );
            cvInitImageHeader( this, cvSize( another.width, another.height),
                               another.depth, another.nChannels, another.origin,
                               another.align );
            cvCreateImageData( this );
            if( another.roi )
                cvSetImageROI( this, cvGetImageROI( &another ));
        }

        cvCopy( (IplImage *)&another, this );
        return *this;
    }
コード例 #2
0
ファイル: camcal.cpp プロジェクト: zxie/surgical
//
// Function: FindCorners
// Purpose : This function finds the corner points in the calibration images
//           using opencv functions cvFindChessBoardCornerGuesses and
//           cvFindCornerSubPix.  After image pixel coordinates are found, their
//           respective world coordinates are assigned. (z coordinate is always
//           zero). When the function cannot find the specified number of
//           coordinates in the image, the image is discarded because of the
//           added complexity in assigning their world coordinates.
// Output  : uveff : coordinates of the chessborder in image plane.
//           XYZeff: coordinates of uveff w.r.t World Coordinate System
//           m_effective_image_no : number of images processed.
//----------------------------------------------------------------------------
void camcal::FindCorners()
{
   IplImage* img  = 0;
   IplImage* img0 = 0;
   IplImage* img1 = 0;
   IplImage* greyimg = 0;

   CvFont dfont;
   cvInitFont (&dfont, CV_FONT_VECTOR0, 0.3, 0.3, 0.0f, 1);

   CvPoint onecorner;

   int numcorners = m_corner_no;

   CvPoint2D64d* uv  = new CvPoint2D64d[m_image_number * m_corner_no];
   CvPoint3D64d* XYZ = new CvPoint3D64d[m_image_number * m_corner_no];

   CvPoint2D32f* corners = new CvPoint2D32f[m_corner_no];
   CvMemStorage* storage = 0;

   m_effective_image_no=-1;

   for( int imgnum=0; imgnum<m_image_number; imgnum++ )
   {
      numcorners = m_corner_no;

      img = m_input_images[imgnum];

      imgsize.width = img->width;
      imgsize.height= img->height;

      img0 = cvCloneImage(img);
      img1 = cvCloneImage(img);
      greyimg = cvCreateImageHeader(imgsize,IPL_DEPTH_8U,1);
      cvCreateImageData(greyimg);

      cvCvtColor(img, greyimg, CV_RGB2GRAY);
      img0 = cvCloneImage(greyimg);

      cvFindChessBoardCornerGuesses(greyimg,
                                    img0,
                                    storage,
                                    cvSize(m_x_height,m_x_width),
                                    corners,
                                    &numcorners);

      if( numcorners != m_corner_no ) {
         cvReleaseImage( &img0 );
         cvReleaseImage( &img1 );
         cvReleaseImage( &greyimg );
         continue;
      }
      else
         m_effective_image_no++;

      // draw a circle at each corner found
      for( int t = 0; t < numcorners; t++ )
      {
         onecorner.x = (int)corners[t].x;
         onecorner.y = (int)corners[t].y;

         cvCircle(img1, onecorner, 8, CV_RGB(0,255,0),2);
         // image, center, radius, color, thickness
      }

      // Find sub-corners
      cvFindCornerSubPix(greyimg,
                         corners,
                         numcorners,
                         cvSize (m_x_height,m_x_width),
                         cvSize(-1, -1),
                         cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, 0.1));

      // correct order
      if( m_apply_ordering ){
         CvPoint3D32f init = FindRectangleCorner( corners, numcorners );
         if( init.z < m_x_height*m_x_width )
            SortPoints(corners, numcorners, &init);
         else
         {
            cout<<"Sort Error";
            cvReleaseImage( &img0 );
            cvReleaseImage( &img1 );
            cvReleaseImage( &greyimg );
            m_effective_image_no--;
            continue;
         }
      }

      //draw a circle and put the corner number at each subcorner found
      for( int t = 0; t < numcorners; t++ )
      {
         onecorner.x = (int)corners[t].x;
         onecorner.y = (int)corners[t].y;

         cvCircle(img1, onecorner, 3, CV_RGB(255,0,0),1);

         char buf[10];
         sprintf( buf, "%d", t );

//          cvPutText(img1,numbers[t], cvPoint(onecorner.x, onecorner.y + 20), &dfont, CV_RGB(255,0,0));
         cvPutText(img1, buf, cvPoint(onecorner.x, onecorner.y + 20), &dfont, CV_RGB(255,0,0));
      }

      // CAMERA CALIBRATION PART
      for( int currPoint=0; currPoint < numcorners; currPoint++ )
      {
         uv[ m_effective_image_no*numcorners + currPoint].x = corners[currPoint].x;
         uv[ m_effective_image_no*numcorners + currPoint].y = corners[currPoint].y;
      }

      int index;
      for( int i = 0; i < m_x_width; i++ )
      {
         for( int j = 0; j < m_x_height; j++ )
         {
            index = m_effective_image_no*numcorners + i*m_x_height+j;

            XYZ[ index ].x = m_grid_width *(m_x_width -i);
            XYZ[ index ].y = m_grid_height*(m_x_height-j);
            XYZ[ index ].z = 0;
         }
      }

      if( m_display_corners )
      {
         cvvNamedWindow( "image", 1 );
         cvvShowImage("image",img1);
         cvvWaitKey(0);
         cvDestroyWindow( "image" );
      }

//		cvReleaseImage( &img );
      cvReleaseImage( &img0 );
      cvReleaseImage( &img1 );
      cvReleaseImage( &greyimg );

   } //loop to next image
   free (corners);

   m_effective_image_no++;

   delete []uveff ; uveff  =NULL;
   delete []XYZeff; XYZeff = NULL;

   if( m_image_number == m_effective_image_no ){
      uveff  = uv;  uv  = NULL;
      XYZeff = XYZ; XYZ = NULL;
   }
   else
   {
      int size = m_effective_image_no * m_corner_no;
      uveff  = new CvPoint2D64d[size];
      XYZeff = new CvPoint3D64d[size];

      for(int ph=0; ph<size; ph++)
      {
         uveff [ph] = uv [ph];
         XYZeff[ph] = XYZ[ph];
      }
      delete []uv;  uv  = NULL;
      delete []XYZ; XYZ = NULL;
   }
}