跳至內容
測試伺服器
開發伺服器

C++程式設計指南#

本主題提供有關 BaslerStereo mini 相機的特定資訊,並非旨在對 pylon C++ API 進行全面概述。

如果您是 pylon 的新手,Basler 建議您先閱讀 pylon C++ Programmer's Guide 的 Getting Started 章節,以熟悉 pylon C++ API。

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 為深度值上色,以實現即時視覺化。
  • 顯示點雲圖: 透過 PCL 顯示即時彩色點雲。
    • 使用範圍元件(Coord3D_ABC32f) 以及顏色的飽和度。
    • 將抓取結果轉換為 pcl::PointCloud<pcl::PointXYZRGB>.
    • 顯示即時點雲檢視器。
  • SavePointCloud: 將一個彩色點雲儲存為 .ply 檔案。
    • 擷取一幀影像,並將彩色點雲匯出至 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 Library#

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::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 C++ 程式設計師指南》中的「擷取影像」章節,以熟悉如何使用 `pylon API` 擷取資料。

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++ 程式設計師指南》中「擷取影像」一節。

以下程式碼片段示範了使用 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 類別。一個容器可以包含一個或多個元件。您可以使用該容器查詢元件的數量,並擷取特定元件。容器中的每個元件皆包含實際資料(例如深度值)及其元資料。

請使用 Pylon::CGrabResultData::GetDataContainer() 方法以存取 GrabResult 的 CPylonDataContainer。使用 Pylon::CPylonDataContainer::GetDataComponent() 方法,透過指定元件的索引來存取元件。

有關 pylon 如何提供存取包含多個組件的GrabResults 的相關資訊,請參閱《pylon C++ 程式設計師指南》中「進階主題」一章的「多組件 Grab 結果」節。

存取深度資料#

Stereo mini 相機透過以下方式提供深度資料: Range 元件。根據所選的像素格式,範圍資料可呈現為:

  • 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++ 程式設計師指南》。