Reader small image

You're reading from  Learning OpenCV 3 Application Development

Product typeBook
Published inDec 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781784391454
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Samyak Datta
Samyak Datta
author image
Samyak Datta

Samyak Datta has a bachelor's and a master's degree in Computer Science from the Indian Institute of Technology, Roorkee. He is a computer vision and machine learning enthusiast. His first contact with OpenCV was in 2013 when he was working on his master's thesis, and since then, there has been no looking back. He has contributed to OpenCV's GitHub repository. Over the course of his undergraduate and master's degrees, Samyak has had the opportunity to engage with both the industry and research. He worked with Google India and Media.net (Directi) as a software engineering intern, where he was involved with projects ranging from machine learning and natural language processing to computer vision. As of 2016, he is working at the Center for Visual Information Technology (CVIT) at the Indian Institute of Information Technology, Hyderabad.
Read more about Samyak Datta

Right arrow

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.

Previous PageNext Page
You have been reading a chapter from
Learning OpenCV 3 Application Development
Published in: Dec 2016Publisher: PacktISBN-13: 9781784391454
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Samyak Datta

Samyak Datta has a bachelor's and a master's degree in Computer Science from the Indian Institute of Technology, Roorkee. He is a computer vision and machine learning enthusiast. His first contact with OpenCV was in 2013 when he was working on his master's thesis, and since then, there has been no looking back. He has contributed to OpenCV's GitHub repository. Over the course of his undergraduate and master's degrees, Samyak has had the opportunity to engage with both the industry and research. He worked with Google India and Media.net (Directi) as a software engineering intern, where he was involved with projects ranging from machine learning and natural language processing to computer vision. As of 2016, he is working at the Center for Visual Information Technology (CVIT) at the Indian Institute of Information Technology, Hyderabad.
Read more about Samyak Datta