Search This Blog

Friday, June 20, 2014

Installing OpenCV on Ubuntu

opencv logo
To install OpenCV (manual build) in Windows refer to this post (for installation without Intel TBB support) or this post (for installation with Intel TBB support)
I have given three different methods to install OpenCV in Ubuntu. They are presented in increasing order of difficulty (I know you're not going to read past the first one).

Method 1: Using apt-get (Simplest method)

The simplest method to install OpenCV in Ubuntu is by using apt-get. The only problem with this method is that it does not installs the latest OpenCV build. Ubuntu repositories have slightly older versions of OpenCV.

But if that's not a problem to you, just type the following in your terminal:

$ sudo apt-get install libopencv*

Method 2: Use installation script

If you want the latest OpenCV or method 1 does not work for you for some reasons, you can use an installation script using the following steps (go to  https://help.ubuntu.com/community/OpenCV for more details):

  • Download the script opencv.sh from here by typing following in terminal:
$ wget https://raw.githubusercontent.com/ashutoshdtu/OpenCV_installation_script/master/opencv.sh
  • Run the opencv.sh script: 

$ chmod +x opencv.sh

$ sudo ./opencv.sh

Method 3: Manual installation from source

The above two methods should be sufficient for successful installation of OpenCV on your ubuntu machine. However if you want to install OpenCV manually,  first install a developer environment to build OpenCV:

$ sudo apt-get -y install build-essential cmake pkg-config

"build-essential" contains tools (like the gcc compiler, make tool, etc) for compiling/building software from source. "cmake" is a cross-platform build system. "pkg-config" is tool that provides a unified interface for querying installed libraries for the purpose of compiling software from its source code.

Install other dependencies:

$ sudo apt-get -y install checkinstall yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg

These contain support for reading and writing image files, drawing on the screen, threading, some needed tools, etc...

Find latest version of OpenCV:

$ version="$(wget -q -O - http://sourceforge.net/projects/opencvlibrary/files/opencv-unix | egrep -m1 -o '\"[0-9](\.[0-9])+' | cut -c2-)"

Download latest OpenCV:

$ wget -O OpenCV-$version.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/$version/opencv-"$version".zip/download

Unzip opencv: 

$ unzip OpenCV-$version.zip

cd to opencv directory and build opencv:

$ cd opencv-$version

$ mkdir build

$ cd build

$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..

$ make -j2

$ sudo make install

Now you have to configure OpenCV. First, open the opencv.conf file with the following code:

$ sudo gedit /etc/ld.so.conf.d/opencv.conf

Add the following line at the end of the file(it may be an empty file, that is ok) and then save it:

/usr/local/lib



Run the following code to configure the library:

$ sudo ldconfig

Now you have to open another file: 

$ sudo gedit /etc/bash.bashrc

Add these two lines at the end of the file and then save it:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH


Finally, close the console and open a new one, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.

Now you have OpenCV 2.4.1 installed in your computer with Python, TBB, OpenGL, video, and Qt support.

1 comment: