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.
git clone https://github.com/Itseez/opencv.git
mkdir release ; cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make install
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 :
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.
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);
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.
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");
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 );
The following snippet shows how to iterate through each pixels of a matrix. src.at
for( int i = 0; i < src.rows; ++i)
for( int j = 0; j < src.cols; ++j )
cout << (int) src.at<uchar>(i,j) ;
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.