C++ プログラマーズガイド#
pylonを初めて使用する場合は、まずpylon C++ プログラマーズガイドの入門セクションを読んで、pylon C++ APIに慣れておくことをBaslerはお勧めします。
C++ プログラミングサンプル#
To open the folder containing the programming samples for Stereo mini cameras, press the Win key, go to the Basler folder, and choose Stereo mini Samples. A File Explorer window opens. The C++ programming samples are located in the cpp folder.
The C++ samples are located in the /opt/pylon/share/pylon/Samples/stereo-mini/cpp folder.
利用可能なStereo mini C++サンプルは次のとおりです。
- FirstSample: 輝度およびレンジデータを取得し、中心ピクセルの値を出力します。
- 以下を使用して最初の Stereo mini デバイスを開きます。
BaslerGenTlStmDeviceClass. - 輝度およびレンジコンポーネントを有効にします。
- 中心ピクセルの輝度(RGBA)およびレンジ値を読み取ります。
- 以下を使用して最初の Stereo mini デバイスを開きます。
- GrabDepthMap: OpenCVを使用してレンジデータをカラー化された深度マップに変換します。
- レンジコンポーネントを使用します(
Coord3D_C16)。 - ライブ視覚化のためにOpenCVで深度値に色を付けます。
- レンジコンポーネントを使用します(
- ShowPointCloud: PCLを使用して、ライブのカラーポイントクラウドを表示します。
- レンジコンポーネントを使用します(
Coord3D_ABC32f)および色のための輝度。 - 取得結果を変換します:
pcl::PointCloud<pcl::PointXYZRGB>. - ライブポイントクラウドビューアーを表示します。
- レンジコンポーネントを使用します(
- SavePointCloud: カラーポイントクラウドを次の形式で保存します: .ply ファイル。
- 1フレームをキャプチャし、カラーポイントクラウドを次へエクスポートします:
pointcloud_color.ply.
- 1フレームをキャプチャし、カラーポイントクラウドを次へエクスポートします:
サンプルのビルドの前提条件#
- Visual Studio 2019 or above
- pylon Software Suite with Stereo mini supplementary package
- Optional: OpenCV library
- Optional: Point Cloud Library (PCL)
- Optional: Boost libraries
- pylon Software Suite with Stereo mini supplementary package
- CMake 3.3 or above
- A C++ compiler (for example,
gcc/g++) - Optional: OpenCV
- Optional: Point Cloud Library (PCL)
- Optional: Boost libraries
On an Ubuntu system, you can install the prerequisites by issuing the following command:
サンプルのビルド方法#
情報
サンプルのビルドを行う前に、サンプルフォルダを読み取りおよび書き込み権限のある場所にコピーしてください。
すべてのC++サンプルでは、ビルドシステムとしてCMakeが使用されます。
- Start Visual Studio.
- On the start page, choose Open Folder (or Open a local folder) and navigate to the copied sample folder.
- Open the cpp subfolder and click the Select Folder button.
- Choose your preferred configuration from the configuration dropdown.
- Press Ctrl+Shift+B to build all samples.
- Start CMake GUI.
- In the Where is the source code field, enter the path to the cpp subfolder of your samples.
- In the Where to build the binaries field, enter a path where you want to store the build output.
- Click Configure.
- Choose your Visual Studio version from the dropdown and click Finish.
- Click Generate.
- In the File Explorer, navigate to your build folder and double-click the generated Visual Studio solution file (.sln) to open it.
- In Visual Studio, press Ctrl+Shift+B to build all samples.
- Create a build directory:
mkdir build && cd build - Run CMake:
cmake .. - Build the samples:
make - Optionally, install the samples:
sudo make install
OpenCVライブラリのインストール#
Download OpenCV from https://github.com/opencv/opencv/releases.
Make sure that you use an OpenCV version that matches your Visual Studio toolchain. If no matching prebuilt package is available, build OpenCV from source with your Visual Studio version.
If CMake doesn't find OpenCV automatically, set OpenCV_DIR to the directory that contains OpenCVConfig.cmake.
Example (CMake command line):
Adding OpenCV to PATH Environment Variable#
Add the OpenCV bin directory to PATH, for example:
Point Cloud Libraryのインストール#
Download PCL from https://github.com/PointCloudLibrary/pcl/releases.
Make sure that the PCL version matches your Visual Studio toolchain.
If CMake doesn't find PCL automatically, set PCL_DIR to the directory that contains PCLConfig.cmake.
Example (CMake command line):
If required by your installation method, add the PCL bin directory to PATH.
If CMake doesn't find PCL, install the PCL development package or build PCL from source.
On Ubuntu systems, install the package with:
If PCL is installed in a non-standard location, set PCL_DIR accordingly:
Info
Samples using PCL can crash depending on the installed PCL/VTK versions because of a known VTK issue used by PCL visualization. Source: https://github.com/PointCloudLibrary/pcl/issues/5237 Use PCL 1.13.1 or newer when possible.
サンプルの実行#
ビルド出力フォルダから実行可能ファイルを実行します(例:)。
./FirstSample/FirstSample
./GrabDepthMap/GrabDepthMap
./ShowPointCloud/ShowPointCloud
./SavePointCloud/SavePointCloud
アプリケーションのビルド方法#
提供されているサンプルセットで示されているように、CMakeを使用して独自のStereo mini C++アプリケーションを作成します。
IDEプロジェクトの使用#
For Visual Studio-based projects, refer to: Common Settings for Building Applications with pylon (Windows)
For Linux-based builds, refer to: Common Settings for Building Applications with pylon (Linux)
CMakeの使用#
Stereo miniサンプルでは、次の構成を持つCMakeセットアップが使用されます。
find_package(pylon 10.0 REQUIRED)- オプション
find_package(OpenCV QUIET)デプスマップの視覚化用 - オプション
find_package(PCL QUIET)点群の視覚化用
Stereo miniアプリケーション用の最小限のCMakeプロジェクトは、次のようになります。
アプリケーションがサンプルと同じオプションの依存関係を使用する場合は、条件付きでそれらを追加します。
find_package(OpenCV QUIET)
find_package(PCL QUIET)
if (OpenCV_FOUND)
target_include_directories(stereo_mini_app PRIVATE ${OpenCV_INCLUDE_DIRS})
target_link_libraries(stereo_mini_app PRIVATE ${OpenCV_LIBS})
endif()
if (PCL_FOUND)
target_include_directories(stereo_mini_app PRIVATE ${PCL_INCLUDE_DIRS})
target_compile_definitions(stereo_mini_app PRIVATE ${PCL_DEFINITIONS})
target_link_libraries(stereo_mini_app PRIVATE ${PCL_LIBRARIES})
endif()
コマンドラインからビルドするには:
カメラ機能へのアクセス#
デバイスを開く#
pylon Viewerの Pylon::CTlFactory::CreateFirstDevice() 最初に検出されたStereo miniカメラのカメラオブジェクトを作成するメソッドです。次を防ぐために、常にStereo miniカメラ用のpylonデバイスクラス識別子を指定してください。 CreateFirstDevice() 別のタイプのカメラのカメラオブジェクトが作成されるのを防ぎます。
// Open first available Stereo mini camera
Pylon::CStereoMiniInstantCamera camera(
Pylon::CTlFactory::GetInstance().CreateFirstDevice(
Pylon::CDeviceInfo().SetDeviceClass(Pylon::BaslerGenTlStmDeviceClass)));
std::cout << "Using device: " << camera.GetDeviceInfo().GetFriendlyName() << std::endl;
camera.Open();
データの取得#
pylon APIを使用してデータを取得する方法に慣れるには、pylon C++ Programmer's GuideのGrabbing Imagesセクションを参照してください。
pylonでは、取得された画像はGrabResultsと呼ばれるデータ構造として表されます。Stereo aceカメラによって取得されたGrabResultsには、複数のコンポーネントが含まれています。デフォルトでは、各GrabResultに強度画像、デプスマップ、および追加情報が格納されます。
// Enable intensity image by enabling the Intensity component
camera.ComponentSelector.FromString("Intensity");
camera.ComponentEnable.SetValue(true);
画像の取り込み#
pylonは、グラブループを設定するためのさまざまなアプローチをサポートし、メモリバッファを処理するためのさまざまなストラテジを提供します。
詳細については、pylon C++ Programmer's GuideのGrabbing Imagesセクションを参照してください。
次のコードスニペットは、 CStereoMiniInstantCamera クラスを使用した一般的なグラブループを示しています。
camera.StartGrabbing();
Pylon::CGrabResultPtr ptrGrabResult;
camera.RetrieveResult(5000, ptrGrabResult, Pylon::TimeoutHandling_ThrowException);
if (ptrGrabResult->GrabSucceeded())
{
const auto intensity = ptrGrabResult->GetDataComponent(Pylon::EComponentType::ComponentType_Intensity);
const auto range = ptrGrabResult->GetDataComponent(Pylon::EComponentType::ComponentType_Range);
// Process components
}
コンポーネントへのアクセス#
GrabResultの各コンポーネントにアクセスするには、 Pylon::CPylonDataContainer の Pylon::CPylonDataComponent クラスを使用できます。コンテナには1つ以上のコンテナを格納できます。コンテナを使用すると、コンテナ内のコンポーネントの数を照会したり、特定のコンポーネントを取得したりできます。コンテナ内の各コンポーネントには、深度値などの実データとメタデータが格納されます。
pylon Viewerの Pylon::CGrabResultData::GetDataContainer() メソッドを使用すると、GrabResultの CPylonDataContainerにアクセスできます。インデックスを指定してコンポーネントにアクセスするには、 Pylon::CPylonDataContainer::GetDataComponent() メソッドを使用します。
pylonが複数のコンポーネントを含むGrabResultへのアクセスを提供する方法の詳細については、pylon C++ Programmer's GuideのAdvanced Topicsの章にあるMulti-Component Grab Resultsセクションを参照してください。
深度データへのアクセス#
Stereo miniカメラは、 Range コンポーネントを介して深度データを提供します。選択した Pixel Formatに応じて、レンジデータは次のように取得できます。
Coord3D_C1616ビット深度値の場合Coord3D_ABC32f直接3D座標の場合(X, Y, Zfloat)
pylon APIの使用#
Stereo miniカメラでのpylon C++ APIの使用に関する詳細については、pylon C++ Programmer's Guideを参照してください。