A quick start – OpenCV fundamentals

Exclusive offer: get 50% off this eBook here
Instant OpenCV Starter [Instant]

Instant OpenCV Starter [Instant] — Save 50%

Get started with OpenCV using practical, hands-on projects with this book and ebook

A$14.99    A$7.50
by Jayneil Dalal Sohil Patel | June 2013 | Open Source

In this article by Jayneil Dalal and Sohil Patel authors of Instant OpenCV Starter [Instant], we will be covering the fundamentals of image processing and help you write your first program in OpenCV by performing a few trivial tasks. All the examples throughout the book have been written in the C++ programming language.

(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:

Module

Feature

Core

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

Imgproc

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.

Video

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

Calib3d

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

Features2d

Salient feature detectors, descriptors, and descriptor matchers.

Objdetect

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

Highgui

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

Gpu

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

Greyscale

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:

Color/RGB

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.

Code

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.

waitKey(0);

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.

Output

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.

Code

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.

resize(org,resized,Size(),0.5,0.5,INTER_LINEAR);

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.

imwrite("<path>/saved.png",final);

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.

Output

Resizing can be demonstrated through the following output:

Summary

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:


Instant OpenCV Starter [Instant] Get started with OpenCV using practical, hands-on projects with this book and ebook
Published: May 2013
eBook Price: A$14.99
See more
Select your format and quantity:

About the Author :


Jayneil Dalal

Jayneil Dalal is an Embedded Linux Engineer/Technical Writer who loves to explore different open source technologies, and he has been a key member of the PandaBoard.org and Beagleboard.org projects at Texas Instruments. He has been selected to give a talk at Linuxfest Northwest 2013 and OSCON 2013. He has previously presented at Linuxcon North America 2012, Drodicon 2012 in Berlin, Southeast Linuxfest 2012, Indiana Linuxfest 2012, Northwest Linuxfest 2012, Scipy 2011, and Opensource bridge 2012. Jayneil can be reached at jayneil.dalal@gmail.com.

Sohil Patel

Sohil Patel is a Computer Vision Engineer and FOSS advocate who loves to explore different open source technologies. His areas of interest include Python, Computer Vision, Augmented Reality, Linux, HCI, Pandaboard, and other open source hardware platforms. He is currently working at Azoi, which is a startup based in India.

Books From Packt


OpenVPN: Building and Integrating Virtual Private Networks
OpenVPN: Building and Integrating Virtual Private Networks

Beginning OpenVPN 2.0.9
Beginning OpenVPN 2.0.9

OpenCV 2 Computer Vision Application Programming Cookbook
OpenCV 2 Computer Vision Application Programming Cookbook

Mastering openFrameworks: Creative Coding Demystified
Mastering openFrameworks: Creative Coding Demystified

Mastering OpenCV with Practical Computer Vision Projects
Mastering OpenCV with Practical Computer Vision Projects

OpenCV Computer Vision with Python
OpenCV Computer Vision with Python

Learning SciPy for Numerical and Scientific Computing
Learning SciPy for Numerical and Scientific Computing

Processing 2: Creative Programming Cookbook
Processing 2: Creative Programming Cookbook


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software