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.

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.

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.

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.

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****.**

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.

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.

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.

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()

`