Image Transformation in NumPy
- Read
- Discuss
Image transformation is altering an image to achieve a desired effect, such as changing its position, orientation, or size. This article will explore several image transformations that can be performed using the NumPy library in Python, including translation, rotation, and flipping.
Display an image
First, let’s start by reading an image using the OpenCV library and displaying it using matplotlib.
import cv2
import matplotlib.pyplot as plt
# Read the image
img = cv2.imread("garden.jpeg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Display the image
plt.imshow(img)
plt.show()
The following will be the output.
Translation in NumPy
The translation is the process of moving an image to a different position. This can be done by creating a transformation matrix and applying it to the image using the cv2.warpAffine() function. The following code demonstrates how to translate an image by 50 pixels in the x-direction and 100 pixels in the y-direction:
# Create the translation matrix
M = np.float32([[1, 0, 50], [0, 1, 100]])
# Perform the translation
img_translated = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
# Display the translated image
plt.imshow(img_translated)
plt.show()
The following will be output:
Rotation using NumPy
Rotation is the process of rotating an image about a given point. This can also be done by creating a transformation matrix and applying it to the image using the cv2.warpAffine() function. The following code demonstrates how to rotate an image by 45 degrees around its center:
import numpy as np
# Perform the rotation
img_rotated = np.rot90(img, k=2)
# Display the rotated image
plt.imshow(img_rotated)
plt.show()
The following will be the output.
The k parameter specifies the number of 90-degree increments by which the image should be rotated. A value of 1 will rotate the image by 90 degrees, a value of 2 will rotate the image by 180 degrees, and a value of -1 will rotate the image by 270 degrees (or 90 degrees clockwise).
This function is useful when you need to rotate an image by a multiple of 90 degrees, but it should be noted that it will not rotate the image by any other degree. For non-90-degree rotation, you can use the following method:
import numpy as np
# Get the image center
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
# Create the rotation matrix
M = cv2.getRotationMatrix2D(center, 45, 1.0)
# Perform the rotation
img_rotated = cv2.warpAffine(img, M, (w, h))
# Display the rotated image
plt.imshow(img_rotated)
plt.show()
The following will be the output:
Flipping Image Using NumPy
Flipping an image using just numpy can be done by using the numpy function np.fliplr() or np.flipud().
np.fliplr() will flip an image horizontally (left to right) and np.flipud() will flip an image vertically (up to down).
Here’s an example of how to flip an image horizontally using np.fliplr():
import numpy as np
# Perform the flip
img_flipped = np.fliplr(img)
# Display the flipped image
plt.imshow(img_flipped)
plt.show()
The following will be the output:
And here’s an example of how to flip an image vertically using np.flipud():
import numpy as np
# Perform the flip
img_flipped = np.flipud(img)
# Display the flipped image
plt.imshow(img_flipped)
plt.show()
The following will be the output:
Leave a Reply
You must be logged in to post a comment.