Skip to Content
OpenCV with Python By Example
book

OpenCV with Python By Example

by Prateek Joshi
September 2015
Beginner to intermediate
296 pages
5h 57m
English
Packt Publishing
Content preview from OpenCV with Python By Example

Image rotation

In this section, we will see how to rotate a given image by a certain angle. We can do it using the following piece of code:

import cv2
import numpy as np

img = cv2.imread('images/input.jpg')
num_rows, num_cols = img.shape[:2]

rotation_matrix = cv2.getRotationMatrix2D((num_cols/2, num_rows/2), 30, 1)
img_rotation = cv2.warpAffine(img, rotation_matrix, (num_cols, num_rows))
cv2.imshow('Rotation', img_rotation)
cv2.waitKey()

If you run the preceding code, you will see an image like this:

Image rotation

What just happened?

In order to understand this, let's see how we handle rotation mathematically. Rotation is also a form of transformation, and we can ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

OpenCV 4 with Python Blueprints - Second Edition

OpenCV 4 with Python Blueprints - Second Edition

Dr. Menua Gevorgyan, Michael Beyeler (USD), Arsen Mamikonyan, Michael Beyeler
OpenCV By Example

OpenCV By Example

Prateek Joshi, David Millán Escrivá, Vinícius G. Mendonça
OpenCV 3 Computer Vision with Python Cookbook

OpenCV 3 Computer Vision with Python Cookbook

Aleksei Spizhevoi, Aleksandr Rybnikov

Publisher Resources

ISBN: 9781785283932Supplemental Content