Load Image from File and Display
In this section, I will show you how to load an image from a file and display the image in a window using OpenCV library functions.
First of all, open your C++ IDE and create a new project. Then you have to configure the new project for OpenCV. If you have not installed OpenCV or configured the visual studio projects for OpenCV yet, please refer to Install OpenCV with Visual Studio.
//Uncomment the following line if you are compiling this code in Visual Studio //#include "stdafx.h" #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main(int argc, char** argv) { // Read the image file Mat image = imread("D:/My OpenCV Website/Lady with a Guitar.jpg"); // Check for failure if (image.empty()) { cout << "Could not open or find the image" << endl; cin.get(); //wait for any key press return -1; } String windowName = "The Guitar"; //Name of the window namedWindow(windowName); // Create a window imshow(windowName, image); // Show our image inside the created window. waitKey(0); // Wait for any keystroke in the window destroyWindow(windowName); //destroy the created window return 0; }
Copy and paste above code snippet into your IDE and run it. Please note that you have to replace "D:/My OpenCV Website/Lady with a Guitar.jpg" in the code with a valid location to an image in your computer. Then you should see a output like the below image.
Explanation
Let's go through the above OpenCV program line by line.
//#include "stdafx.h"This is the pre-compiled header file generated by Visual Studio IDE. If you are using Visual Studio, please uncomment this line to avoid a compile error.
#include <opencv2/opencv.hpp> #include <iostream>opencv2/opencv.hpp header file itself will include all other neccessary header files in OpenCV library. You have to include only this header file in order to compile your OpenCV codes. For more information, please refer OpenCV C++ API.
iostream header file should also be included because we use cout and cin in our program to print an error message to the console.
using namespace cv; using namespace std;All OpenCV functions, classes and data structures are declared inside cv namespace. So, we have to add the 'using namespace cv' line in the top of our program. Otherwise we have to append 'cv::' specifier before each OpenCV functions, classes and data structures. (e.g - cv::Mat, cv::imread() , etc). For more information, please refer OpenCV C++ API.
We have to use the std namespace also because cout, endl and cin functions are inside the std namespace.
Mat image = imread("D:/My OpenCV Website/Lady with a Guitar.jpg");The function loads an image from the file "D:/My OpenCV Website/Lady with a Guitar.jpg" and returns it as a Mat object.
In your code, you have to replace the "D:/My OpenCV Website/Lady with a Guitar.jpg" with a valid location to an image file in your computer.
Mat imread(const String& filename, int flags = IMREAD_COLOR)
This function loads an image from the specified file and return is as a Mat object. If the function cannot read the file, it will return an empty Mat object.
- filename - You have to give the relative or absolute path of an image file. If you are giving the relative path, it should be relative to your cpp file. jpeg, jpg, bmp, png, tiff and tif image file types are always supported. Other image file types are supported depending on your platform and installed codecs.
- flags - There are several possible values for the flag argument. In the above program, I did not pass any value to this argument such that default IMREAD_COLOR argument will be used.
- IMREAD_UNCHANGED - The image will be loaded as it is. If you want to get the alpha channel in your input image (if it is available), you have to use this flag.
- IMREAD_GRAYSCALE - The image will be load as a gray-scale image (i.e. - Single channel image, Black and white image)
- IMREAD_COLOR - The image will be loaded as a BGR image (i.e. - 3 channel image, color image)
// Check for failure if (image.empty()) { cout << "Could not open or find the image" << endl; cin.get(); //wait for any key press return -1; }If imread() function fails to load the image, the returned Mat object will be empty. If the Mat object is empty, image.empty() function will return true. In such scenarios, our program will print an error message to the console and wait for any key press. When the user press any key in the keyboard, the program will exit returning -1. It's a good practice to check whether the image is empty and exit the program. Otherwise your program will crash while trying to execute the imshow() function.
namedWindow(windowName); // Create a window
This function creates a window with the name of "The Guitar". The name of the window is used later in this code to identify the window.
void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE)
This function creates a window which can be used to place images and track bars. If a window already exists with the given name, this function does nothing.
This function creates a window which can be used to place images and track bars. If a window already exists with the given name, this function does nothing.
- winname - Name of the window. That name will display in the title bar of the newly created window. This name is also the identifier for this window and it will be used in the later OpenCV function calls to identify the window.
- flags - Determine the size of the window. In the above program, I did not pass any value to this argument such that default WINDOW_AUTOSIZE argument will be used.
- WINDOW_AUTOSIZE - User cannot resize the window. Image will be displayed in its original size.
- WINDOW_NORMAL- User can resize the window.
imshow(windowName, image); // Show our image inside the created window.This function display the image in the window created in a previous step.
void imshow(const String& winname, InputArray mat)
This function shows the image in a window specified by winname. If the window is created with WINDOW_AUTOSIZE flag, image will be displayed in its original size. Otherwise image may be scaled to the size of the window. If the window has not been created by calling to namedWindow() function, this function will create a window with the WINDOW_AUTOSIZE flag.
This function call should be followed by waitKey(int) function call in order to provide sufficient time to paint and display the image in the window for the specified time duration in milliseconds. If you do not call waitKey(int) function, the image will not be displayed in the window.
This function call should be followed by waitKey(int) function call in order to provide sufficient time to paint and display the image in the window for the specified time duration in milliseconds. If you do not call waitKey(int) function, the image will not be displayed in the window.
- winname - Name of the window which created by namedWindow() function.
- mat - Mat object which holds the image
waitKey(0); // Wait for any keystroke in the windowwaitKey(0) function waits for a key press forever. When any key is pressed, this function returns the ASCII value of the key and your program will continue.
If positive value is passed to the waitKey() function, it waits for a key press for only the time duration, specified by passed value in milliseconds. If any key is pressed during this period, this function returns the ASCII value of the key and your program will continue. If no key is pressed during this period, it will return -1 and program will continue.
This function only works if there is at least one active HIGHGUI window opened by the program.
destroyWindow(windowName); //destroy the created windowThis function closes the opened window identified with the name, windowName and de-allocate any associated memory usage. This function is not essential for the above program because when the program exits, operating system usually close all the opened windows and de-allocate any associated memory usage.
Summary
In the above section, you have learned,
- How to load an image from a file
- How to handle the error scenario when the loading of the image is failed
- How to create a window and display the image
- How to wait without exiting the program until the user press a key
- How to destroy created windows
Create a Blank Image & Display
The following program is also very much similar to the previous program. The only difference is that this program creates a blank image instead of loading an existing image from a file.
//Uncomment the following line if you are compiling this code in Visual Studio //#include "stdafx.h" #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main(int argc, char** argv) { //create a new image which consists of //3 channels //image depth of 8 bits //800 x 600 of resolution (800 wide and 600 high) //each pixels initialized to the value of (100, 250, 30) for Blue, Green and Red planes respectively. Mat image(600, 800, CV_8UC3, Scalar(100, 250, 30)); String windowName = "Window with Blank Image"; //Name of the window namedWindow(windowName); // Create a window imshow(windowName, image); // Show our image inside the created window. waitKey(0); // Wait for any keystroke in the window destroyWindow(windowName); //destroy the created window return 0; }
Copy and paste above simple code snippet into your IDE and run it. Then you should see a output like the below image.
Explanation for New OpenCV Functions Found in this Section
Mat image(600, 800, CV_8UC3, Scalar(100, 250, 30));
This is one of many constructors available in Mat class. It creates an image which is 600 pixels high and 800 pixels wide. 24 bits are allocated for each pixel in the image. 24 bits will consist of three unsigned 8 bit integers representing blue, green and red planes respectively. The value of the three integers should be from 0 to 255.
Then this constructor initializes each pixels of the created image with the 3 unsigned integers (100, 250, 30). So, it initializes the blue channel with 100, green channel with 250 and red channel with 30. Because the value of the green channel is significantly larger than that of other channels, the output image is greenish. You can try different combinations of these three values and see the output image.
Then this constructor initializes each pixels of the created image with the 3 unsigned integers (100, 250, 30). So, it initializes the blue channel with 100, green channel with 250 and red channel with 30. Because the value of the green channel is significantly larger than that of other channels, the output image is greenish. You can try different combinations of these three values and see the output image.
Mat::Mat(int rows, int cols, int type, const Scalar& s)
This constructor will create a Mat object with specified number of rows and number of cols and initialize each element with the value given in s.
This constructor will create a Mat object with specified number of rows and number of cols and initialize each element with the value given in s.
- rows - Number of rows in the 2D array ( i.e. - height of the image in pixels )
- cols - Number of columns in the 2D array ( i.e. - width of the image in pixels )
- type - Data type of the 2D array which specify the depth and the data type of each element in each channel and number of channels. (Of course, if number of channels is more than 1, 2D array becomes 3D array). More details of data types can be found in the OpenCV C++ API. Possible values for this argument are,
- CV_8UC1
- CV_8UC2
- CV_8UC3
- CV_8UC4
- CV_8UC(n)
- CV_8SC1
- CV_8SC2
- CV_8SC3
- CV_8SC4
- CV_8SC(n)
- CV_16UC1
- CV_16UC2
- CV_16UC3
- CV_16UC4
- CV_16UC(n)
- CV_16SC1
- CV_16SC2
- CV_16SC3
- CV_16SC4
- CV_16SC(n)
- CV_32SC1
- CV_32SC2
- CV_32SC3
- CV_32SC4
- CV_32SC(n)
- CV_32FC1
- CV_32FC2
- CV_32FC3
- CV_32FC4
- CV_32FC(n)
- CV_64FC1
- CV_64FC2
- CV_64FC3
- CV_64FC4
- CV_64FC(n)
- s - Initialize each array element (pixels) with the value given by s for each channel.
Summary
In the above section, you have learned how to create an image of desired size and type and initialize all pixels to a specified value.Next Tutorial : Play Video from File or Camera