Reading and displaying an image

In this section, we will try to develop the code for reading and displaying an image using C++ and OpenCV. The entire code for this is as follows it is then explained line by line:

#include <opencv2/opencv.hpp>#include <iostream>using namespace cv;using namespace std;int main(int argc, char** argv){  // Read the image   Mat img = imread("images/cameraman.tif",0);  // Check for failure in reading an Image  if (img.empty())   {    cout << "Could not open an image" << endl;    return -1;  }  //Name of the window  String win_name = "My First Opencv Program";   // Create a window  namedWindow(win_name);   // Show our image inside the created window.  imshow(win_name, img);   // Wait for any keystroke in the window   waitKey(0); 

Get Hands-On GPU-Accelerated Computer Vision with OpenCV and CUDA now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.