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

C++程式設計指南#

本主題提供 Basler Stereo 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 將深度值著色以進行即時視覺化。
  • ShowPointCloud: 使用 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

如何建構範例#

資訊

在建置範例之前,請將範例資料夾複製到您具有讀寫權限的位置。

CMake 用作所有 C++ 範例的建置系統。

  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++ Programmer's Guide 的 Grabbing Images 一節,以熟悉如何使用 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++ 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 類別。一個 Container 可以包含一個或多個組件。您可以使用 Container 來查詢組件的數量並檢索特定的組件。Container 中的每個組件都包含實際資料(例如深度值)及其後設資料。

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

有關 pylon 如何提供對包含多個組件的 GrabResults 的存取之詳細資訊,請參閱 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#

有關將 pylon C++ API 與 Stereo mini 相機搭配使用的更多詳細資訊,請參閱 pylon C++ Programmer's Guide。