Book Image

Learning OpenCV 3 Application Development

By : Samyak Datta
Book Image

Learning OpenCV 3 Application Development

By: Samyak Datta

Overview of this book

Computer vision and machine learning concepts are frequently used in practical computer vision based projects. If you’re a novice, this book provides the steps to build and deploy an end-to-end application in the domain of computer vision using OpenCV/C++. At the outset, we explain how to install OpenCV and demonstrate how to run some simple programs. You will start with images (the building blocks of image processing applications), and see how they are stored and processed by OpenCV. You’ll get comfortable with OpenCV-specific jargon (Mat Point, Scalar, and more), and get to know how to traverse images and perform basic pixel-wise operations. Building upon this, we introduce slightly more advanced image processing concepts such as filtering, thresholding, and edge detection. In the latter parts, the book touches upon more complex and ubiquitous concepts such as face detection (using Haar cascade classifiers), interest point detection algorithms, and feature descriptors. You will now begin to appreciate the true power of the library in how it reduces mathematically non-trivial algorithms to a single line of code! The concluding sections touch upon OpenCV’s Machine Learning module. You will witness not only how OpenCV helps you pre-process and extract features from images that are relevant to the problems you are trying to solve, but also how to use Machine Learning algorithms that work on these features to make intelligent predictions from visual data!
Table of Contents (16 chapters)
Learning OpenCV 3 Application Development
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Exploring the Mat class: loading images


We have covered enough theory without writing any actual code. And this is precisely what we are going to do now--explore OpenCV and try to learn more about the Mat class! We just read about the utility of Mat object both as a structure for storing images and as a multidimensional array. We'll start by witnessing the former. To that end, we will be writing our first Hello World OpenCV program that will read an image from the disk, load the image data onto a Mat object, and then display the image. All of this will be done using OpenCV and C++. So, let's begin!

At the very outset, we include the relevant header files and namespace declarations:

#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/core/core.hpp> 
 
using namespace std; 
using namespace cv; 

The highgui header contains declarations for the functions that do the following:

  1. Read an image from disk and store it in a Mat object : imread().

  2. Display the contents of a Mat object (the image) onto a window : imshow().

The core.hpp header file contains declarations for the Mat class. Now we come to the actual piece of code that performs the intended operations:

int main() { 
    Mat image = imread("image.png", IMREAD_COLOR); 
    imshow("Output", image); 
    waitKey(0);  
 
    return 0; 
} 

The first thing we encounter in the code snippet is the imread() function. It basically allows you to read an image from the disk and load its contents on to a Mat object. It accepts a couple of arguments.

The first argument is the full path name of the image file on disk. Here, we pass image.png as our path (make sure to give the complete path here; if you just pass the name of the image as we have done, ensure that the file lies in the same directory as your code).

The second argument is an OpenCV flag that tells us the format in which to load the image onto the Mat object. The different possible flags along with their descriptions are given in the following table. Out of all these flags, you will be using a couple of them quite frequently: IMREAD_UNCHANGED and IMREAD_GRAYSCALE. The former loads the image as is, whereas the latter always converts the image into a single channel grayscale image.

Flag

Description

IMREAD_UNCHANGED

If set, return the loaded image as is

IMREAD_GRAYSCALE

If set, always convert the image to the single channel grayscale image

IMREAD_COLOR

If set, always convert the image to the three channel BGR color image

IMREAD_ANYDEPTH

If set, return the 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit

IMREAD_ANYCOLOR

If set, the image is read in any possible color format

IMREAD_LOAD_GDAL

If set, use the gdal driver for loading the image

Then comes imshow(). It does the opposite of what imread() accomplishes. It takes the contents of a Mat object and displays it on the screen inside a window. This function also accepts two arguments:

  1. The first argument is the name that appears in the title of the window that displays the image. Here, we have named the window Output.

  2. The second argument is, of course, the Mat object which stores the image.

The waitkey() method pauses the program for a specified amount of time, waiting for a keystroke. If, however, we pass 0 as an argument, it would wait indefinitely for us to press the key. Had we not included the waitKey(0) statement in our code, the OpenCV window with the image would have flashed on our screens and disappeared, following which our program would have terminated return 0. Having the waitKey(0) after imshow() displays the image and then waits for the user to press a key, and only then does the program terminate.