Simple application of Homography transformations & Template matching: An Image Processing Introduction
For this article, I will discuss two topics about image processing: Homography Matrix & Template Matching
Homography is defined as mapping between two different projections of an image and is represented by a 3x3 transformation matrix in a homogenous coordinates space. We can use this to transform an image through a combination of rotation, translation, scaling, or skew operations. Let’s use this chess board as an example:
The image above was taken in an angle where it appears to be slanted. Let’s try to make it on top-view:
src = np.array([391, 100,
]).reshape((4, 2))dst = np.array([100, 100,
]).reshape((4, 2))from skimage import transform
tform = transform.estimate_transform('projective', src, dst)tf_img = transform.warp(chess, tform.inverse)
fig, ax = plt.subplots()
_ = ax.set_title('projective transformation')
Using the transform function, we were able to transform the chessboard image into top view. There are many homography transformation available for use. Try explore and play with it.
Template matching is a technique for finding a reference image known as template image in a bigger image known as the source image. This technique is particularly useful in image detection and recognition, as well as object tracking.
The implementation for template matching is quite simple. We will use a template and it will be compared to the image pixel by pixel to see if they are similar or not. Let’s use this image for this one:
The aircraft carrier is a good image for template matching. Let’s use an aircraft on its flight deck and try to do a template matching to the entire image:
from skimage.io import imread, imshow
from skimage.color import rgb2graycarrier = imread(‘aircraft_carrier.jpg’)
carrier_gray = rgb2gray(carrier)
template = carrier_gray[648:744,775:838]
from skimage.feature import match_template
result = match_template(carrier_gray, template)
As you can see from the resulting image above, we were able to find the areas within the image that matched with our template. They are denoted by yellow pixels which denote high values compared to sea and the aircraft carrier itself.
And there you have it! That’s a simple application of homography transformation and template matrix.