Image Transformations using OpenCV in Python (original) (raw)
Image transformations are fundamental operations in image processing that modify an image's position, orientation, size, or shape. OpenCV (Open Source Computer Vision Library) is a popular open-source library that provides efficient functions for performing these transformations in Python.
- Translate images by shifting them horizontally or vertically.
- Modify image geometry through reflection, rotation, scaling, and shearing operations.
- Extract important regions from images using cropping techniques.
Image Translation
In computer vision and image processing, image translation refers to shifting an image from one position to another. It changes the location of objects in the image without altering their shape, size, or orientation.
- The image is loaded using the
imread()function after importing the NumPy and OpenCV modules. - The warpAffine()method performs the translation using the transformation matrix
M. - In the matrix,
x = 100shifts the image 100 pixels to the right andy = 50shifts it 50 pixels downward, while(cols, rows)preserves the original image dimensions. Python `
import numpy as np import cv2 as cv img = cv.imread('girlImage.jpg', 0) rows, cols = img.shape M = np.float32([[1, 0, 100], [0, 1, 50]]) dst = cv.warpAffine(img, M, (cols, rows)) cv.imshow('img', dst) cv.waitKey(0) cv.destroyAllWindows()
`
**Output:

Image Reflection
Image reflection is used to flip the image vertically or horizontally. For reflection along the x-axis, we set the value of Sy to -1, Sx to 1, and vice-versa for the y-axis reflection.
- For horizontal reflection, the value of
Syis set to-1whileSxremains1, causing the image to flip along the x-axis. - For vertical reflection, the value of
Sxis set to-1whileSyremains1, causing the image to flip along the y-axis. Python `
import numpy as np import cv2 as cv img = cv.imread('girlImage.jpg', 0) rows, cols = img.shape M = np.float32([[1, 0, 0], [0, -1, rows], [0, 0, 1]]) reflected_img = cv.warpPerspective(img, M, (int(cols), int(rows))) cv.imshow('img', reflected_img) cv.imwrite('reflection_out.jpg', reflected_img) cv.waitKey(0) cv.destroyAllWindows()
`
**Output:

Image Rotation
Image rotation is the process of rotating an image by a specified angle around a fixed point, usually its center. It is widely used in image processing, computer vision, and data augmentation tasks.
- The
getRotationMatrix2D()function creates the transformation matrix required for rotation. - The image is rotated by 30 degrees around its center point
(cols/2, rows/2). - The scale factor
0.6reduces the image size to 60% of its original dimensions while applying the rotation. Python `
import numpy as np import cv2 as cv img = cv.imread('girlImage.jpg', 0) rows, cols = img.shape M = np.float32([[1, 0, 0], [0, -1, rows], [0, 0, 1]]) img_rotation = cv.warpAffine(img, cv.getRotationMatrix2D((cols/2, rows/2), 30, 0.6), (cols, rows)) cv.imshow('img', img_rotation) cv.imwrite('rotation_out.jpg', img_rotation) cv.waitKey(0) cv.destroyAllWindows()
`
**Output:

Image Scaling
Image scaling is a process used to resize a digital image. We perform two things in the image scaling either we enlarge the image or we shrink the image, OpenCV has a built-in function cv2.resize() for image scaling****.**
- The image is shrunk to 350 × 300 pixels using the
resize()function withINTER_AREAinterpolation. - The shrunk image is enlarged by 1.5× along both axes using
INTER_CUBICinterpolation for smoother scaling. Python `
import numpy as np import cv2 as cv img = cv.imread('girlImage.jpg', 0) rows, cols = img.shape img_shrinked = cv.resize(img, (250, 200), interpolation=cv.INTER_AREA) cv.imshow('img', img_shrinked) img_enlarged = cv.resize(img_shrinked, None, fx=1.5, fy=1.5, interpolation=cv.INTER_CUBIC) cv.imshow('img', img_enlarged) cv.waitKey(0) cv.destroyAllWindows()
`
**Output:

Image Cropping
Image cropping is the process of removing unwanted portions of an image to focus on a specific region.
- OpenCV stores images as NumPy arrays, allowing cropping through array indexing.
- The expression
img[100:300, 100:300]extracts a 200 × 200 pixel region from the image. Python `
import numpy as np import cv2 as cv img = cv.imread('girlImage.jpg', 0) cropped_img = img[100:300, 100:300] cv.imwrite('cropped_out.jpg', cropped_img) cv.waitKey(0) cv.destroyAllWindows()
`
**Output:

Image Shearing in X-Axis
Image shearing in the x-axis shifts image pixels horizontally, causing the image to appear slanted while preserving parallelism.
- The transformation matrix
[[1, 0.5, 0], [0, 1, 0], [0, 0, 1]]applies a shearing factor of 0.5 along the x-axis. - The
warpPerspective()function performs the transformation, and the output size is increased to accommodate the sheared image. Python `
import numpy as np import cv2 as cv img = cv.imread('girlImage.jpg', 0) rows, cols = img.shape M = np.float32([[1, 0.5, 0], [0, 1, 0], [0, 0, 1]]) sheared_img = cv.warpPerspective(img, M, (int(cols1.5), int(rows1.5))) cv.imshow('img', sheared_img) cv.waitKey(0) cv.destroyAllWindows()
`
**Output:

Image Shearing in Y-Axis
Image shearing in the y-axis shifts image pixels vertically, causing the image to appear slanted while preserving parallelism.
- The transformation matrix
[[1, 0, 0], [0.5, 1, 0], [0, 0, 1]]applies a shearing factor of0.5 along the y-axis. - The
warpPerspective()function performs the transformation, and the output size is increased to accommodate the sheared image. Python `
import numpy as np import cv2 as cv img = cv.imread('girlImage.jpg', 0) rows, cols = img.shape M = np.float32([[1, 0, 0], [0.5, 1, 0], [0, 0, 1]]) sheared_img = cv.warpPerspective(img, M, (int(cols1.5), int(rows1.5))) cv.imshow('sheared_y-axis_out.jpg', sheared_img) cv.waitKey(0) cv.destroyAllWindows()
`