C++ 程序员指南#
如果您是 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) 和距离值。
- 使用以下命令打开第一个 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 库#
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()
若要从命令行构建:
访问相机功能#
打开设备#
使用 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#
有关将 pylon C++ API 与 Stereo mini 相机配合使用的更多详细信息,请参阅 pylon C++ Programmer's Guide。