Geometric Transformations of Images using OpenCV|Image Processing Part-1
In this Article, we will learn how can we perform geometric transformations algorithms on images using OpenCV in Python language.
We will start off by talking a little about Image Processing, then we will move on to see different applications/ scenarios where image processing can come in handy. So, let’s begin!
What is Image Processing ?
It is important to know what exactly image processing is and what is its role in the bigger picture before diving into its how’s. Image Processing is most commonly termed as ‘Digital Image Processing’ and the domain in which it is frequently used is ‘Computer Vision’.
Both Image Processing algorithms and Computer Vision (CV) algorithms take an image as input; however, in image processing, the output is also an image, whereas in computer vision the output can be some features/information about the image.
Install OpenCV
To install OpenCV on your system, run the following pip command-
For Windows:
pip install opencv-python
For MacOS:
brew install opencv3 — with-contrib — with-python3
Now OpenCV is installed successfully in our system and we are ready. Let’s have some fun with some images!
Let’s start using OpenCV library-
1. Importing the required libraries
cv2- For OpenCV (Used for Image Processing)
matplotlib- For Matplotlib (Used fot Plotting and Visualization)
numpy- For Numpy (Used for Scientific Computing)
2. Reading an image using OpenCV library
We can read an image by using OpenCV and Matplotlib library.
3. Analysis of images
To know about the image like type of image or shape of image.
4. Geometric Transformations of images
Learning to apply different geometric transformation to images like scaling, translation, rotation, affine transformation etc.
- Resizing (Scaling) the image
Scaling is just resizing of the image. OpenCV comes with a function of cv2.resize() for this purpose. The size of the image can be specified manually, or you can specify the scaling factor.
- Rotating the image
Rotation of an image for an angle theta is achieved by the transformation matrix of the form-
But OpenCV provides scaled rotation with adjustable center of rotation so that you can rotate at any location you prefer. Modified transformation matrix is given by-
To find this transformation matrix, OpenCV provides a function, cv2.getRotationMatrix2D.
- Affine Transformations
In affine transformation, all parallel lines in the original image will still be parallel in the output image. To find the transformation matrix, we need three points from input image and their corresponding locations in output image. Then cv2.getAffineTransform will create a 2x3 matrix which is to be passed to cv2.warpAffine.
- Perspective Transformations
For perspective transformation, you need a 3x3 transformation matrix. Straight lines will remain straight even after the transformation. To find this transformation matrix, we need 4 points on the input image and corresponding points on the output image. Among these 4 points, 3 of them should not be collinear. Then transformation matrix can be found by the function cv2.getPerspectiveTransform. Then apply cv2.warpPerspective with this 3x3 transformation matrix.
This brings us to the end of this article. I hope you have understood the basics of Image Processing with OpenCV in Python clearly. Make sure you practice as much as possible.
Do look out for other Jupyter notebooks in the series which will explain the various other aspects of Image Processing with OpenCV in Python.
If you wish to check out more resources related to Data Science and Machine Learning you can refer to my Github account.
Hope you like the post.