imag上的三维旋转

2024-04-28 09:23:17 发布

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

我试图得到一些代码,将执行一个图像的透视转换(在本例中是三维旋转)。

import os.path
import numpy as np
import cv

def rotation(angle, axis):
    return np.eye(3) + np.sin(angle) * skew(axis) \
               + (1 - np.cos(angle)) * skew(axis).dot(skew(axis))

def skew(vec):
    return np.array([[0, -vec[2], vec[1]],
                     [vec[2], 0, -vec[0]],
                     [-vec[1], vec[0], 0]])

def rotate_image(imgname_in, angle, axis, imgname_out=None):
    if imgname_out is None:
        base, ext = os.path.splitext(imgname_in)
        imgname_out = base + '-out' + ext
    img_in = cv.LoadImage(imgname_in)
    img_size = cv.GetSize(img_in)
    img_out = cv.CreateImage(img_size, img_in.depth, img_in.nChannels)
    transform = rotation(angle, axis)
    cv.WarpPerspective(img_in, img_out, cv.fromarray(transform))
    cv.SaveImage(imgname_out, img_out)

当我绕z轴旋转时,一切都按预期工作,但绕x轴或y轴旋转似乎完全不起作用。我需要旋转一个小到pi/200的角度,然后才能得到看起来完全合理的结果。知道怎么回事吗?


Tags: pathinimportimgosdefnpout
2条回答

首先,建立旋转矩阵

    [cos(theta)  -sin(theta)  0]
R = [sin(theta)   cos(theta)  0]
    [0            0           1]

应用此坐标变换可以绕原点旋转。

相反,如果要围绕图像中心旋转,则必须首先移动图像中心 到原点,然后应用旋转,然后将所有内容移回。你可以用 翻译矩阵:

    [1  0  -image_width/2]
T = [0  1  -image_height/2]
    [0  0   1]

然后,用于平移、旋转和反向平移的变换矩阵变为:

H = inv(T) * R * T

我得考虑一下如何把倾斜矩阵和3D变换联系起来。我认为最简单的方法是建立一个4D变换矩阵,然后将其投影回二维齐次坐标。但目前,斜矩阵的一般形式是:

    [x_scale 0       0]
S = [0       y_scale 0]
    [x_skew  y_skew  1]

x_skewy_skew值通常很小(1e-3或更小)。

代码如下:

from skimage import data, transform
import numpy as np
import matplotlib.pyplot as plt

img = data.camera()

theta = np.deg2rad(10)
tx = 0
ty = 0

S, C = np.sin(theta), np.cos(theta)

# Rotation matrix, angle theta, translation tx, ty
H = np.array([[C, -S, tx],
              [S,  C, ty],
              [0,  0, 1]])

# Translation matrix to shift the image center to the origin
r, c = img.shape
T = np.array([[1, 0, -c / 2.],
              [0, 1, -r / 2.],
              [0, 0, 1]])

# Skew, for perspective
S = np.array([[1, 0, 0],
              [0, 1.3, 0],
              [0, 1e-3, 1]])

img_rot = transform.homography(img, H)
img_rot_center_skew = transform.homography(img, S.dot(np.linalg.inv(T).dot(H).dot(T)))

f, (ax0, ax1, ax2) = plt.subplots(1, 3)
ax0.imshow(img, cmap=plt.cm.gray, interpolation='nearest')
ax1.imshow(img_rot, cmap=plt.cm.gray, interpolation='nearest')
ax2.imshow(img_rot_center_skew, cmap=plt.cm.gray, interpolation='nearest')
plt.show()

以及输出:

Rotations of cameraman around origin and center+skew

我不明白你建立旋转矩阵的方法。对我来说似乎很复杂。通常,它是通过构造一个零矩阵,将1放在不需要的轴上,将公共的sincos-cossin放在两个使用的维度中来构建的。然后将所有这些相乘。

你从哪里得到这个np.eye(3) + np.sin(angle) * skew(axis) + (1 - np.cos(angle)) * skew(axis).dot(skew(axis))构造的?

尝试从基本构建块构建投影矩阵。构造一个旋转矩阵是相当容易的,而且“旋转矩阵点偏斜矩阵”应该可以工作。

你可能需要注意旋转中心。您的图像可能位于z轴上的虚拟位置1,因此通过在x或y轴上旋转,它会移动一点。 所以你需要使用一个平移,使z变成0,然后旋转,然后向后平移。(仿射坐标系中的平移矩阵也很简单。见维基百科:https://en.wikipedia.org/wiki/Transformation_matrix

相关问题 更多 >