OpenCV Installation in Linux

The packages can be installed using a terminal and the following commands or by using Synaptic Manager:

[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev


Getting the Latest Stable OpenCV Version

Building OpenCV from Source Using CMake, Using the Command Line

  1. Create a temporary directory, which we denote as <cmake_binary_dir>, where you want to put the generated Makefiles, project files as well the object files and output binaries.

  2. Enter the <cmake_binary_dir> and type

    cmake [<some optional parameters>] <path to the OpenCV source directory>

    For example

    cd ~/opencv
    mkdir release
    cd release
  3. Enter the created temporary directory (<cmake_binary_dir>) and proceed with:

    sudo make install


Use cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local .. , without spaces after -D if step 2 do not work.

If the size of the created library is a critical issue (like in case of an Android build) you can use the install/strip command to get the smallest size as possible. The stripped version appears to be twice as small. However, we do not recommend using this unless those extra megabytes do really matter.


Now you have to open another file:

1 sudo gedit /etc/bash.bashrc

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

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

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.9 installed in your computer with 3D visualization, Python, Java, TBB, OpenGL, video, and Qt support.

Check out the cool Qt interface which provides image viewing capabilities with zoom, as well as the ability to save the current image with just one click.

If you zoom in enough, you can see the RGB (or intensity) values for each pixel.

Now let’s build some samples included in OpenCV:

1 cd ~/opencv-2.4.9/samples/c
2 chmod +x
3 ./
These examples use the old C interface:
1 ./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml"--scale=1.5 lena.jpg

1 ./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml"--nested-cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_eye.xml" --scale=1.5 lena.jpg

The following examples use the new C++ interface:

1 ~/opencv-2.4.9/build/bin/cpp-example-grabcut ~/opencv-2.4.9/samples/cpp/lena.jpg

1 ~/opencv-2.4.9/build/bin/cpp-example-calibration_artificial

Now let’s run some Python code:

1 python ~/opencv-2.4.9/samples/python2/

Now you can build a Java sample using ant for example. (Make sure that you change /home/samontab/ with your actual home directory):

1 cd ~/opencv-2.4.9/samples/java/ant
2 ant -DocvJarDir=/home/samontab/opencv-2.4.9/build/bin -DocvLibDir=/home/samontab/opencv-2.4.9/build/lib


Now let’s read a video and use OpenGL with Qt through this great sample that detects the features from the video, then estimates the 3D location of the structure using POSIT, and finally uses OpenGL to draw in 3D (great sample Javier):

1 cd ~/opencv-2.4.9/samples/cpp/Qt_sample
2 mkdir build
3 cd build
4 cmake ..
5 make
6 ./OpenGL_Qt_Binding


And finally, let’s build a sample using the 3D visualization module viz:

1 cd ~/opencv-2.4.9/samples/cpp/tutorial_code/viz
2 g++ -o widget_pose `pkg-config opencv --cflags` widget_pose.cpp `pkg-config opencv --libs`
3 ./widget_pose


As you can see, now you can use OpenCV with C++, C, Python, and Java. The Qt enhanced 2D interface is enabled, 3D data can be displayed using OpenGL directly, or using the new viz module. Multi threading functionality is enabled using TBB. Also, video support is enabled as well.






Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s