Difference between revisions of "Jetson/Installing OpenCV"

From eLinux.org
Jump to: navigation, search
(Created the OpenCV native installation instructions. (Needs to be formatted))
 
(Natively compiling the OpenCV library from scratch onboard the Jetson device: Rephrased intro sentence)
Line 6: Line 6:
  
 
== Natively compiling the OpenCV library from scratch onboard the Jetson device ==
 
== Natively compiling the OpenCV library from scratch onboard the Jetson device ==
First you should download & install the CUDA Toolkit by following the steps at [[Tegra/Installing_CUDA|Installing CUDA]].
+
First you should download & install the CUDA Toolkit by following the [[Tegra/Installing_CUDA|Installing CUDA]] instructions.
 
If you haven't added the "universal" repository to Ubuntu, then do it now:
 
If you haven't added the "universal" repository to Ubuntu, then do it now:
 
     sudo add-apt-repository universe
 
     sudo add-apt-repository universe

Revision as of 07:47, 5 June 2014

Installing OpenCV On Linux4Tegra

You have two options for developing OpenCV applications for Jetson TK1: native compilation (compiling code onboard the Jetson TK1) and 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 scratch onboard the Jetson device

First you should download & install the CUDA Toolkit by following the Installing CUDA instructions. 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
   # OpenGL development libraries (to allow creating graphical windows)
   sudo apt-get install libglew1.6-dev
   # Eigen3 math development libraries
   sudo apt-get install libeigen3-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:
       wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.9/opencv-2.4.9.zip

Install the GTK development package so that OpenCV will be able to create GUI windows (such as if you attac a HDMI screen to your board):

   sudo apt-get install libgtk2.0-dev

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), copying the OpenCV library to "/usr/local/include" and "/usr/local/lib":

   sudo make -j4 install

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

   # 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! Now you can start building your own projects using OpenCV on CPU and GPU, such as by following the [TODO Using OpenCV with gcc and CMake] introduction tutorial or playing with the sample GPU programs in the *samples/gpu* folder of OpenCV. To get offline documentation and tutorials of OpenCV, the easiest way is to download the PDF files from the [TODO OpenCV Documentation page], or if you want offline HTML docs then try building it with *sudo apt-get install python sphinx-common python-sphinx texlive-binaries && make html_docs* then open the *“doc/_html/index.html”* page.