使用 CMake 配置 C++ 示例并在 Windows 的 Visual Studio 中构建它们
本教程展示了如何使用CMake配置Zivid C++示例,然后在Windows中使用Visual Studio构建它们。
要求
可选要求
一些示例依赖于外部库,特别是:
备注
The following instructions have been tested with these versions of the dependencies:
Visual Studio 2019 and higher
CMake 3.24 and higher
PCL 1.12.1
Eigen 3.4.0
OpenCV 4.6.0
If you are using other versions, you may encounter issues.
说明
克隆C++示例
克隆 GitHub Zivid C++ Samples 存储库。要克隆它,请按键盘上的 Win + R 键启动命令提示符,然后输入 cmd 并按 Enter。
Navigate to a location where you want to clone the repository, e.g.
cd C:\Users\Public
然后运行以下命令:
git clone https://github.com/zivid/zivid-cpp-samples
使用CMake配置解决方案
通过命令提示符或 CMake GUI 运行 CMake。
Create a build directory in the directory of zivid-cpp-samples and configure the solution by running:
mkdir build && cd build
cmake -DUSE_EIGEN3=OFF -DUSE_OPENCV=OFF -DUSE_PCL=OFF ../source
小技巧
确保 cmake
在您的系统路径中才能使用上述命令。请参阅下面的下拉列表,了解如何将 CMake 添加到系统路径。
将 CMake 添加到系统 PATH
在文件资源管理器中打开生成的 Visual Studio 解决方案 ZividCppSamples.sln
以进一步构建示例。
构建解决方案
在 Visual Studio 中,将构建配置从 Debug 更改为 Release 。按下 Ctrl + Shift + B 构建解决方案。
![C++ 示例的 Visual Studio 生成事件的屏幕截图。](../../../_images/visual-studio-cpp-build-events-cpp-samples.png)
运行示例程序
导航到 Release 文件夹并通过双击文件资源管理器中的 exe
或使用命令提示符来运行示例程序,例如
.\CaptureVis3D.exe
![CaptureVis3D示例的屏幕截图。](../../../_images/screenshot_from_capture_vis3d.png)
运行示例需要Sample Data
一些示例需要额外的数据才能运行,例如点云、文件相机、转换关系等。请参阅 Sample Data 以获取有关如何下载和安装的说明这些。
使用可选依赖项配置示例
一些示例依赖于 Point Cloud Library 、 Eigen 、 OpenCV 和/或 Halcon 。如果您想运行具有这些依赖项中的一个或多个的示例,请按照以下下拉列表中的说明进行操作。
可选依赖项
根据您要运行的示例程序安装所需的依赖项;见下表。
示例程序 |
PCL |
Eigen |
OpenCV |
OpenCV_Contrib |
Halcon |
---|---|---|---|---|---|
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 --fresh -DEIGEN3_INCLUDE_DIR=<path> -DUSE_OPENCV=OFF -DUSE_PCL=OFF ../source
where <path>
is the full path to where you installed Eigen, e.g. C:/Program Files/Eigen
.
通过在 https://github.com/PointCloudLibrary/pcl/releases 选择 .exe
下载并安装预构建的 PCL binaries 。选中安装第三方库的选项。
![Windows 上 PCL 安装向导的屏幕截图。](../../../_images/install_pcl_screenshot.png)
在配置 Zivid PCL 代码示例之前,必须将某些设置包含在 PATH 系统变量中。在您的 PC 上,导航至 Control Panel → System and Security → System → Advanced System Settings → Environment Variables 。在 系统变量 下,选择 Path
变量并单击 Edit 。
![Windows 系统信息截图。](../../../_images/environment_variables_screenshot_windows.png)
在 Edit environment variable 窗口中,单击 New 并添加 PCL binaries 和 OpenNI2(第三方库)的路径,例如
C:\Program Files\PCL 1.12.1\bin
C:\Program Files\OpenNI2\Tools
if you have installed PCL 1.12.1 and OpenNI2 in Program Files. You may have to sign out of your Windows account and in again for the changes to take place.
警告
You may have to manually link to some of the third-party PCL libraries, like VTK.
If you encounter errors, add the path to the binaries of the library in the PATH system variable.
E.g. for VTK, you would add C:/Program Files/PCL 1.12.1/3rdParty/VTK/bin
if you installed PCL 1.12.1 in Program Files.
配置 CMake 项目时,应已配置 PCL 的路径。
从 https://opencv.org/releases/ 下载并安装预构建的 OpenCV binaries 。将它们提取到您希望放置的目录,例如
C:\Users\Public
警告
预构建的 OpenCV binaries 本身不足以满足依赖于 OpenCV_Contrib 的代码示例。这些示例需要使用额外的模块构建 OpenCV。为此,请按照以下下拉列表中的说明进行操作。
使用额外模块构建 OpenCV
如果您想使用依赖于 OpenCV Contrib 模块(例如 ArUco 标记检测)的代码示例,您需要使用这些模块从头开始构建 OpenCV。 https://github.com/opencv/opencv_contrib 中给出了执行此操作的一般说明,但也在此处进行了重复描述。
将 opencv_contrib 存储库克隆到您所需的位置,例如
cd C:\Users\Public
git clone https://github.com/opencv/opencv_contrib
然后通过命令提示符或 CMake GUI 为 OpenCV 配置 CMake。
导航到您的 OpenCV 构建文件夹并配置 CMake
cd <path-to-opencv-build>
cmake -A x64 -DOPENCV_EXTRA_MODULES_PATH=<path-to-extra-modules> ..\sources
其中 <path-to-opencv-build>
是 OpenCV 构建目录的路径,例如 C:/Users/Public/opencv/build
, <path-to-extra-modules>
是 OpenCV Contrib 模块的路径,例如 C:/Users/Public/opencv_contrib/modules
。
打开 Visual Studio 解决方案 OpenCV.sln
。
在 Visual Studio 中,按下 Ctrl + Shift + B 按键构建 OpenCV(这可能需要很长时间)。使用额外模块构建 OpenCV 后,您可以继续执行以下步骤。
在配置 Zivid OpenCV 代码示例之前,必须将某些设置包含在 PATH 系统变量中。在您的 PC 上,导航至 Control Panel → System and Security → System → Advanced System Settings → Environment Variables 。在 系统变量 下,选择 Path
变量并单击 Edit 。
![Windows 系统信息截图。](../../../_images/environment_variables_screenshot_windows.png)
在 Edit environment variable 窗口中,单击 New 并添加 OpenCV binaries 的路径,例如
C:\Program Files\opencv\build\x64\vc15\bin
if you have installed OpenCV 4.6.0 and extracted it to C:/Program Files/opencv
.
警告
您可能需要退出 Windows 帐户并再次登录才能使更改生效。
配置 CMake 项目时,请包含 OpenCV 以配置依赖于它的示例。
导航到您在 zivid-cpp-samples 中创建的构建目录并运行
cmake --fresh -DOpenCV_DIR=<path> -DUSE_EIGEN3=OFF -DUSE_PCL=OFF ../source
where <path>
is the full path to the OpenCV build folder, e.g. C:/Program Files/opencv/build
.
如果您使用 OpenCV Contrib 中的额外模块构建了 OpenCV,请记住还需设置 USE_ARUCO
cmake --fresh -DOpenCV_DIR=<path> -DUSE_ARUCO=ON -DUSE_EIGEN3=OFF -DUSE_PCL=OFF ../source
您需要在 PC 上安装 Halcon 才能运行 Halcon C++ 示例。请转至 在Windows中安装Zivid + HALCON 了解更多说明。
安装 Halcon 后,配置 CMake 以使用 Halcon 示例。
如果您想一次配置多个依赖项,只需在 CMake 中同时添加多个依赖项即可。例如,如果您想使用需要 Eigen、PCL 和 OpenCV 等依赖项的所有示例,您可以运行
cmake --fresh -DEIGEN3_INCLUDE_DIR=<path-to-eigen> -DUSE_PCL=ON -DOpenCV_DIR=<path-to-opencv> ../source
其中 <path-to-eigen>
和 <path-to-opencv>
是 Eigen 和 OpenCV 文件夹的路径。
调试示例程序
如需debug示例,您必须将 C:/Program Files/Zivid/bin_debug
添加到 PATH 环境变量中。请按照以下下拉列表中的说明执行此操作。
调试示例程序
在您的PC上,导航到 Control Panel → System and Security → System → Advanced System Settings → Environment Variables。
![Windows 系统信息截图。](../../../_images/windows_system_info.png)
在 系统变量 下,选择 Path
变量并单击 Edit 。
![Windows 系统信息截图。](../../../_images/environment_variables_screenshot_windows.png)
在 Environment variables 窗口中,单击 New (窗口右上角,在用户变量部分)并添加以下值:
C:\Program Files\Zivid\bin_debug
![显示如何将 :code:`C:\Program Files\Zivid\bin_debug` 路径添加到 PATH 变量的屏幕截图。](../../../_images/edit_path_variable_screenshot_windows_for_bin_debug.png)
备注
如果要调试依赖于输入文件的程序,则必须将工作目录更改为 ${TargetDir}
。
![展示了如何在 Visual Studio 中设置调试工作目录的GIF。](../../../_images/visual-studio-set-target-directory-for-debug.gif)
将构建配置从 Debug 更改为 Release 。然后点击 F5 构建并运行调试。