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:
23 Responses
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?
I’d like it too (for facial recognition, face/object tracking)
Thanks Jim
Jim,
Im getting “camera open failed” do you have any sugestions?
Greetings
Which example are you using?
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.
The packages are located in the Universe repository. Do you have access problems due to firewall issues?
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?
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!
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!
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.
Could you also provide for python3 ? It worked for python2 but did not for python3.
You should be able to modify the scripts for your purposes. Good luck on your project!
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?
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
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?
Which Jetson are you using, and what version of L4T?
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
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.
Thanks a lot.
I’ll try this weekends and report here.
I tried without reinstall L4T, but failed.
I’ll reinstall L4T with Nvidia SDK manager then try.
did it work?
I just tried and did it work. Thanks kangalow.
You are welcome and thanks for reading!