Skip to content

Instantly share code, notes, and snippets.

@GongXinyuu
Created January 29, 2019 06:25
Show Gist options
  • Select an option

  • Save GongXinyuu/abe0b9fa8269269501919ce469bb633a to your computer and use it in GitHub Desktop.

Select an option

Save GongXinyuu/abe0b9fa8269269501919ce469bb633a to your computer and use it in GitHub Desktop.
image rotation function
import numpy as np
from imageio import imsave, imread
import matplotlib.pyplot as plt
import math
from numpy.linalg import inv
img_name = 'xxx.jpg'
img = imread(img_name)
h, w, c = np.shape(img)
def inverse_rotation(img, alpha):
inv_affine_matrix = inv(np.array([[math.cos(alpha), math.sin(alpha), 0],
[-math.sin(alpha), math.cos(alpha), 0],
[0, 0, 1]]))
new_img = np.zeros_like(img)
for c_idx in range(c):
for ny_idx in range(h):
for nx_idx in range(w):
v = np.matmul(np.array([ny_idx, nx_idx, 1]), inv_affine_matrix)
y_idx = v[0]
x_idx = v[1]
new_img[ny_idx, nx_idx, c_idx] = img[int(y_idx)%h, int(x_idx)%w, c_idx]
return new_img
n_img = inverse_rotation(img, alpha=-math.pi/4)
plt.imshow(n_img)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment