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

2024-05-14 05:49:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个numpy数组,用于读取FITS文件中的图像。我用scipy.ndimage.interpolation.rotate把它旋转了N度。然后我想找出原始非旋转帧中的某个点(x,y)在旋转图像中的结束位置——即,旋转的帧坐标(x,y’)是什么?

这应该是一个非常简单的旋转矩阵问题,但如果我做通常的数学或基于编程的旋转方程,新的(x',y')不会在原来的地方结束。我怀疑这也与需要一个平移矩阵有关,因为scipy rotate函数是基于原点(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

另外,以下两个相关问题的答案对我没有帮助:


Tags: from图像importdata矩阵scipy数组point
1条回答
网友
1楼 · 发布于 2024-05-14 05:49:29

与通常的旋转一样,需要先平移到原点,然后旋转,然后再向后平移。在这里,我们可以把图像的中心作为原点。

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

相关问题 更多 >