Skip to content

Instantly share code, notes, and snippets.

@darkleaf
Last active October 13, 2015 23:38
Show Gist options
  • Save darkleaf/4274054 to your computer and use it in GitHub Desktop.
Save darkleaf/4274054 to your computer and use it in GitHub Desktop.
цои
import scipy.misc
import numpy as np
import matplotlib.pyplot as plt
import math
hx = 7.5
hy = 6.3
fi = -15 * np.pi / 180
kx = 1.2
ky = 0.9
img = scipy.misc.lena()
Nx = img.shape[0]
Ny = img.shape[1]
x0 = (Nx-1)/2
y0 = (Ny-1)/2
H = np.array([[hx], [hy]])
F = np.array([[np.cos(fi), -np.sin(fi)],
[np.sin(fi), np.cos(fi)]])
K = np.array([[kx, 0],
[0, ky]])
O = np.array([[1, 0],
[0, -1]])
Aff = np.array([[1.3, 0.5],
[0.2, 1.1]])
A = np.eye(2, 2)
X = np.zeros([2, 1])
def interpolate(img, x, y):
drob_x = math.modf(x)[0]
int_x = math.modf(x)[1]
drob_y = math.modf(y)[0]
int_y = math.modf(y)[1]
res_x = int_x if drob_x < 0.5 else int_x + 1
res_y = int_y if drob_y < 0.5 else int_y + 1
if res_x >= 0 and res_x < Nx and res_y >= 0 and res_y < Ny:
return img[res_x, res_y]
return 0
def action(img, A, X):
result = np.zeros([Nx, Ny])
for i in range(Nx):
for j in range(Ny):
v = np.array([[i - x0], [j - y0]])
v = np.dot(A, v) + X
result[i, j] = interpolate(img, v[0,0] + x0, v[1,0] + y0)
return result
def move(img):
return action(img, A, H)
def turn(img):
return action(img, F, X)
def scale(img):
return action(img, K, X)
def mirror(img):
return action(img, O, X)
def compose(img):
a = np.dot(F, K)
a = np.dot(a, O)
return action(img, a, H)
def affine(img):
return action(img, Aff, X)
def inv_affine(img):
a = np.linalg.inv(Aff)
return action(img, a, X)
#move_img = move(img)
#turn_img = turn(move_img)
#scale_img = scale(turn_img)
#mirror_img = mirror(scale_img)
#compose_img = compose(img)
affine_img = affine(img)
inv_affine_img = inv_affine(affine_img)
#plt.matshow(move_img)
#plt.matshow(turn_img)
#plt.matshow(scale_img)
#plt.matshow(mirror_img)
#plt.matshow(compose_img)
plt.gray()
plt.matshow(affine_img)
plt.matshow(inv_affine_img)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment