Image Processing using OpenCV

neupane parlad
The Startup
Published in
5 min readDec 21, 2020

--

Photo by NASA on Unsplash

Around 2010, I remember rotating image was popular among adults. Simple tasks like resizing, affine, rotate, transformation has been used whether it’s a photo album from the old days or road analysis by electronic vehicle. A few weeks ago, I had the opportunity to read articles about self-driving cars and image processing.

In the good old days of high school math, we all have done matrix transformation. While thinking about this topic, I find this as the closest one to define what image Transformation is. Image Transformation is similar to matrix transformation. After all, behind every image, there is a bit matrix. This link (https://onlineimagetools.com/convert-gif-to-base64) has so many options to convert one image from another. Give it a try.

A real png image and bits inside the image.

We will cover the following topics in this series. In this article, we are covering image transformation and translation.

Topic

  • Image Transformation
  • Image Scaling
  • Image Shearing
  • Image Reflection
  • Image Rotation
  • Image Cropping

We will be using a library called OpenCV to process images. It’s an openSource library with tons of features, implementation, and documentation.

import cv2

Here is a Wikipedia panel about programming language in OpenCV:

OpenCV is written in C++ and its primary interface is in C++, but it still retains a less comprehensive though extensive older C interface. All of the new developments and algorithms appear in the C++ interface. There are bindings in Python, Java, and MATLAB/OCTAVE. The API for these interfaces can be found in the online documentation.[12] Wrappers in several programming languages have been developed to encourage adoption by a wider audience. In version 3.4, JavaScript bindings for a selected subset of OpenCV functions were released as OpenCV.js, to be used for web platforms.

1. Image transformation

Image transformation is a process of changing the coordinates. It maps some point(x, y) to point(x’, y’) in a coordinate system. Let’s make a fancy name image Transformation to geometric transformation of an object. I am sure more peoples now think Image Transformation as something they have done it before.

In a real-world of computer science, there are so many users and applications of Image Transformation. Electronic vehicle identifies the signs on the roads using image processing and transformation. For example, a speed limit sign is on the same pole with 5 or 6 other signs. So how do electric vehicle identifies the speed limit? One way is to capture the image of all the signs and identify the image that is similar to other speed limits it has train to identify . A model in a self-driving car is trained with thousands of speed limit signs from the real world and GAN.

Let’s talk about linear Transformation. Because, Tasks like scaling, rotating, skewing are implemented as linear Transformation and you can think of them as linear algebra operations. Linear Transformation is a function T: Rn → Rm where it satisfied this condition:

Something is a linear Transformation if and only if,

L.T ==> T(a’ + b’) = T(a’) + T(b’)

L.T ==> T(ca’) = c(T(a’))

You can find more information about Linear transformation in Khan Academy here https://www.khanacademy.org/math/linear-algebra/matrix-transformations/linear-transformations/v/linear-transformations.

  1. Affine Transformation
  2. Projected (Non-affine) Transformation

Affine Transformation preserves all parallel lines from the original images. It’s mainly used for scaling, skewing, and rotations.

Projective transformation is a transformation that maps lines to lines but doesn’t necessarily preserve parallelism like Affine transformation. We can say affine Transformation is a special kind of projective transformation.

Fig: Projective and Affine Transformation

As we can see from the image above, an affine transformation is a transformation of a triangle where projective Transformation is a transformation of an arbitrary quadrangle.

Implementing Affine Transformation

To find the Affine Transformation, we need three points from an input image and their corresponding location in the output image. Cv2.getAffineTransform will create 3x3 matrix which will be pass to cv2.warpAffine.

import cv2
import numpy as np
from matplotlib import pyplot as plt
img=cv2.imread('calendar.jpg')rows,cols,ch = img.shapecoordinate_point_on_src = np.float32([[50,50],[200,50],[50,250]])coordinate_point_on_dest = np.float32([[10,100],[200,50],[70,250]])M=cv2.getAffineTransform(coordinate_point_on_src,coordinate_point_on_dest)dst = cv2.warpAffine(img,M,(cols,rows))plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

Output:

Fig: original Image and Affine Transformation Image

Implementing Projective Transformation

It can present the idea of what happens to an image when the point of view is changed, that is why maps are confusing. When using Google Maps as a pedestrian, we need to identify where we are and landscape perspective. Suppose, in a street, there is a building infant and building front part is square, when you walk toward that building from the middle you will see a square shape, but if you look at that building from the side can be more like a trapezoid. Here are the many applications such as Augment Reality, Image Reflection, Image registration, or the computation of camera motion between two images, etc.

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('calendar.jpg')
rows,cols,ch = img.shape
coordinate_point_on_src = np.float32([[560,650],[368,900],[280,387],[389,390]])
coordinate_point_on_dest = np.float32([[100,100],[300,900],[100,300],[300,300]])
transform=cv2.getPerspectiveTransform(coordinate_point_on_src,coordinate_point_on_dest)from_prespective = cv2.warpPerspective(img,transform,(100,100))plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

Conclusion

In this tutorial, we talked about image transformation and mathematical implementation. OpenCV is a library with many useful features. Then we talked about image transformation. They are affine and Projective Transformation. Then we looked into the implementation of each of them in python using the OpenCV library. The next article will be about Image Scaling and Shading.

--

--

neupane parlad
The Startup

Follow me for Data Science, Data Engineering and Data Analysis Articles. Follow me on twitter at @parladN