OpenCV | Displaying an Image

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:



filter_none

edit
close

play_arrow

link
brightness_4
code

if (!img_file->imageData)

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

cvNamedWindow( “Display”, CV_WINDOW_AUTOSIZE );

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

cvShowImage("Display", img_file);

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

cvWaitKey(0);

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

cvReleaseImage( &img_file );

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

cvDestroyWindow("Display");

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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");
}

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.