Build OpenCV 3.4 with CUDA on NVIDIA Jetson TX2

In order for OpenCV to get access to CUDA acceleration on the NVIDIA Jetson TX2 running L4T 28.2 (JetPack 3.2), you need to build the library from source. Looky here:

Background

With the latest release of L4T, 28.2, OpenCV version 3.3 may be installed through the JetPack installer. At the time of the L4T release, OpenCV did not provide support for CUDA 9.0 with which L4T 28.2 ships. Over the next couple of months, version OpenCV 3.4 added CUDA 9.0 support.

So what does that mean? Well, if you want OpenCV CUDA support under L4T 28.2 you need to compile it from source. Fortunately we have some convenience scripts to help with that task in the JetsonHacks repository buildOpenCVTX2 on Github.

Installation

You should note that OpenCV is a rich environment, and can be custom tailored to your needs. As such, some of the more common options are in the build command, but are not comprehensive. Modify the options to suit your needs.

Library location

With this script release, the script now installs OpenCV in /usr/local. Earlier versions of this script installed in /usr. You may have to set your include and libraries and/or PYTHONPATH to point to the new version. See the Examples folder. Alternatively, you may want to change the script to install into the /usr directory.

All of this may lead to a conflict. You may consider removing OpenCV installed by JetPack before performing this script installation:

$ sudo apt-get purge libopencv*

Options

Make sure to read through the install script. In the script, here are some of the options that were included:

  • CUDA
  • Fast Math (cuBLAS)
  • OpenGL
  • GStreamer 1.0
  • Video 4 Linux (V4L)
  • Python 2.7 and Python 3.5 support

Build and Install

To download the source, build and install OpenCV:

$ git clone https://github.com/jetsonhacks/buildOpenCVTX2.git
$ cd buildOpenCVTX2
$ ./buildOpenCV.sh

You can remove the sources and build files after you are done:

$ ./removeOpenCVSources.sh

This will remove the OpenCV source, as well as the opencv_extras directories.

Examples

There are a couple of demos in the Examples folder.

There are two example programs here. Both programs require OpenCV to be installed with GStreamer support enabled. Both of these examples were last tested with L4T 28.2, OpenCV 3.4.1

The first is a simple C++ program to view the onboard camera feed from the Jetson Dev Kit.

To compile gstreamer_view.cpp:

$ gcc -std=c++11 ‘pkg-config –cflags opencv’ ‘pkg-config –libs opencv’ gstreamer_view.cpp -o gstreamer_view -lstdc++ -lopencv_core -lopencv_highgui -lopencv_videoio

to run the program:

$ ./gstreamer_view

The second is a Python program that reads the onboard camera feed from the Jetson Dev Kit and does Canny Edge Detection.

To run the Canny detection demo (Python 2.7):

$ python cannyDetection.py

With Python 3.3:

$ python3 cannyDetection.py

With the Canny detection demo, use the less than (<) and greater than (>) to adjust the edge detection parameters. You can pass the command line flags —video_device=<videoDeviceNumber> to use a USB camera instead of the built in camera.

Notes

9 Comments

  1. I remember building OpenCV on the Jetson a while back, but I don’t remember if I enabled CUDA 9 support. Is there a way to tell whether this support is built in from the command-line?

  2. how to install opencv_contrib? I have changed the “DOWNLOAD_OPENCV_EXTRAS” to “YES”,but it did not work,there is still no “xfeatures2d.hpp”.please help me ,thanks!

  3. I have Ubuntu 14.04 and installed Jetpack 3.1. When I attempt to cross compile OpenCV 3.3.0 for TX2 the cuda libs (cudart, nppc, nppial, nppicc, etc) for aarch64 are not found. The linker is using -L/usr/local/cuda-8.0/lib64 to find the libs but those are for x64 and are unusable for cross compiling for TX2. I cannot find them in the target filesystem “Jetpack/64_TX2/Linux_for_Tegra_tx2/rootfs/*” . So where after installing Jetpack 3.1 are the libs for aarch64 cuda?

  4. Hi,
    the only change required in the script is setting DOWNLOAD_OPENCV_EXTRAS to YES, right?
    Now I have an opencv and an opencv_extra folder in my home directory and, for example, a tracking.hpp file (which is part of the contrib repository) in /usr/local/include/opencv2/video/
    So my question is what PATH variable do I have to set to get access to the extra modules?

    Best,
    Julz

  5. The script buildOpenCV.sh fails at line number 111, with errors indicating it failed to clone opencv/opencv git. Has anyone experienced similar issues and what could I do about it?
    The error lines:
    error: RPC failed; curl 56 GnuTLS recv error (-9); A TLS packet with unexpected length was received.
    fatal: The remote end hung up unexpectedly
    fatal: early EOF
    fatal: index-pack failed

    Cloning directly with ‘git clone https://github.com/opencv/opencv‘ produces the same outcome. I can only assume at this point something is wrong with that repository.

Leave a Reply

Your email address will not be published.


*