JetsonHacks

Developing on NVIDIA® Jetson™ for AI on the Edge

Build OpenCV on the NVIDIA Jetson TX2

Note: This article has been updated for L4T 28.2. Please see Build OpenCV 3.4 with CUDA on NVIDIA Jetson TX2

As a developer, sometimes you need to build OpenCV from source to get the configuration desired. There is a script on the JetsonHacks Github account to help in the process. Looky here:

Background

JetPack can install a CPU and GPU accelerated version of the OpenCV libraries, called OpenCV4Tegra, on the Jetson. OpenCV4Tegra is version 2.4.13 as of this writing. This is great for many applications, especially when you are writing your own apps. However, some libraries require different modules and such that require upstream OpenCV versions.

Installation

The community has gathered the recipe(s) for building OpenCV for version later than OpenCV 3.0. There is a repository on the JetsonHacks Github account which contains a build script to help in the process.

To download the source and build OpenCV:

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

Once finished building, you are ready to install.

As explained in the video, navigate to the build directory to install the newly built libraries:

$ cd ~/opencv/build
$ sudo make install

Once you have generated the build files, you can use the ccmake tool to examine the different options and modules available.

Remember to setup you OpenCV library paths correctly.

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 OpenCV4Tegra and the source built OpenCV on their system. Some people have noted success using both however, check the forums.
  • Sometimes the make tool does not build everything. Experience dictates to go back to the build directory and run make again, just to be sure
  • Different modules and setting may require different dependencies, make sure to look for error messages when building.
  • After building, you should run the tests. The build script includes the testing options. All tests may not pass.
  • The build script adds support for Python 2.7
  • The compiler assumes that the Jetson TX2 aarch64 (ARMv8) architecture is NEON enabled, therefore you do not have to enable the NEON flag for the build

The information for this script was gathered from several places:

Facebook
Twitter
LinkedIn
Reddit
Email
Print

23 Responses

  1. Can you please compare performance of OpenCV 2.4.13 and 3.2 on some typical image operations? Will UMat from 3.2 do automatically the same thing as fast as cuda:: 2.4 methods and do it asynchronous?

  2. Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package libavcodec-dev
    E: Unable to locate package libavformat-dev
    E: Unable to locate package libavutil-dev
    E: Unable to locate package libpostproc-dev
    E: Unable to locate package libswscale-dev
    E: Unable to locate package libeigen3-dev
    E: Unable to locate package libtbb-dev
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package python-py
    E: Unable to locate package python-pytest

    what is it?
    the latest version of the jetpack has been fully installed.

  3. There are two tx2 devices, one is good and the other is not sudden.
    How is there a solution? How do I check my firewall?

  4. Does the own compiled version of cv2 (mostly for on-board camera) confilcts with the cv2 module in ROS package?
    Or can I just don’t source the ros’s cv when I’m using the custom version of cv?

    Thanks!

    1. I believe that the ROS package just blindly installs its own version of OpenCV, ignoring what’s already on the Jetson. I know that people have worked on solutions to this, but I don’t have one at the moment. Thanks for reading!

  5. Could you write up a tutorial too on removing either OpenCV or CV4Tegra as you did mention that most would not be using both and using either one instead.

  6. I first installed it with -DBUILD_opencv-python3=OFF. After that, I set -DBUILD_opencv-python3=ON and reinstalled without removing the previous installation. But I can’t import cv2 in python3. Do I need to reinstall after removing the existing one? If so, how do I remove the old one?

  7. The build script made it to 38% and then failed on a Jetson Nano. Any ideas? TIA.
    [ 38%] Building CXX object modules/cudev/CMakeFiles/opencv_cudev.dir/src/stub.cpp.o
    CMakeFiles/Makefile2:1731: recipe for target ‘modules/cudev/CMakeFiles/opencv_cudev.dir/all’ failed
    make[1]: *** [modules/cudev/CMakeFiles/opencv_cudev.dir/all] Error 2
    Makefile:162: recipe for target ‘all’ failed
    make: *** [all] Error 2
    Make did not successfully build
    Please fix issues and retry build

  8. Hello. I really enjoy your youtube channel.

    I follow this page but I had an error.

    “make[2]: *** No rule to make target ‘/usr/lib/aarch64-linux-gnu/libGL.so’, needed by ‘lib/libopencv_cudev.so.3.4.1’. Stop.
    CMakeFiles/Makefile2:1626: recipe for target ‘modules/cudev/CMakeFiles/opencv_cudev.dir/all’ failed
    make[1]: *** [modules/cudev/CMakeFiles/opencv_cudev.dir/all] Error 2
    Makefile:162: recipe for target ‘all’ failed
    make: *** [all] Error 2
    Make did not successfully build
    Please fix issues and retry build”

    Would you please help?

      1. Here is my Jetson TX2

        NVIDIA Jetson TX2
        * Jetpack UNKNOWN [L4T 32.1.0]
        * CUDA GPU architecture 6.2
        * NV Power Mode: MAXN – Type: 0
        – Libraries:
        * CUDA 10.0.166
        * cuDNN 7.3.1.28-1+cuda10.0
        * TensorRT 5.0.6.3-1+cuda10.0
        * Visionworks 1.6.0.500n
        * OpenCV NOT_INSTALLED compiled CUDA: NO
        – Jetson Performance: inactive
        – Jetson Easy v1.5.0

        1. You are using a later version of L4T.
          I believe that you need to comment out the following lines in the script:

          # Clean up the OpenGL tegra libs that usually get crushed
          cd /usr/lib/aarch64-linux-gnu/
          sudo ln -sf tegra/libGL.so libGL.so

          Note that you may need to do a new L4T install before this works.

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