
Pylonの画像オブジェクトからHALCONの画像オブジェクトへの受け渡し方法についてご紹介したいと思います。

質問です。そもそも、なぜそんなことをする必要があるのですか?
最初からHALCONの画像オブジェクトだけではできないのでしょうか。

良い質問ですね。以前、【HALCON】pylon対応表で少し説明したのですが、実は、PylonとHALCONの互換性の関係で、最初からHALCONの画像オブジェクトを作ることができないケースがあるのです。
例えば、Basler社のカメラace2シリーズを使おうと思うと、Pylonはバージョン6.2以上が必要です。ですが、HALCONはPylon6.2には現時点では対応していないため、HALCONから直接Pylonを制御することができないのです。
そこで、撮像はPlyonで行い、画像処理はHALCONで行うという棲み分けが必要になりますので、Pylonで撮像した画像オブジェクトをHALCONの画像オブジェクトへ受け渡す、という処理が必要になるのです。

なるほど。

それでは早速コードを見ていきましょう。
Pylon SDKのサンプルコードを使いましょう。
C:\Program Files\Basler\pylon 6\Development\Samples\C#\Basler.Pylon\Grabの中にあるgrab.csを使っていきます。

grabResult.PixelDataPointerでgrabResultのポインタ情報を取得できますので、これをHALCONの画像オブジェクトのポインタに渡すことができます。
次のようにgrabResultのポインタ情報を取得し、HALCONのHTupleオブジェクト、ここではhv_pointer、に格納しておきます。
実際のコードは次の通りです。
hv_pointer = grabResult.PixelDataPointer;

簡単ですね。

次に、HALCONの画像オブジェクトの生成です。
HOperatorSet.GenImage1オペレーターを使って、先ほどのhv_pointerを指定してください。
GenImage1オペレーターは、画像の型、画像の幅、画像の高さ、そしてポインタを指定し、画像オブジェクトを生成するオペレーターです。
実際のコードは次の通りです。
HOperatorSet.GenImage1(out ho_Image, "byte", hv_Width, hv_Height, hv_pointer);

こちらもとても簡単ですね。

全体のコードは次の通りです。
using System;
using Basler.Pylon;
namespace Grab
{
class Grab
{
internal static void Main()
{
// The exit code of the sample application.
int exitCode = 0;
try
{
// Create a camera object that selects the first camera device found.
// More constructors are available for selecting a specific camera device.
using (Camera camera = new Camera())
{
// Print the model name of the camera.
Console.WriteLine( "Using camera {0}.", camera.CameraInfo[CameraInfoKey.ModelName] );
// Set the acquisition mode to free running continuous acquisition when the camera is opened.
camera.CameraOpened += Configuration.AcquireContinuous;
// Open the connection to the camera device.
camera.Open();
// The parameter MaxNumBuffer can be used to control the amount of buffers
// allocated for grabbing. The default value of this parameter is 10.
camera.Parameters[PLCameraInstance.MaxNumBuffer].SetValue( 5 );
// Start grabbing.
camera.StreamGrabber.Start();
// Grab a number of images.
for (int i = 0; i < 10; ++i)
{
// Wait for an image and then retrieve it. A timeout of 5000 ms is used.
IGrabResult grabResult = camera.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException);
using (grabResult)
{
// Image grabbed successfully?
if (grabResult.GrabSucceeded)
{
//ポインタ情報の取得
hv_pointer = grabResult.PixelDataPointer;
//grabResultをho_Imageにコピー
HOperatorSet.GenImage1(out ho_Image, "byte", hv_Width, hv_Height, hv_pointer);
//ここでHALCONを使った画像処理を実行
}
else
{
Console.WriteLine( "Error: {0} {1}", grabResult.ErrorCode, grabResult.ErrorDescription );
}
}
}
// Stop grabbing.
camera.StreamGrabber.Stop();
// Close the connection to the camera device.
camera.Close();
}
}
catch (Exception e)
{
Console.Error.WriteLine( "Exception: {0}", e.Message );
exitCode = 1;
}
finally
{
// Comment the following two lines to disable waiting on exit.
Console.Error.WriteLine( "\nPress enter to exit." );
Console.ReadLine();
}
Environment.Exit( exitCode );
}
}
}


コメント