OpenCV to FlyCapture2 Image

2.7k Views Asked by At

I have an bumblebee2 and I'm using the flycapture SDK to capture the incoming images. I then convert the left flycapture2 image to an openCV format so I can do some basic manipulations to it. Then I'd like to feed it back into the Flycapture SDK but I cant seem to figure out how. To convert from Flycapture to OpenCV I do the following:

FlyCapture2::Image cf2Img; 
grabbedImage.Convert(FlyCapture2::PIXEL_FORMAT_BGR, &cf2Img ); 
unsigned int rowBytes = (double)cf2Img.GetReceivedDataSize()/(double)cf2Img.GetRows(); 
cv::Mat cvImage = cv::Mat( cf2Img.GetRows(), cf2Img.GetCols(), CV_8UC3, cf2Img.GetData(), rowBytes ); 

I then do my manipulations (thresholding/contour detections/background removal etc), I'd then like to feed this image back into Flycapture. My attempts at converting it back haven't worked.

Does anyone have any code they have used before to take an OpenCV format back to Flycapture?

2

There are 2 best solutions below

0
On

I work for Point Grey and I'll try to help here. Please note though you can contact us directly via our support site at ptgrey.com/support/ and we can help you out as well.

Looking at the code you attached and looking at the openCV source, when you make the cvImage, you are just reassigning the pointer to the data, you are not making an actual copy of the data.

So as long as the size of the data stays the same (ie. you keep it at 24 bits per pixel), any changes you make to the openCV image should be reflected in the flycapture (cf2Img) data and be able to save properly.

If you can explain the problems you are having trying to move back to a flycapture image, or send us the source code of how you are doing that, we can help you further.

To summarize, I expect any manipulations you do to the cvImage after the code you have provided should just be reflected in cf2Img without the need to convert back, assuming you are not changing the bit depth of the image.

I hope that helps, but please let me know if I can help clarify anything or if you can provide an example of the failure to convert back to fc2.

Thank you, Point Grey Support

0
On

With a gray-scale 8bit depth image I solved doing this with FlyCapture2 v2.13.3.61_x64 and Visual Studio:

  using namespace FlyCapture2;
  [...]   
 
  Camera cam;
  [...] 

  // Acquire the image 
  cam.RetrieveBuffer(&image);
  if (error != PGRERROR_OK)
  {
    PrintError(error);
    break;
  }

  // Copy FlyCapture2 image into OpenCV struct
  // convert to rgb
  Image rgbImage;
  image.Convert( FlyCapture2::PIXEL_FORMAT_BGR, &rgbImage );

  // convert to OpenCV Mat
  unsigned int rowBytes = 
           (double)rgbImage.GetReceivedDataSize()/(double)rgbImage.GetRows();
       
  cv::Mat img = cv::Mat(rgbImage.GetRows(), rgbImage.GetCols(), CV_8UC3, 
                        rgbImage.GetData(),rowBytes);

  // show the captured image
  cv::imshow("image", img);
  key = cv::waitKey(30);   

This is also good for me:


using namespace FlyCapture2;

const int col_size   = 24;
const int row_size   = 480;
const int data_size  = row_size * col_size;

[...]

// Grab camera grayscale image and convert into
// an OpenCV image
void GrabImages( Camera* pcam,
                 IplImage* pimg,
                 IplImage* pimg_bw,
                 CvMemStorage* pstorage,
                 int cnt )
{
    Image image;
    [...]
 
    // Retrieve image before starting main loop
    Error error = pcam->RetrieveBuffer( &image );
    if ( error != PGRERROR_OK )
    {
        // TODO.
    }
 
    // Copy FlyCapture2 image into OpenCV struct
    memcpy( pimg->imageData, image.GetData(), data_size );
 
    // Save the bitmap to file
    cvSaveImage( "orig.bmp", pimg );

    [...]