Reading and Displaying an image in OpenCV using C++

13 Jan, 2021
In this article, we will discuss to open an image using OpenCV (Open Source Computer Vision) in C++. Unlike python, any additional libraries in C++ are not required. OpenCV C++ comes with this amazing image container Mat that handles everything for us. The only change seen from a standard C++ program is the inclusion of namespace cv which contains all the OpenCV functions, classes, and data structures. Following functions are required for reading and displaying an image in OPenCV:
imread(): This function is used to read images and takes the following 2 arguments:
filename: The complete address of the image to be loaded is of type string. For example: “C:\users\downloads\sample.jpg”
flag: It is an optional argument and determines the mode in which the image is read and can take several values likeIMREAD_COLOR: The default mode in which the image is loaded if no arguments are provided. It loads the image in BGR format.IMREAD_UNCHANGED: It loads the image in the original form. It also includes the alpha channel if present inside the image.IMREAD_GRAYSCALE: It loads the image as a grayscale image.
IMREAD_COLOR: The default mode in which the image is loaded if no arguments are provided. It loads the image in BGR format.
IMREAD_UNCHANGED: It loads the image in the original form. It also includes the alpha channel if present inside the image.
IMREAD_GRAYSCALE: It loads the image as a grayscale image.
Output: returns Image as a Mat Object
Usage:
// Reading the image file Mat image = imread(“C:/users/downloads/default.jpg”, IMREAD_grayscale); 
imshow(): This function is used to display images and takes the following two arguments:
winname or window name: This is the title of the window displaying the image and is of type string.
image: This is the image to be displayed. Its type is Mat, the C++ image container.
Output: Create a window displaying the image.
Usage:
// Show our image inside the created windowimshow(“Window Name”, image);
Mat::empty(): This helps us in error handling in case the imread() function fails to load the image or the image doesn’t exist at the specified path and tells us if the Mat container is empty or not.
WaitKey(): This function helps to display images for a longer duration by keeping the window open until the user presses a key.
Below is the program for the same:
C++
// C++ program for the above approach#include <iostream>#include <opencv2/opencv.hpp>using namespace cv;using namespace std;  // Driver codeint main(int argc, char** argv){    // Read the image file as    // imread("default.jpg");    Mat image = imread("Enter the Address"                       "of Input Image",                       IMREAD_GRAYSCALE);      // Error Handling    if (image.empty()) {        cout << "Image File "             << "Not Found" << endl;          // wait for any key press        cin.get();        return -1;    }      // Show Image inside a window with    // the name provided    imshow("Window Name", image);      // Wait for any keystroke    waitKey(0);    return 0;}
Input Image:
Output Image:
Image-Processing
OpenCV
C++
C++ Programs
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
std::string class in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
Header files in C/C++ and its uses
Sorting a Map by value in C++ STL
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
C++ program for hashing with chaining