使用 CMake 配置 C++ 示例并在 Windows 的 Visual Studio 中构建它们
本教程展示了如何使用CMake配置Zivid C++示例,然后在Windows中使用Visual Studio构建它们。
要求
可选要求
一些示例依赖于外部库,特别是:
备注
下列指令已使用这些版本的依赖项进行了测试:
Visual Studio 2019 及更高版本
CMake 3.24 及更高版本
PCL 1.12.1
Eigen 3.4.0
OpenCV 4.7.0
Open3D 0.19.0
如果您使用其他版本,可能会遇到问题。
说明
克隆C++示例
克隆 GitHub Zivid C++ Samples 存储库。要克隆它,请按键盘上的 Win + R 键启动命令提示符,然后输入 cmd 并按 Enter。
导航到要克隆存储库的位置,例如
cd C:\Users\Public
然后运行以下命令:
git clone https://github.com/zivid/zivid-cpp-samples
使用CMake配置解决方案
通过命令提示符或 CMake GUI 运行 CMake。
在 zivid-cpp-samples 目录中创建一个构建目录,并通过运行以下命令配置解决方案:
mkdir build && cd build
cmake -DUSE_EIGEN3=OFF -DUSE_OPENCV=OFF -DUSE_PCL=OFF -DUSE_OPEN3D=OFF ../source
小技巧
确保 cmake
在您的系统路径中才能使用上述命令。请参阅下面的下拉列表,了解如何将 CMake 添加到系统路径。
将 CMake 添加到系统 PATH
在文件资源管理器中打开生成的 Visual Studio 解决方案 ZividCppSamples.sln
以进一步构建示例。
打开 CMake GUI 并选择构建文件夹的源文件夹和目标。

单击 Configure 。为项目指定生成器并确保(可选)平台为 x64
。单击 Finish 。

再次点击 Configure 。

Click OK on the CMake error and disable the variables USE_EIGEN3
, USE_OPENCV
, USE_PCL
and USE_OPEN3D
to configure the solution without these dependencies.

再一次单击 Configure ,然后单击 Generate 和 Open Project 打开 Visual Studio 解决方案。
构建解决方案
在 Visual Studio 中,将构建配置从 Debug 更改为 Release 。按下 Ctrl + Shift + B 构建解决方案。

运行示例程序
导航到 Release 文件夹并通过双击文件资源管理器中的 exe
或使用命令提示符来运行示例程序,例如
.\CaptureVis3D.exe

运行示例需要Sample Data
一些示例需要额外的数据才能运行,例如点云、文件相机、转换关系等。请参阅 Sample Data 以获取有关如何下载和安装的说明这些。
使用可选依赖项配置示例
Some of the samples depend on Point Cloud Library, Eigen, OpenCV, Open3D and/or Halcon. If you want to run samples with one or more of these dependencies, follow the instructions in the below dropdown.
可选依赖项
根据您要运行的示例程序安装所需的依赖项;见下表。
示例程序 |
PCL |
Eigen |
OpenCV |
Halcon |
Open3D |
---|---|---|---|---|---|
ReadPCLVis3D |
YES |
||||
CaptureWritePCLVis3D |
YES |
||||
CaptureHDRVisNormals |
YES |
||||
StitchByTransformation |
YES |
||||
StitchByTransformationFromZDF |
YES |
||||
MaskPointCloud |
YES |
YES |
|||
ROIBoxViaCheckerboard |
YES |
||||
UtilizeHandEyeCalibration |
YES |
||||
PoseConversions |
YES |
||||
ROIBoxViaArucoMarker |
YES |
YES |
|||
CaptureUndistort2D |
YES |
||||
CreateDepthMap |
YES |
||||
ProjectAndFindMarker |
YES |
||||
ReprojectPoints |
YES |
||||
ReadAndProjectImage |
YES |
||||
GammaCorrection |
YES |
||||
AllocateMemoryForPointCloudData |
YES |
||||
TransformPointCloudViaCheckerboard |
YES |
||||
TransformPointCloudViaArucoMarker |
YES |
||||
CaptureHalconViaGenICam |
YES |
||||
CaptureHalconViaZivid |
YES |
从 http://eigen.tuxfamily.org/ 下载 Eigen 的最新稳定版本,并将文件夹解压到您指定的目录。在配置 CMake 项目时,选择包含 Eigen 以配置依赖于它的示例。
导航到您在 zivid-cpp-samples 中创建的构建目录并运行
cmake -DEIGEN3_INCLUDE_DIR=<path> -DUSE_OPENCV=OFF -DUSE_PCL=OFF -DUSE_EIGEN3=OFF ../source其中
<path>
是安装 Eigen 的完整路径,例如C:/Program Files/Eigen
。通过在 https://github.com/PointCloudLibrary/pcl/releases 选择
.exe
下载并安装预构建的 PCL binaries 。选中安装第三方库的选项。![]()
在配置 Zivid PCL 代码示例之前,必须将某些设置包含在 PATH 系统变量中。在您的 PC 上,导航至 Control Panel → System and Security → System → Advanced System Settings → Environment Variables 。在 系统变量 下,选择
Path
变量并单击 Edit 。![]()
在 Edit environment variable 窗口中,单击 New 并添加 PCL binaries 和 OpenNI2(第三方库)的路径,例如
C:\Program Files\PCL 1.12.1\bin C:\Program Files\OpenNI2\Tools如果您已在 Program Files 中安装了 PCL 1.12.1 和 OpenNI2。您可能需要退出 Windows 帐户并再次登录以使更改生效。
警告
您可能必须手动链接到某些第三方 PCL 库,例如 VTK。如果遇到错误,请在 PATH 系统变量中添加库二进制文件的路径。例如,对于 VTK,如果您在 Program Files 中安装了 PCL 1.12.1,则应添加
C:/Program Files/PCL 1.12.1/3rdParty/VTK/bin
。配置 CMake 项目时,应已配置 PCL 的路径。
从 https://opencv.org/releases/ 下载并安装预构建的 OpenCV binaries 。将它们提取到您希望放置的目录,例如
C:\Users\Public在配置 Zivid OpenCV 代码示例之前,必须将某些设置包含在 PATH 系统变量中。在您的 PC 上,导航至 Control Panel → System and Security → System → Advanced System Settings → Environment Variables 。在 系统变量 下,选择
Path
变量并单击 Edit 。![]()
在 Edit environment variable 窗口中,单击 New 并添加 OpenCV binaries 的路径,例如
C:\Program Files\opencv\build\x64\vc16\bin如果您已经安装了 OpenCV 4.7.0 并将其解压到
C:/Program Files/opencv
。警告
您可能需要退出 Windows 帐户并再次登录才能使更改生效。
配置 CMake 项目时,请包含 OpenCV 以配置依赖于它的示例。
导航到您在 zivid-cpp-samples 中创建的构建目录并运行
cmake -DOpenCV_DIR=<path> -DUSE_EIGEN3=OFF -DUSE_PCL=OFF ../source其中
<path>
是 OpenCV 构建文件夹的完整路径,例如C:/Program Files/opencv/build
。您需要在 PC 上安装 Halcon 才能运行 Halcon C++ 示例。请转至 在Windows中安装Zivid + HALCON 了解更多说明。
安装 Halcon 后,配置 CMake 以使用 Halcon 示例。
Download and install prebuilt Open3D binaries from https://github.com/isl-org/Open3D/releases. Open3D does not support multi-configuration builds, so you must choose which to configure for.
Extract them to your desired destination, e.g.
C:\Program Files\Open3DC:\Program Files\Open3D-DebugIn order for CMake to find Open3D you must do one of the following:
set the
CMAKE_INSTALL_PREFIX
variable to point to the folder containing theCMake
folder, or you mustset the
Open3D_DIR
variable to point to the folder containing theOpen3DConfig.cmake
file导航到您在 zivid-cpp-samples 中创建的构建目录并运行
cmake -DUSE_OPEN3D=ON -DCMAKE_INSTALL_PREFIX=<path-to-open3d> -DUSE_EIGEN3=OFF -DUSE_OPENCV=OFF -DUSE_PCL=OFF ../sourcewhere
<path-to-open3d>
is the path to the Open3D folder, e.g.C:/Program Files/Open3D
.In order to run the Open3D samples, you must add the Open3D binary folder to your PATH environment variable.
If you want to configure multiple dependencies at once, you can simply add multiple dependencies at the same time in CMake. For example, if you want to use all samples that require Eigen and Open3D, you can run
mkdir build
cd build
cmake -DEIGEN3_INCLUDE_DIR=/usr/include/eigen3 -DUSE_OPEN3D=ON -DUSE_PCL=OFF -DUSE_OPENCV=OFF ../source
cmake -DEIGEN3_INCLUDE_DIR=<path-to-eigen> -DUSE_OPEN3D=ON -DCMAKE_INSTALL_PREFIX=<path-to-open3d> -DUSE_PCL=OFF -DUSE_OPENCV=OFF ../source
where <path-to-open3d>
is the path to the Open3D folder, e.g. C:/Program Files/Open3D
.
调试示例程序
如需debug示例,您必须将 C:/Program Files/Zivid/bin_debug
添加到 PATH 环境变量中。请按照以下下拉列表中的说明执行此操作。
调试示例程序
在您的PC上,导航到 Control Panel → System and Security → System → Advanced System Settings → Environment Variables。

在 系统变量 下,选择 Path
变量并单击 Edit 。

在 Environment variables 窗口中,单击 New (窗口右上角,在用户变量部分)并添加以下值:
C:\Program Files\Zivid\bin_debug

备注
如果要调试依赖于输入文件的程序,则必须将工作目录更改为 ${TargetDir}
。

将构建配置从 Release 更改为 Debug 。然后单击 F5 以使用调试模式进行构建和运行。
Version History
SDK |
Changes |
---|---|
2.16.0 |
Added instructions for configuring C++ samples with optional dependencies on Ubuntu. Added instructions for configuring Open3D on both Ubuntu and Windows. |