Scikit Image - Rotating an image



Rotating an image refers to the process of changing the orientation of the image by a certain angle around a specified point. This transformation rotates the image's pixels, rearranging them in a way that the entire image appears as if it has been rotated. When an image is rotated, each pixel is moved to a new position based on the specified rotation angle and the center point of rotation.

Rotation is a common image transformation used in various applications, including computer vision, graphics, and image processing. It can be applied for a variety of purposes, such as image alignment, object recognition, image visualization, and image augmentation in machine learning.

The Scikit-image library provides the skimage.transform.rotate() function to perform image rotation. It takes an input image, the desired rotation angle (in degrees), and optional parameters to control the behaviour of the rotation.

Using the skimage.transform.rotate() function

The rotate() function is used to rotate an input image by a certain angle around a specified center or its own center.

Syntax

Following is the syntax of this function −

skimage.transform.rotate(image, angle, resize=False, center=None, order=None, mode='constant', cval=0, clip=True, preserve_range=False)

Parameters

  • image: Input image to be rotated.
  • angle (float): Rotation angle in degrees in the counter-clockwise direction.
  • resize (bool, optional): Determines whether the shape of the output image will be automatically calculated so that the complete rotated image exactly fits. The default value is False.
  • center (iterable of length 2): The rotation center. This parameter defines the center point around which the image will be rotated. If the center is set to None, the image is rotated around its own center, which is calculated as (cols / 2 - 0.5, rows / 2 - 0.5), where cols and rows are the width and height of the input image, respectively. Please note that the center parameter uses (cols, rows) ordering, which is different from the usual (rows, cols) ordering in skimage and other libraries.
  • order (int, optional): The order of spline interpolation used during the rotation. The default value is 0 if the image.dtype is bool and 1 otherwise. The order must be in the range 0-5.
  • mode (str, optional): Determines how points outside the boundaries of the input image are filled. It can take one of the following values: {constant, edge, symmetric, reflect, wrap}.
  • cval (float, optional): Used in conjunction with mode 'constant', it specifies the value to be used for filling points outside the image boundaries. The default value is 0.
  • clip (bool, optional): Specifies whether to clip the output to the range of values of the input image. Setting the clip to True (default) ensures that the output image does not have values beyond the input range.
  • preserve_range (bool, optional): Specifies whether to keep the original range of pixel values in the output image. If set to False, the input image is converted according to the conventions of img_as_float.

Return Value

It returns the rotated image as an ndarray, which is the rotated version of the input image.

Example

Here's an example of how to use the skimage.transform.rotate() function to rotate an image.

import numpy as np
import matplotlib.pyplot as plt
from skimage import io, transform

# Load the input image 
image = io.imread('Images/Tajmahal.jpg')

# Rotate the image by 45 degrees counter-clockwise around its center
angle = 45
rotated_image = transform.rotate(image, angle, resize=True)

# Display the original and  rotated images side by side
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(image)
axes[0].set_title('Original Image')

axes[1].imshow(rotated_image)
axes[1].set_title('Rotated Image (Angle = 45)')

# Show the plot
plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

Example

Here's another example that demonstrates how to use skimage.transform.rotate() to rotate an image by a specified angle(clockwise direction) around a custom center point.

import numpy as np
import matplotlib.pyplot as plt
from skimage import io, transform

# Load the input image 
image = io.imread('Images/Tajmahal.jpg')

# Define the rotation angle and center point
angle = -45
center = (image.shape[1] // 2, image.shape[0] // 2)  # Center point as (x, y)

# Rotate the image by the specified angle around the custom center
rotated_image = transform.rotate(image, angle, center=center, resize=True)

# Display the original and  rotated images side by side
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(image)
axes[0].set_title('Original Image')

axes[1].imshow(rotated_image)
axes[1].set_title('Rotated Image (Angle = -45)')

# Show the plot
plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

Advertisements