图像旋转
def pixel_map(img,x,y):
height,width,channel = img.shape
x_f,y_f = int(x),int(y)
if 0 < x_f < height and 0 < y_f < width:
return img[x_f,y_f]
else:
return 0
def img_rotate(img,theta):
height,width,channel = img.shape
rot = np.array([[np.cos(theta),-np.sin(theta),0],
[np.sin(theta),np.cos(theta),0],
[0,0,1]])
x,y = np.meshgrid(np.arange(0,height),np.arange(0,width))
x = x - x.mean()
y = y - y.mean()
x = x.reshape(height*width)
y = y.reshape(height*width)
xyz = np.stack([x,y,np.ones_like(x)])
xyz_rot = rot @ xyz
x,y,_ = xyz_rot
x += height/2
y += width/2
x,y = x.reshape(height,width),y.reshape(height,width)
img_rot = np.zeros_like(img)
for i in range(height):
for j in range(width):
img_rot[i,j,:] = pixel_map(img,x[i,j],y[i,j])
return img_rot.transpose([1,0,2])
其中pixel_map的方法可以采用更好的选择,这里只是采用最简单的一种。
效果