Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
OpenCV with Python By Example

You're reading from  OpenCV with Python By Example

Product type Book
Published in Sep 2015
Publisher Packt
ISBN-13 9781785283932
Pages 296 pages
Edition 1st Edition
Languages
Author (1):
Prateek Joshi Prateek Joshi
Profile icon Prateek Joshi

Table of Contents (19) Chapters

OpenCV with Python By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Applying Geometric Transformations to Images Detecting Edges and Applying Image Filters Cartoonizing an Image Detecting and Tracking Different Body Parts Extracting Features from an Image Creating a Panoramic Image Seam Carving Detecting Shapes and Segmenting an Image Object Tracking Object Recognition Stereo Vision and 3D Reconstruction Augmented Reality Index

Reading, displaying, and saving images


Let's see how we can load an image in OpenCV-Python. Create a file named first_program.py and open it in your favorite code editor. Create a folder named images in the current folder and make sure that you have an image named input.jpg in that folder.

Once you do that, add the following lines to that Python file:

import cv2
img = cv2.imread('./images/input.jpg')
cv2.imshow('Input image', img)
cv2.waitKey()

If you run the preceding program, you will see an image being displayed in a new window.

What just happened?

Let's understand the previous piece of code, line by line. In the first line, we are importing the OpenCV library. We need this for all the functions we will be using in the code. In the second line, we are reading the image and storing it in a variable. OpenCV uses NumPy data structures to store the images. You can learn more about NumPy at http://www.numpy.org

So if you open up the Python shell and type the following, you will see the datatype printed on the terminal:

>>> import cv2
>>> img = cv2.imread('./images/input.jpg')
>>> type(img)
<type 'numpy.ndarray'>

In the next line, we display the image in a new window. The first argument in cv2.imshow is the name of the window. The second argument is the image you want to display.

You must be wondering why we have the last line here. The function, cv2.waitKey(), is used in OpenCV for keyboard binding. It takes a number as an argument, and that number indicates the time in milliseconds. Basically, we use this function to wait for a specified duration, until we encounter a keyboard event. The program stops at this point, and waits for you to press any key to continue. If we don't pass any argument or if we pass 0 as the argument, this function will wait for a keyboard event indefinitely.

Loading and saving an image

OpenCV provides multiple ways of loading an image. Let's say we want to load a color image in grayscale mode. We can do that using the following piece of code:

import cv2
gray_img = cv2.imread('images/input.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('Grayscale', gray_img)
cv2.waitKey()

Here, we are using the flag cv2.IMREAD_GRAYSCALE to load the image in grayscale mode. You can see that from the image being displayed in the new window. Next, is the input image:

Following is the corresponding grayscale image:

We can save this image into a file as well:

cv2.imwrite('images/output.jpg', gray_img)

This will save the grayscale image into an output file named output.jpg. Make sure you get comfortable with reading, displaying, and saving images in OpenCV, because we will be doing this quite a bit during the course of this book.

You have been reading a chapter from
OpenCV with Python By Example
Published in: Sep 2015 Publisher: Packt ISBN-13: 9781785283932
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}