Thursday, June 27, 2013

Most Complete Guide For Installing, Compiling, and Running OpenCV on Ubuntu!

I went through many guides to install OpenCV on my Ubuntu machine, but most of them are incomplete. A complete installation of OpenCV, with working video encoding/decoding functionalities, requires support of ffmpeg and x264, which in turn depend on many other libraries. 

In this guide, we will first install x264 and ffmpeg, and eventually OpenCV. Finally we will see how to configure OpenCV and compile programs. Most of the information is found at the following three blogs: [1], [2], and [3].


1. Remove old ffmpeg and x264

  • sudo apt-get remove ffmpeg x264 libx264-dev libav-tools libvpx-dev

2. Install all dependencies for ffmpeg and x264

  • sudo apt-get install build-essential checkinstall git cmake libfaac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev libx11-dev libxfixes-dev libxvidcore-dev texi2html yasm zlib1g-dev pkg-config
  • Download and install yasm 1.2.0 from here
    • ./configure --prefix=/usr
    • make
    • sudo make install
Image I/O
sudo apt-get install libtiff4-dev libjpeg-dev libjasper-dev
Video I/O
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev
Python
sudo apt-get install python-dev python-numpy
Download and install libjpeg
sudo apt-get install libjpeg8 libjpeg8-dev
Other third-party libraries
sudo apt-get install libtbb-dev
GUI
sudo apt-get install libqt4-dev libgtk2.0-dev

3. Install x.264 and ffmpeg

In install x264 and ffmpeg, I mainly follow the guide given at [3]. Below are the main steps.

X264

Download and install latest x264 from videolan.org.
  • git clone --depth 1 git://git.videolan.org/x264
  • cd x264
  • ./configure --enable-static --enable-shared
  • make
  • sudo checkinstall --pkgname=x264 --pkgversion="3:$(./version.sh | awk -F'[" ]' '/POINT/{print $4"+git"$5}')" --backup=no --deldoc=yes --fstrans=no --default

libvpx

  • git clone --depth 1 http://git.chromium.org/webm/libvpx.git
  • cd libvpx
  • ./configure --enable-shared
  • make
  • sudo checkinstall --pkgname=libvpx --pkgversion="1:$(date +%Y%m%d%H%M)-git" --backup=no \--deldoc=yes --fstrans=no --default
If you get some error like "relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC",  compile libvpx with  "fPIC" option. You can do it with the following modified configure command": 
CFLAGS=-fPIC ./configure --enable-shared


Install librtmp

apt-get install librtmp-dev

FFMPEG

Downlaod latest ffmpeg 1.2.1 from here. Go to the download folder and run the following commands:
  • tar -xvf ffmpeg-1.2.1.tar.bz2
  • cd ffmpeg-1.2.1/
  • ./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb   --enable-libopencore-amrwb --enable-librtmp --enable-libtheora --enable-libvorbis   --enable-libvpx --enable-libx264 --enable-nonfree --enable-version3 --enable-x11grab --enable-shared --enable-pic
  • make
  • sudo make install

4. Install OpenCV

Get a copy of the latest source code here for OpenCV 2.4.5. Open a terminal and go to the directory where you have downloaded the tar file, and run the following commands:
  • tar -xvf opencv-2.4.5.tar.gz
  • cd opencv-2.4.5/
  • mkdir build
  • cd build/
  • cmake -D WITH_QT=ON -D WITH_XINE=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D BUILD_EXAMPLES=ON CMAKE_BUILD_TYPE=RELEASE -D BUILD_NEW_PYTHON_SUPPORT=ON -D CMAKE_INSTALL_PREFIX=/usr/local ..
  • make
  • sudo make install

5. Configure OpenCV

We need to tell linux where the shared libraries for OpenCV are located by entering the following shell command:
  • export LD_LIBRARY_PATH=/usr/local/lib

Add the command to your .bashrc file so that you don’t have to enter every time your start a new terminal.
Alternatively, you can configure the system wide library search path. Using your favorite editor, add a single line containing the text /usr/local/lib to the end of a file named/etc/ld.so.conf.d/opencv.conf. In the standard Ubuntu install, the opencv.conf file does not exist; you need to create it. Using vi, for example, enter the following commands:
  • sudo vi /etc/ld.so.conf.d/opencv.conf
  • G
  • o
  • /usr/local/lib
  • :wq!

After editing the opencv.conf file, enter the following command:
  • sudo ldconfig /etc/ld.so.conf

Using your favorite editor, add the following two lines to the end of /etc/bash.bashrc:
  • PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
  • export PKG_CONFIG_PATH

After completing the previous steps, your system should be ready to compile code that uses the OpenCV libraries. The following example shows one way to compile code for OpenCV:
  • g++ `pkg-config opencv --cflags` my_code.cpp  -o my_code `pkg-config opencv --libs` 

If you are using a Makefile, append CFLAGS and LDFLAGS as follows:
#Compiler flags
CFLAGS=-c `pkg-config opencv --cflags`
#Linker flags
LDFLAGS= `pkg-config opencv --libs`

Note: Sometimes Makefile does not include libraries if you put LDFLAGS in the middle of link command, i.e.
  • g++ $(OBJECTS) $(LDFLAGS) -o output
It is better to put $(LDFLAGS) at the end of the command after specifying output file, i.e. 
  • g++ $(OBJECTS) -o output $(LDFLAGS)

       

[1] http://karytech.blogspot.ca/2012/05/opencv-24-on-ubuntu-1204.html
[2] http://www.ozbotz.org/opencv-installation/\
[3] https://gist.github.com/faleev/3435377

How to connect to uOttawa network through vpnc on Ubuntu Linux!

Basic Setup

If you find is difficult to install standard university provided vpn software, you can use the vpn client that comes with Ubuntu, vpnc.  All you need is the information to create a configuration file. University provides a configuration file in a different format. We need to conver it in right format, copy at right place, and just run it.

Following are the steps to connect your Ubuntu linux machine to university vpn:
  1. Download the empl.pcf file from the following link: 
  2. Open the terminal and go to the folder where you downloaded empl.pcf and run the following command:
    • pcf2vpnc empl.pcf  empl.conf
  3. Copy the empl.conf file to /etc/vpnc/
    • sudo cp empl.conf>/etc/vpnc/
  4. Run vpnc with empl as argument
    • sudo vpnc empl
  5. Enter username (for employees its "E" followed by your employee number without the leading '100',e.g. E123456) and password (infoweb password).
  6. You can also hard code username and password in empl.conf file by uncommenting 
    • Xauth username
    • Xauth password
  7. To disconnect, run following command
    • sudo vpnc-disconnect

If some of the commands do not work on your computer, you may have to install new software. Find more information here

Advanced

Same procedure should work to connect to any private network. You just need the following information from the private network owner: 

IPSec gateway
IPSec ID
IPSec secret
IKE Authmode
Xauth username
Xauth password

Obtain this information and store in myvpn.conf; and follow the instructions above to run the vpn client.

Saturday, June 22, 2013

Converting pdf figure into vectored eps figure!

I used linux "convert" command to convert pdf figures into eps files, but the output figure quality was very bad. Probably its not a vectored eps figure. Therefore, I searched more and found that eps figures created by pdftops are vectored.

To convert file.pdf into file.eps, run the following command in the terminal:

pdftops file.pdf file.eps 

Wednesday, June 12, 2013

How to create vectored eps file from LibreOffice Draw!

While writing papers in Latex, we need to have vectored eps files for good quality figures. LibreOffice Draw does not have any option to save as eps in the "save as" list. However, you can export a figure (or selection to avoid white space around the figure) as eps file.

Following are the steps:
  • Create a new figure or  open existing figure in LibreOffice Draw. You can even open Microsoft Office figures in LibreOffice Draw. 
  • Select the figure you want to save. Most of the times we want to save the whole figure, so we select all (ctrl+A). If you do not select the figure, LibreOffice Draw will store it as a page with unnecessary white space around the figure.
  • Click File->Export; a window will pop-up
  • At the bottom, from "File type", select eps format.
  • Check the "Selection" box.
  • Save at your desired location by clicking on save
*Note: For some figures the LibreOffice may have problem predicting size during exporting. It happened to me with tables without boundary. Try adding boundaries to avoid that. You can select line color white if you do not want the border to be visible.