OpenCV: Lets discuss our first code

#include "stdafx.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/photo/photo.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/contrib/contrib.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/core/core.hpp"

using namespace cv;
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
Mat img = imread("Deepika_Padukone.jpg", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img'

if (img.empty()) //check whether the image is loaded or not
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;

namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window

waitKey(0); //wait infinite time for a keypress

destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"

return 0;

Above is the same code which we copied while setting up OpenCV and visual studio.

  • Mat img = imread(const string& filename, int flags=CV_LOAD_IMAGE_COLOR)
Mat is a data structure to store images in a matrix. It is declared in “opencv2/core/core.hpp” header file.


imread() is a function declared in “opencv2/highgui/highgui.hpp” header file. It loads an image from a file and stores it in Mat data structure.


Arguments of imread() function
  • filename – location of the file. If you just give the filename only, that image should be in the same folder as your C++ file. Otherwise you have to give the full path to your image.
  • flags – There are four possible inputs
    • CV_LOAD_IMAGE_UNCHANGED – image-depth=8 bits per pixel in each channel,  no. of channels=unchanged
    • CV_LOAD_IMAGE_GRAYSCALE – image depth=8 bits,  no. of channels=1
    • CV_LOAD_IMAGE_COLOR – image-depth=?,  no. of channels=3
    • CV_LOAD_IMAGE_ANYDEPTH – image-depth=unchanged ,  no. of channels=?
    • CV_LOAD_IMAGE_ANYCOLOR – image-depth=?,  no. of channels=unchanged
You can combine these above parameters to get desired image output.
e.g –
CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR – image-depth=unchanged,  no. of channels=unchanged
CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYDEPTH – image-depth=unchanged,  no. of channels=3
If you are not sure what to do, use CV_LOAD_IMAGE_COLOR as the 2nd parameter of imread() function.
  • if (img.empty())
If imread() function fails to load the image, ‘img’ will not be loaded any data. Therefore ‘img.empty()‘ should return true. It’s a good practice to check whether the image is loaded successfully and if not exit the program. Otherwise your program will crash when executing imshow()function.
  • bool Mat::empty()
This function returns true, if Mat::data==NULL or Mat::total() == 0
  •  //system(“pause”);
If you are using Visual Studio, it’s better to uncomment this line because it will pause the program until user press any key. If we don’t uncomment it, the program will exit immediately so that user will not see the error message.
  • void namedWindow(const string& winname, int flags = WINDOW_AUTOSIZE);
This function creates a window.
Parameters –
    • winname – Title of the window. That name will display in the title bar of the newly created window
    • flags – determine the size of the window. There are two options
      • WINDOW_AUTOSIZE – User cannot resize the image. Image will be displayed in its original size
      • CV_WINDOW_NORMAL – Image will resized if you resize the the window
  • void imshow(const string& winname, InputArray mat);
This function shows the image which is stored in the ‘mat’ in a window specified by winname. If the window is created withWINDOW_AUTOSIZE flag, image will be displayed in its original size. Otherwise image may be scaled to the size of the window.
Parameters –
    • winname – Title of the window. This name is used to identify the window created by namedWindow() function.
    • mat – hold the image data
  • int waitKey(int delay = 0)
waitKey() function wait for keypress for certain time, specified by delay (in milliseconds). If delay is zero or negative, it will wait for infinite time. If any key is pressed, this function returns the ASCII value of the key and your program will continue. If there is no key press for the specified time, it will return -1 and program will continue.
  • void destroyWindow(const string& winname)
This function closes the opened window, with the title of winname and deallocate any associated memory usage. This function is not essential for this application because when the program exits, operating system usually close all the opened windows and deallocate any associated memory usage.
When running this program, the image of ‘Deepika_Padukone.jpg‘ is loaded into the variable, ‘img’ of type Mat. Then a window named ‘MyWindow‘ is opened. After that  ‘img‘ is loaded to that window. The window with the image will be displayed until any key is pressed.

One thought on “OpenCV: Lets discuss our first code”

Leave a Reply

Your email address will not be published. Required fields are marked *