【问题标题】:Rotated image coordinates after scipy.ndimage.interpolation.rotate?scipy.ndimage.interpolation.rotate 之后的旋转图像坐标?
【发布时间】:2017-10-11 06:13:44
【问题描述】:

我有一个从 FITS 文件中读取的图像的 numpy 数组。我使用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

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

【问题讨论】:

  • 您能否提供一个(或多个)示例来测试我们的解决方案的正确性?
  • 我刚刚添加了一个使用 scipy 自己的杂项浣熊脸和左眼原始 (x,y) 点的示例。旋转 66 度后框架中的新 (xrot,yrot) 应指向左眼。

标签: python numpy matplotlib scipy rotation


【解决方案1】:

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

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()

【讨论】:

  • 不错的解决方案!只是想你想知道,source code for ndimage.rotate 以 0.5 的递减量计算图像的中心。
  • @unutbu 我从微观上不这么认为,但你当然是对的。 that would matter 的图像无论如何都不太适合旋转,并且新的 matplotlib 版本中的散布字形无论如何都会偏离几个像素。无论如何,我更新了解决方案。
猜你喜欢
  • 2017-07-21
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
  • 2020-05-27
  • 1970-01-01
  • 2013-05-10
相关资源
最近更新 更多