Rotating Images with OpenCV and Imutils.

Samuel Ozechi
Analytics Vidhya
Published in
5 min readJan 29, 2022

In this article, I will describe the steps required to rotate images around any point and by specified angles of rotation using both OpenCV and Imutils.

Rotating Images with OpenCV

To rotate your images with OpenCV:

1. Load the desired image using the OpenCV imread function.

Script: Loading and displaying the original image

I imported the OpenCV library (as cv2) and loaded an image of the Eiffel Tower from my working directory using the imread function before displaying the image with the imshow function. Note that the supplied parameter value to the imread function should contain the path to the image if it’s not in the working directory.

Output:

2. Get the spatial dimensions of the image (height, width and centre coordinates).

Script: Get the image’s height, width and centre coordinates.

The first two values of the image.shape attribute returns the image’s height and width respectively. Integer division is used to get the values of the centre location as the function to rotate the image only accepts integer values. The centre location values are useful in rotating the image around the centre.

3. Define a rotation matrix to rotate the image around a desired point and angle of rotation.

Script: Define a rotation matrix to rotate the image 45⁰ around the centre.

The cv2.getRotationMatrix2D function (as its name implies) defines a 2D matrix to rotate the image according to the parameter values passed. The function takes a parameter of the point of rotation (the centre of the image in this example denoted by the coordinates (centerX, centerY)), an angle of rotation in degrees (45 degrees in this case) and a floating point value (1.0 in this case) that specifies the scale of the rotated image in relation to the original image. 1.0 scale value in our example scales the rotated image with the same dimensions as the original image. Higher scale values expand the image while lower values shrink the image.

4. Apply rotation to the image according to the rotation matrix and display the rotated image.

Script: Rotate the image by 45⁰ around the centre.

The rotation matrix (M) is passed to the cv2.warpAffine function which applies rotation to the image according to the values specified in the rotation matrix. The function also takes a tuple of the image’s width and height as arguments.

Output:

The image is rotated 450 about the centre. Notice the image is rotated in an anticlockwise direction? We can also rotate the images in a clockwise direction by passing negative values for the angle of rotation in the rotation matrix.

Rotating Images in a clockwise direction with OpenCV.

Script:

Output:

Let’s Compare with what it would look like if we passed 90⁰ (anticlockwise 90⁰ rotation) as the rotation angle instead of -90⁰.

Notice the image is rotated in an anticlockwise direction, I also reduced the image scale by half (by passing 0.5 as the scale value).

While I have previously rotated images along the centre (centerX, centerY), images can be rotated about any point in the image using similar steps as previously mentioned.

Rotating Images about arbitrary points.

Script: Rotate the image 30 degrees about points (15, 10).

Output:

Notice how the rotation cuts off certain parts of the image? I will show how to avoid this later in the article.

Rotating Images using Imutils

Just as with shifting images, the Imutils package offers simple steps in rotating images along any point and about any angle with the use of a simple to-use rotate helper function.

Script: Rotate the image by 30⁰ in a clockwise direction using Imutils.

The imutils.rotate function is called to rotate the image in the desired direction by passing the image and angle of rotation as arguments.

Output:

Just as in the previous image, the rotation cuts off certain portions of the image, while this might be intended in some use cases, we could ensure that all parts of the image are still in view.

Script: Rotate the image by 30⁰ while ensuring that the entire image is in view.

Output:

This time I use a cv2.rotate_bound helper function as opposed to the rotate function to ensure that all parts of the image are in view in the rotated image.

Summary: In this article, I showed the steps required to rotate images using both OpenCV and Imutil packages about the centre or any other arbitrary points In clockwise or anticlockwise directions. I also showed how to rotate the image while preserving the full view, without cropping any parts. You can find the code and image used in the article’s GitHub repo.

--

--