scipy.ndimage.interpolation.rotate后的旋转图像坐标?

时间:2017-10-10 01:41:30

标签: python numpy matplotlib scipy rotation

我有一个numpy数组用于从FITS文件读入的图像。我使用scipy.ndimage.interpolation.rotate将其旋转了N度。然后我想弄清楚原始非旋转帧中的某些点(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

P.S。以下两个相关问题的答案对我没有帮助:

1 个答案:

答案 0 :(得分: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()

enter image description here