C++程式設計人員指南#
如果您是 pylon 的新手,Basler 建議您先閱讀 pylon C++ Programmer's Guide 中的 Getting Started 章節,以熟悉 pylon C++ API。
C++ Programming Samples#
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 檔案中。
- 擷取一影格並將彩色點雲匯出至
pointcloud_color.ply.
- 擷取一影格並將彩色點雲匯出至
建置範例的前置作業#
- 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:
如何建置範例#
資訊
在建置範例之前,請將範例資料夾複製到您具有讀取和寫入存取權的位置。
CMake 用作所有 C++ 範例的建置系統。
- 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 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):
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()
若要從命令列進行建置:
存取相機 Property#
開啟裝置#
請使用 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 支援用於設定擷取迴圈的不同方法,並提供用於處理 Memory 緩衝區的不同策略。
如需更多詳細資料,請參閱 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 類別。容器可以容納一個或多個元件。您可以使用容器來查詢元件數量並擷取特定元件。容器中的每個元件都包含實際資料(例如深度值)以及其詮釋資料。
請使用 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浮點數)
使用 pylon API#
如需關於搭配 Stereo mini 相機使用 pylon C++ API 的更詳細資訊,請參閱 pylon C++ Programmer's Guide。