Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
OpenCV 3.x with Python By Example - Second Edition

You're reading from  OpenCV 3.x with Python By Example - Second Edition

Product type Book
Published in Jan 2018
Publisher Packt
ISBN-13 9781788396905
Pages 268 pages
Edition 2nd Edition
Languages
Authors (2):
Gabriel Garrido Calvo Gabriel Garrido Calvo
Profile icon Gabriel Garrido Calvo
Prateek Joshi Prateek Joshi
Profile icon Prateek Joshi
View More author details
Toc

Table of Contents (17) Chapters close

Title Page
Copyright and Credits
Contributors
Packt Upsell
Preface
1. Applying Geometric Transformations to Images 2. Detecting Edges and Applying Image Filters 3. Cartoonizing an Image 4. Detecting and Tracking Different Body Parts 5. Extracting Features from an Image 6. Seam Carving 7. Detecting Shapes and Segmenting an Image 8. Object Tracking 9. Object Recognition 10. Augmented Reality 11. Machine Learning by an Artificial Neural Network 1. Other Books You May Enjoy

Image scaling


In this section, we will discuss resizing an image. This is one of the most common operations in computer vision. We can resize an image using a scaling factor, or we can resize it to a particular size. Let's see how to do that:

import cv2
img = cv2.imread('images/input.jpg')
img_scaled = cv2.resize(img,None,fx=1.2, fy=1.2, interpolation = cv2.INTER_LINEAR)
cv2.imshow('Scaling - Linear Interpolation', img_scaled)
img_scaled = cv2.resize(img,None,fx=1.2, fy=1.2, interpolation = cv2.INTER_CUBIC)
cv2.imshow('Scaling - Cubic Interpolation', img_scaled)
img_scaled = cv2.resize(img,(450, 400), interpolation = cv2.INTER_AREA)
cv2.imshow('Scaling - Skewed Size', img_scaled)
cv2.waitKey()

What just happened?

Whenever we resize an image, there are multiple ways to fill in the pixel values. When we are enlarging an image, we need to fill up the pixel values in between pixel locations. When we are shrinking an image, we need to take the best representative value. When we are scaling by a non-integer value, we need to interpolate values appropriately, so that the quality of the image is maintained. There are multiple ways to do interpolation. If we are enlarging an image, it's preferable to use linear or cubic interpolation. If we are shrinking an image, it's preferable to use area-based interpolation. Cubic interpolation is computationally more complex, and hence slower than linear interpolation. However, the quality of the resulting image will be higher.

OpenCV provides a function called resize to achieve image scaling. If you don't specify a size (by using None), then it expects the x and y scaling factors. In our example, the image will be enlarged by a factor of 1.2. If we do the same enlargement using cubic interpolation, we can see that the quality improves, as seen in the following figure. The following screenshot shows what linear interpolation looks like:

Here is the corresponding cubic interpolation:

If we want to resize it to a particular size, we can use the format shown in the last resize instance. We can basically skew the image and resize it to whatever size we want. The output will look something like the following:

You have been reading a chapter from
OpenCV 3.x with Python By Example - Second Edition
Published in: Jan 2018 Publisher: Packt ISBN-13: 9781788396905
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 €14.99/month. Cancel anytime}