1 Building OpenCV for Tegra with CUDA {#tutorial_building_tegra_cuda}
2 ===================================
4 @prev_tutorial{tutorial_arm_crosscompile_with_cmake}
5 @next_tutorial{tutorial_display_image}
10 OpenCV with CUDA for Tegra
11 ==========================
13 This document is a basic guide to building the OpenCV libraries with CUDA support for use in the Tegra environment. It covers the basic elements of building the version 3.1.0 libraries from source code for three (3) different types of platforms:
15 * NVIDIA DRIVE™ PX 2 (V4L)
16 * NVIDIA<sup>®</sup> Tegra<sup>®</sup> Linux Driver Package (L4T)
17 * Desktop Linux (Ubuntu 14.04 LTS and 16.04 LTS)
19 This document is not an exhaustive guide to all of the options available when building OpenCV. Specifically, it covers the basic options used when building each platform but does not cover any options that are not needed (or are unchanged from their default values). Additionally, the installation of the CUDA toolkit is not covered here.
21 This document is focused on building the 3.1.0 version of OpenCV, but the guidelines here may also work for building from the master branch of the git repository. There are differences in some of the CMake options for builds of the 2.4.13 version of OpenCV, which are summarized below in the @ref tutorial_building_tegra_cuda_opencv_24X section.
23 Most of the configuration commands are based on the system having CUDA 8.0 installed. In the case of the Jetson TK1, an older CUDA is used because 8.0 is not supported for that platform. These instructions may also work with older versions of CUDA, but are only tested with 8.0.
25 ### A Note on Native Compilation vs. Cross-Compilation
27 The OpenCV build system supports native compilation for all the supported platforms, as well as cross-compilation for platforms such as ARM and others. The native compilation process is simpler, whereas the cross-compilation is generally faster.
29 At the present time, this document focuses only on native compilation.
31 Getting the Source Code {#tutorial_building_tegra_cuda_getting_the_code}
32 =======================
34 There are two (2) ways to get the OpenCV source code:
36 * Direct download from the [OpenCV downloads](http://opencv.org/releases.html) page
37 * Cloning the git repositories hosted on [GitHub](https://github.com/opencv)
39 For this guide, the focus is on using the git repositories. This is because the 3.1.0 version of OpenCV will not build with CUDA 8.0 without applying a few small upstream changes from the git repository.
44 Start with the `opencv` repository:
46 # Clone the opencv repository locally:
47 $ git clone https://github.com/opencv/opencv.git
49 To build the 3.1.0 version (as opposed to building the most-recent source), you must check out a branch based on the `3.1.0` tag:
52 $ git checkout -b v3.1.0 3.1.0
54 __Note:__ This operation creates a new local branch in your clone's repository.
56 There are some upstream changes that must be applied via the `git cherry-pick` command. The first of these is to apply a fix for building specifically with the 8.0 version of CUDA that was not part of the 3.1.0 release:
58 # While still in the opencv directory:
59 $ git cherry-pick 10896
61 You will see the following output from the command:
63 [v3.1.0 d6d69a7] GraphCut deprecated in CUDA 7.5 and removed in 8.0
64 Author: Vladislav Vinogradov <vlad.vinogradov@itseez.com>
65 1 file changed, 2 insertions(+), 1 deletion(-)
67 Secondly, there is a fix for a CMake macro call that is problematic on some systems:
69 $ git cherry pick cdb9c
71 You should see output similar to:
73 [v3.1.0-28613 e5ac2e4] gpu samples: fix REMOVE_ITEM error
74 Author: Alexander Alekhin <alexander.alekhin@itseez.com>
75 1 file changed, 1 insertion(+), 1 deletion(-)
77 The last upstream fix that is needed deals with the `pkg-config` configuration file that is bundled with the developer package (`libopencv-dev`):
79 $ git cherry-pick 24dbb
81 You should see output similar to:
83 [v3.1.0 3a6d7ab] pkg-config: modules list contains only OpenCV modules (fixes #5852)
84 Author: Alexander Alekhin <alexander.alekhin@itseez.com>
85 1 file changed, 7 insertions(+), 4 deletions(-)
87 At this point, the `opencv` repository is ready for building.
92 The `opencv_extra` repository contains extra data for the OpenCV library, including the data files used by the tests and demos. It must be cloned separately:
94 # In the same base directory from which you cloned OpenCV:
95 $ git clone https://github.com/opencv/opencv_extra.git
97 As with the OpenCV source, you must use the same method as above to set the source tree to the 3.1.0 version. When you are building from a specific tag, both repositories must be checked out at that tag.
100 $ git checkout -b v3.1.0 3.1.0
102 You may opt to not fetch this repository if you do not plan on running the tests or installing the test-data along with the samples and example programs. If it is not referenced in the invocation of CMake, it will not be used.
104 __Note:__ If you plan to run the tests, some tests expect the data to be present and will fail without it.
106 Preparation and Prerequisites {#tutorial_building_tegra_cuda_preparation}
107 =============================
109 To build OpenCV, you need a directory to create the configuration and build the libraries. You also need a number of 3rd-party libraries upon which OpenCV depends.
111 Prerequisites for Ubuntu Linux
112 ------------------------------
114 These are the basic requirements for building OpenCV for Tegra on Linux:
116 * CMake 2.8.10 or newer
117 * CUDA toolkit 8.0 (7.0 or 7.5 may also be used)
118 * Build tools (make, gcc, g++)
119 * Python 2.6 or greater
121 These are the same regardless of the platform (DRIVE PX 2, Desktop, etc.).
123 A number of development packages are required for building on Linux:
141 Some of the packages above are in the `universe` repository for Ubuntu Linux systems. If you have not already enabled that repository, you need to do the following before trying to install all of the packages listed above:
143 $ sudo apt-add-repository universe
144 $ sudo apt-get update
146 The following command can be pasted into a shell in order to install the required packages:
148 $ sudo apt-get install \
165 (Line-breaks and continuation characters are added for readability.)
167 If you want the Python bindings to be built, you will also need the appropriate packages for either or both of Python 2 and Python 3:
169 * python-dev / python3-dev
170 * python-numpy / python3-numpy
171 * python-py / python3-py
172 * python-pytest / python3-pytest
174 The commands that will do this:
176 $ sudo apt-get install python-dev python-numpy python-py python-pytest
178 $ sudo apt-get install python3-dev python3-numpy python3-py python3-pytest
180 Once all the necessary packages are installed, you can configure the build.
182 Preparing the Build Area
183 ------------------------
185 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:
190 You are now ready to configure and build OpenCV.
192 Configuring OpenCV for Building {#tutorial_building_tegra_cuda_configuring}
193 ===============================
195 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.
197 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.
199 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.
201 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.
203 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 test` automatically 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.
205 Vibrante V4L Configuration
206 --------------------------
208 Supported platform: Drive PX 2
211 -DCMAKE_BUILD_TYPE=Release \
212 -DCMAKE_INSTALL_PREFIX=/usr \
219 -DBUILD_EXAMPLES=ON \
221 -DBUILD_opencv_python2=ON \
222 -DBUILD_opencv_python3=OFF \
227 -DWITH_GSTREAMER=OFF \
228 -DWITH_GSTREAMER_0_10=OFF \
235 -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \
236 -DCUDA_ARCH_BIN=6.2 \
238 -DINSTALL_C_EXAMPLES=ON \
239 -DINSTALL_TESTS=OFF \
240 -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
243 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`:
245 -DBUILD_opencv_python2=OFF
247 Jetson L4T Configuration
248 ------------------------
255 Configuration is slightly different for the Jetson TK1 and the Jetson TX1 systems.
260 -DCMAKE_BUILD_TYPE=Release \
261 -DCMAKE_INSTALL_PREFIX=/usr \
262 -DCMAKE_CXX_FLAGS=-Wa,-mimplicit-it=thumb \
269 -DBUILD_EXAMPLES=ON \
271 -DBUILD_opencv_python2=ON \
272 -DBUILD_opencv_python3=OFF \
277 -DWITH_GSTREAMER=OFF \
278 -DWITH_GSTREAMER_0_10=OFF \
285 -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-6.5 \
286 -DCUDA_ARCH_BIN=3.2 \
288 -DINSTALL_C_EXAMPLES=ON \
289 -DINSTALL_TESTS=OFF \
290 -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
293 __Note:__ This uses CUDA 6.5, not 8.0.
298 -DCMAKE_BUILD_TYPE=Release \
299 -DCMAKE_INSTALL_PREFIX=/usr \
306 -DBUILD_EXAMPLES=ON \
308 -DBUILD_opencv_python2=ON \
309 -DBUILD_opencv_python3=OFF \
310 -DENABLE_PRECOMPILED_HEADERS=OFF \
314 -DWITH_GSTREAMER=OFF \
315 -DWITH_GSTREAMER_0_10=OFF \
322 -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \
323 -DCUDA_ARCH_BIN=5.3 \
325 -DINSTALL_C_EXAMPLES=ON \
326 -DINSTALL_TESTS=OFF \
327 -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
330 __Note:__ This configuration does not set the `ENABLE_NEON` parameter.
332 Ubuntu Desktop Linux Configuration
333 ----------------------------------
337 * Ubuntu Desktop Linux 14.04 LTS
338 * Ubuntu Desktop Linux 16.04 LTS
340 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.
343 -DCMAKE_BUILD_TYPE=Release \
344 -DCMAKE_INSTALL_PREFIX=/usr \
351 -DBUILD_EXAMPLES=ON \
353 -DBUILD_opencv_python2=ON \
354 -DBUILD_opencv_python3=OFF \
358 -DWITH_GSTREAMER=OFF \
359 -DWITH_GSTREAMER_0_10=OFF \
366 -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \
367 -DCUDA_ARCH_BIN='3.0 3.5 5.0 6.0 6.2' \
369 -DINSTALL_C_EXAMPLES=ON \
370 -DINSTALL_TESTS=OFF \
371 -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
374 This configuration is nearly identical to that for V4L and L4T, except that the `CUDA_ARCH_BIN` parameter specifies multiple architectures so as to support a variety of GPU boards. For a desktop, you have the option of omitting this parameter, and CMake will instead run a small test program that probes for the supported architectures. However, the libraries produced might not work on Ubuntu systems with different cards.
376 As with previous examples, the configuration given above builds the Python bindings for Python 2 (but not Python 3) as part of the build process.
378 Building OpenCV {#tutorial_building_tegra_cuda_building}
381 Once `cmake` finishes configuring OpenCV, building is done using the standard `make` utility.
386 The only parameter that is needed for the invocation of `make` is the `-j` parameter for specifying how many parallel threads to use. This varies depending on the system and how much memory is available, other running processes, etc. The following table offers suggested values for this parameter:
388 |Platform|Suggested value|Notes|
389 |--------|---------------|-----|
391 |Jetson TK1|3|If the build fails due to a compiler-related error, try again with a smaller number of threads. Also consider rebooting the system if it has been running for a long time since the last reboot.|
393 |Ubuntu Desktop|7|The actual value will vary with the number of cores you have and the amount of physical memory. Because of the resource requirements of compiling the CUDA code, it is not recommended to go above 7.|
395 Based on the value you select, build (assuming you selected 6):
399 By default, CMake hides the details of the build steps. If you need to see more detail about each compilation unit, etc., you can enable verbose output:
403 Testing OpenCV {#tutorial_building_tegra_cuda_testing}
406 Once the build completes successfully, you have the option of running the extensive set of tests that OpenCV provides. If you did not clone the `opencv_extra` repository and specify the path to `testdata` in the `cmake` invocation, then testing is not recommended.
411 To run the basic tests under Linux, execute:
415 This executes `ctest` to carry out the tests, as specified in CTest syntax within the OpenCV repository. The `ctest` harness takes many different parameters (too many to list here, see the manual page for CTest to see the full set), and if you wish to pass any of them, you can do so by specifying them in a `make` command-line parameter called `ARGS`:
417 $ make test ARGS="--verbose --parallel 3"
419 In this example, there are two (2) arguments passed to `ctest`: `--verbose` and `--parallel 3`. The first argument causes the output from `ctest` to be more detailed, and the second causes `ctest` to run as many as three (3) tests in parallel. As with choosing a thread count for building, base any choice for testing on the available number of processor cores, physical memory, etc. Some of the tests do attempt to allocate significant amounts of memory.
421 ### Known Issues with Tests
423 At present, not all of the tests in the OpenCV test suite pass. There are tests that fail whether or not CUDA is compiled, and there are tests that are only specific to CUDA that also do not currently pass.
425 __Note:__ There are no tests that pass without CUDA but fail only when CUDA is included.
427 As the full lists of failing tests vary based on platform, it is impractical to list them here.
429 Installing OpenCV {#tutorial_building_tegra_cuda_installing}
432 Installing OpenCV is very straightforward. For the Linux-based platforms, the command is:
436 Depending on the chosen installation location, you may need root privilege to install.
438 Building OpenCV 2.4.X {#tutorial_building_tegra_cuda_opencv_24X}
439 =====================
441 If you wish to build your own version of the 2.4 version of OpenCV, there are only a few adjustments that must be made. At the time of this writing, the latest version on the 2.4 tree is 2.4.13. These instructions may work for later versions of 2.4, though they have not been tested for any earlier versions.
443 __Note:__ The 2.4.X OpenCV source does not have the extra modules and code for Tegra that was upstreamed into the 3.X versions of OpenCV. This part of the guide is only for cases where you want to build a vanilla version of OpenCV 2.4.
445 Selecting the 2.4 Source
446 ------------------------
448 First you must select the correct source branch or tag. If you want a specific version such as 2.4.13, you want to make a local branch based on the tag, as was done with the 3.1.0 tag above:
450 # Within the opencv directory:
451 $ git checkout -b v2.4.13 2.4.13
453 # Within the opencv_extra directory:
454 $ git checkout -b v2.4.13 2.4.13
456 If you simply want the newest code from the 2.4 line of OpenCV, there is a `2.4` branch already in the repository. You can check that out instead of a specific tag:
460 There is no need for the `git cherry-pick` commands used with 3.1.0 when building the 2.4.13 source.
465 Configuring is done with CMake as before. The primary difference is that OpenCV 2.4 only provides Python bindings for Python 2, and thus does not distinguish between Python 2 and Python 3 in the CMake parameters. There is only one parameter, `BUILD_opencv_python`. In addition, there is a build-related parameter that controls features in 2.4 that are not in 3.1.0. This parameter is `BUILD_opencv_nonfree`.
467 Configuration still takes place in a separate directory that must be a sibling to the `opencv` and `opencv_extra` directories.
469 ### Configuring Vibrante V4L
474 -DCMAKE_BUILD_TYPE=Release \
475 -DCMAKE_INSTALL_PREFIX=/usr \
482 -DBUILD_EXAMPLES=ON \
484 -DBUILD_opencv_nonfree=OFF \
485 -DBUILD_opencv_python=ON \
490 -DWITH_GSTREAMER=OFF \
491 -DWITH_GSTREAMER_0_10=OFF \
498 -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \
499 -DCUDA_ARCH_BIN=6.2 \
501 -DINSTALL_C_EXAMPLES=ON \
503 -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
506 ### Configuring Jetson L4T
511 -DCMAKE_BUILD_TYPE=Release \
512 -DCMAKE_INSTALL_PREFIX=/usr \
519 -DBUILD_EXAMPLES=ON \
521 -DBUILD_opencv_nonfree=OFF \
522 -DBUILD_opencv_python=ON \
527 -DWITH_GSTREAMER=OFF \
528 -DWITH_GSTREAMER_0_10=OFF \
535 -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-6.5 \
536 -DCUDA_ARCH_BIN=3.2 \
538 -DINSTALL_C_EXAMPLES=ON \
540 -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
546 -DCMAKE_BUILD_TYPE=Release \
547 -DCMAKE_INSTALL_PREFIX=/usr \
554 -DBUILD_EXAMPLES=ON \
556 -DBUILD_opencv_nonfree=OFF \
557 -DBUILD_opencv_python=ON \
558 -DENABLE_PRECOMPILED_HEADERS=OFF \
562 -DWITH_GSTREAMER=OFF \
563 -DWITH_GSTREAMER_0_10=OFF \
570 -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \
571 -DCUDA_ARCH_BIN=5.3 \
573 -DINSTALL_C_EXAMPLES=ON \
575 -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
578 ### Configuring Desktop Ubuntu Linux
580 For both 14.04 LTS and 16.04 LTS:
583 -DCMAKE_BUILD_TYPE=Release \
584 -DCMAKE_INSTALL_PREFIX=/usr \
591 -DBUILD_EXAMPLES=ON \
593 -DBUILD_opencv_nonfree=OFF \
594 -DBUILD_opencv_python=ON \
598 -DWITH_GSTREAMER=OFF \
599 -DWITH_GSTREAMER_0_10=OFF \
606 -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \
607 -DCUDA_ARCH_BIN='3.0 3.5 5.0 6.0 6.2' \
609 -DINSTALL_C_EXAMPLES=ON \
611 -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
614 Building, Testing and Installing
615 --------------------------------
617 Once configured, the steps of building, testing, and installing are the same as above for the 3.1.0 source.
619 CMake Parameter Reference {#tutorial_building_tegra_cuda_parameter_reference}
620 =========================
622 The following is a table of all the parameters passed to CMake in the recommended invocations above. Some of these are parameters from CMake itself, while most are specific to OpenCV.
624 |Parameter|Our Default Value|What It Does|Notes|
625 |---------|-----------------|------------|-----|
626 |BUILD_EXAMPLES|ON|Governs whether the C/C++ examples are built| |
627 |BUILD_JASPER|OFF|Governs whether the Jasper library (`libjasper`) is built from source in the `3rdparty` directory| |
628 |BUILD_JPEG|OFF|As above, for `libjpeg`| |
629 |BUILD_PNG|OFF|As above, for `libpng`| |
630 |BUILD_TBB|OFF|As above, for `tbb`| |
631 |BUILD_TIFF|OFF|As above, for `libtiff`| |
632 |BUILD_ZLIB|OFF|As above, for `zlib`| |
633 |BUILD_JAVA|OFF|Controls the building of the Java bindings for OpenCV|Building the Java bindings requires OpenCV libraries be built for static linking only|
634 |BUILD_opencv_nonfree|OFF|Controls the building of non-free (non-open-source) elements|Used only for building 2.4.X|
635 |BUILD_opencv_python|ON|Controls the building of the Python 2 bindings in OpenCV 2.4.X|Used only for building 2.4.X|
636 |BUILD_opencv_python2|ON|Controls the building of the Python 2 bindings in OpenCV 3.1.0|Not used in 2.4.X|
637 |BUILD_opencv_python3|OFF|Controls the building of the Python 3 bindings in OpenCV 3.1.0|Not used in 2.4.X|
638 |CMAKE_BUILD_TYPE|Release|Selects the type of build (release vs. development)|Is generally either `Release` or `Debug`|
639 |CMAKE_INSTALL_PREFIX|/usr|Sets the root for installation of the libraries and header files| |
640 |CUDA_ARCH_BIN|varies|Sets the CUDA architecture(s) for which code is compiled|Usually only passed for platforms with known specific cards. OpenCV includes a small program that determines the architectures of the system's installed card if you do not pass this parameter. Here, for Ubuntu desktop, the value is a list to maximize card support.|
641 |CUDA_ARCH_PTX|""|Builds PTX intermediate code for the specified virtual PTX architectures| |
642 |CUDA_TOOLKIT_ROOT_DIR|/usr/local/cuda-8.0 (for Linux)|Specifies the location of the CUDA include files and libraries| |
643 |ENABLE_NEON|ON|Enables the use of NEON SIMD extensions for ARM chips|Only passed for builds on ARM platforms|
644 |ENABLE_PRECOMPILED_HEADERS|OFF|Enables/disables support for pre-compiled headers|Only specified on some of the ARM platforms|
645 |INSTALL_C_EXAMPLES|ON|Enables the installation of the C example files as part of `make install`| |
646 |INSTALL_TESTS|ON|Enables the installation of the tests as part of `make install`| |
647 |OPENCV_TEST_DATA_PATH|../opencv_extra/testdata|Path to the `testdata` directory in the `opencv_extra` repository| |
648 |WITH_1394|OFF|Specifies whether to include IEEE-1394 support| |
649 |WITH_CUDA|ON|Specifies whether to include CUDA support| |
650 |WITH_FFMPEG|ON|Specifies whether to include FFMPEG support| |
651 |WITH_GSTREAMER|OFF|Specifies whether to include GStreamer 1.0 support| |
652 |WITH_GSTREAMER_0_10|OFF|Specifies whether to include GStreamer 0.10 support| |
653 |WITH_GTK|ON|Specifies whether to include GTK 2.0 support|Only given on Linux platforms, not Microsoft Windows|
654 |WITH_OPENCL|OFF|Specifies whether to include OpenCL runtime support| |
655 |WITH_OPENEXR|OFF|Specifies whether to include ILM support via OpenEXR| |
656 |WITH_OPENMP|OFF|Specifies whether to include OpenMP runtime support| |
657 |WITH_TBB|ON|Specifies whether to include Intel TBB support| |
658 |WITH_VTK|OFF|Specifies whether to include VTK support| |
660 Copyright © 2016, NVIDIA CORPORATION. All rights reserved.