방법: pylon GrabResult를 MVTec HALCON HImage로 변환하기#
이미지 취득에는 pylon을 사용하고 추가적인 이미지 처리에 MVTec HALCON을 사용하려면 pylon GrabResult를 HALCON HImage로 변환해야 합니다.
다음 코드 조각은 HALCON의 일반적인 설정 및 다음 Pixel Format(Mono8, BGR / RGB, 그리고 pylon CImageFormatConverter 클래스를 사용하는 Bayer Pixel Format)에 대한 이미지 생성 방법을 보여줍니다.
아래 코드는 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 솔루션은 여기에서 확인할 수 있습니다.