使用可选依赖项配置 C++ 示例
本教程介绍如何使用 CMake 为 Zivid C++ 示例配置可选依赖项。
要求
可选要求
一些示例依赖于外部库,特别是:
备注
以下说明已使用这些版本的依赖项进行了测试:
Visual Studio 2019 及更高版本
CMake 3.24 及更高版本
PCL 1.12.1
Eigen 3.4.0
OpenCV 4.7.0
如果您使用其他版本,您可能会遇到问题。
根据您要运行的示例程序安装所需的依赖项;请参阅下表。
示例程序 |
PCL |
Eigen |
OpenCV |
OpenCV (CUDA) |
Halcon |
|---|---|---|---|---|---|
ReadPCLVis3D |
YES |
||||
CaptureWritePCLVis3D |
YES |
||||
CaptureAndVisualizeNormals |
YES |
||||
StitchByTransformation |
YES |
||||
StitchByTransformationFromZDF |
YES |
||||
StitchViaLocalPointCloudRegistration |
YES |
||||
StitchContinuouslyRotatingObject |
YES |
||||
StitchUsingRobotMountedCamera |
YES |
||||
MaskPointCloud |
YES |
||||
Capture2DAnd3D |
YES |
YES |
|||
ROIBoxViaCheckerboard |
YES |
||||
UtilizeHandEyeCalibration |
YES |
||||
PoseConversions |
YES |
||||
ROIBoxViaArucoMarker |
YES |
||||
CaptureUndistort2D |
YES |
||||
CreateDepthMap |
YES |
||||
ProjectAndFindMarker |
YES |
||||
ReprojectPoints |
YES |
||||
ReadProjectAndCaptureImage |
YES |
||||
GammaCorrection |
YES |
||||
AllocateMemoryForPointCloudData |
YES |
||||
CaptureAndConvertImageWithOpenCVOnCuda |
YES |
||||
TransformPointCloudViaCheckerboard |
YES |
||||
TransformPointCloudViaArucoMarker |
YES |
||||
CaptureHalconViaGenICam |
YES |
||||
CaptureHalconViaZivid |
YES |
通过运行以下命令从包管理器安装 Eigen 3:
sudo apt install libeigen3-dev
导航到您在 zivid-cpp-samples 中创建的构建目录并运行
mkdir build
cd build
cmake -DEIGEN3_INCLUDE_DIR=/usr/include/eigen3 -DUSE_OPENCV=OFF -DUSE_PCL=OFF ../source
cmake --build .
通过运行以下命令从包管理器安装 PCL:
sudo apt install libpcl-dev
导航到您在 zivid-cpp-samples 中创建的构建目录并运行
mkdir build
cd build
cmake -DUSE_PCL=ON -DUSE_EIGEN3=OFF -DUSE_OPENCV=OFF ../source
cmake --build .
通过运行以下命令从包管理器安装 OpenCV:
sudo apt install libopencv-dev clang-tidy
导航到您在 zivid-cpp-samples 中创建的构建目录并运行
mkdir build
cd build
cmake -DUSE_OPENCV=ON -DUSE_EIGEN3=OFF -DUSE_PCL=OFF ../source
cmake --build .
To install CUDA follow the instructions on the NVIDIA linux guide.
Older graphics cards may not support all the CUDA instructions required and will require additional configuration. You can find more information on the official OpenCV CUDA introduction and OpenCV contrib repository.
警告
If you encounter random system freezes while using cmake --build ., try adding the --parallel n option with n lower than your CPU core count.
Install tools needed to build OpenCV with CUDA support by running:
sudo apt install clang-tidy ninja-build cmake
Build OpenCV with CUDA support:
wget https://github.com/opencv/opencv/archive/refs/tags/4.13.0.zip
unzip 4.13.0.zip -d opencv
rm 4.13.0.zip
wget https://github.com/opencv/opencv_contrib/archive/refs/tags/4.13.0.zip
unzip 4.13.0.zip -d opencv
rm 4.13.0.zip
cd opencv/opencv-4.13.0
mkdir build
cd build
cmake -S .. -D WITH_CUDA=ON -G Ninja -D CMAKE_BUILD_TYPE=Release -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.13.0/modules/ -D ENABLE_CUDA_FIRST_CLASS_LANGUAGE=ON -D CUDAToolkit_ROOT=/usr/local/cuda
cmake --build .
cmake --install . --prefix local/
Navigate to the build directory you created in zivid-cpp-samples and run:
mkdir build
cd build
cmake -S ../source -D CMAKE_PREFIX_PATH=<path-to-opencv-build>/local/lib/cmake/opencv4 -D USE_OPENCV=ON -D USE_CUDA=ON -D USE_OPENCV_CUDA=ON -D EIGEN3_INCLUDE_DIR=/usr/include/eigen3 -D CUDAToolkit_ROOT=/usr/local/cuda -G Ninja -D CMAKE_BUILD_TYPE=Release -D CMAKE_EXPORT_COMPILE_COMMANDS=ON
cmake --build .
When running the CUDA sample, include the built OpenCV libraries in LD_LIBRARY_PATH:
export LD_LIBRARY_PATH=<path-to-opencv-build>/local/lib:$LD_LIBRARY_PATH
Download the latest stable release of Eigen from https://libeigen.gitlab.io/ and extract the folder to your desired destination. When configuring the CMake project, include Eigen to configure the samples that depend on it.
导航到您在 zivid-cpp-samples 中创建的构建目录并运行
cmake -DEIGEN3_INCLUDE_DIR=<path> -DUSE_OPENCV=OFF -DUSE_PCL=OFF ../source
其中 <path> 是安装 Eigen 的完整路径,例如 C:/Program Files/Eigen 。
在 https://github.com/PointCloudLibrary/pcl/releases 中选择 .exe 下载并安装预构建的 PCL 二进制文件。勾选安装第三方库的选项。
在配置 Zivid PCL 代码示例之前,必须将一些设置添加到 PATH 系统变量中。在您的电脑上,导航至 Control Panel → System and Security → System → Advanced System Settings → Environment Variables 。在 系统变量 下,选择 Path 变量,然后点击 Edit 。
在 Edit environment variable 窗口中,单击 New 并添加 PCL 二进制文件和 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 二进制文件。解压到你想要的路径,例如
C:\Users\Public
在配置 Zivid OpenCV 代码示例之前,必须将一些设置添加到 PATH 系统变量中。在您的电脑上,导航至 Control Panel → System and Security → System → Advanced System Settings → Environment Variables 。在 系统变量 下,选择 Path 变量,然后点击 Edit 。
在 Edit environment variable 窗口中,单击 New 并添加 OpenCV 二进制文件的路径,例如
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 。
您需要在电脑上安装 Halcon 才能运行 Halcon C++ 示例。请参阅 在Windows中安装Zivid + HALCON 获取更多说明。
安装 Halcon 后,配置 CMake 以使用 Halcon 示例。
如果要一次性配置多个依赖项,只需在 CMake 中同时添加多个依赖项即可。例如,如果要使用所有需要 Eigen、PCL 和 OpenCV 的示例,可以运行以下命令:
cmake -DEIGEN3_INCLUDE_DIR=<path-to-eigen> -DUSE_PCL=ON -DOpenCV_DIR=<path-to-opencv> ../source
其中 <path-to-eigen> 和 <path-to-opencv> 分别是 Eigen 和 OpenCV 文件夹的路径。