旋转 NumPy 二维数组
我有灰度图像作为 2D NumPy 数组。我需要围绕它们内部不同浮动角度的一点旋转。旋转不需要到位,我允许插值。
我想使用 NumPy 但也允许单步进入/退出。我尝试使用 PIL Image.rotate(theta)
但不明白如何将其应用于我的数组以及如何取回数组。
I have greyscale images as 2D NumPy arrays. I need to rotate around one point inside them of different float angles. The rotation doesn't need to be in place and I allow interpolation.
I'd like to use NumPy but also allow for step in/out. I tried using PIL Image.rotate(theta)
but don't understand how to apply that to my arrays and how to get an array back.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑
scipy.ndimage.interpolation.shift()
和rotate()
用于 2D numpy 数组的插值平移和旋转。Consider
scipy.ndimage.interpolation.shift()
androtate()
for interpolated translations and rotations of 2D numpy arrays.这些操作在维基百科的转换矩阵页面上进行了描述。输出
P' = R*P
,其中P'
是输出点,R
是包含旋转正弦和余弦的 2x2 变换矩阵角度,P
是输入点。如果您想围绕原点以外的其他位置旋转,请在旋转之前移动原点:P' = T + R*(PT)
,其中T
是平移坐标。基本矩阵运算不进行插值,因此如果您不使用 基于 NumPy 的图像处理库 您需要进行反向变换:对于每个整数输出坐标找到浮点坐标将旋转到其中的点的值,并从周围像素插入该输入点的值。
The operations are described on Wikipedia's Transformation matrix page. The output
P' = R*P
whereP'
is the output point,R
is the 2x2 transformation matrix containing sine and cosine of the rotation angle, andP
is the input point. If you want to rotate around something other than the origin then shift origin prior to rotation:P' = T + R*(P-T)
whereT
is the translation coordinate.The basic matrix operations don't do interpolation so if you aren't using a NumPy-based image processing library you'll want to do a reverse transform: for each integer output coordinate find the floating point coordinate of the point that would be rotated into it and interpolate the value of that input point from the surrounding pixels.
示例:
输出:
An example:
Output: