Opening and Displaying Images with OpenCV
Reading an Image
Use OpenCV’s imread function to read in an image from a file. You can visit this page to see all of the supported image types. Images are stored in NumPy arrays, so all NumPy array operations are available when using the OpenCV library. OpenCV supports several options for reading in images, though we will make use of two extensively in this class. All list of all options are shown here.
To begin, import the NumPy and OpenCV libraries
import numpy as np
Next, we will call the function cv2.imread. You can specify either a relative path or a complete file path. Here is an example:
src = cv2.imread("images/cat.png")
The image will be loaded as-is, and the color channels will be stored in Blue Green Red order.
Alternatively, the image can be read and converted to grayscale at the time of reading. Doing so would look something like this:
src = cv2.imread("sample_images/dog.jpg", cv2.IMREAD_GRAYSCALE)
cv2.IMREAD_GRAYSCALE is an enumerator, which evaluates to 0. It’s quite common for programmers to simply write 0, so be on the lookout for both.
Displaying an Image
Now that we can read in an image, let’s talk about displaying images in Python. First, we need to call the function cv2.imshow. This function requires that we first specify the window name.
cv2.imshow("Picture of Cat", src)
Notice that the string “Picture of Cat” is the title on the window displaying the image. If we were to display a second image and wanted to keep the first image open, we would need to make sure the title of the window is different. If the title is the same, the image of the cat will be replaced with the new image. Sometimes, though, this is a desired behavior – especially when you are displaying frames of a video. You won’t want every frame to open in a new window.
Gone too soon??
If you have been coding along, you may have encountered a situation where you ran cv2.imshow and nothing happened. This is because you computer needs just the slightest delay to handle this request. OpenCV provides a method for this, cv2.waitKey. The waitKey function takes one argument, which is the time to wait in milliseconds. Therefore, cv2.waitKey(1000) would wait one second before continuing. Calling cv2.waitKey(0) is the exception. This will force your program to wait indefinitely until a key is pressed before continuing.
Closing a window
You may want to close a window before your program finished (an aside, all windows when close when the program completes execution). Two methods will be useful for doing so, cv2.destroyWindow and cv2.destroyAllWindows. The former requires one input argument, the title of the window you want to close. For instance, if I had several windows open and I just wanted to close the window displaying the cat, I could write the following:
cv2.destroyWindow("Picture of Cat")
Alternatively, you can call cv2.destroyAllWindows() with no arguments to close all OpenCV windows that are open.
Note: In this course, we will exclusively use the OpenCV library for reading, manipulating, and images and videos. If you have previously used another library such as PIL to edit and display images, you may be wondering if you can use that in conjunction with OpenCV. While it may be possible, I would highly suggest against it. All assignments in this course should be completed using OpenCV, NumPy, and the standard Python library. If any additional modules are required, those will be specified to you.