Introduction to Image Processing using OpenCV in C++

Image Processing is a programming practice of mathematically analysing and manipulating digital images. This tutorial introduces the basic concepts of 2d Image Processing using the popular open source library OpenCV.


Image Processing involves analysing pixels of 2d images and applying signal processing techniques. There are many image processing libraries available for different programming languages. Matlab for example has a built-in library to perform common image processing tasks. In this tutorial, we will consider the popular library, OpenCV for C++.


To install OpenCV library, you need to first download the source code and then depending on your operating system, compile the library. Then, whenever you want to compile a C++ program, you would need to pass the necessary flags to include the library headers as we will see later in this tutorial.


  • Download the source code

    git clone https://github.com/Itseez/opencv.git

  • Change the working directory to the downloaded folder and run :

    mkdir release ; cd release



    sudo make install

  • Download the latest zip file of the library from here and extract to a location of your choice.
  • Add the location as a header souce in your C++ compiler application.

  • Install MacPorts from here.
  • Execute the following to automatically download and compile OpenCV library along with it's dependencies!

    sudo port selfupdate

    sudo port install opencv

Understanding Image storage techniques and Matrices

Image in digital form is stored as an array of numerical information. If you zoom in on an image deep enough, you start noticing a series of square blocks with each block having uniform color. This block is called a pixel.

An image can be formed by combining thousands to millions of these tiny pixels. One can infer that to store any image digitally, one needs to get the following information:

What is the color of a pixel given its position in the image ?

Now position of a point in 2-dimensional system can be determined by calculating its distance from a horizontal axis and vertical axis respectively. This kind of information is perfectly represented in the form of matrix.

Suppose there is an image whose height is 300 pixels and width is 500 pixels. To represent this image, we use of matrix of order 300x500. The value at each position in matrix represent the intensity of color at the corresponding position in the image.

OpenCV has a built-in datatype to represent image matrices called Mat. For example, to initialize an image of dimensions 300x500 pixels, the syntax is :

Mat image(300,500);

Color storing methods

There are three major types of color storing methods :


It is the most fundamental way to store an image. The image is least defined black and white in color. Intensity information is stored either as 0 or 1.

Mat image(300,500);

Gray Scale

Basically black and white image but more defined than a binary image. In this, the intensity value stored usually ranges from 0 to 255, i.e. 1 byte of data.

Mat image(300,500, CV_8U1);

RGB Image

It is one of the most standard method to store color images. It is multichannel method, i.e. there are three different intensities stored for each pixel. The three channels correspond to intensities of Red, Blue and Green color.

Mat image(300,500, CV_8U3);

Here, in CV_8U3, 8 means 8 bits, U means unsigned char and 3 means 3 channels.

Some basic OpenCV functions

Below are some of the most fundamental OpenCV functions used to get started with image processing. These include functions to read image from files, storing them as C++ objects, accessing pixel for reading and writing information and finally displaying an image.

Read Image from file

The function imread opens the file provided in the location and saves it in the form of image matrix for processing.

Mat src = imread("/path/to/image.jpg");

Display image

First, a window is created which will show the image. Second, we load the image matrix to the newly created window which displays the image.

namedWindow( "Display window", WINDOW_AUTOSIZE );

imshow( "Display window", src );

Looping through pixels

The following snippet shows how to iterate through each pixels of a matrix. src.at(i,j) is a template function, taking the datatype input as the datatype of intensity of color. In case of grayscale image, the datatype of color is uchar (1 byte) and to print the ascii value we type case the output to int.

for( int i = 0; i < src.rows; ++i)

for( int j = 0; j < src.cols; ++j )

cout << (int) src.at<uchar>(i,j) ;

Rohan Raja

Recently graduated, majoring in Mathematics and Computing from IIT Kharagpur, Rohan is a technology enthusiast and passionate programmer. Likes to apply Mathematics and Artificial Intelligence to devise creative solutions to common problems.