Open In App

OpenCV | Displaying an Image

Improve
Improve
Like Article
Like
Save
Share
Report

To read an image file from videos or cameras having a wide range of types, OpenCV provides a good amount of utilities. OpenCV has such a toolkit known as HighGUI, which is a part of one of its utilities. Some of these utilities are used in this article to display and open an image on our system.
Let’s understand line by line execution of the program

Code:




IplImage* img_file = cvLoadImage("..input\\abcd.PNG");


This particular line will load the image with a high-level routine – cvLoadImage(). Based on the file name, it determines the file format to be loaded and then allocates the required memory for the image data structure automatically. One can read a huge range of different image formats from cvLoadImage(). These image formats can be – JPEG, BMP, PNG, JPE, DIB, PBM, PPM, RAS, SR and TIFF. Then, a pointer to the assigned image data structure is returned. The returned pointer is used to manipulate the image and its data. This structure is IplImage. IplImage is the OpenCV construct which is used by OpenCV for handling all different kind of images. These images can be a single-channel image, multi-channel images, floating-point valued images or integer values images.
Here in the code above, the path to the image is given and it is different for each user. So, it can be set according to the location of the image on the user system.

Code:




if (!img_file->imageData)


Using this line of code, it can be checked whether the image actually exists or not. If there is no imageData, then, in that case, we can detect it easily using this.

Code:




cvNamedWindow( “Display”, CV_WINDOW_AUTOSIZE );


Now, cvNamedWindow() is another high-level function provided by the HighGUI library, It is responsible for opening a window on the screen. It is this window which has the image display. Using this, a name can also be assigned to the image window (“Display” in the code above). This name is further used in the code to make any HighGUI call.
The second parameter to cvNamedWindow() defines the window properties and it can be either set to 0 (the default value) or to the CV_WINDOW_AUTOSIZE(as in the above code). In the case of ‘0’ value, the size of the window will be the same irrespective of the image size and the image will be scaled according to the default window size. In the case of ‘CV_WINDOW_AUTOSIZE’, the size of the window may vary as per the image size. The window will be scaled according to the default image size and the image will have its true size.

Code:




cvShowImage("Display", img_file);


cvShowImage() is used to display an image in the form as an IplImage* pointer, in an existing window. That means it needs an already existing window, which is created using cvNamedWindow(). The image is redrawn with the image present in it and window resize accordingly (if created with CV_WINDOW_AUTOSIZE), when we call cvShowImage().

Code:




cvWaitKey(0);


cvWaitKey() is defined to ask the program to wait or stop for a keypress. If it is given a positive argument, then the program will wait for that number of milliseconds. Then, even if no key is pressed, it will continue automatically. Otherwise as in the code above, using a negative or ‘0’ number means that the program will wait for keypress indefinitely.

Code:




cvReleaseImage( &img_file );


Now, we can free the allocated memory to the image, once we are done. A pointer to the IplImage* pointer is expected for this operation. The pointer ‘img_file’ will be set to NULL.

Code:




cvDestroyWindow("Display");


Finally, the window is also destroyed using cvDestroyWindow(). It will close and de-allocate the window memory or any associated data usage which can be (image buffer, copy of pixel info from *img_file). For a simple program, cvDestroyWindow() or cvReleaseImage() function need not be used as all resources close automatically by the OS but it is good to do it on your own.

Code : Display an Image using OpenCV.




#include <opencv2\opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <highlevelmonitorconfigurationapi.h>
  
using namespace cv;
using namespace std; 
  
int main(int argc, char** argv) {
    IplImage* img_file = cvLoadImage("..input\\abcd.jpg");
  
    if (!img_file->imageData) {
        cout << "Sorry";
        return -1;
    }
          
    cvNamedWindow("Display", CV_WINDOW_AUTOSIZE);
    cvShowImage("Display", img_file);
    cvWaitKey(0);
    cvReleaseImage(&img_file);
    cvDestroyWindow("Display");
}


Output :

This program compiles, run and then loads an image into the window using memory and displays it in the window on the screen. It will then form the user keypress and then it closes and exits.



Last Updated : 09 Sep, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads