stereoCam::stereoCam() {

   
   // Find cameras on the 1394 buses
   d = dc1394_new ();

   // Enumerate cameras connected to the PC
   err = dc1394_camera_enumerate (d, &list);
   if ( err != DC1394_SUCCESS )
   {
       fprintf( stderr, "Unable to look for cameras\n\n"
             "Please check \n"
	     "  - if the kernel modules `ieee1394',`raw1394' and `ohci1394' "
	     "are loaded \n"
             "  - if you have read/write access to /dev/raw1394\n\n");
       nThisCam=-1;
       //return 1;
    }

    if (list->num == 0)
    {
        fprintf( stderr, "No cameras found!\n");
        nThisCam=-1;
        //return 1;
    }

    printf( "There were %d camera(s) found attached to your PC\n", list->num  );

    // Identify cameras. Use the first stereo camera that is found
    for ( nThisCam = 0; nThisCam < list->num; nThisCam++ )
    {
        camera = dc1394_camera_new(d, list->ids[nThisCam].guid);

        if(!camera)
        {
            printf("Failed to initialize camera with guid %llx", 
		   list->ids[nThisCam].guid);
            continue;
        }

        printf( "Camera %d model = '%s'\n", nThisCam, camera->model );

        if ( isStereoCamera(camera))
        {
            printf( "Using this camera\n" );
            break;
        }
        dc1394_camera_free(camera);
   }

   if ( nThisCam == list->num )
   {
      printf( "No stereo cameras were detected\n" );
      nThisCam=-1;
      //return 0;
   }
   // Free memory used by the camera list
   dc1394_camera_free_list (list);


   // query information about this stereo camera
   err = queryStereoCamera( camera, &stereoCamera );
   if ( err != DC1394_SUCCESS )
   {
      fprintf( stderr, "Cannot query all information from camera\n" );
      cleanup_and_exit( camera );
   }

   if ( stereoCamera.nBytesPerPixel != 2 )
   {
      // can't handle XB3 3 bytes per pixel
      fprintf( stderr,
	       "Example not updated to work with XB3 in 3 camera mode yet!\n" );
      cleanup_and_exit( stereoCamera.camera );
   }

   // set the capture mode
   printf( "Setting stereo video capture mode\n" );
   err = setStereoVideoCapture( &stereoCamera );
   if ( err != DC1394_SUCCESS )
   {
      fprintf( stderr, "Could not set up video capture mode\n" );
      cleanup_and_exit( stereoCamera.camera );
   }

   // have the camera start sending us data
   printf( "Start transmission\n" );
   err = startTransmission( &stereoCamera );
   if ( err != DC1394_SUCCESS )
   {
      fprintf( stderr, "Unable to start camera iso transmission\n" );
      cleanup_and_exit( stereoCamera.camera );
   }

   // give the auto-gain algorithms a chance to catch up
   //   printf( "Giving auto-gain algorithm a chance to stabilize\n" );
   //   sleep( 5 );

   // Allocate all the buffers.
   // Unfortunately color processing is a bit inefficient 
   // because of the number of
   // data copies.  Color data needs to be
   // - de-interleaved into separate bayer tile images
   // - color processed into RGB images
   // - de-interleaved to extract the green channel 
   //   for stereo (or other mono conversion)

   // size of buffer for all images at mono8
   nBufferSize = stereoCamera.nRows *
                                stereoCamera.nCols *
                                stereoCamera.nBytesPerPixel;
   // allocate a buffer to hold the de-interleaved images
   pucDeInterlacedBuffer = new unsigned char[ nBufferSize ];
   pucRGBBuffer 	= new unsigned char[ 3 * nBufferSize ];
   pucGreenBuffer 	= new unsigned char[ nBufferSize ];

   // do stereo processing

   printf( "Getting TriclopsContext from camera (slowly)... \n" );
   e = getTriclopsContextFromCamera( &stereoCamera, &triclops );
   TRIERR(e);
   printf( "...got it\n" );

   // make sure we are in subpixel mode
   e = triclopsSetSubpixelInterpolation( triclops, 1 );
   TRIERR(e);
   e = triclopsSetDisparityMapping(triclops,0,60);
   TRIERR(e);
   e = triclopsSetDisparityMappingOn(triclops,1);
   TRIERR(e);
   e = triclopsSetUniquenessValidationMapping(triclops,0);
   TRIERR(e);
   e = triclopsSetTextureValidationMapping(triclops,0);
   TRIERR(e);

   isInRoutine=false;

}
Example #2
0
int main( int argc, char *argv[] )
{
   dc1394camera_t* 	camera;
   dc1394error_t 	err;
   dc1394_t * d;
   dc1394camera_list_t * list;
   unsigned int nThisCam;

   // Find cameras on the 1394 buses
   d = dc1394_new ();

   // Enumerate cameras connected to the PC
   err = dc1394_camera_enumerate (d, &list);
   if ( err != DC1394_SUCCESS )
   {
       fprintf( stderr, "Unable to look for cameras\n\n"
             "Please check \n"
	         "  - if the kernel modules `ieee1394',`raw1394' and `ohci1394' "
	         "are loaded \n"
	         "  - if you have read/write access to /dev/raw1394\n\n");
       return 1;
    }

    if (list->num == 0)
    {
        fprintf( stderr, "No cameras found!\n");
        return 1;
    }

    printf( "There were %d camera(s) found attached to your PC\n", list->num  );

    // Identify cameras. Use the first stereo camera that is found
    for ( nThisCam = 0; nThisCam < list->num; nThisCam++ )
    {
        camera = dc1394_camera_new(d, list->ids[nThisCam].guid);

        if(!camera)
        {
            printf("Failed to initialize camera with guid %llx", list->ids[nThisCam].guid);
            continue;
        }

        printf( "Camera %d model = '%s'\n", nThisCam, camera->model );

        if ( isStereoCamera(camera))
        {
            printf( "Using this camera\n" );
            break;
        }
        dc1394_camera_free(camera);
   }

   if ( nThisCam == list->num )
   {
      printf( "No stereo cameras were detected\n" );
      return 0;
   }

   // Free memory used by the camera list
   dc1394_camera_free_list (list);

   PGRStereoCamera_t stereoCamera;

   // query information about this stereo camera
   err = queryStereoCamera( camera, &stereoCamera );
   if ( err != DC1394_SUCCESS )
   {
      fprintf( stderr, "Cannot query all information from camera\n" );
      cleanup_and_exit( camera );
   }

   // set the capture mode
   printf( "Setting stereo video capture mode\n" );
   err = setStereoVideoCapture( &stereoCamera );
   if ( err != DC1394_SUCCESS )
   {
      fprintf( stderr, "Could not set up video capture mode\n" );
      cleanup_and_exit( stereoCamera.camera );
   }

   // have the camera start sending us data
   printf( "Start transmission\n" );
   err = startTransmission( &stereoCamera );
   if ( err != DC1394_SUCCESS )
   {
      fprintf( stderr, "Unable to start camera iso transmission\n" );
      cleanup_and_exit( stereoCamera.camera );
   }

   // give the auto-gain algorithms a chance to catch up
   printf( "Wait for the auto-gain algorithm to stabilize\n" );
   sleep( 5 );


   // Allocate all the buffers.
   // Unfortunately color processing is a bit inefficient because of the number of
   // data copies.  Color data needs to be
   // - de-interleaved into separate bayer tile images
   // - color processed into RGB images
   // - de-interleaved to extract the green channel for stereo (or other mono conversion)

   // size of capture buffer
   unsigned int   nBufferSize = stereoCamera.nRows *
                                stereoCamera.nCols *
                                stereoCamera.nBytesPerPixel;
   // allocate a buffer to hold the de-interleaved images
   unsigned char* pucDeInterlacedBuffer = new unsigned char[ nBufferSize ];

   if ( stereoCamera.bColor )
   {
      unsigned char* pucRGBBuffer 	= new unsigned char[ 3 * nBufferSize ];
      unsigned char* pucGreenBuffer 	= new unsigned char[ nBufferSize ];
      unsigned char* pucRightRGB	= NULL;
      unsigned char* pucLeftRGB		= NULL;
      unsigned char* pucCenterRGB	= NULL;
      TriclopsInput input;

      // get the images from the capture buffer and do all required processing
      // note: produces a TriclopsInput that can be used for stereo processing
      extractImagesColor( &stereoCamera,
			  DC1394_BAYER_METHOD_NEAREST,
			  pucDeInterlacedBuffer,
			  pucRGBBuffer,
			  pucGreenBuffer,
			  &pucRightRGB,
			  &pucLeftRGB,
			  &pucCenterRGB,
			  &input );

      // write the color images to file
      if ( !writePpm( "right.ppm", pucRightRGB, stereoCamera.nCols, stereoCamera.nRows ) )
	 printf( "wrote right.ppm\n" );
      if ( !writePpm( "left.ppm", pucLeftRGB, stereoCamera.nCols, stereoCamera.nRows ) )
	 printf( "wrote left.ppm\n" );
      if ( pucCenterRGB != pucLeftRGB )
	 if ( !writePpm( "center.ppm", pucCenterRGB, stereoCamera.nCols, stereoCamera.nRows ) )
	    printf( "wrote center.ppm\n" );

      delete[] pucRGBBuffer;
      delete[] pucGreenBuffer;
   }
   else
   {
      unsigned char* pucRightMono	= NULL;
      unsigned char* pucLeftMono	= NULL;
      unsigned char* pucCenterMono	= NULL;
      TriclopsInput input;
      // get the images from the capture buffer and do all required processing
      // note: produces a TriclopsInput that can be used for stereo processing
      extractImagesMono( &stereoCamera,
			  pucDeInterlacedBuffer,
			  &pucRightMono,
			  &pucLeftMono,
			  &pucCenterMono,
			  &input );


      // write the greyscale images to file
      if ( !writePgm( "right.pgm", pucRightMono, stereoCamera.nCols, stereoCamera.nRows ) )
	 printf( "wrote right.pgm\n" );
      if ( !writePgm( "left.pgm", pucLeftMono, stereoCamera.nCols, stereoCamera.nRows ) )
	 printf( "wrote left.pgm\n" );
      if ( pucCenterMono != pucLeftMono )
	 if ( !writePgm( "center.pgm", pucCenterMono, stereoCamera.nCols, stereoCamera.nRows ) )
	    printf( "wrote center.pgm\n" );
   }

   printf( "Stop transmission\n" );
   //  Stop data transmission
   if ( dc1394_video_set_transmission( stereoCamera.camera, DC1394_OFF ) != DC1394_SUCCESS )
   {
      fprintf( stderr, "Couldn't stop the camera?\n" );
   }

   delete[] pucDeInterlacedBuffer;

   // close camera
   cleanup_and_exit( camera );

   return 0;
}