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 将范围数据转换为彩色深度图。
- 使用 range 组件(
Coord3D_C16)。 - 使用 OpenCV 对深度值进行彩色化以供实时可视化。
- 使用 range 组件(
- ShowPointCloud: 使用 PCL 显示实时彩色点云。
- 使用 range 组件(
Coord3D_ABC32f)以及用于色彩的强度。 - 将采集结果转换为
pcl::PointCloud<pcl::PointXYZRGB>. - 显示实时点云查看器。
- 使用 range 组件(
- 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 API 获取数据,请参考 pylon C++ Programmer's Guide 中的 Grabbing Images 一节。
pylon 将抓取的图像表示为名为 GrabResults 的数据结构。由 Stereo ace 相机获取的 GrabResult 包含多个组件。默认情况下,每个 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。