我正在尝试旋转在0到1之间变化的标量场(2D / 3D)。它以已知的角度(\ theta)对齐。为了在知道角度的情况下实现场的旋转,我曾尝试粗略地以相反的方向旋转它。以下是代码,
import numpy as np
import matplotlib.pyplot as plt
Nx = 100
Ny = 100
theta = np.pi/6.
image = 1.0*np.zeros((Nx,Ny))
#Creating a dummy field -> Square
sizex = Nx/5
sizey = Ny/5
for i in range(int(Nx/2.-sizex/2.),int(Nx/2.+sizex/2.)):
for j in range(int(Ny/2.-sizey/2.),int(Ny/2.+sizey/2.)):
image[i,j]=1.0
rot_mat = 1.0*np.zeros((2,2))
rot_mat[0,0]=np.cos(theta)
rot_mat[0,1]=-np.sin(theta)
rot_mat[1,0]=np.sin(theta)
rot_mat[1,1]=np.cos(theta)
rot_image = 1.0*np.zeros((Nx,Ny))
rot_centre_x = Nx/2
rot_centre_y = Ny/2
for i in range(0,Nx):
for j in range(0,Ny):
rot_x = (i-rot_centre_x)*rot_mat[0,0]+(j-rot_centre_y)*rot_mat[0,1] + rot_centre_x
rot_y = (i-rot_centre_x)*rot_mat[1,0]+(j-rot_centre_y)*rot_mat[1,1] + rot_centre_y
if rot_x<Nx-1 and rot_x>0 and rot_y<Ny-1 and rot_y>0:
rot_image[i,j] = image[int(rot_x),int(rot_y)]
fig, (ax1,ax2) = plt.subplots(1,2, sharey=True)
ax1.imshow(image)
ax1.set_title('Before')
ax2.imshow(rot_image)
ax2.set_title('After')
plt.show()
有什么办法可以改善这个状况?
已解决 使用@Mstaino的答案
答案 0 :(得分:2)
rot_image = spimg.rotate(image,theta, reshape=False, order=1)
您可以调整参数以适合您的旋转插值。希望它能起作用