Skip to content

How to: Convert a pylon GrabResult Into a MVTec HALCON HImage#

If you want to use pylon for image acquisition and MVTec Halcon for further image processing, you must convert the pylon GrabResult into a Halcon HImage.

The following code snippets show the general set up for Halcon and image creation for the following pixel formats: Mono8, BGR / RGB, and also Bayer pixel formats (by using the pylon CImageFormatConverter class).

The code below shows how to set up a HALCON HWindow for displaying a HALCON HImage.

...
/* Create an instant camera object with the first camera device found */
CInstantCamera camera(CTlFactory::GetInstance().CreateFirstDevice());

/* Get the camera nodes to Width and Height */
camera.Open();
INodeMap& nodemap = camera.GetNodeMap();
CIntegerPtr Width = nodemap.GetNode("Width");
CIntegerPtr Height = nodemap.GetNode("Height");

/* Create HWindow for display */
HWindow window(16, 16, Width->GetValue(), Height->GetValue(), 0, "visible", "");
/* Destination image (except for MONO8) */
HImage image;
...

Here's the code if you want your image to be used for Mono8. For Mono8, we can directly use the HImage constructor.

...
if (ptrGrabResult->GetPixelType() == EPixelType::PixelType_Mono8)
{
       HImage img("byte",
                 (Hlong)ptrGrabResult->GetWidth(),
                 (Hlong)ptrGrabResult->GetHeight(),
                 (uint8_t*)ptrGrabResult->GetBuffer());
       /* Show HImage in HWindow */
       window.AttachBackgroundToWindow(img);
}
...

For BGR8packed (BGR, 24 bits per pixel), use this code:

...
else if (ptrGrabResult->GetPixelType() == EPixelType::PixelType_BGR8packed)
{
       image.GenImageInterleaved((uint8_t*)ptrGrabResult->GetBuffer(),
                                 "bgr",
                                 (Hlong)ptrGrabResult->GetWidth(),
                                 (Hlong)ptrGrabResult->GetHeight(),
                                 -1,
                                 "byte",
                                 (Hlong)ptrGrabResult->GetWidth(),
                                 (Hlong)ptrGrabResult->GetHeight(),
                                 0, 0, -1, 0);
       window.AttachBackgroundToWindow(image);
}
...

Don't mix up BGR and RGB, otherwise your colors will be wrong. If you're using BGR on your camera (EPixelType::PixelType_BGR8packed), you also need the "bgr" attribute for the HImage.

If you're using RGB on your camera (EPixelType::PixelType_RGB8packed), you also need the "rgb" attribute for the HImage.

So for RGB8packed (RGB, 24 bits per pixel), use this code:

...
else if (ptrGrabResult->GetPixelType() == EPixelType::PixelType_RGB8packed)
{
       image.GenImageInterleaved((uint8_t*)ptrGrabResult->GetBuffer(),
                                 "rgb",
                                 (Hlong)ptrGrabResult->GetWidth(),
                                 (Hlong)ptrGrabResult->GetHeight(),
                                 -1,
                                 "byte",
                                 (Hlong)ptrGrabResult->GetWidth(),
                                 (Hlong)ptrGrabResult->GetHeight(),
                                 0, 0, -1, 0);
       window.AttachBackgroundToWindow(image);
}
...

For Bayer pixel formats, use the pylon CImageFormatConverter class to convert the GrabResult into a BGR CPylonImage prior to creating the HImage:

...
else
{
       CPylonImage pylonimage;
       CImageFormatConverter fc;
       fc.OutputPixelFormat = PixelType_BGR8packed;
       /* Convert GrabResult to BGR image first */
       fc.Convert(pylonimage, ptrGrabResult);
       /* Now create the BGR HImage */
       image.GenImageInterleaved((uint8_t*)pylonimage.GetBuffer(),
                                 "bgr",
                                 (Hlong)ptrGrabResult->GetWidth(),
                                 (Hlong)ptrGrabResult->GetHeight(),
                                 -1,
                                 "byte",
                                 (Hlong)ptrGrabResult->GetWidth(),
                                 (Hlong)ptrGrabResult->GetHeight(),
                                 0, 0, -1, 0);
       window.AttachBackgroundToWindow(image);
}
...

You can find Visual Studio Solutions for C++ and C# here.

Back to Knowledge Articles