Python OpenCV: Reading and displaying an image

The objective of this post is to explain how to read and display an image with Python and OpenCV.


Introduction

The objective of this post is to explain how to read and display an image with Python and OpenCV. This will be a very simple introductory code.

You can check here how to install OpenCV on Windows. The easiest way is installing it from the pre-built binaries, as indicated here. Don’t forget to install Numpy, which is also needed to support the OpenCV functionality. The easiest way is doing it via pip, with the following command:

pip install numpy


The code

First of all, we need to import the cv2 module, which we will use to access the image processing functionality.

Then, to read an image, we simply call the imread function of the cv2 module. This will return an image as a numpy ndarray. We can confirm this by calling the type function and passing as input the object returned by the imread function.

Note that if the file is not in Python’s working directory, we need to specify the full path, as indicated bellow. In my case, I was reading an image from my desktop.

image = cv2.imread('C:/Users/N/Desktop/Test.jpg')
print type(image)

Then, to display the image we read with the previous function, we call the imshow function of the cv2 module. This function will display the image in a window and it receives as input the name of the window and the image we previously got with the imread function [1].

cv2.imshow('Test image',image)

Then, we will call the waitKey function, which will wait for a keyboard event and receives as input a delay in milliseconds [2]. If we pass a value lesser or equal than 0, it will wait indefinitely for a key event [2].  So, the execution of our program will block here until we press a key.

After the user presses a key, we will assume the window with the image should be destroyed. To to so, we can call the destroyAllWindows function, which will destroy all the windows previously created [2]. Note that if we want to destroy a specific window, we can call the destroyWindow function, which receives as input the name of the window we want to destroy [2]. This second function is useful if we create multiple windows.

cv2.waitKey(0)
cv2.destroyAllWindows()

Check the full code bellow.

import cv2

image = cv2.imread('C:/Users/N/Desktop/Test.jpg')
print type(image)

cv2.imshow('Test image',image)
cv2.waitKey(0)
cv2.destroyAllWindows()


Running the code

After finishing the code, just run it on IDLE. A window with the image should pop, as indicated in figure 1. Also, in IDLE’s console, the type printed for our image should be of type ndarray, as expected.

To finish the program, just press any key on your keyboard and the window should be destroyed.

Python OpenCV reading image.png

Figure 1 – Output of the image reading program.


Related content

 

References

[1] https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html#display-an-image

[2] http://docs.opencv.org/3.0-beta/modules/highgui/doc/user_interface.html#cv2.waitKey

 

Technical details

  • Python version: 2.7.8
  • OpenCV version: 3.2.0
Advertisements
This entry was posted in Python and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s