How to Install OpenCV (3.4.0) on Jetson TX2

2019-05-15 update: Added the Installing OpenCV 3.4.6 on Jetson Nano post. If you are installing OpenCV on a Jetson Nano, or on a Jetson TX2 / AGX Xavier with JetPack-4.2, do check out the new post.

2018-08-08 update: Verified opencv-3.4.2 built and run OK under JetPack-3.3.

2018-03-27 update: Update for JetPack-3.2.

2018-01-29 update: 1. Updated to opencv-3.4.0; 2. Added more apt package clean-up steps at the beginning; 3. Using Qt/OpenGL backend instead of GTK+ (this resolves the ‘protobuf double free’ issue for caffe: reference); 4. Install python3/python2 ‘matplotlib’ before building opencv (this is recommended by opencv’s official documentation).

2017-10-20 update: I added installation steps for python2, as well as the resulting cmake configuration on my Jetson TX2.

When I started programming with python3 on Jetson TX2, I found that import cv2 did not work. It appeared that the pre-installed OpenCV4Tegra ( only provided bindings for python2.7. After some research, I found the recommended solution to this problem was to re-compile OpenCV.

I was kind of reluctant to remove OpenCV4Tegra and re-build OpenCV by myself. But anyway I finally decided to do it. I documented the procedure below.


  • Complete installation of JetPack-3.3 (or JetPack-3.2.1 or JetPack-3.1) on the target Jetson TX2.


Installation Steps

I’d start by cleaning up older opencv packages and installing necessary dependencies for building opencv.

Regarding the python matplotlibrc modifications below, refer to this StackOverflow thread for more details.

### Remove all old opencv stuffs installed by JetPack (or OpenCV4Tegra)
$ sudo apt-get purge libopencv*
### I prefer using newer version of numpy (installed with pip), so
### I'd remove this python-numpy apt package as well
$ sudo apt-get purge python-numpy
### Remove other unused apt packages
$ sudo apt autoremove
### Upgrade all installed apt packages to the latest versions (optional)
$ sudo apt-get update
$ sudo apt-get dist-upgrade
### Update gcc apt package to the latest version (highly recommended)
$ sudo apt-get install --only-upgrade g++-5 cpp-5 gcc-5
### Install dependencies based on the Jetson Installing OpenCV Guide
$ sudo apt-get install build-essential make cmake cmake-curses-gui \
                       g++ libavformat-dev libavutil-dev \
                       libswscale-dev libv4l-dev libeigen3-dev \
                       libglew-dev libgtk2.0-dev
### Install dependencies for gstreamer stuffs
$ sudo apt-get install libdc1394-22-dev libxine2-dev \
                       libgstreamer1.0-dev \
### Install additional dependencies according to the pyimageresearch
### article
$ sudo apt-get install libjpeg8-dev libjpeg-turbo8-dev libtiff5-dev \
                       libjasper-dev libpng12-dev libavcodec-dev
$ sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev \
                       libatlas-base-dev gfortran
$ sudo apt-get install libopenblas-dev liblapack-dev liblapacke-dev
### Install Qt5 dependencies
$ sudo apt-get install qt5-default
### Install dependencies for python3
$ sudo apt-get install python3-dev python3-pip python3-tk
$ sudo pip3 install numpy
$ sudo pip3 install matplotlib
### Modify matplotlibrc (line #41) as 'backend      : TkAgg'
$ sudo vim /usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/matplotlibrc
### Also install dependencies for python2
### Note that I install numpy with pip, so that I'd be using a newer
### version of numpy than the apt-get package
$ sudo apt-get install python-dev python-pip python-tk
$ sudo pip2 install numpy
$ sudo pip2 install matplotlib
### Modify matplotlibrc (line #41) as 'backend      : TkAgg'
$ sudo vim /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/matplotlibrc

Before downloading and building opencv-3.4.0, I’d first do some modifications according to this post, in order to fix OpenGL related compilation problems . More specifically, I’d modify /usr/local/cuda/include/cuda_gl_interop.h and fix the symbolic link of

$ sudo vim /usr/local/cuda/include/cuda_gl_interop.h
$ cd /usr/lib/aarch64-linux-gnu/
$ sudo ln -sf tegra/

Here’s how the relevant lines (line #62~68) of cuda_gl_interop.h look like after the modification.

//#if defined(__arm__) || defined(__aarch64__)
//#ifndef GL_VERSION
//#error Please include the appropriate gl headers before including cuda_gl_interop.h
 #include <GL/gl.h>

Next, download opencv-3.4.0 source code, cmake and compile. Note that opencv_contrib modules (cnn/dnn stuffs) would cause problem on pycaffe, so after some experiments I decided not to include those modules at all.

### Download opencv-3.4.0 source code
$ mkdir -p ~/src
$ cd ~/src
$ wget \
$ unzip
### Build opencv (CUDA_ARCH_BIN="6.2" for TX2, or "5.3" for TX1)
$ cd ~/src/opencv-3.4.0
$ mkdir build
$ cd build
$ make -j4
$ sudo make install

Just for reference, here’s the resulting opencv-3.4.0 cmake configuration for my Jetson TX2 system.

-- General configuration for OpenCV 3.4.0 =====================================
--   Version control:               unknown
--   Platform:
--     Timestamp:                   2018-01-29T07:58:45Z
--     Host:                        Linux 4.4.38-tegra aarch64
--     CMake:                       3.5.1
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
--   CPU/HW features:
--     Baseline:                    NEON FP16
--       required:                  NEON
--       disabled:                  VFPV3
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ Compiler:                /usr/bin/c++  (ver 5.4.0)
--     C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):
--     Linker flags (Debug):
--     ccache:                      NO
--     Precompiled headers:         YES
--     Extra dependencies:          dl m pthread rt /usr/lib/aarch64-linux-gnu/ /usr/lib/aarch64-linux-gnu/ cudart nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cufft -L/usr/local/cuda-8.0/lib64
--     3rdparty dependencies:
--   OpenCV modules:
--     To be built:                 calib3d core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev dnn features2d flann highgui imgcodecs imgproc ml objdetect photo python2 python3 python_bindings_generator shape stitching superres video videoio videostab
--     Disabled:                    js world
--     Disabled by dependency:      -
--     Unavailable:                 java ts viz
--     Applications:                apps
--     Documentation:               NO
--     Non-free algorithms:         NO
--   GUI: 
--     QT:                          YES (ver 5.5.1)
--       QT OpenGL support:         YES (Qt5::OpenGL 5.5.1)
--     GTK+:                        NO
--     OpenGL support:              YES (/usr/lib/aarch64-linux-gnu/ /usr/lib/aarch64-linux-gnu/
--     VTK support:                 NO
--   Media I/O: 
--     ZLib:                        /usr/lib/aarch64-linux-gnu/ (ver 1.2.8)
--     JPEG:                        /usr/lib/aarch64-linux-gnu/ (ver )
--     WEBP:                        build (ver encoder: 0x020e)
--     PNG:                         /usr/lib/aarch64-linux-gnu/ (ver 1.2.54)
--     TIFF:                        /usr/lib/aarch64-linux-gnu/ (ver 42 / 4.0.6)
--     JPEG 2000:                   /usr/lib/aarch64-linux-gnu/ (ver 1.900.1)
--     OpenEXR:                     build (ver 1.7.1)
--   Video I/O:
--     DC1394:                      YES (ver 2.2.4)
--     FFMPEG:                      YES
--       avcodec:                   YES (ver 56.60.100)
--       avformat:                  YES (ver 56.40.101)
--       avutil:                    YES (ver 54.31.100)
--       swscale:                   YES (ver 3.1.101)
--       avresample:                NO
--     GStreamer:                   
--       base:                      YES (ver 1.8.3)
--       video:                     YES (ver 1.8.3)
--       app:                       YES (ver 1.8.3)
--       riff:                      YES (ver 1.8.3)
--       pbutils:                   YES (ver 1.8.3)
--     libv4l/libv4l2:              1.10.0 / 1.10.0
--     v4l/v4l2:                    linux/videodev2.h
--     gPhoto2:                     NO
--   Parallel framework:            pthreads
--   Trace:                         YES (built-in)
--   Other third-party libraries:
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.2.92)
--     Custom HAL:                  YES (carotene (ver 0.0.1))
--   NVIDIA CUDA:                   YES (ver 8.0, CUFFT CUBLAS FAST_MATH)
--     NVIDIA GPU arch:             62
--     NVIDIA PTX archs:
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/nvidia/src/opencv-3.4.0/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.12)
--     Libraries:                   /usr/lib/aarch64-linux-gnu/ (ver 2.7.12)
--     numpy:                       /usr/local/lib/python2.7/dist-packages/numpy/core/include (ver 1.14.0)
--     packages path:               lib/python2.7/dist-packages
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.5.2)
--     Libraries:                   /usr/lib/aarch64-linux-gnu/ (ver 3.5.2)
--     numpy:                       /usr/local/lib/python3.5/dist-packages/numpy/core/include (ver 1.14.0)
--     packages path:               lib/python3.5/dist-packages
--   Python (for build):            /usr/bin/python2.7
--   Java:
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
--   Matlab:                        NO
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- Configuring done
-- Generating done
-- Build files have been written to: /home/nvidia/src/opencv-3.4.0/build

To verify the installation:

$ ls /usr/local/lib/python3.5/dist-packages/cv2.*
$ ls /usr/local/lib/python2.7/dist-packages/cv2.*
$ python3 -c 'import cv2; print(cv2.__version__)'
$ python2 -c 'import cv2; print(cv2.__version__)'


With opencv-3.4.0 properly installed on the Jetson TX2, we could use a python script to capture and display live video from either the Jetson onboard camera, a USB webcam or an IP CAM. Just follow along this post: How to Capture and Display Camera Video with Python on Jetson TX2.

blog built using the cayman-theme by Jason Long. LICENSE