我有以下图像 I1。我没有捕捉到它。我从谷歌下载的
我将已知的单应性 h 应用到 I1 以获得以下图像 I2。
我想假设相机拍摄了上面这张 I2 的照片。我找到了这个“相机”的相机矩阵。令这个相机矩阵为k
。现在,我想围绕相机轴旋转该图像 I2。根据已接受答案中的解释这个问题 https://stackoverflow.com/questions/47990350/python-rotate-image-about-camera-axis,我需要设置旋转矩阵R然后执行k*R*inv(k)*h
在图像 I1 上得到所需的旋转图像 I3。
当我尝试设置这个旋转矩阵 R 时,我一直遇到问题。我使用过这个方法 https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle设置矩阵 R。
为了测试我的代码,我最初尝试将图像绕 z 轴旋转 10 度,但没有得到正确的输出。
我的部分Python代码:
theta_in_degrees = 10
theta_in_radians = theta_in_degrees*math.pi/180
ux=0.0
uy=0.0
uz=1.0
vector_normalize_factor = math.sqrt(ux*ux+uy*uy+uz*uz)
ux=ux/vector_normalize_factor
uy=uy/vector_normalize_factor
uz=uz/vector_normalize_factor
print "ux*ux+uy*uy+uz*uz = ", ux*ux+uy*uy+uz*uz
rotation_matrix = np.zeros([3,3])
c1 = math.cos(theta_in_radians)
c2 = 1-c1
s1 = math.sin(theta_in_radians)
rotation_matrix[0][0] = c1+ux*ux*c2
rotation_matrix[0][1] = ux*uy*c2-uz*s1
rotation_matrix[0][2] = ux*uz*c2+uy*s1
rotation_matrix[1][0] = uy*ux*c2+uz*s1
rotation_matrix[1][1] = c1+uy*uy*c2
rotation_matrix[1][2] = uy*uz*c2-ux*s1
rotation_matrix[2][0] = uz*ux*c2-uy*s1
rotation_matrix[2][1] = uz*uy*c2+ux*s1
rotation_matrix[2][2] = c1+uz*uz*c2
print "rotation_matrix = ", rotation_matrix
R = rotation_matrix
#Calculate homography H1 between reference top view and rotated frame
k_inv = np.linalg.inv(k)
Hi = k.dot(R)
Hii = k_inv.dot(h)
H1 = Hi.dot(Hii)
print "H1 = ", H1
im_out = cv2.warpPerspective(im_src, H1, (im_dst.shape[1],im_dst.shape[0]))
Here, img_src
是 I1 的来源。
当我尝试上面的代码时得到的结果是一个黑色图像,图像的任何部分都不可见。但是,当我改变值时theta_in_degrees
到以下值,这些是我的输出:
0.00003
0.00006
0.00009
为什么旋转仅适用于如此小的值theta_in_degrees
?此外,图像中可见的旋转实际上并不是围绕 z 轴发生的。为什么图像不绕 z 轴旋转?我哪里出了问题以及如何解决这些问题?
h 矩阵:
[[ 1.71025842e+00 -7.51761942e-01 1.02803446e+02]
[ -2.98552735e-16 1.39232576e-01 1.62792482e+02]
[ -1.13518150e-18 -2.27094753e-03 1.00000000e+00]]
k矩阵:
[[ 1.41009391e+09 0.00000000e+00 5.14000000e+02]
[ 0.00000000e+00 1.78412347e+02 1.17000000e+02]
[ 0.00000000e+00 0.00000000e+00 1.00000000e+00]]
Edit:
在采纳托比·柯林斯的建议后,我将左上角的值设置为k
与k[1][1]
。现在,当我绕 z 轴执行旋转时,我得到了所有值的正确旋转图像theta_in_degrees
从 0 到 360。但是,当我尝试通过将上面代码中的 ux、uy 和 uz 更改为以下内容来绕 y 轴旋转图像时,我得到了荒谬的旋转结果:
ux=0.0
uy=1.0
uz=0.0
一些不同值的样本theta_in_degrees
绕y轴旋转对应的结果如下所示:
-10
-40
-90
-110
我到底哪里还出错了?另外,为什么旋转图像中连续黄色条纹的长度和宽度会出现如此巨大的下降?以及为什么图像的一部分会环绕(例如旋转-90度和-110度的结果)?
我的问题的第二部分是这样的:我的旋转轴的矢量方程是(320, 0, -10)+t(0, 1, 0)
。为了使用这个方法 https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle,为了计算旋转矩阵,我需要定义ux
, uy
and uz
的旋转轴使得ux^2+uy^2+uz^2=1
。如果需要围绕其中一个坐标轴进行旋转(正如我目前出于测试目的所做的那样),这将很简单。但我如何获得这些值ux
, uy
and uz
if the t
我的旋转轴的矢量方程是变量吗?我也愿意接受有关寻找合适旋转矩阵 R 的任何其他方法的建议,以便旋转围绕我提到的轴发生(例如,x 度)。