Monday, April 21, 2014

Install OpenCV 2.4.x on Ubuntu 12.04 LTS with CUDA 5.5 or 6, OpenNI, GStreamer, FFMPEG, QT5, Java ...

I thought I would compile a post on how I was able to set up my OpenCV environment; the information to build OpenCV with many dependencies is somewhat lacking.

Here are my cmake results. Hopefully I can assist you to getting to this point.
 --   Linker flags (Release):     
 --   Linker flags (Debug):      
 --   Precompiled headers:     YES  
 --  OpenCV modules:  
 --   To be built:         core flann imgproc highgui features2d calib3d ml video legacy objdetect photo gpu ocl nonfree contrib java python stitching superres ts videostab  
 --   Disabled:          world  
 --   Disabled by dependency:   -  
 --   Unavailable:         androidcamera dynamicuda viz  
 --  GUI:   
 --   QT 5.x:           YES (ver 5.0.2)  
 --   QT OpenGL support:      YES (Qt5::OpenGL 5.0.2)  
 --   OpenGL support:       YES (/usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/  
 --   VTK support:         NO  
 --  Media I/O:   
 --   ZLib:            /usr/lib/x86_64-linux-gnu/ (ver  
 --   JPEG:            build (ver 62)  
 --   PNG:             build (ver 1.5.12)  
 --   TIFF:            build (ver 42 - 4.0.2)  
 --   JPEG 2000:          build (ver 1.900.1)  
 --   OpenEXR:           /usr/lib/ /usr/lib/ /usr/lib/ /usr/lib/ /usr/lib/ (ver 1.6.1)  
 --  Video I/O:  
 --   DC1394 1.x:         NO  
 --   DC1394 2.x:         YES (ver 2.2.0)  
 --   FFMPEG:           YES  
 --    codec:           YES (ver 53.35.0)  
 --    format:          YES (ver 53.21.1)  
 --    util:           YES (ver 51.22.2)  
 --    swscale:          YES (ver 2.1.0)  
 --    gentoo-style:       YES  
 --   GStreamer:            
 --    base:           YES (ver 0.10.36)  
 --    app:            YES (ver 0.10.36)  
 --    video:           YES (ver 0.10.36)  
 --   OpenNI:           YES (ver 1.5.7, build 10)  
 --   OpenNI PrimeSensor Modules: YES (/usr/lib/  
 --   PvAPI:            NO  
 --   GigEVisionSDK:        NO  
 --   UniCap:           NO  
 --   UniCap ucil:         NO  
 --   V4L/V4L2:          Using libv4l (ver 0.8.6)  
 --   XIMEA:            NO  
 --   Xine:            NO  
 --  Other third-party libraries:  
 --   Use IPP:           NO  
 --   Use Eigen:          YES (ver 2.0.17)  
 --   Use TBB:           NO  
 --   Use OpenMP:         NO  
 --   Use GCD           NO  
 --   Use Concurrency       NO  
 --   Use C=:           NO  
 --   Use Cuda:          YES (ver 5.5)  
 --   Use OpenCL:         YES  
 --   Use CUFFT:          YES  
 --   Use CUBLAS:         NO  
 --   USE NVCUVID:         NO  
 --   NVIDIA GPU arch:       11 12 13 20 21 30 35  
 --   NVIDIA PTX archs:      30  
 --   Use fast math:        NO  
 --  OpenCL:  
 --   Version:           dynamic  
 --   Include path:        /home/andrew/Development/OpenCV/opencv-2.4.9/3rdparty/include/opencl/1.2  
 --   Use AMD FFT:         NO  
 --   Use AMD BLAS:        NO  
 --  Python:  
 --   Interpreter:         /usr/bin/python (ver 2.7.3)  
 --   Libraries:          /usr/lib/ (ver 2.7.3)  
 --   numpy:            /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.6.1)  
 --   packages path:        lib/python2.7/dist-packages  
 --  Java:  
 --   ant:             /usr/bin/ant (ver 1.8.2)  
 --   JNI:             /usr/lib/jvm/java-7-openjdk-amd64/include /usr/lib/jvm/java-7-openjdk-amd64/include /usr/lib/jvm/java-7-openjdk-amd64/include  
 --   Java tests:         YES  
 --  Documentation:  
 --   Build Documentation:     YES  
 --   Sphinx:           /usr/bin/sphinx-build (ver 1.1.3)  
 --   PdfLaTeX compiler:      /usr/bin/pdflatex  
 --  Tests and samples:  
 --   Tests:            YES  
 --   Performance tests:      YES  
 --   C/C++ Examples:       YES  
 --  Install path:         /usr/local  
 --  cvconfig.h is in:       /home/andrew/Development/OpenCV/opencv-2.4.9/Build  
 -- -----------------------------------------------------------------  
 -- Configuring done  
 -- Generating done  
 -- Build files have been written to: /home/andrew/Development/OpenCV/opencv-2.4.9/Build  


We will start with installing Cuda first :). There are two options to go about this, (i) install Cuda from the package manager by adding the Nvidia repos to the sources list, (ii) Install from .run file. We will be using the package manager for installing Cuda. I have installed Cuda using both the package manager and the .run file; believe when I say this, it is much less of an headache with the package manager.

First lets make sure we are set to install CUDA
 lspci | grep -i nvidia  
This checks to make sure there is an Nvidia device in your computer. Here is my output...
 0f:00.0 VGA compatible controller: NVIDIA Corporation G94GL [Quadro FX 1800] (rev a1)  
I am using g++ version 4.6.3, you can check your version of g++ with:
 gcc -v  

Navigate to and download the .deb for your version of Ubuntu.

Navigate to where you downloaded the .deb file ie "cd ~/Downloads/" in the terminal. Run:
 sudo dpkg -i cuda-repo-<distro>_<version>_<architecture>.deb  
 sudo apt-get update  

Now here is when things get tricky. Cuda 6 was released a few days ago, and I'm not 100% sure if its gonna play nice with OpenCV. So for now we will go with Cuda 5.5.
To install Cuda 5.5:

 sudo apt-get install cuda-5-5  

Note: This command did not work for me... it complained about the dependency issues that didn't make a whole lot of sense. The way I installed it was:

 sudo apt-get install aptitude  
 sudo aptitude install cuda-5-5  

I allowed aptitude to continue with the installation, as it took care all of the dependency issues for me. I am also not responsible if you wreck your desktop...!!!
Reboot when the installation is finished.

To test your Cuda installation:
 cd /usr/local/cuda-5.5/samples/1_Utilities/deviceQuery  
 sudo make  

Here are the results I got.
 CUDA Device Query (Runtime API) version (CUDART static linking)  
 Detected 1 CUDA Capable device(s)  
 Device 0: "Quadro FX 1800"  
  CUDA Driver Version / Runtime Version     5.5 / 5.5  
  CUDA Capability Major/Minor version number:  1.1  
  Total amount of global memory:         767 MBytes (804585472 bytes)  
  ( 8) Multiprocessors, ( 8) CUDA Cores/MP:   64 CUDA Cores  
  GPU Clock rate:                1375 MHz (1.38 GHz)  
  Memory Clock rate:               800 Mhz  
  Memory Bus Width:               192-bit  
  Maximum Texture Dimension Size (x,y,z)     1D=(8192), 2D=(65536, 32768), 3D=(2048, 2048, 2048)  
  Maximum Layered 1D Texture Size, (num) layers 1D=(8192), 512 layers  
  Maximum Layered 2D Texture Size, (num) layers 2D=(8192, 8192), 512 layers  
  Total amount of constant memory:        65536 bytes  
  Total amount of shared memory per block:    16384 bytes  
  Total number of registers available per block: 8192  
  Warp size:                   32  
  Maximum number of threads per multiprocessor: 768  
  Maximum number of threads per block:      512  
  Max dimension size of a thread block (x,y,z): (512, 512, 64)  
  Max dimension size of a grid size  (x,y,z): (65535, 65535, 1)  
  Maximum memory pitch:             2147483647 bytes  
  Texture alignment:               256 bytes  
  Concurrent copy and kernel execution:     Yes with 1 copy engine(s)  
  Run time limit on kernels:           Yes  
  Integrated GPU sharing Host Memory:      No  
  Support host page-locked memory mapping:    Yes  
  Alignment requirement for Surfaces:      Yes  
  Device has ECC support:            Disabled  
  Device supports Unified Addressing (UVA):   No  
  Device PCI Bus ID / PCI location ID:      15 / 0  
  Compute Mode:  
    < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >  
 deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 5.5, CUDA Runtime Version = 5.5, NumDevs = 1, Device0 = Quadro FX 1800  
 Result = PASS  

If your were able to build the Cuda example and run it... then success!!


Follow this guy's directions on adding the Ubuntu repo and installing QT5.


 sudo apt-get install libgtk-3-0 libgtk-3-dev  

OpenNI + SensorKinect + Primesense 

Okay here is where things get weird. First we need to build and install OpenNI. So clone OpenNI SDK git repo.

 git clone  
 cd OpenNI  

Also may want to check out unstable branch... "git checkout unstable"
Follow the directions in the README file building and installing the libraries.
           1) GCC 4.x  
             Or via apt:  
             sudo apt-get install g++  
           2) Python 2.6+/3.x  
             Or via apt:  
             sudo apt-get install python  
           3) LibUSB 1.0.x  
             Or via apt:  
             sudo apt-get install libusb-1.0-0-dev  
           4) FreeGLUT3  
             Or via apt:  
             sudo apt-get install freeglut3-dev  
           5) JDK 6.0  
             Or via apt:  
              Ubuntu 10.x:                      
               sudo add-apt-repository "deb lucid partner"  
               sudo apt-get update  
               sudo apt-get install sun-java6-jdk  
              Ubuntu 12.x:                      
               sudo apt-get install openjdk-6-jdk  
      Optional Requirements (To build the documentation):  
           1) Doxygen  
             Or via apt:  
             sudo apt-get install doxygen  
           2) GraphViz  
             Or via apt:  
             sudo apt-get install graphviz  
      Optional Requirements (To build the Mono wrapper):  
           1) Mono  
             Or via apt:  
             sudo apt-get install mono-complete  
      Building OpenNI:  
           1) Go into the directory: "Platform/Linux/CreateRedist".  
             Run the script: "./RedistMaker".  
             This will compile everything and create a redist package in the "Platform/Linux/Redist" directory.  
             It will also create a distribution in the "Platform/Linux/CreateRedist/Final" directory.  
           2) Go into the directory: "Platform/Linux/Redist".  
             Run the script: "sudo ./" (needs to run as root)  
              The install script copies key files to the following location:  
               Libs into: /usr/lib  
               Bins into: /usr/bin  
               Includes into: /usr/include/ni  
               Config files into: /var/lib/ni  
           To build the package manually, you can run "make" in the "Platform\Linux\Build" directory.  
           If you wish to build the Mono wrappers, also run "make mono_wrapper" and "make mono_samples".  

Okay now checkout the code for connecting to Primesense hardware.
 git clone  
 cd Sensor  
 git checkout unstable  

Building and installing the Primesense modules is strangely familiar to that of OpenNI... Once again follow the directions of the README.
           1) GCC 4.x  
             Or via apt:  
             sudo apt-get install g++  
           2) Python 2.6+/3.x  
             Or via apt:  
             sudo apt-get install python  
           3) OpenNI 1.5.x.x  
      Building Sensor:  
           1) Go into the directory: "Platform/Linux/CreateRedist".  
             Run the script: "./RedistMaker".  
             This will compile everything and create a redist package in the "Platform/Linux/Redist" directory.  
             It will also create a distribution in the "Platform/Linux/CreateRedist/Final" directory.  
           2) Go into the directory: "Platform/Linux/Redist".  
             Run the script: "sudo ./" (needs to run as root)  
              The install script copies key files to the following location:  
               Libs into: /usr/lib  
               Bins into: /usr/bin  
               Config files into: /usr/etc/primesense  
               USB rules into: /etc/udev/rules.d   
               Logs will be created in: /var/log/primesense  
           To build the package manually, you can run "make" in the "Platform\Linux\Build" directory.  
           Important: Please note that even though the directory is called Linux, you can also use it to compile it for 64-bit targets and pretty much any other linux based environment.       

Do the same thing with the KinectSensor code:  
 cd SensorKinect  
 git checkout unstable  

Refer to the README for build and install directions... should be the same as the Primesense.
Plug in your OpenNI device and run:
NiViewer should be in your system path if the install went correctly.


This should the trick
 sudo apt-get install libgstreamer0.10-0 libgstreamer0.10-dev gstreamer0.10-tools gstreamer0.10-plugins-base libgstreamer-plugins-base0.10-dev gstreamer0.10-plugins-good gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad gstreamer0.10-ffmpeg  


Now we need clone build and install FFMPEG from source. Sound familiar?
 git clone git:// ffmpeg
 cd ffmpeg  
Configure the environoment.
 ./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab --enable-shared --enable-pic  
Then if all went smooth
 make -j8
 sudo make install  


 sudo apt-get install openjdk-7-jdk  


There are other libraries out there that OpenCV depends on; libjpeg, libpng, libtiff, v4l, DC1934, and many more. You can install these yourself with the package manager. 

Configure OpenCV for Build

Download the latest OpenCV release form Sourceforge
Unzip the package and navigate into the directory.

 cd OpenCV<>  
 mkdir build  
 cd build  

Your window should look similar to this after you specify where the source is and were you want to build the binaries and after pressing configure.

Select the packages you would like install. Hit configure again. Check to make sure that the cmake output is listing dependencies as YES. Otherwise it will not get built with OpenCV.  I did have to edit the CUDA_TOOLKIT_ROOT_DIR to /usr/local/cuda-5-5 for cmake to find the correct libraries. 

For java:
 export JAVA_HOME=/usr/lib/jvm/java-7-oracle  

Hit configure once again. If you are satisfied with the results we are ready to build!
Make sure you are in your 'build' directory where the make files reside.
 make -j8  
 sudo make install  

That should do it!
Now for some house cleaning...
 sudo gedit /etc/  

Add this line to the file:

Configure the library
 sudo ldconfig  

One more thing...
 sudo gedit /etc/bash.bashrc  

Add these two lines to the end of the file and save it.

The installation should be complete! Navigate to build/bin/ and try out the plethora of samples to make sure everything is working correctly!