Load & Display Image


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.


Loading and Displaying an image with OpenCV


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.
  1. 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.
  2. 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.
  1. 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.
  2. 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.
  1. winname -  Name of the window which created by namedWindow() function.
  2. mat - Mat object which holds the image



waitKey(0); // Wait for any keystroke in the window
waitKey(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 window
This 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.


Creating a blank image and display



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.

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.
  1. rows - Number of rows in the 2D array ( i.e. - height of the image in pixels )
  2. cols - Number of columns in the 2D array ( i.e. - width of the image in pixels )
  3. 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)
  4. 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



14 comments:

  1. what does return -1 in if function mean?

    ReplyDelete
    Replies
    1. The program will terminate with the exit code of -1. Terminating with non-zero exit code indicates that program does not run successfully.

      Delete
  2. I have followed all the steps to setup the visual studio and opencv mine is visual studio 2017
    it tells me 'cv': a namespace with this name does not exist

    ReplyDelete
    Replies
    1. Did you include the following header file at the top of your code?
      #include

      Delete
  3. my console showed...but image doesn't show

    ReplyDelete
  4. hi, i have a question about this, i´m could track objets for his shape but in real time video? and i could detect if the object don´t have a correct position?. I´m very new in this so if could help me i would thank you

    ReplyDelete
    Replies
    1. Please see the following tutorials
      https://www.opencv-srf.com/2010/09/object-detection-using-color-seperation.html
      https://www.opencv-srf.com/2011/09/object-detection-tracking-using-contours.html

      Delete
  5. hi.. i have tried the above code..the code got executed but the image is not displaying.

    ReplyDelete
  6. dear sir,
    I very appreciate your work, I think it's a great.
    One question, what the arguments inside main functions using for?
    Best regards!

    ReplyDelete
    Replies
    1. Please clarify your question. Are you referring to the argv and argc arguments of the main function?

      Delete
  7. I have a webcam. I want to take a photo from my webcam and transmitted into a computer. Can you help me?

    ReplyDelete
    Replies
    1. Capture a frame from the output of the webcam on a key press or a mouse click. It is explained in the https://www.opencv-srf.com/2017/12/play-video-from-file-or-camera.html.

      Then save the captured image. It is explained in the https://www.opencv-srf.com/2018/01/save-images-and-videos-to-file.html.

      Delete
  8. Grate tutorials! Only one question: window name should be always unique?

    ReplyDelete