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:
Read an image from disk and store it in a Mat object :
imread()
.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 |
|
If set, return the loaded image as is |
|
If set, always convert the image to the single channel grayscale image |
|
If set, always convert the image to the three channel BGR color image |
|
If set, return the 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit |
|
If set, the image is read in any possible color format |
|
If set, use the |
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:
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
.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.