A few months ago, I was teaching an online seminar on the basics of computer vision.
And do you know what the most common question I got asked was?
How do I use OpenCV to load an image and display it on my screen?
It’s a pretty basic concept, but I think many instructors (myself included) quickly jump over this question and immediately dive into more advanced techniques such as blurring, edge detection, and thresholding.
Displaying an image to your screen is a simple way to debug a computer vision program, so let’s take a couple minutes and answer the this question.
How-To: OpenCV Load an Image
The objective of this post is to show you how to read an image off of disk using OpenCV, display it on your screen, and then wait for a key press to close the window and terminate the script.
While simply displaying an image on your screen isn’t practical by itself, it’s an important technique that you will use a lot when you’re developing (and more importantly, debugging) your own computer vision applications.
You see, displaying an image on your screen is much like a
When it comes to debugging, nothing beats a few well placed
The same is true in computer vision.
A few well placed calls to
cv2.imshow will quickly help you resolve the problem.
So let’s go ahead and jump into some code:
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
Lines 1-2 handle importing the packages that we’ll need —
argparse to parse command line arguments and
cv2 for our OpenCV bindings.
Then, on Lines 4-6 we parse our command line arguments. We only need a single switch,
--image, which is the path to where our image resides on disk.
Loading the image using OpenCV is taken care on Line 8 by making a call to the
cv2.imread function. This function takes a single parameter — the path to where the image resides on disk, which is supplied as a command line argument.
Finally, we can display our image to our screen on Lines 10-11.
Displaying the image to our screen is handled by the
cv2.imshow function. The first argument to
cv2.imshow is a string containing the name of our window. This text will appear in the titlebar of the window. The second argument is the image that we loaded off of disk on Line 8.
After we have made a call to the
cv2.imshow function, we then need to wait for a key press using the
cv2.waitKey function on Line 11.
It’s very important that we make a call to this function, otherwise our window will close automatically!
cv2.waitKey function pauses execution of our Python script and waits for a key press. If we removed Line 11, then the window containing our image would close automatically. By making a call to
cv2.waitKey, we are able to pause the execution of our script, thus displaying our image on our screen, until we press any key on our keyboard.
The only argument
cv2.waitKey takes is an integer, which is a delay in milliseconds. If this value is positive, then after the specified number of milliseconds elapses the window will close automatically. If the number of milliseconds is zero, then the function will wait infinitely until a key is pressed.
The return value of
cv2.waitKey is either the code of the pressed key, or
-1, indicating that no key was pressed prior to the supplied amount of milliseconds elapsing.
We can execute our script by issuing the following command:
$ python load_image.py --image doge.jpg
You should then see an image on your screen:
Clearly a wild Doge has appeared! And I’m all out of Pokeballs…
Pressing any key on your keyboard will un-pause the script and close the window.
In this blog post I answered one of the most common questions I get asked: “How do I use OpenCV to load an image and display it on my screen?”
In order to load an image off of disk and display it using OpenCV, you first need to call the
cv2.imread function, passing in the path to your image as the sole argument.
Then, a call to
cv2.imshow will display your image on your screen.
But be sure to then use
cv2.waitKey to wait for a key press, otherwise the window created by
cv2.imshow will close automatically.
Learn the Basics of Computer Vision in a Single Weekend
If you’re interested in learning the basics of computer vision, but don’t know where to start, you should definitely check out my new eBook, Practical Python and OpenCV.
In this book I cover the basics of computer vision and image processing…and I can teach you in a single weekend!
I know, it sounds too good to be true.
But I promise you, this book is your guaranteed quick-start guide to learning the fundamentals of computer vision. After reading this book you will be well on your way to becoming an OpenCV guru!
So if you’re looking to learn the basics of OpenCV, definitely check out my book. You won’t be disappointed.