Skip to content

Instantly share code, notes, and snippets.

@chsasank
Created April 20, 2016 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chsasank/4bda6a6dc7973ae206b09134b92d20f2 to your computer and use it in GitHub Desktop.
Save chsasank/4bda6a6dc7973ae206b09134b92d20f2 to your computer and use it in GitHub Desktop.
Homogprahies useful for data augmentation. Quite fast.
import numpy as np
from skimage import io
from skimage import transform
import matplotlib.pyplot as plt
import math
import time
I = io.imread('random_img.jpg')
# Homography
theta = -10*np.pi/180
rotation_matrix = np.array([[math.cos(theta), -math.sin(theta), 0],
[math.sin(theta), math.cos(theta), 0],
[0, 0, 1]])
shear = 0.1
shear_matrix = np.array([[1.0, -math.sin(shear), 0.0],
[0.0, math.cos(shear), 0.0],
[0.0, 0.0, 1.0]])
tx, ty = 10,20
translation_matrix = np.array([[1, 0, tx],
[0, 1, ty],
[0, 0, 1 ]])
H = np.dot(np.dot(rotation_matrix, translation_matrix), shear_matrix)
# Do all of these at once
t0 = time.time()
t = transform.ProjectiveTransform(H)
I1 = transform.warp(I, t, mode='edge')
t1 = time.time()
print('time taken all at once: ', t1-t0)
plt.imshow(I1)
plt.show()
# Do these one after another
t0 = time.time()
t = transform.ProjectiveTransform(rotation_matrix)
I1 = transform.warp(I, t, mode='edge')
t = transform.ProjectiveTransform(translation_matrix)
I1 = transform.warp(I1, t, mode='edge')
t = transform.ProjectiveTransform(shear_matrix)
I1 = transform.warp(I1, t, mode='edge')
t1 = time.time()
print('time taken one after another: ', t1-t0)
plt.imshow(I1)
plt.show()
@NirZarrabi
Copy link

This code creates an Affine transformation and not Homography

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment