旋转 NumPy 二维数组

发布于 2024-12-14 22:50:36 字数 161 浏览 0 评论 0原文

我有灰度图像作为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还在原地等你 2024-12-21 22:50:36

考虑 scipy.ndimage.interpolation.shift()rotate() 用于 2D numpy 数组的插值平移和旋转。

Consider scipy.ndimage.interpolation.shift() and rotate() for interpolated translations and rotations of 2D numpy arrays.

不忘初心 2024-12-21 22:50:36

这些操作在维基百科的转换矩阵页面上进行了描述。输出 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 where P' is the output point, R is the 2x2 transformation matrix containing sine and cosine of the rotation angle, and P 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) where T 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.

彡翼 2024-12-21 22:50:36

示例:

import pandas as pd
import numpy as np
bd = np.matrix([[44., -1., 40., 42., 40., 39., 37., 36., -1.],
                [42., -1., 43., 42., 39., 39., 41., 40., 36.],
                [37., 37., 37., 35., 38., 37., 37., 33., 34.],
                [35., 38., -1., 35., 37., 36., 36., 35., -1.],
                [36., 35., 36., 35., 34., 33., 32., 29., 28.],
                [38., 37., 35., -1., 30., -1., 29., 30., 32.]])

def rotate45(array):
    rot = []
    for i in range(len(array)):
        rot.append([0] * (len(array)+len(array[0])-1))
        for j in range(len(array[i])):
            rot[i][int(i + j)] = array[i][j]
    return rot

df_bd = pd.DataFrame(data=np.matrix(rotate45(bd.transpose().tolist())))
df_bd = df_bd.transpose()
print df_bd

输出:

44   0   0   0   0   0   0   0   0
42  -1   0   0   0   0   0   0   0
37  -1  40   0   0   0   0   0   0
35  37  43  42   0   0   0   0   0
36  38  37  42  40   0   0   0   0
38  35  -1  35  39  39   0   0   0
0   37  36  35  38  39  37   0   0
0    0  35  35  37  37  41  36   0
0    0   0  -1  34  36  37  40  -1
0    0   0   0  30  33  36  33  36
0    0   0   0   0  -1  32  35  34
0    0   0   0   0   0  29  29  -1
0    0   0   0   0   0   0  30  28
0    0   0   0   0   0   0   0  32

An example:

import pandas as pd
import numpy as np
bd = np.matrix([[44., -1., 40., 42., 40., 39., 37., 36., -1.],
                [42., -1., 43., 42., 39., 39., 41., 40., 36.],
                [37., 37., 37., 35., 38., 37., 37., 33., 34.],
                [35., 38., -1., 35., 37., 36., 36., 35., -1.],
                [36., 35., 36., 35., 34., 33., 32., 29., 28.],
                [38., 37., 35., -1., 30., -1., 29., 30., 32.]])

def rotate45(array):
    rot = []
    for i in range(len(array)):
        rot.append([0] * (len(array)+len(array[0])-1))
        for j in range(len(array[i])):
            rot[i][int(i + j)] = array[i][j]
    return rot

df_bd = pd.DataFrame(data=np.matrix(rotate45(bd.transpose().tolist())))
df_bd = df_bd.transpose()
print df_bd

Output:

44   0   0   0   0   0   0   0   0
42  -1   0   0   0   0   0   0   0
37  -1  40   0   0   0   0   0   0
35  37  43  42   0   0   0   0   0
36  38  37  42  40   0   0   0   0
38  35  -1  35  39  39   0   0   0
0   37  36  35  38  39  37   0   0
0    0  35  35  37  37  41  36   0
0    0   0  -1  34  36  37  40  -1
0    0   0   0  30  33  36  33  36
0    0   0   0   0  -1  32  35  34
0    0   0   0   0   0  29  29  -1
0    0   0   0   0   0   0  30  28
0    0   0   0   0   0   0   0  32
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文