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 transformations

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:

A chess board

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,
14, 271,
347, 624,
747, 298,
]).reshape((4, 2))
dst = np.array([100, 100,
100, 650,
650, 650,
650, 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.imshow(tf_img)
_ = ax.set_title('projective transformation')
Resulting transformation on top view

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

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:

An aircraft carrier

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 rgb2gray
carrier = imread(‘aircraft_carrier.jpg’)
carrier_gray = rgb2gray(carrier)
template = carrier_gray[648:744,775:838]
imshow(template);
Aircraft that we will use as a template
from skimage.feature import match_template
result = match_template(carrier_gray, template)
imshow(result, cmap=’viridis’);
Resulting template matching image

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.

A developer by day, a programmer by night

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store