Starting Computer Vision Development with OpenCV on Ubuntu

Here I will cover how you can install OpenCV on your Ubuntu system, ways that you can build C++ programs based on this Open Source Computer Vision Library, read from files or camera, do basic image processing operations, draw or write some stuff and save our work.

Table Of Contents

  • Intro
    • About OpenCV
    • Requirements
  • Installing
    • Prepare Your System
    • Get OpenCV
    • Build
    • Configure
  • DevelopingwithOpenCV
    • Application 1 - Simple Window
    • Terminal Build
      • g++
      • make
      • CMake
    • KDevelop Build
    • Application 2 - Load img
    • Application 3 - Load video
    • Application 4 - Draw and process


This is a programming topic, and some previous programming knowledge in any language will help, however I will present all the tools and commands needed to build small programs, so anyone with good logic reasoning skill may be able to build the samples and benefit from it.

Also, it is an introducing material, and will not cover complex programming or scientific concepts, I will focus on how to put things to work instead, give you some sample code for you to try it yourself, become used to it and have fun.

You are encouraged to open the API reference manual on other tab and refer to it whenever you find a new object, function or parameter on the code here provided, once you first built it, do some changes on your own for a better comprehension .

Also, it is a good idea to use man and look the official documentation of each of the build tools, files and terms here presented.

What is OpenCV

OpenCV stands for Open Source Computer Vision Library,  and it main purpose is to create a standardized approach to the computer vision matter. It provide a set of modules that helps you on things such as linear algebra, image processing,machine learning, hardware abstraction, high level GUI, as well as interface to OpenGL,OpenCL, CUDA and so on. Have also in mind, that it is distributed under BSD licence, which  is quite permissive and good  for both research or commercial purposes.

The modules we are going to use in our programs are.

  • Core - the basis of OpenCV
  • HighGUI - Provides a fairly simple and useful high level graphical user interface
  • improc - Provides image processing capabilities


These are the requirements shown on the OpenCV docs, some of them are optional, but it is always a good idea to met all requirements when learning anything.

  • GCC 4.4.x or later
  • CMake 2.6 or higher
  • Git
  • GTK+2.x or higher, including headers (libgtk2.0-dev)
  • pkg-config
  • Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
  • ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
  • [optional] libtbb2 libtbb-dev, libdc1394 2.x, libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev

How to Install

In this first part of the article I will show you how to prepare your system, get, build and install OpenCV and some post-install configuration


First update your apt database

apt-get update

Install the things from requirement section with apt-get.

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

Get OpenCV

We are going to use OpenCV 2.4.11, open a terminal session and use wget as follows:


You can also clone latest release later, and take a look here, it is migration tutorial where you can learn how to make transition between OpenCV releases.

git clone

Build The Sources

Enter the in package directory

cd *2.4.11*

Create a directory for our OpenCV build, and enter on it

mkdir build && cd build

Within the build directory, use cmake to prepare it.

cmake ..

Now that we have a Makefile, we can make and install

make && make install
Building OpenCV
Building OpenCV

How to Configure

Test if your system can find the OpenCV paths, run the following command:

 pkg-config --cflags --libs opencv

If it gives you an error message, your PKG_CONFIG_PATH environment variable may not be pointing to OpenCV and you must add the path on it. Try to run this and the test once again

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

You can append the last command on a profile script to avoid the need to run it every time,  try .bashrc:

vi ~/.bashrc

Developing with OpenCV

We are now ready to start some OpenCV code, here are some sample programs and a ground-up approach on ways to build them.

First Application - Simple Window

This first application simply open a window on the screen, it is not much useful, but good to test your install and learn how to build OpenCV applications


//include high gui headers

#include "opencv2/highgui/highgui.hpp"

//we are using standard and OpenCV namespaces

using namespace cv;
using namespace std;

int main( int argc, const char** argv ){

//create a window named theWindow

namedWindow("theWindow", CV_WINDOW_AUTOSIZE);

//wait user hit some key


//destroy window named theWindow


return 0;


Terminal build

Create a directory for the project

mkdir firstcv && cd firstcv

Create the c++ file with Vi

vi main.cpp

now build it directly with g++

g++ -o firstcv $(pkg-config --libs --cflags opencv) main.cpp


The firstcv application is quite simple and it is easy to build it directly, however as the code become complex, your build commands become cryptic soon, also cleaning and the general maintenance becomes a mess. For this reason we use make

Create a file named Makefile with the following content:

g++ -o firstcv \
-I/usr/local/include/opencv  \
-L/usr/local/lib \
-lopencv_highgui \
-lopencv_core \
-lm \

rm firstcv

It will basically call the compiler with a prepared set of parameters when the all is the target and remove the executable when target is clean, for more details on Makefile, look here

Another fact about make is that if no target id given, it will use the first target, so to build the code you just need make:



The last approach to build applications, but the creation of Makefiles can also become complex when you build across systems. You can then generate your build with CMake, as OpenCV project itself does, it simplify the build management and increase the compatibility across platforms.

Instead of working on the Makefile, we create now a file called CMakeLists.txt with content similar to this:

cmake_minimum_required(VERSION 2.6)
find_package( OpenCV REQUIRED )
add_executable(firstcv main.cpp)
target_link_libraries( firstcv ${OpenCV_LIBS} )

It says:

cmake_minimum_required: minimum CMake version is 2.6

project:name of you project

include_directories:Aditional includes path

find_package: look for the OpenCV package, as we have done with pkg-config earlier

add_executable: the resulting binaries and it's source files.

install: Install path

target_link_libraries: Shared objects to be linked, once again it works like pkg-config

Then create a build directory and enter on it.

mkdir build && cd build

Prepare your build with cmake

cmake ..

With a Makefile in hand, make it:


Just run your program


KDevelop build

Despite you can build entire systems from console terminal, you may do much more by using features like code formatting, syntax highlight code reference, debugger and others by using an IDE. KDevelop is great, simple and I am going to show you how to create your projects based on its Cmake terminal application template.

Click on menu Project->New From Template, then select Standard at Category, Terminal at project type and name it.

creating firstcv from CMake template
Creating firstcv project from CMake template

Once the project is created, change the contents of the main.cpp file with the one provided earlier

Paste contents of firstcv
Paste contents of firstcv

On the projects panel, double-click on the project name, then on  CMakeLists.txt file and change it according to the CMake section.

Paste contents of CmakeLists.txt file
Paste contents of CmakeLists.txt file

Build your project, click on build or hit  [F8] key and run it with [F9], at first run you will need to set the run parameters as follows

Set launch settings
Set launch settings

Now you are able to build OpenCV code, for the next applications you just need to repeat the project creation or replace the source of the main.cpp file.

Application 2 - Show Image

This application simply display the contents of an image file on the window

#include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;

int main( int argc, const char** argv ){

//create a Mat object and load the contents of the linoxide.png file on it

Mat img = imread("linoxide.png", CV_LOAD_IMAGE_UNCHANGED);

//if the img is empty, die with error exit status

if (img.empty()){return -1;}

namedWindow("theWindow", CV_WINDOW_AUTOSIZE);

//show the img on theWindow

imshow("theWindow", img);



return 0;


Here is the program running:

Showing image with OpenVC
Showing image with OpenVC

Application 3 - Load Video

This time we are going to read the content directly from our camera, it is just few changes on the last code

#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

int main( int argc, const char** argv ){

//Initiate camera

VideoCapture cap(0); Mat img;

namedWindow("theWindow", CV_WINDOW_AUTOSIZE);


//if can't read from camera, program dies

if (! { return -1; }

imshow("theWindow", img );

if (waitKey(20) == 27) {return 0;}



Application 4 - Image processing

Now that you can load your images and have a basic program flow, let's enhance this, create some controls, draw something resembling Linoxide's logo,do some trasformations on the images and save them.

//we include High level GUI and Media I/O
#include "opencv2/highgui/highgui.hpp"

//Including Image processing capabilities
#include "opencv2/imgproc/imgproc.hpp"

//OpenCV core module is implicitly included

//we are using both standard and OpenCV namespaces
using namespace std;
using namespace cv;

int main(int argc, char **argv) {

//Initiate video capture device on cap VideoCapture cap(0);

//If could not open capture device, program die if(!cap.isOpened()){return -1;}

//Here is our frame Mat frame;

//We will display our frames here namedWindow("display", CV_WINDOW_AUTOSIZE);

//This window hold trackbars to change the program behaviour namedWindow("config", CV_WINDOW_AUTOSIZE);

//by default we will not invert out frame bool invertimg = false;

//we will draw Linoxide logo sketch bool draw = true;

// create a holder for our frame flip mode int flipmode = 0; // create a trackbar to change the flip mode cvCreateTrackbar("Flip Mode:", "config", &flipmode, 3);

//width and height value holders for image resizing int screen_width = 640; int screen_height = 480;

//create trackbars the config window to change the size properties cvCreateTrackbar("Width:", "config", &screen_width, 1024); cvCreateTrackbar("Height:", "config", &screen_height, 768);

//create a vector of compression parameters that will be used to take a snapshot vector<int> compression_pars; compression_pars.push_back(CV_IMWRITE_PNG_COMPRESSION); compression_pars.push_back(9);


if(!{return -1;} // could not read frame, program die

//if invertimg is true, invert each of the matrix elements by reducing it    s value from the maximum possible if(invertimg == true){ frame =  Scalar::all(255) - frame; }

//flip the frame according to the values on the flip mode trackbar if(flipmode > 0){flip(frame,frame,1);} //flip horizontal if(flipmode > 1){flip(frame,frame,0);} //flip vertical, now is fliped on both directions if(flipmode > 2){flip(frame,frame,1);} //flip horizontal, now only fliped vertically

//resize the frame according to the values on the trackbars resize(frame,frame, Size(screen_width,screen_height) );

if (draw == true){

//draw a white rectangle to use as background, the color is Scalar(B,G,R) rectangle(frame, Point(5,25), Point(140,70), Scalar(255,255,255), -1, 8, 0);

//dark-yellow rectangle, note that alpha value can be used rectangle(frame, Point(5,25), Point(54,55), Scalar(00,225,255,0), -1, 8, 0);

//bright-blue rectangle, the thickness is negative, so it will be completly filled rectangle(frame, Point(57,25), Point(140,55), Scalar(255,225,100), -1, 8, 0);

//black rectangle, start at point x5,y57, ends at x140,y70 rectangle(frame, Point(5,57), Point(140,70), Scalar(0,0,0), -1, 8, 0);

//write the text on the frame, separate by font characteristcs and position putText(frame, "L       ", Point(10,50), FONT_HERSHEY_SIMPLEX, 1, Scalar(0,0,0),2,0); putText(frame, " IN     ", Point(14,50), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,0,0),2,0); putText(frame, "   O    ", Point(10,50), FONT_HERSHEY_SIMPLEX, 1, Scalar(255,255,255),2,0); putText(frame, "     XIDE", Point(14,50), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255,255,255),2,0); putText(frame, "Linux Freedom Thoughts", Point(10,65), FONT_HERSHEY_PLAIN, 0.6, Scalar(255,255,255),0,0);

} //show our frame on the display window imshow("display",frame);

switch(waitKey(10)){ case 27:  //[esc], finish program with success return 0; case 'i': //invert image toggle if (invertimg == false){invertimg = true;}else {invertimg = false;} case 'd': //draw Linoxide toggle if (draw == false){draw = true;}else {draw = false;} case 's': //take a snapshot, save into snapshot.png imwrite("snapshot.png", frame, compression_pars);


} //finish with success return 0;


Here is it

Image processing application running
Image processing application running


That's all for now, you should be able to build programs based on the OpenCV library. The concepts here introduced should give you understanding about the ideas behind the library. But that's only the beginning ,you can dig into the official tutorials, go deeper in reference and sample of OpenCV and take a look at more advanced things, like object detection, face detection, object tracking, template matching, machine learning , 3D mapping.

Thanks for reading, I hope you like it and make great things with OpenCV!

1 thought on “Starting Computer Vision Development with OpenCV on Ubuntu”... add one

Leave a Comment