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 使用的设备
- 抓取深度图: 使用 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)用于点云可视化
一个最小的 CMake 项目Stereo mini 应用程序可能如下所示:
如果您的应用程序使用与示例相同的可选依赖项,请有条件地添加它们:
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 摄像头防止 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 的数据结构。GrabResults 由 pylon 获取。 Stereo ace 相机包含多个组件。默认情况下,每个 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 如何提供对包含多个组件的 GrabResult 的访问的更多信息,请参阅pylon C++ 程序员指南的“高级主题”章节中的“多组件 Grab 结果”部分。
访问深度数据#
Stereo mini 摄像头通过以下方式提供深度数据: Range 组件。根据所选像素格式,范围数据可按以下方式提供:
Coord3D_C16对于 16 位深度值Coord3D_ABC32f对于直接 3D 坐标(X, Y, Z浮标)
使用pylon API#
有关使用 pylon C++ API 的更多详细信息,请参阅Stereo mini 有关摄像头,请参阅Pylon C++ 程序员指南。