c++opencvimage-processingcomputer-visiondiplib

Basler camera and DIPlib


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);

Solution

  • 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");