如何根据相机校准数据绘制相机和图像位置?

发布于 2024-12-16 20:26:34 字数 757 浏览 0 评论 0原文

我有相机的内在和外在参数。 外部是一个具有旋转和平移的 4 x 4 矩阵。

我有如下示例数据,每张相机拍摄的图像都有一个。

2.11e-001 -3.06e-001 -9.28e-001 7.89e-001 
6.62e-001 7.42e-001 -9.47e-002 1.47e-001 
7.18e-001 -5.95e-001 3.60e-001 3.26e+000 
0.00e+000 0.00e+000 0.00e+000 1.00e+000

我想绘制 Matlab 校准工具包页面上给出的图像< /a> 相机居中以世界为中心 然而,我无法计算出如何绘制这两张图像的数学原理。

我唯一的线索来自此页面http://en.wikipedia.org/wiki/Camera_resectioning。这告诉我相机位置可以通过 C = − R` 找到。知道

如何完成这项任务吗?

I have the intrisic and extrinsic parameters of the camera.
The extrinsic is a 4 x 4 matrix with rotation and translation.

I have sample data as under, I have this one per camera image taken.

2.11e-001 -3.06e-001 -9.28e-001 7.89e-001 
6.62e-001 7.42e-001 -9.47e-002 1.47e-001 
7.18e-001 -5.95e-001 3.60e-001 3.26e+000 
0.00e+000 0.00e+000 0.00e+000 1.00e+000

I would like to plot the image as given on the Matlab calibration toolkit page Camera Centered or World centered
However I'm unable to figure out the Math of how to plot these 2 images.

The only lead I have is from this page http://en.wikipedia.org/wiki/Camera_resectioning. Which tells me that the camera position can be found by C = − R` . T

Any idea how to achieve this task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

祁梦 2024-12-23 20:26:34

假设要绘制的平面的角点是 3x1 列向量,a = [0 0 0]'、b = [w 0 0]'、c = [wh 0]' 和 d = [0 h 0]' 。

假设您提供的校准矩阵为 A,并由旋转矩阵 R = A(1:3, 1:3) 和平移矩阵 T = A(1:3, 4) 组成。

绘制第一个视图
将平面的每个角 x_w(即 a、b、c 或 d)变换到相机中的坐标 x_c

对于具有旋转 R_i 和平移 T_i 的每个姿势 A_i,通过x_c = R_i*x_w + T_i

然后用变换后的平面绘制平面角落。

绘制相机时,其在相机坐标中的投影中心为[0 0 0]',相机x轴为[1 0 0]',y轴为[0 1 0]',z轴为[0 0 1] '。

请注意,在图中,相机 y 轴朝下,因此您可能需要通过乘以 B = [1 0 0; 对所有计算出的坐标应用额外的旋转。 0 0 1; 0 -1 0]。

绘制第二个视图
绘制平面很简单,因为我们处于世界坐标中。只需使用 a、b、c 和 d 绘制平面即可。

为了绘制摄像机,每个摄像机中心为 c = -R'*T。相机轴是旋转矩阵 R 的行,因此例如,在您提供的矩阵中,x 轴是
[2.11e-001 -3.06e-001 -9.28e-001]'。您还可以通过将相机坐标中给出的每个点 x_c 通过 x_w = R'*(x_c - T) 转换为世界坐标 x_w 并绘制它来绘制相机。

Assume the corners of the plane that you want to draw are 3x1 column vectors, a = [0 0 0]', b = [w 0 0]', c = [w h 0]' and d = [0 h 0]'.

Assume that the calibration matrix that you provide is A and consists of a rotation matrix R = A(1:3, 1:3) and a translation T = A(1:3, 4).

To draw the first view
For every pose A_i with rotation R_i and translation T_i, transform each corner x_w (that is a, b, c or d) of the plane to its coordinates x_c in the camera by

x_c = R_i*x_w + T_i

Then draw the plane with transformed corners.

To draw the camera, its centre of projection in camera coordinates is [0 0 0]' and the camera x axis is [1 0 0]', y axis is [0 1 0]' and z axis is [0 0 1]'.

