Jetson/Installing OpenCV

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 " on those two files. eg: For OpenCV v2.4.10.1, run the following 2 commands to install the prebuilt packages: sudo dpkg -i libopencv4tegra_2.4.10.1_armhf.deb sudo dpkg -i libopencv4tegra-dev_2.4.10.1_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
Just like with the CUDA development guide, you have two options for developing OpenCV applications for Jetson TK1:
 * 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: sudo apt-get install build-essential make cmake cmake-curses-gui g++ sudo apt-get install libavformat-dev libavutil-dev libswscale-dev sudo apt-get install libv4l-dev sudo apt-get install libeigen3-dev sudo apt-get install libglew1.6-dev sudo apt-get install libgtk2.0-dev
 * 1) Some general development libraries
 * 1) libav video input/output development libraries
 * 1) Video4Linux camera development libraries
 * 1) Eigen3 math development libraries
 * 1) OpenGL development libraries (to allow creating graphical windows)
 * 1) GTK development libraries (to allow creating graphical windows)

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: wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.9/opencv-2.4.9.zip

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: ccmake .. (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

Testing OpenCV
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: sudo apt-get install build-essential g++ cd ~/opencv-2.4.9/samples/cpp g++ edge.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -o edge ./edge
 * 1) Make sure we have installed a C++ compiler.
 * 1) 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.

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
 * 1) If you have a USB webcam plugged in to your board, then test one of the live camera programs.

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
 * 1) Test a GPU accelerated OpenCV sample.

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.