首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按一定角度旋转图像

按一定角度旋转图像
EN

Stack Overflow用户
提问于 2019-01-04 02:38:06
回答 1查看 126关注 0票数 0

我正在尝试旋转一个在0和1之间变化的标量字段(2D/3D)。这是以已知角度(\θ)对齐的。为了获得已知角度的场的旋转,我粗略地尝试以相反的方式旋转它。以下是代码:

代码语言:javascript
复制
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的答案解决了问题

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-04 03:17:00

检查scipy.ndimage.rotate

代码语言:javascript
复制
rot_image = spimg.rotate(image,theta, reshape=False, order=1)

您可以调整参数以适合您的旋转插值。希望它能起作用

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54027970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档