The Wayback Machine - https://web.archive.org/web/20240621080237/https://www.geeksforgeeks.org/image-transformations-using-opencv-in-python/
Open In App

Image Transformations using OpenCV in Python

Last Updated : 07 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this tutorial, we are going to learn Image Transformation using the OpenCV module in Python.

What is Image Transformation?

Image Transformation involves the transformation of image data in order to retrieve information from the image or preprocess the image for further usage. In this tutorial we are going to implement the following image transformation:

  • Image Translation
  • Reflection 
  • Rotation
  • Scaling
  • Cropping
  • Shearing in x-axis
  • Shearing in y-axis

What is OpenCV?

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in commercial products.  By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as NumPy, Python is capable of processing the OpenCV array structure for analysis.

Image Translation

In computer vision or image processing, image translation is the rectilinear shift of an image from one location to another, so the shifting of an object is called translation. In other words,  translation is the shifting of an object’s location.

Python3




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


In the above code, we have imported NumPy and OpenCV module then read the image by using imread() function, and then translation takes place with the warpAffine() method which is defined as follows:

In the first argument, we passed the image, in the second argument it takes a matrix as a parameter in the matrix we give x = 100, which means we are telling the function to shift the image 70 units on the right side and y= 50, which means we are telling the function to shift the image 50 units downwards.  In the third argument, where we mentioned the cols and rows, we told the function to do not to crop the image from both the x and y sides.

dst = cv.warpAffine(img,M,(cols,rows))

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.

Python3




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


To flip the image horizontally:

M = np.float32([[1, 0, 0], [0, -1, rows],[0,  0, 1]])

To flip the image vertically:

M = np.float32([[-1, 0, cols], [0, 1, 0], [0, 0, 1]])

Output:

 

Image Rotation

Image rotation is a common image processing routine with applications in matching, alignment, and other image-based algorithms, in image rotation the image is rotated by a definite angle. It is used extensively in data augmentation, especially when it comes to image classification.

Python3




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


We have used the get rotation matrix function to define the parameter required in the warpAffine function to tell the function to make a matrix that can give a required rotation angle( here it is 30 degrees) with shrinkage of the image by 40%.   

img_rotation = cv.warpAffine(img,
                             cv.getRotationMatrix2D((cols/2, rows/2), 30, 0.6),
                             (cols, rows))

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.

Shrinking an image:

img_shrinked = cv2.resize(image, (350, 300),
                          interpolation = cv2.INTER_AREA) 

Note:  Here 350 and 300 are the height and width of the shrunk image respectively

Enlarging Image:

img_enlarged = cv2.resize(img_shrinked, None,
                          fx=1.5, fy=1.5,
                          interpolation=cv2.INTER_CUBIC)

Python3




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

Cropping is the removal of unwanted outer areas from an image.

cropped_img = img[100:300, 100:300]

OpenCV loads the image as a NumPy array, we can crop the image simply by indexing the array, in our case, we choose to get 200 pixels from 100 to 300 on both axes.

Python3




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

While the shearing image is on the x-axis, the boundaries of the image that are parallel to the x-axis keep their location, and the edges parallel to the y-axis change their place depending on the shearing factor.

M = np.float32([[1, 0.5, 0], [0, 1, 0], [0, 0, 1]])
sheared_img = cv.warpPerspective(img, M,
                                 (int(cols*1.5),
                                 int(rows*1.5)))

Python3




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(cols*1.5), int(rows*1.5)))
cv.imshow('img', sheared_img)
cv.waitKey(0)
cv.destroyAllWindows()


Output:

 

Image Shearing in Y-Axis

When shearing is done in the y-axis direction, the boundaries of the image that are parallel to the y-axis keep their location, and the edges parallel to the x-axis change their place depending on the shearing factor.

M = np.float32([[1, 0, 0], [0.5, 1, 0], [0, 0, 1]])
sheared_img = cv.warpPerspective(img, M,
                                 (int(cols*1.5),
                                 int(rows*1.5)))

Python3




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(cols*1.5), int(rows*1.5)))
cv.imshow('sheared_y-axis_out.jpg', sheared_img)
cv.waitKey(0)
cv.destroyAllWindows()


Output:

 



Previous Article
Next Article

Similar Reads

Convert OpenCV image to PIL image in Python
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a
3 min read
OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
Content has been removed on Author's request.
1 min read
Image resizing using Seam carving using OpenCV in Python
Seam carving is an effective image processing technique with the help of which an image can be resized without removing important elements from the image. The basic approach is to find all the continuous pixels with low energy from left to right or from top to bottom. After the region is selected, it is removed from the original image, leaving only
2 min read
Transition from OpenCV 2 to OpenCV 3.x
OpenCV is one of the most popular and most used Computer vision libraries. It contains tools to carry out image and video processing. When OpenCV 3..4.1 is an improved version of OpenCV 2.4 as it introduced new algorithms and features. Although some of the existing modules were rewritten and moved to sub-modules. In this articles, I will focus on t
2 min read
Top Books for Learning OpenCV: Computer Vision with OpenCV Library
OpenCV or Open Source Computer Vision Library, is an open-source computer vision and machine learning software library. It's extensively used for real-time computer vision tasks such as object detection, face recognition, image processing, etc. Whether you're a beginner or an experienced developer looking to deepen your understanding of OpenCV, her
5 min read
Reading an image in OpenCV using Python
Prerequisite: Basics of OpenCV In this article, we'll try to open an image by using OpenCV (Open Source Computer Vision) library. Following types of files are supported in OpenCV library: Windows bitmaps - *.bmp, *.dibJPEG files - *.jpeg, *.jpgPortable Network Graphics - *.png WebP - *.webp Sun rasters - *.sr, *.ras TIFF files - *.tiff, *.tif Raste
5 min read
Find the Solidity and Equivalent Diameter of an Image Object Using OpenCV Python
In this article, we will see how we can find the solidity and the equivalent diameter of an object present in an image with help of Python OpenCV. Function to Find Solidity The solidity of an image is the measurement of the overall concavity of a particle. We can define the solidity of an object as the ratio of the contour area to its convex hull a
4 min read
Python | Detect corner of an image using OpenCV
OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos. Let's see how to detect the corner in the image. cv2.goodFeaturesToTrack() method finds N strongest corners in the image by Shi-Tomasi metho
2 min read
Image Pyramid using OpenCV | Python
Image Pyramids are one of the most beautiful concept of image processing.Normally, we work with images with default resolution but many times we need to change the resolution (lower it) or resize the original image in that case image pyramids comes handy. The pyrUp() function increases the size to double of its original size and pyrDown() function
1 min read
Negative transformation of an image using Python and OpenCV
Image is also known as a set of pixels. When we store an image in computers or digitally, it’s corresponding pixel values are stored. So, when we read an image to a variable using OpenCV in Python, the variable stores the pixel values of the image. When we try to negatively transform an image, the brightest areas are transformed into the darkest an
4 min read
Practice Tags :