Note that in the drawing, the camera y-axis is pointing down, so you might want to apply an additional rotation on all the computed coordinates by multiplication with B = [1 0 0; 0 0 1; 0 -1 0].

Draw the second view
Drawing the plane is trivial since we are in world coordinates. Just draw the plane using a, b, c and d.

To draw the cameras, each camera centre is c = -R'*T. The camera axes are the rows of the rotation matrix R, so for instance, in the matrix you provided, the x-axis is
[2.11e-001 -3.06e-001 -9.28e-001]'. You can also draw a camera by transforming each point x_c given in camera coordinates to world coordinates x_w by x_w = R'*(x_c - T) and draw it.

妞丶爷亲个 2024-12-23 20:26:34

opencv 现在有一个示例,用于可视化外部从他们的 相机校准生成example

它输出类似于原始问题的内容:
**以相机为中心的可视化**

There is now an example in opencv for visualizing the extrinsics generated from their camera calibration example

It outputs something similar to the original questions ask:
**camera centric viz**

假情假意假温柔 2024-12-23 20:26:34

这就是绘制摄像机的方法。
红色箭头是相机方向(-z 或 z),蓝色箭头是相机方向。所有示例摄像机都指向 [0,0,0]。

代码输出图像

代码:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

def plot_cameras(extrinsic_matrices):
    """
    Visualizes the extrinsics of multiple cameras in 3D space.

    Parameters:
    - extrinsic_matrices (numpy.ndarray): Array of camera extrinsics matrices (Nx4x4).
    """
    ax = plt.figure().add_subplot(projection='3d')

    for camera_extrinsics in extrinsic_matrices:
        # Extract translation and rotation from camera extrinsics matrix
        translation = camera_extrinsics[:3, 3]
        rotation_matrix = camera_extrinsics[:3, :3]

        # Plot camera position
        ax.scatter(*translation, marker='o')

        # Plot camera orientation axes
        origin = translation
        for i in range(3):
            axis_direction = rotation_matrix[:,i] 
            if i == 0:
                ax.quiver(*origin, *axis_direction, length=0.5, normalize=True)
            else:
                ax.quiver(*origin, *axis_direction, length=1, normalize=True)
        # Plot camera direction
        z = -1 * rotation_matrix[:,2]
        ax.quiver(*origin, *z, length=1, normalize=True, color='r', alpha=0.5)

    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    ax.set_title('Multiple Cameras Extrinsics Visualization')

    ax.set_zlim(-2,2)

    plt.show()


# List of extrinsic matrices or poses
# Example poses all pointing at [0,0,0]
poses = np.array([[
        [-9.9990219e-01,  4.1922452e-03, -1.3345719e-02, -5.3798322e-02],
        [-1.3988681e-02, -2.9965907e-01,  9.5394367e-01,  3.8454704e+00],
        [-4.6566129e-10,  9.5403719e-01,  2.9968831e-01,  1.2080823e+00],
        [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00,  1.0000000e+00]],

       [[-9.3054223e-01,  1.1707554e-01, -3.4696460e-01, -1.3986591e+00],
        [-3.6618456e-01, -2.9751042e-01,  8.8170075e-01,  3.5542498e+00],
        [ 7.4505806e-09,  9.4751304e-01,  3.1971723e-01,  1.2888215e+00],
        [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00,  1.0000000e+00]],

       [[ 4.4296363e-01,  3.1377721e-01, -8.3983749e-01, -3.3854935e+00],
        [-8.9653969e-01,  1.5503149e-01, -4.1494811e-01, -1.6727095e+00],
        [ 0.0000000e+00,  9.3675458e-01,  3.4998694e-01,  1.4108427e+00],
        [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00,  1.0000000e+00]],

       [[ 7.9563183e-01,  5.2092606e-01, -3.0920234e-01, -1.2464346e+00],
        [-6.0578054e-01,  6.8418401e-01, -4.0610620e-01, -1.6370665e+00],
        [-1.4901161e-08,  5.1041979e-01,  8.5992539e-01,  3.4664700e+00],
        [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00,  1.0000000e+00]],

       [[-6.5665424e-01, -2.3678228e-01,  7.1605819e-01,  2.8865230e+00],
        [ 7.5419182e-01, -2.0615987e-01,  6.2345237e-01,  2.5132170e+00],
        [-1.4901163e-08,  9.4943780e-01,  3.1395501e-01,  1.2655932e+00],
        [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00,  1.0000000e+00]]])

