1

How can I convert a Pylon image (from the Basler camera library) to a DIPlib image in the C++ language?

The code below illustrates how to convert the Pylon image to an OpenCV image:

// Convert the grabbed buffer to a pylon image.
formatConverter.Convert(pylonImage, ptrGrabResult);

// Create an OpenCV image from a pylon image.
openCvImage = cv::Mat(ptrGrabResult->GetHeight(), ptrGrabResult->GetWidth(), CV_8UC3, (uint8_t *)pylonImage.GetBuffer());
cvtColor(openCvImage, openCvImage, COLOR_BGR2GRAY);
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120

1 Answers1

0

I searched online for documentation to the Pylon API, but couldn't find it. Nonetheless, using the example for OpenCV you gave, it is easy to do the equivalent in DIPlib:

dip::Image image(
   NonOwnedRefToDataSegment(pylonImage.GetBuffer()), // a random, non-owning pointer
   pylonImage.GetBuffer(),          // data
   dip::DT_UINT8,                   // pixel type (8-bit unsigned)
   { ptrGrabResult->GetWidth(),
     ptrGrabResult->GetHeight() },  // width and heigh
   {},                              // this one can stay empty
   dip::Tensor{3}                   // 3 channels
);
image.SetColorSpace("RGB");

See the documentation to this constructor as well as this section of the DIPlib documentation for more details.

Alternatively, you can use this simplified alternative to the constructor above, making the code a lot simpler. This constructor requires "normal" data ordering, which you do have in this case:

dip::Image image(
   static_cast<uint8_t*>(pylonImage.GetBuffer()) // data pointer
   { ptrGrabResult->GetWidth(),
     ptrGrabResult->GetHeight() },               // width and heigh
   3                                             // 3 channels
);
image.SetColorSpace("RGB");
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • Severity Code Description Project File Line Suppression State Error C2664 'dip::Image::Image(const dip::DataSegment &,void *,dip::DataType,dip::UnsignedArray,dip::IntegerArray,const dip::Tensor &,dip::sint,dip::ExternalInterface *)': cannot convert argument 1 from 'uint8_t *' to 'dip::UnsignedArray' servoqei c:\users\tcc\source\repos\servoqei\servoqei\pylonframe.cpp 93 I am receiving above error message – Ahmet Manyasli Apr 22 '20 at 15:24
  • @AhmetManyasli: Could you please submit an issue in the [issue tracker](https://github.com/DIPlib/diplib/issues), including more of the code and the full error message? It'll be easier to communicate that way than through comments here. Thanks! – Cris Luengo Apr 22 '20 at 15:28