Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 图像的三维旋转_Python_Image Processing_Opencv - Fatal编程技术网

Python 图像的三维旋转

Python 图像的三维旋转,python,image-processing,opencv,Python,Image Processing,Opencv,我正在尝试获取一些代码,这些代码将对图像执行透视变换(在本例中为3d旋转) 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): re

我正在尝试获取一些代码,这些代码将对图像执行透视变换(在本例中为3d旋转)

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的角度,然后才能得到合理的结果。你知道哪里不对吗?

我不明白你建立旋转矩阵的方法。这对我来说似乎相当复杂。通常,它将通过构造一个零矩阵来构建,将
1
放在不需要的轴上,并将公共
sin
cos
-cos
sin
放入两个使用的维度中。然后把这些加起来

你从哪里得到的
np.eye(3)+np.sin(角度)*skew(轴)+(1-np.cos(角度))*skew(轴)、dot(轴)的构造

尝试从基本构建块构建投影矩阵。构造旋转矩阵相当容易,“旋转矩阵-点-斜矩阵”应该可以工作

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

首先,构建旋转矩阵,形式如下

    [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_-skew
y_-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()
以及输出: