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

C++ プログラマーズガイド#

このトピックでは、Basler Stereo miniカメラ固有の情報について説明します。pylon C++ APIの包括的な概要を提供することを目的としたものではありません。

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)およびレンジ値を読み取ります。
  • GrabDepthMap: OpenCVを使用してレンジデータをカラー化された深度マップに変換します。
    • レンジコンポーネントを使用します(Coord3D_C16)。
    • ライブ視覚化のためにOpenCVで深度値に色を付けます。
  • ShowPointCloud: PCLを使用して、ライブのカラーポイントクラウドを表示します。
    • レンジコンポーネントを使用します(Coord3D_ABC32f)および色のための輝度。
    • 取得結果を変換します: pcl::PointCloud<pcl::PointXYZRGB>.
    • ライブポイントクラウドビューアーを表示します。
  • SavePointCloud: カラーポイントクラウドを次の形式で保存します: .ply ファイル。
    • 1フレームをキャプチャし、カラーポイントクラウドを次へエクスポートします: pointcloud_color.ply.

サンプルのビルドの前提条件#

  • 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:

sudo apt -y install build-essential cmake libopencv-dev libpcl-dev libboost-all-dev

サンプルのビルド方法#

情報

サンプルのビルドを行う前に、サンプルフォルダを読み取りおよび書き込み権限のある場所にコピーしてください。

すべてのC++サンプルでは、ビルドシステムとしてCMakeが使用されます。

  1. Start Visual Studio.
  2. On the start page, choose Open Folder (or Open a local folder) and navigate to the copied sample folder.
  3. Open the cpp subfolder and click the Select Folder button.
  4. Choose your preferred configuration from the configuration dropdown.
  5. Press Ctrl+Shift+B to build all samples.
  1. Start CMake GUI.
  2. In the Where is the source code field, enter the path to the cpp subfolder of your samples.
  3. In the Where to build the binaries field, enter a path where you want to store the build output.
  4. Click Configure.
  5. Choose your Visual Studio version from the dropdown and click Finish.
  6. Click Generate.
  7. In the File Explorer, navigate to your build folder and double-click the generated Visual Studio solution file (.sln) to open it.
  8. In Visual Studio, press Ctrl+Shift+B to build all samples.
  1. Create a build directory:
    mkdir build && cd build
  2. Run CMake:
    cmake ..
  3. Build the samples:
    make
  4. 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):

cmake -DOpenCV_DIR="C:\Program Files\OpenCV\build\x64\vc16\lib" ..

Adding OpenCV to PATH Environment Variable#

Add the OpenCV bin directory to PATH, for example:

C:\Program Files\OpenCV\build\x64\vc16\bin

If CMake doesn't find OpenCV, install the OpenCV development package or build OpenCV from source.

On Ubuntu systems, install the package with:

sudo apt-get -y install libopencv-dev

If OpenCV is installed in a non-standard location, set OpenCV_DIR accordingly:

cmake -DOpenCV_DIR=$HOME/OpenCV/lib ..

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):

cmake -DPCL_DIR="C:\Program Files\PCL 1.14.0\cmake" ..

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:

sudo apt-get -y install libpcl-dev

If PCL is installed in a non-standard location, set PCL_DIR accordingly:

cmake -DPCL_DIR=$HOME/PCL/cmake ..

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プロジェクトの使用#

CMakeの使用#

Stereo miniサンプルでは、次の構成を持つCMakeセットアップが使用されます。

  • find_package(pylon 10.0 REQUIRED)
  • オプション find_package(OpenCV QUIET) デプスマップの視覚化用
  • オプション find_package(PCL QUIET) 点群の視覚化用

Stereo miniアプリケーション用の最小限のCMakeプロジェクトは、次のようになります。

cmake_minimum_required(VERSION 3.21)
project(stereo_mini_app LANGUAGES CXX)

# Locate pylon on Windows
list(APPEND CMAKE_PREFIX_PATH $ENV{PYLON_DEV_DIR})

find_package(pylon 10.0 REQUIRED)

add_executable(stereo_mini_app main.cpp)
target_link_libraries(stereo_mini_app PRIVATE pylon::pylon)
cmake_minimum_required(VERSION 3.21)
project(stereo_mini_app LANGUAGES CXX)

# Locate pylon on Linux
list(APPEND CMAKE_PREFIX_PATH $ENV{PYLON_ROOT})

find_package(pylon 10.0 REQUIRED)

add_executable(stereo_mini_app main.cpp)
target_link_libraries(stereo_mini_app PRIVATE pylon::pylon)

アプリケーションがサンプルと同じオプションの依存関係を使用する場合は、条件付きでそれらを追加します。

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()

コマンドラインからビルドするには:

mkdir -p build
cd build
cmake ..
cmake --build .

カメラ機能へのアクセス#

デバイスを開く#

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_C16 16ビット深度値の場合
  • Coord3D_ABC32f 直接3D座標の場合(X, Y, Z float)
camera.ComponentSelector.FromString("Range");
camera.PixelFormat.FromString("Coord3D_C16");

pylon APIの使用#

Stereo miniカメラでのpylon C++ APIの使用に関する詳細については、pylon C++ Programmer's Guideを参照してください。