Wednesday, 20 May 2015

Read an image and display it using OpenCV

The following code shows how to read an image and display it using OpenCV.


#include <iostream> //preprocessor directive

#include "opencv2/opencv.hpp" //include OpenCV libraries


//Main function (starting point of execution)
 
 
int main(int argc, char argv[])


{
// Read the image from file (replace the image file name in your code)

cv::Mat imgIn = cv::imread("your_image_filename.jpg");

//Check if the image is empty

if ( imgIn.empty() )


{
 
//Print error message

std::cout << "Error occured when loading the image" << std::endl;

//Return negative 1 to indicate an error has occured in the program

return -1;


}
 
//Create a window

cv::namedWindow("My Window", 1);

//Display the image

imshow("My Window", imgIn);

// Wait until a key is pressed



cv::waitKey(0);
 
//Return 0 to indicate normal run of the program

return 0;


}

No comments:

Post a Comment