如何:將 pylon GrabResult 轉換為 MVTec HALCON HImage#
如果您想要使用 pylon 進行影像擷取,並使用 MVTec HALCON 進行後續的影像處理,您必須將 pylon GrabResult 轉換為 HALCON HImage。
以下程式碼片段顯示了適用於以下 Pixel Format 的 HALCON 一般設定與影像建立方式:Mono8、BGR / RGB,以及 Bayer Pixel Format(透過使用 pylon CImageFormatConverter 類別)。
下方的程式碼顯示如何設定 HALCON HWindow 以顯示 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;
...
如果您希望影像用於 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 解決方案。