plot_cameras(poses)

解释为什么 Z 是相机指向的位置。

This is how you can plot the cameras.
Red arrows are camera directions (-z or z), blue arrows are cameras orientations. All of example cameras point at [0,0,0].

code output image

Code:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

def plot_cameras(extrinsic_matrices):
    """
    Visualizes the extrinsics of multiple cameras in 3D space.

    Parameters:
    - extrinsic_matrices (numpy.ndarray): Array of camera extrinsics matrices (Nx4x4).
    """
    ax = plt.figure().add_subplot(projection='3d')

    for camera_extrinsics in extrinsic_matrices:
        # Extract translation and rotation from camera extrinsics matrix
        translation = camera_extrinsics[:3, 3]
        rotation_matrix = camera_extrinsics[:3, :3]

        # Plot camera position
        ax.scatter(*translation, marker='o')

        # Plot camera orientation axes
        origin = translation
        for i in range(3):
            axis_direction = rotation_matrix[:,i] 
            if i == 0:
                ax.quiver(*origin, *axis_direction, length=0.5, normalize=True)
            else:
                ax.quiver(*origin, *axis_direction, length=1, normalize=True)
        # Plot camera direction
        z = -1 * rotation_matrix[:,2]
        ax.quiver(*origin, *z, length=1, normalize=True, color='r', alpha=0.5)

    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    ax.set_title('Multiple Cameras Extrinsics Visualization')

    ax.set_zlim(-2,2)

    plt.show()


# List of extrinsic matrices or poses
# Example poses all pointing at [0,0,0]
poses = np.array([[
        [-9.9990219e-01,  4.1922452e-03, -1.3345719e-02, -5.3798322e-02],
        [-1.3988681e-02, -2.9965907e-01,  9.5394367e-01,  3.8454704e+00],
        [-4.6566129e-10,  9.5403719e-01,  2.9968831e-01,  1.2080823e+00],
        [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00,  1.0000000e+00]],

       [[-9.3054223e-01,  1.1707554e-01, -3.4696460e-01, -1.3986591e+00],
        [-3.6618456e-01, -2.9751042e-01,  8.8170075e-01,  3.5542498e+00],
        [ 7.4505806e-09,  9.4751304e-01,  3.1971723e-01,  1.2888215e+00],
        [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00,  1.0000000e+00]],

       [[ 4.4296363e-01,  3.1377721e-01, -8.3983749e-01, -3.3854935e+00],
        [-8.9653969e-01,  1.5503149e-01, -4.1494811e-01, -1.6727095e+00],
        [ 0.0000000e+00,  9.3675458e-01,  3.4998694e-01,  1.4108427e+00],
        [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00,  1.0000000e+00]],

       [[ 7.9563183e-01,  5.2092606e-01, -3.0920234e-01, -1.2464346e+00],
        [-6.0578054e-01,  6.8418401e-01, -4.0610620e-01, -1.6370665e+00],
        [-1.4901161e-08,  5.1041979e-01,  8.5992539e-01,  3.4664700e+00],
        [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00,  1.0000000e+00]],

       [[-6.5665424e-01, -2.3678228e-01,  7.1605819e-01,  2.8865230e+00],
        [ 7.5419182e-01, -2.0615987e-01,  6.2345237e-01,  2.5132170e+00],
        [-1.4901163e-08,  9.4943780e-01,  3.1395501e-01,  1.2655932e+00],
        [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00,  1.0000000e+00]]])

plot_cameras(poses)

Explanation why Z is where camera is pointing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文