pythonmathimage-rotation

How to write the rotation 90 degree formula without python package?


I can only turn 180 degrees now:

import numpy as np
import math
import matplotlib.pyplot as plt

def my_flip(img1, type):

    nr, nc = img1.shape[:2]
    
    u = np.zeros( [ nr, nc ], dtype = 'uint8' )

    A =np.mat([[math.cos(math.pi),-math.sin(math.pi)], [math.sin(math.pi), math.cos(math.pi)]])
    #A =np.mat([[math.cos(math.pi),-math.sin(math.pi)], [math.sin(math.pi), math.cos(math.pi)]])
    for r in range(nr):
        for l in range(nc):
            v = np.dot(A.I, np.array([r, l]).T)
            u[r, l] = img1[int(v[0, 0]), int(v[0, 1])]

    
    return u

I don't want to use package, I want to write the math formula in python


Solution

  • Rotation by 90 degrees around (0,0) is described by simple formula:

    newx = -oldy
    newy = oldx
    

    You can apply these transformations to np arrays without using rotation matrices of general kind.

    Also rotation by 180 is

    newx = -oldx
    newy = -oldy
    

    Array negation looks simpler than matrix formation and other deeds, doesn't it?