Tegra平台构建_OpenCVBuilding OpenCV for Tegra with CUDA

内容目录

  • 获取源码
  • 编译准备
  • 编译配置
  • 编译Opencv
  • 测试Opencv
  • 安装Opencv
  • 编译Opencv2.4.x
  • CMake参数指南

Tegra平台下基于CUDA使用Opencv

本文档是构建OpenCV库的基本指南,CUDA支持在Tegra环境中使用。它涵盖了从源代码中构建版本3.1.0库的基本元素,包括三种不同类型的平台:

  • NVIDIA DRIVE™ PX 2 (V4L)
  • NVIDIA® Tegra® Linux Driver Package (L4T)
  • Desktop Linux (Ubuntu 14.04 LTS and 16.04 LTS)

本文档并不是对构建OpenCV时可用的所有选项的详尽指南。具体来说,它涵盖了构建每个平台时所使用的基本选项,但不包括任何不需要的选项(或与它们的默认值保持不变)。另外,CUDA工具包的安装也不在这里。

本文档的重点是构建OpenCV的3.1.0版本,但是这里的指导原则也可以从git存储库的主分支中构建。关于OpenCV 2.4.13版本的构建,有些CMake选项存在差异,这在OpenCV 2.4的构建中总结如下Building OpenCV 2.4.X部分。

大多数配置命令都基于安装了CUDA 8.0的系统。在Jetson TK1的例子中,使用了一个较旧的CUDA,因为该平台不支持8.0。这些指令可能也适用于旧版本的CUDA,但只测试了8.0。

关于本地编译和交叉编译的说明

OpenCV构建系统支持所有支持平台的本机编译,以及ARM等平台的交叉编译。本机编译过程更简单,而交叉编译通常更快。
目前,本文只关注本机编译。

获取源码

对于本指南,重点是使用git存储库。这是因为OpenCV的3.1.0版本如果不应用git存储库中的一些微小的上游更改,就无法使用CUDA 8.0构建。

# Clone the opencv repository locally:
$ git clone https://github.com/opencv/opencv.git

想要编译3.1.0版本 (而不是编译最新的版本), 你必须checkout到3.1.0分支:

$ cd opencv
$ git checkout -b v3.1.0 3.1.0

注意:该操作在你克隆仓库中创建一个新的本地分支。

有一些上游更改必须通过git cherry-pick命令进行应用。第一种方法是应用修复程序,具体地使用8.0版本的CUDA,而不是3.1.0版本的一部分:

# While still in the opencv directory:
$ git cherry-pick 10896

你将看到以下命令的输出:

[v3.1.0 d6d69a7] GraphCut deprecated in CUDA 7.5 and removed in 8.0
 Author: Vladislav Vinogradov <[email protected]>
 1 file changed, 2 insertions(+), 1 deletion(-)

其次,对于CMake宏调用有一个修复,这在一些系统上是有问题的:

$ git cherry pick cdb9c

你应该看到如下类似输出:

[v3.1.0-28613 e5ac2e4] gpu samples: fix REMOVE_ITEM error
 Author: Alexander Alekhin <[email protected]>
 1 file changed, 1 insertion(+), 1 deletion(-)

需要的最后一个上游修复涉及到与开发人员包捆绑在一起的pkg-config配置文件 (libopencv-dev):

$ git cherry-pick 24dbb

你应该看到如下类似输出:

[v3.1.0 3a6d7ab] pkg-config: modules list contains only OpenCV modules (fixes #5852)
 Author: Alexander Alekhin <[email protected]>
 1 file changed, 7 insertions(+), 4 deletions(-)

到此为止,opencv仓库已经可以编译了。

OpenCV Extra

opencv_extra仓库包含OpenCV库的额外数据,包括测试和演示使用的数据文件。必须分别克隆:

# In the same base directory from which you cloned OpenCV:
$ git clone https://github.com/opencv/opencv_extra.git

与OpenCV源代码一样,必须使用与上面相同的方法将源代码树设置为3.1.0版本。当您从特定的标记构建时,两个存储库都必须在该标记处签出。

$ cd opencv_extra
$ git checkout -b v3.1.0 3.1.0

如果你不打算运行测试或安装测试数据以及示例和示例程序,你可以选择不获取这个存储库。如果在CMake的调用中没有引用它,就不会使用它。

注意:如果您计划运行测试,一些测试期望数据出现,如果没有数据就会失败。

编译准备

要编译OpenCV,您需要一个目录来创建配置并构建库。您还需要一些OpenCV所依赖的第三方库。

Ubuntu Linux准备

这些是在Linux上为Tegra编译OpenCV的基本要求:

  • CMake 2.8.10 or newer
  • CUDA toolkit 8.0 (7.0 or 7.5 may also be used)
  • Build tools (make, gcc, g++)
  • Python 2.6 or greater

这些要求是与平台无关的(DRIVE PX 2, Desktop等等)。

在Linux上编译opencv所需要的软件包:

  • libglew-dev
  • libtiff5-dev
  • zlib1g-dev
  • libjpeg-dev
  • libpng12-dev
  • libjasper-dev
  • libavcodec-dev
  • libavformat-dev
  • libavutil-dev
  • libpostproc-dev
  • libswscale-dev
  • libeigen3-dev
  • libtbb-dev
  • libgtk2.0-dev
  • pkg-config

上面的一些包在Ubuntu Linux系统的通用库中。如果你还没有启用该仓库,那么在尝试安装上面列出的所有包之前,需要执行以下操作:

$ sudo apt-add-repository universe
$ sudo apt-get update

可以将以下命令粘贴到shell中,以便安装所需的包:

$ sudo apt-get install \
    libglew-dev \
    libtiff5-dev \
    zlib1g-dev \
    libjpeg-dev \
    libpng12-dev \
    libjasper-dev \
    libavcodec-dev \
    libavformat-dev \
    libavutil-dev \
    libpostproc-dev \
    libswscale-dev \
    libeigen3-dev \
    libtbb-dev \
    libgtk2.0-dev \
    pkg-config

(为了可读性,添加了换行符和延续字符 / Line-breaks and continuation characters are added for readability.)

如果希望构建Python绑定,还需要为Python 2和Python 3中的任意一个或两个都提供适当的包:

  • python-dev / python3-dev
  • python-numpy / python3-numpy
  • python-py / python3-py
  • python-pytest / python3-pytest

命令如下:

$ sudo apt-get install python-dev python-numpy python-py python-pytest
# And, optionally:
$ sudo apt-get install python3-dev python3-numpy python3-py python3-pytest

一旦安装了所有必要的包,你就可以做编译前的配置了

准备编译区域

Software projects that use the CMake system for configuring their builds expect the actual builds to be done outside of the source tree itself. For configuring and building OpenCV, create a directory called "build" in the same base directory into which you cloned the git repositories:

使用CMake系统来配置构建的软件项目期望在源代码树本身之外完成实际的构建。为了配置和构建OpenCV,在复制git存储库的同一基本目录中创建一个名为“build”的目录:

$ mkdir build
$ cd build

You are now ready to configure and build OpenCV.

现在可以配置和编译OpenCV了。

编译前的配置

The CMake configuration options given below for the different platforms are targeted towards the functionality needed for Tegra. They are based on the original configuration options used for building OpenCV 2.4.13.

下面给出的针对不同平台的CMake配置选项针对的是Tegra所需的功能。它们基于构建OpenCV 2.4.13所用的原始配置选项。

The build of OpenCV is configured with CMake. If run with no parameters, it detects what it needs to know about your system. However, it may have difficulty finding the CUDA files if they are not in a standard location, and it may try to build some options that you might otherwise not want included, so the following invocations of CMake are recommended.

使用Cmake对Opencv进行编译。如果不带参数运行,它会检测到需要了解的系统信息。但是,如果CUDA文件不在标准位置,它可能很难找到它们,并且它可能尝试构建一些您可能不希望包含的选项,因此建议使用以下CMake调用。

In each cmake command listed in the following sub-sections, line-breaks and indentation are added for readability. Continuation characters are also added in examples for Linux-based platforms, allowing you to copy and paste the examples directly into a shell. When entering these commands by hand, enter the command and options as a single line. For a detailed explanation of the parameters passed to cmake, see the "CMake Parameter Reference" section.

在下面的小节中列出的每个cmake命令中,为了可读性,都添加了换行符和缩进。在基于linux平台的示例中还添加了延续字符,允许您直接将示例复制粘贴到shell中。手动输入这些命令时,将命令和选项作为一行输入。有关传递给cmake的参数的详细说明,请参阅“cmake参数引用”一节。

For the Linux-based platforms, the shown value for the CMAKE_INSTALL_PREFIX parameter is /usr. You can set this to whatever you want, based on the layout of your system.

对于基于linux的平台,CMAKE_INSTALL_PREFIX参数的显示值是/usr。您可以根据系统的布局将其设置为任意值。

In each of the cmake invocations below, the last parameter, OPENCV_TEST_DATA_PATH, tells the build system where to find the test-data that is provided by the opencv_extra repository. When this is included, a make install installs this test-data alongside the libraries and example code, and a make testautomatically provides this path to the tests that have to load data from it. If you did not clone the opencv_extra repository, do not include this parameter.

在下面的cmake调用中,最后一个参数OPENCV_TEST_DATA_PATH告诉构建系统在哪里找到opencv_extra存储库提供的测试数据。当包含这部分内容时,make install会将这些测试数据与库和示例代码一起安装,而make testtest会自动提供这个路径,以供必须从其中加载数据的测试使用。如果没有克隆opencv_extra存储库,不要包含这个参数。

Vibrante V4L 环境下编译配置

Supported platform: Drive PX 2

支持的平台:Drive PX 2

$ cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DBUILD_PNG=OFF \
    -DBUILD_TIFF=OFF \
    -DBUILD_TBB=OFF \
    -DBUILD_JPEG=OFF \
    -DBUILD_JASPER=OFF \
    -DBUILD_ZLIB=OFF \
    -DBUILD_EXAMPLES=ON \
    -DBUILD_opencv_java=OFF \
    -DBUILD_opencv_python2=ON \
    -DBUILD_opencv_python3=OFF \
    -DENABLE_NEON=ON \
    -DWITH_OPENCL=OFF \
    -DWITH_OPENMP=OFF \
    -DWITH_FFMPEG=ON \
    -DWITH_GSTREAMER=OFF \
    -DWITH_GSTREAMER_0_10=OFF \
    -DWITH_CUDA=ON \
    -DWITH_GTK=ON \
    -DWITH_VTK=OFF \
    -DWITH_TBB=ON \
    -DWITH_1394=OFF \
    -DWITH_OPENEXR=OFF \
    -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \
    -DCUDA_ARCH_BIN=6.2 \
    -DCUDA_ARCH_PTX="" \
    -DINSTALL_C_EXAMPLES=ON \
    -DINSTALL_TESTS=OFF \
    -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
    ../opencv

The configuration provided above builds the Python bindings for Python 2 (but not Python 3) as part of the build process. If you want the Python 3 bindings (or do not want the Python 2 bindings), change the values of BUILD_opencv_python2 and/or BUILD_opencv_python3 as needed. To enable bindings, set the value to ON, to disable them set it to OFF:

上面提供的配置作为构建过程的一部分为Python 2(而不是Python 3)构建Python绑定。如果希望使用Python 3绑定(或者不希望使用Python 2绑定),请根据需要更改BUILD_opencv_python2和/或BUILD_opencv_python3的值。要启用绑定,请将值设置为ON,以禁用它们将其设置为OFF:

-DBUILD_opencv_python2=OFF

Jetson L4T 环境下编译配置

支持的平台:

  • Jetson TK1
  • Jetson TX1

Configuration is slightly different for the Jetson TK1 and the Jetson TX1 systems.

Jetson TK1和Jetson TX1系统的配置略有不同。

Jetson TK1

$ cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_CXX_FLAGS=-Wa,-mimplicit-it=thumb \
    -DBUILD_PNG=OFF \
    -DBUILD_TIFF=OFF \
    -DBUILD_TBB=OFF \
    -DBUILD_JPEG=OFF \
    -DBUILD_JASPER=OFF \
    -DBUILD_ZLIB=OFF \
    -DBUILD_EXAMPLES=ON \
    -DBUILD_opencv_java=OFF \
    -DBUILD_opencv_python2=ON \
    -DBUILD_opencv_python3=OFF \
    -DENABLE_NEON=ON \
    -DWITH_OPENCL=OFF \
    -DWITH_OPENMP=OFF \
    -DWITH_FFMPEG=ON \
    -DWITH_GSTREAMER=OFF \
    -DWITH_GSTREAMER_0_10=OFF \
    -DWITH_CUDA=ON \
    -DWITH_GTK=ON \
    -DWITH_VTK=OFF \
    -DWITH_TBB=ON \
    -DWITH_1394=OFF \
    -DWITH_OPENEXR=OFF \
    -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-6.5 \
    -DCUDA_ARCH_BIN=3.2 \
    -DCUDA_ARCH_PTX="" \
    -DINSTALL_C_EXAMPLES=ON \
    -DINSTALL_TESTS=OFF \
    -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
    ../opencv

Note: This uses CUDA 6.5, not 8.0.

注意:这里编译时使用的版本为6.5,而不是8.0

Jetson TX1

$ cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DBUILD_PNG=OFF \
    -DBUILD_TIFF=OFF \
    -DBUILD_TBB=OFF \
    -DBUILD_JPEG=OFF \
    -DBUILD_JASPER=OFF \
    -DBUILD_ZLIB=OFF \
    -DBUILD_EXAMPLES=ON \
    -DBUILD_opencv_java=OFF \
    -DBUILD_opencv_python2=ON \
    -DBUILD_opencv_python3=OFF \
    -DENABLE_PRECOMPILED_HEADERS=OFF \
    -DWITH_OPENCL=OFF \
    -DWITH_OPENMP=OFF \
    -DWITH_FFMPEG=ON \
    -DWITH_GSTREAMER=OFF \
    -DWITH_GSTREAMER_0_10=OFF \
    -DWITH_CUDA=ON \
    -DWITH_GTK=ON \
    -DWITH_VTK=OFF \
    -DWITH_TBB=ON \
    -DWITH_1394=OFF \
    -DWITH_OPENEXR=OFF \
    -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \
    -DCUDA_ARCH_BIN=5.3 \
    -DCUDA_ARCH_PTX="" \
    -DINSTALL_C_EXAMPLES=ON \
    -DINSTALL_TESTS=OFF \
    -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
    ../opencv

Note: This configuration does not set the ENABLE_NEON parameter.

注意:这个配置没有设置ENABLE_NEON参数。

Ubuntu Desktop Linux 环境下编译配置

支持的平台:

  • Ubuntu Desktop Linux 14.04 LTS
  • Ubuntu Desktop Linux 16.04 LTS

The configuration options given to cmake below are targeted towards the functionality needed for Tegra. For a desktop system, you may wish to adjust some options to enable (or disable) certain features. The features enabled below are based on the building of OpenCV 2.4.13.

下面提供给cmake的配置选项针对的是Tegra所需的功能。对于桌面系统,你可能希望调整一些选项以启用(或禁用)某些特性。下面启用的特性基于OpenCV 2.4.13的构建。

$ cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DBUILD_PNG=OFF \
    -DBUILD_TIFF=OFF \
    -DBUILD_TBB=OFF \
    -DBUILD_JPEG=OFF \
    -DBUILD_JASPER=OFF \
    -DBUILD_ZLIB=OFF \
    -DBUILD_EXAMPLES=ON \
    -DBUILD_opencv_java=OFF \
    -DBUILD_opencv_python2=ON \
    -DBUILD_opencv_python3=OFF \
    -DWITH_OPENCL=OFF \
    -DWITH_OPENMP=OFF \
    -DWITH_FFMPEG=ON \
    -DWITH_GSTREAMER=OFF \
    -DWITH_GSTREAMER_0_10=OFF \
    -DWITH_CUDA=ON \
    -DWITH_GTK=ON \
    -DWITH_VTK=OFF \
    -DWITH_TBB=ON \
    -DWITH_1394=OFF \
    -DWITH_OPENEXR=OFF \
    -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \
    -DCUDA_ARCH_BIN='3.0 3.5 5.0 6.0 6.2' \
    -DCUDA_ARCH_PTX="" \
    -DINSTALL_C_EXAMPLES=ON \
    -DINSTALL_TESTS=OFF \
    -DOP
同样需要注意的是:这里的cuda版本为8.0,根据自己需要可以更改

猜你喜欢

转载自blog.csdn.net/wanzew/article/details/81583743