More than meets the eye

Image Transformations using Homography and Texture Metrics in Python

Nico Aguila
Towards Data Science

--

Photo by Cookie the Pom on Unsplash

From my previous articles, I have discussed different approaches in terms of image processing. Some of which are image cleaning and even object detection. For this article, we will be discussing more on transformation and feature generation for our images, namely: Homography and Texture Metrics.

Homography

The transformations that Homography can do include rotation, translation, scaling, or even skewing. This is made possible by mapping coordinates between two planar projections.

An example of this is evident with our document scanner apps, where pieces of paper are then transformed to a readable perspective. Let’s try that out as an example with our codes below:

Image by Author

In order to generate a top-down view of the card, we have to compute for the homography matrix. This is done by getting the coordinates of the corners of the card, as well as coordinates where the card will be projected into a top-down view.

#import libraries
from skimage.io import imread, imshow
import matplotlib.pyplot as plt
import numpy as np
#load the card
cards = imread('medium/cards.jpg')
#initialize source and destination coordinates
src = np.array([319, 134,
273, 61,
133, 103,
162, 188,
]).reshape((4, 2))
dst = np.array([203, 37,
314, 36,
314, 181,
203, 182,
]).reshape((4, 2))

There are different types of transformations for the type of projection that we want the image to appear like. Since we want to preserve the lines of the card shape, we would use a projective type of transformation. The different types can be found here for your reference.

#transform image point of view
from skimage import transform
tform = transform.estimate_transform('projective', src, dst)
tf_img = transform.warp(cards, tform.inverse)
fig, ax = plt.subplots()
ax.imshow(tf_img)
_ = ax.set_title('projective transformation')
Image by Author

Texture Metrics

Since some images are very sensitive in nature where color information may be inconsistent in more than one area, another measure can be considered to characterize our images. Texture is this alternative form where the spatial arrangements of the intensity values are taken into account.

A sample texture metric can be done by using entropy as a metric. The benefit of entropy is that it can detect variations in the intensity values in grayscale. We can see entropy in action given the codes below

import matplotlib.pyplot as plt
import numpy as np
from skimage import data
from skimage.util import img_as_ubyte
from skimage.filters.rank import entropy
from skimage.morphology import disk
image = img_as_ubyte(data.brick())fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(12, 4),
sharex=True, sharey=True)
img0 = ax0.imshow(image, cmap=plt.cm.gray)
ax0.set_title("Image")
ax0.axis("off")
fig.colorbar(img0, ax=ax0)
img1 = ax1.imshow(entropy(image, disk(5)), cmap='gray')
ax1.set_title("Entropy")
ax1.axis("off")
fig.colorbar(img1, ax=ax1)
fig.tight_layout()plt.show()
Image by Author

And there you have it, sample outputs you can do with Homography and Texture Metrics.

--

--