コンテンツにスキップ
ステージングサーバー
開発サーバー

ハウツー:pylon GrabResult を MVTec HALCON HImage に変換する方法#

画像取得にpylonを使用し、その後の画像処理にMVTec HALCONを使用する場合は、pylon GrabResultをHALCON HImageに変換する必要があります。

以下のコードスニペットは、HALCONの一般的なセットアップと、Mono8、BGR / RGB、およびBayer Pixel Format(pylonのCImageFormatConverterクラスを使用)の各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、1ピクセルあたり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、1ピクセルあたり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ソリューションはこちらからご覧いただけます。

ナレッジ記事に戻る