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

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: 1つのカラー点群を次の形式で保存します。 .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 .

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

デバイスを開く#

VisualAppletsデザインの主要なデータ形式として、 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では、取得した画像がGrabResultと呼ばれるデータ構造として表されます。Stereo aceカメラによって取得されたGrabResultには、複数のコンポーネントが含まれています。デフォルトでは、各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つ以上のコンポーネントを保持できます。コンテナを使用して、コンポーネントの数を確認したり、特定のコンポーネントを取得したりできます。コンテナ内の各コンポーネントには、デプス値などの実際のデータと、そのメタデータが保持されます。

VisualAppletsデザインの主要なデータ形式として、 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 浮動小数点数)
camera.ComponentSelector.FromString("Range");
camera.PixelFormat.FromString("Coord3D_C16");

pylon APIの使用#

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