跳转到内容
STAGING SERVER
DEVELOPMENT SERVER

如何操作:将 pylon GrabResult 转换为 MVTec HALCON HImage#

如果您想使用 pylon 进行图像采集,并使用 MVTec HALCON 进行后续的图像处理,则必须将 pylon GrabResult 转换为 HALCON HImage。

以下代码片段展示了 HALCON 的常规设置,以及针对以下 Pixel Format 创建图像的方法:Mono8、BGR / RGB 以及 Bayer Pixel Format(通过使用 pylon CImageFormatConverter 类)。

以下代码展示了如何设置用于显示 HALCON HImage 的 HALCON HWindow。

...
/* 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;
...

如果您希望将图像用于 Mono8,请使用以下代码。对于 Mono8,我们可以直接使用 HImage 构造函数。

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

对于 BGR8packed(BGR,每个像素 24 位),请使用以下代码:

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

请勿混淆 BGR 和 RGB,否则您的颜色将不正确。如果您在相机上使用 BGR(EPixelType::PixelType_BGR8packed),您还需要为 HImage 使用“bgr”属性。

如果您在相机上使用 RGB(EPixelType::PixelType_RGB8packed),您还需要为 HImage 使用“rgb”属性。

因此,对于 RGB8packed(RGB,每个像素 24 位),请使用以下代码:

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

对于 Bayer Pixel Format,请在创建 HImage 之前使用 pylon CImageFormatConverter 类将 GrabResult 转换为 BGR CPylonImage:

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

您可以在此处找到适用于 C++ 和 C# 的 Visual Studio 解决方案。

返回知识库文章