qua*_*ash 4 python numpy matplotlib rotation scipy
我有一个numpy数组用于从FITS文件读入的图像.我用它旋转了N度scipy.ndimage.interpolation.rotate.然后我想弄清楚原始非旋转帧中的某些点(x,y)在旋转图像中的最终位置 - 即,旋转的帧坐标(x',y')是什么?
这应该是一个非常简单的旋转矩阵问题,但如果我使用通常的基于数学或编程的旋转方程,则新的(x',y')不会最终到达它们原来的位置.我怀疑这与需要翻译矩阵有关,因为scipy旋转函数基于原点(0,0)而不是图像数组的实际中心.
有人可以告诉我如何获得旋转的帧(x',y')?例如,你可以使用
from scipy import misc
from scipy.ndimage import rotate
data_orig = misc.face()
data_rot = rotate(data_orig,66) # data array
x0,y0 = 580,300 # left eye; (xrot,yrot) should point there
Run Code Online (Sandbox Code Playgroud)
PS以下两个相关问题的答案对我没有帮助:
Imp*_*est 10
像往常一样旋转,需要转换到原点,然后旋转,然后转换回来.在这里,我们可以将图像的中心作为原点.
import numpy as np
import matplotlib.pyplot as plt
from scipy import misc
from scipy.ndimage import rotate
data_orig = misc.face()
x0,y0 = 580,300 # left eye; (xrot,yrot) should point there
def rot(image, xy, angle):
im_rot = rotate(image,angle)
org_center = (np.array(image.shape[:2][::-1])-1)/2.
rot_center = (np.array(im_rot.shape[:2][::-1])-1)/2.
org = xy-org_center
a = np.deg2rad(angle)
new = np.array([org[0]*np.cos(a) + org[1]*np.sin(a),
-org[0]*np.sin(a) + org[1]*np.cos(a) ])
return im_rot, new+rot_center
fig,axes = plt.subplots(2,2)
axes[0,0].imshow(data_orig)
axes[0,0].scatter(x0,y0,c="r" )
axes[0,0].set_title("original")
for i, angle in enumerate([66,-32,90]):
data_rot, (x1,y1) = rot(data_orig, np.array([x0,y0]), angle)
axes.flatten()[i+1].imshow(data_rot)
axes.flatten()[i+1].scatter(x1,y1,c="r" )
axes.flatten()[i+1].set_title("Rotation: {}deg".format(angle))
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5685 次 |
| 最近记录: |