A quick start – OpenCV fundamentals

(For more resources related to this topic, see here.)

The OpenCV library has a modular structure, and the following diagram depicts the different modules available in it:

A brief description of all the modules is as follows:




A compact module defining basic data structures, including the dense multidimensional array Mat and basic functions used by all other modules.


An image processing module that includes linear and non-linear image filtering, geometrical image transformations (resize, affine and perspective warping, generic table-based remapping), color space conversion, histograms, and so on.


A video analysis module that includes motion estimation, background subtraction, and object tracking algorithms.


Basic multiple-view geometry algorithms, single and stereo camera calibration, object pose estimation, stereo correspondence algorithms, and elements of 3D reconstruction.


Salient feature detectors, descriptors, and descriptor matchers.


Detection of objects and instances of the predefined classes; for example, faces, eyes, mugs, people, cars, and so on.


An easy-to-use interface to video capturing, image and video codecs, as well as simple UI capabilities.


GPU-accelerated algorithms from different OpenCV modules.

Task 1 – image basics

When trying to recreate the physical world around us in digital format via a camera, for example, the computer just sees the image in the form of a code that just contains the numbers 1 and 0. A digital image is nothing but a collection of pixels (picture elements) which are then stored in matrices in OpenCV for further manipulation. In the matrices, each element contains information about a particular pixel in the image. The pixel value decides how bright or what color that pixel should be. Based on this, we can classify images as:

  • Greyscale
  • Color/RGB


Here the pixel value can range from 0 to 255 and hence we can see the various shades of gray as shown in the following diagram. Here, 0 represents black and 255 represents white:

A special case of grayscale is the binary image or black and white image. Here every pixel is either black or white, as shown in the following diagram:


Red, Blue, and Green are the primary colors and upon mixing them in various different proportions, we can get new colors. A pixel in a color image has three separate channels— one each for Red, Blue, and Green. The value ranges from 0 to 255 for each channel, as shown in the following diagram:

Task 2 – reading and displaying an image

We are now going to write a very simple and basic program using the OpenCV library to read and display an image. This will help you understand the basics.


A simple program to read and display an image is as follows:

// opencv header files #include "opencv2/highgui/highgui.hpp" #include "opencv2/core/core.hpp" // namespaces declaration using namespace cv; using namespace std; // create a variable to store the image Mat image; int main( int argc, char** argv ) { // open the image and store it in the 'image' variable // Replace the path with where you have downloaded the image image=imread("<path to image">/lena.jpg"); // create a window to display the image namedWindow( "Display window", CV_WINDOW_AUTOSIZE ); // display the image in the window created imshow( "Display window", image ); // wait for a keystroke waitKey(0); return 0; }

Code explanation

Now let us understand how the code works. Short comments have also been included in the code itself to increase the readability.

#include "opencv2/highgui/highgui.hpp" #include "opencv2/core/core.hpp"

The preceding two header files will be a part of almost every program we write using the OpenCV library. As explained earlier, the highgui header is used for window creation, management, and so on, while the core header is used to access the Mat data structure in OpenCV.

using namespace cv; using namespace std;

The preceding two lines declare the required namespaces for this code so that we don't have to use the :: (scope resolution) operator every time for accessing the functions.

Mat image;

With the above command, we have just created a variable image of the datatype Mat that is frequently used in OpenCV to store images.

image=imread("<path to image">/lena.jpg");

In the previous command, we opened the image lena.jpg and stored it in the image variable. Replace <path to image> in the preceding command with the location of that picture on your PC.

namedWindow( "Display window", CV_WINDOW_AUTOSIZE );

We now need a window to display our image. So, we use the above function to do the same. This function takes two parameters, out of which the first one is the name of the window. In our case, we would like to name our window Display Window. The second parameter is optional, but it resizes the window based on the size of the image so that the image is not cropped.

imshow( "Display window", image );

Finally, we are ready to display our image in the window we just created by using the preceding function. This function takes two parameters out of which the first one is the window name in which the image has to be displayed. In our case, obviously, that will be Display Window . The second parameter is the image variable containing the image that we want to display. In our case, it's the image variable.


Last but not least, it is advised that you use the preceding function in most of the codes that you write using the OpenCV library. If we don't write this code, the image will be displayed for a fraction of a second and the program will be immediately terminated. It happens so fast that you will not be able to see the image. What this function does essentially is that it waits for a keystroke from the user and hence it delays the termination of the program. The delay here is in milliseconds.


The image can be displayed as follows:

Task 3 – resizing and saving an image

We are now going to write a very simple and basic program using the OpenCV library to resize and save an image.


The following code helps you to resize a given image:

// opencv header files #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/core/core.hpp" // namespaces declaration using namespace std; using namespace cv; int main(int argc, char** argv) { // create variables to store the images Mat org, resized,saved; // open the image and store it in the 'org' variable // Replace the path with where you have downloaded the image org=imread("<path to image>/lena.png"); //Create a window to display the image namedWindow("Original Image",CV_WINDOW_AUTOSIZE); //display the image imshow("Original Image",org); //resize the image resize(org,resized,Size(),0.5,0.5,INTER_LINEAR); namedWindow("Resized Image",CV_WINDOW_AUTOSIZE); imshow("Resized Image",resized); //save the image //Replace <path> with your desired location imwrite("<path>/saved.png",resized; namedWindow("Image saved",CV_WINDOW_AUTOSIZE); saved=imread("<path to image>/saved.png"); imshow("Image saved",saved); //wait for a keystroke waitKey(0); return 0; }

Code explanation

Only the new functions/concepts will be explained in this case.

#include "opencv2/imgproc/imgproc.hpp"

Imgproc is another useful header that gives us access to the various transformations, color conversions, filters, histograms, and so on.

Mat org, resized;

We have now created two variables, org and resized, to store the original and resized images respectively.


We have used the preceding function to resize the image. The preceding function takes six parameters, out of which the first one is the variable containing the source image to be modified. The second one is the variable to store the resized image. The third parameter is the output image size. In this case we have not specified this, but we have instead used the Size() function, which will automatically calculate it based on the values of the fourth and fifth parameters. The fourth and fifth parameters are the scale factors along the horizontal and vertical axes respectively. The sixth parameter is for choosing the type of interpolation method. We have used the bilinear interpolation, which is the default method.


Finally, using the preceding function, you can save an image to a particular location on our PC. The function takes two parameters, out of which the first one is the location where you want to store the image and the second is the variable in which the image is stored. This function is very useful when you want to perform multiple operations on an image and save the image on your PC for future reference. Replace <path> in the preceding function with your desired location.


Resizing can be demonstrated through the following output:


This section showed you how to perform a few of the basic tasks in OpenCV as well as how to write your first OpenCV program.

Resources for Article :

Further resources on this subject:

You've been reading an excerpt of:

Instant OpenCV Starter

Explore Title