Luc*_*uca 4 python opencv transformation image-processing scikit-image
我在 python 中创建一个旋转矩阵如下:
import numpy as np
def make_rot(angle):
cost = np.cos(np.deg2rad(angle))
sint = np.sin(np.deg2rad(angle))
rot = np.array([[cost, -sint, 0],
[sint, cost, 0],
[0, 0, 1]])
return rot
Run Code Online (Sandbox Code Playgroud)
这是在维基百科页面中定义的:http : //en.wikipedia.org/wiki/Rotation_matrix
我以 45 度的角度参数运行它,我得到类似的结果:
[[ 0.70710678 -0.70710678 0. ]
[ 0.70710678 0.70710678 0. ]
[ 0. 0. 1. ]]
Run Code Online (Sandbox Code Playgroud)
现在,我使用 OpenCV getRotationMatrix2DAPI 如下:
import cv2
M = cv2.getRotationMatrix2D((0, 0), 45, 1)
Run Code Online (Sandbox Code Playgroud)
我得到的矩阵是矩阵的逆矩阵(转置,因为它是一个旋转矩阵)。结果如下:
[[ 0.70710678 0.70710678 0. ]
[-0.70710678 0.70710678 0. ]]
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,它是相反的。我在 OpenCV 文档中没有找到任何关于这种行为的内容。
现在,我可以在 OpenCV 中使用这个矩阵skimage并按如下方式转换图像:
# openCV
M = cv2.getRotationMatrix2D((0, 0), 45, 1)
dst = cv2.warpAffine(image2, M, (coumns, rows))
# skimage
from skimage import transform as tf
tform = tf.AffineTransform(matrix=make_rot(45))
dst = tf.warp(image_2, tform)
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,使用我的矩阵和 OpenCV 矩阵的结果是相同的。
我的问题是为什么 OpenCV 使用逆变换?我想知道这是否是他们在文档中没有提到的内容,或者我是否以某种方式使用了这个错误。
我认为问题在于,在传统的右手坐标系中,旋转矩阵看起来与您使用的完全一样。而在图像(和 OpenCV 处理图像)中,原点在左上角,x 轴向右(像往常一样),但 y 轴向下而不是向上,这意味着坐标系是左手和旋转矩阵是不同的。让我们在这个坐标系中构建旋转矩阵。如果我们表示 A - 线性变换(在我们的例子中是旋转),那么我们得到 A(i)=(cos(angle),-sin(angle)),其中 i - 是对应于 x 轴的基向量;A(j)=(sin(angle),cos(angle)) 其中 j- 是 y 轴的基向量。因此,旋转矩阵看起来与 OpenCV 中的完全一样:A(i) 是它的第一列,A(j)n 是第二列。