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.
- OpenCV.org – Tutorial Building Tegra CUDA
- Jetson Developers Forum
- JK Jung How to Install OpenCV (3.4.0) on Jetson TX2 which is a comprehensive walk through of a customized installation with emphasis on Python and Matplotlib.
The information for this script was gathered from several places:
20 Responses
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?
I do not know. I suppose you could look at the library and see which version it is linked against.
You may be able to check with Python:
$ python
>>> import cv2
>>> print(cv2.getBuildInformation())
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!
You will need to modify the script to clone the opencv_contrib repository and set the include paths appropriately.
I’d like to modify the script but I don’t know how to modify? please help me
Open up a text editor on the file, modify the file, and save it.
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?
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
You’ll have to look it up, it’s something like: OPENCV_EXTRA_MODULES_PATH
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.
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
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!
Hi,
Is this article mean, by using this build, openCV API use “GPU”?
Or uses “ARM”?
Thanks
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!
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
I do not know how to answer your question. I do not know what “OpenCV from Internet” might mean. I would advise you not to use this articles information. Please see:
https://devtalk.nvidia.com/default/topic/1042421/jetson-tx2/opencv-with-gpu-acceleration-on-tx2/post/5287588/
and:
https://jkjung-avt.github.io/opencv3-on-tx2/
which may be able to help.
Just to be clear, this is outdated since Yashas Samaga has since added CUDA support to OpenCV and L4T 32+ comes with a newer version of opencv right? This really helped me get started, but I just wanted to clarify for anyone else who struggled with using CUDA with OpenCV’s dnn module for example. https://gist.github.com/YashasSamaga/a84cf2826ab2dc755005321fe17cd15d
Thank you for sharing these scripts!!! Saved me a ton of trouble.
Successful install of openCV 4.3.0 on:
TX2 18.04
L4T r32.3.1
Did modify the script (https://gist.github.com/YashasSamaga/a84cf2826ab2dc755005321fe17cd15d) a bit to account for
OPENCV_VERSION=4.3.0
# Jetson TX2
ARCH_BIN=6.2
and a few other cmake parameters that i required.
Thanks for sharing the link Jesse
You are welcome, and thanks for reading!