如何操作:將 pylon GrabResult 轉換為 MVTec HALCON HImage#
若您想使用 pylon 進行影像擷取,並使用MVTec Halcon 進行後續的影像處理,則必須將 pylon 的 GrabResult 轉換為 Halcon 的 HImage。
以下程式碼片段展示了 Halcon 的一般設定,以及用於以下像素格式的影像建立方式:Mono8、BGR/RGB,以及拜耳(Bayer)像素格式(透過使用 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);
}
...
對於拜耳像素格式,請在建立 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 解決方案。