Installing OpenCV (including the GPU module) on Jetson TK1
First you should download & install the CUDA Toolkit by following the Installing CUDA instructions, since it is needed by OpenCV.
Once you have the CUDA Toolkit installed, you can begin to install OpenCV. You have several options (deciding between prebuilt library vs building the library from source, and then deciding whether to compile your code onboard the Jetson TK1 or cross-compile it from your desktop).
Prebuilt OpenCV library versus Building the OpenCV library from source
There are two compatible implementations of OpenCV available for Jetson TK1, but only the first option gives you the CPU optimized library, so you are highly recommended to use this first option:
Option 1) Prebuilt OpenCV4Tegra library for L4T:
The Jetson TK1 Support Page contains the two .deb packages for the latest prebuilt OpenCV4Tegra library (CPU & GPU optimized version of OpenCV) that you can simply install onto your device by running "sudo dpkg -i <file.deb>" on those two files. eg: For OpenCV v22.214.171.124, run the following 2 commands to install the prebuilt packages:
sudo dpkg -i libopencv4tegra_126.96.36.199_armhf.deb sudo dpkg -i libopencv4tegra-dev_188.8.131.52_armhf.deb
Continue to the "Testing OpenCV" section lower in this page to ensure OpenCV4Tegra works on your device.
Option 2) Building the public OpenCV library from source:
If you want the latest OpenCV code, or you want to customize the OpenCV library, and don't want NVIDIA's CPU & multi-core optimizations of OpenCV4Tegra, then follow the instructions below to compile the OpenCV library from source code. Note that you won't obtain the full performance of OpenCV4Tegra.
Native vs Cross-development
- native compilation (compiling code onboard the Jetson TK1)
- cross-compilation (compiling code on an x86 desktop in a special way so it can execute on the Jetson TK1 target device).
Native compilation is generally the easiest option, but takes longer to compile, whereas cross-compilation is typically more complex to configure and debug, but for large projects it will be noticeably faster at compiling.
Natively compiling the OpenCV library from source onboard the device
Note: Compiling OpenCV from source will not give you NVIDIA's CPU optimizations that are only available in the closed-source prebuilt OpenCV4Tegra packages.
If you haven't added the "universal" repository to Ubuntu, then do it now:
sudo add-apt-repository universe sudo apt-get update
Now you need to install many libraries:
# Some general development libraries sudo apt-get install build-essential make cmake cmake-curses-gui g++ # libav video input/output development libraries sudo apt-get install libavformat-dev libavutil-dev libswscale-dev # Video4Linux camera development libraries sudo apt-get install libv4l-dev # Eigen3 math development libraries sudo apt-get install libeigen3-dev # OpenGL development libraries (to allow creating graphical windows) sudo apt-get install libglew1.6-dev # GTK development libraries (to allow creating graphical windows) sudo apt-get install libgtk2.0-dev
Download the source code of OpenCV for Linux onto the device. eg: Open a web-browser to "www.opencv.org" & click on "OpenCV for Linux/Mac", or from the command-line you can run this on the device:
Unzip the OpenCV source code:
cd Downloads unzip opencv-2.4.9.zip mv opencv-2.4.9 ~
Configure OpenCV using CMake:
cd ~/opencv-2.4.9 mkdir build cd build cmake -DWITH_CUDA=ON -DCUDA_ARCH_BIN="3.2" -DCUDA_ARCH_PTX="" -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF ..
If you want to customize any more of the build settings such as whether to support Firewire cameras or Qt GUI, it is easiest to use the curses interactive version of CMake from here on:
(Change any settings you want, then click Configure and Generate).
Now you should be ready to build OpenCV and then install it. Unfortunately, OpenCV is currently experiencing a problem with CMake where installing the built libraries (that normally takes a few seconds) re-compiles the whole OpenCV (that normally takes close to an hour). So to save time, instead of running "make -j4 ; make install", we will build & install OpenCV using a single command.
To build & install the OpenCV library using all 4 Tegra CPU cores (takes around 40 minutes), including copying the OpenCV library to "/usr/local/include" and "/usr/local/lib":
sudo make -j4 install
Finally, make sure your system searches the "/usr/local/lib" folder for libraries:
echo "# Use OpenCV and other custom-built libraries." >> ~/.bashrc echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/" >> ~/.bashrc source ~/.bashrc
Compile & run a few of the OpenCV sample programs to make sure OpenCV is working. To get the OpenCV sample codes, you can download the full OpenCV source code, or just download each sample individually from the CPP folder and the GPU folder. For simple programs you can just link to a few OpenCV libs, but for other programs you might as well link to all the OpenCV libs:
# Make sure we have installed a C++ compiler. sudo apt-get install build-essential g++ # Test a simple OpenCV program. Creates a graphical window, hence you should plug a HDMI monitor in or use a remote viewer such as X Tunneling or VNC or TeamViewer on your desktop. cd ~/opencv-2.4.9/samples/cpp g++ edge.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -o edge ./edge
# If you have a USB webcam plugged in to your board, then test one of the live camera programs. g++ laplace.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_calib3d -lopencv_contrib -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_video -lopencv_videostab -o laplace ./laplace
# Test a GPU accelerated OpenCV sample. cd ../gpu g++ houghlines.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_calib3d -lopencv_contrib -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_video -lopencv_videostab -o houghlines ./houghlines ../cpp/logo_in_clutter.png
If the houghlines GPU sample program worked then you have successfully installed OpenCV and its GPU module!
Getting to know OpenCV
Now you can start building your own projects using OpenCV on CPU and GPU, such as by following the Using OpenCV with gcc and CMake introduction tutorial then following many of the official OpenCV Tutorials, playing with the sample GPU programs in the samples/gpu folder of OpenCV and the many sample CPU programs in the samples/cpp folder. To get offline documentation and tutorials of OpenCV, the easiest way is to download the OpenCV Reference Manual PDF file, or if you want offline HTML docs then try building it:
sudo apt-get install python sphinx-common python-sphinx texlive-binaries make html_docs
Then open the "doc/_html/index.html" page.
You can learn more about NVIDIA's Tegra hardware acceleration of OpenCV on the OpenCV Performance page, including a long list showing how much electrical power is used by Jetson TK1 for various OpenCV & computer vision sample programs.