JetsonHacks

Developing on NVIDIA® Jetson™ for AI on the Edge

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

  • This is meant to be a template for building your own custom version of OpenCV, pick and choose your own modules and options
  • Most people do NOT have both the JetPack installed and the source built OpenCV on their system. Some people have noted success using both however, check the forums.
  • Different modules and setting may require different dependencies, make sure to look for error messages when building.
  • The information for this script was gathered from several places:

Facebook
Twitter
LinkedIn
Reddit
Email
Print

20 Responses

  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.

  6. Hi,

    1. Is this article means, we can use OpenCV with “GPU(or CUDA)”?
    or with “ARM”?

    2. How can I use OpenCV code uses “ARM” or “CUDA”?

    Thanks

    1. ARM is the type of CPU. PCs use x86, the Jetson uses an ARM CPU. On the Jetson, the GPU consists of “CUDA Cores”. We cover using a combination of the CPU and GPU here. Thanks for reading!

    1. ARM is the type of CPU. PCs use x86, the Jetson uses an ARM CPU. On the Jetson, the GPU consists of “CUDA Cores”. We cover using a combination of the CPU and GPU here. As a user, you just use regular OpenCV code. Thanks for reading!

  7. Hi, I’m sorry for my unclear question.

    What I’m concern is how can I use some openCV functions to use ARM core or GPU core.
    For example, cv2.read(), cv2.BFMatcher() and so on.

    Let me summarize what I understand in this blog, Please advice If I’m wrong.
    1. I understand if I install openCV from internet (not jetsonhacks) then the openCV functions will use ARM core (not GPU). Is it right?
    2. And If I follow the steps at jetsonhacks, the openCV functions will use GPU core, not ARM core.
    Is it right? or some limited openCV functions only can be supported by jetsonhacks?

    thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclaimer

Some links here are affiliate links. If you purchase through these links I will receive a small commission at no additional cost to you. As an Amazon Associate, I earn from qualifying purchases.

Books, Ideas & Other Curiosities