文档结构  
翻译进度:已翻译     翻译赏金:0 元 (?)    ¥ 我要打赏
参与翻译: coyee (5)

目标

本文中你将学到:

  • 如何使用 makeTransformToGlobal 来计算构成
  • 如何使用 makeCameraPose 和 Viz3d::setViewerPose
  • 如何通过轴和查看锥体(frustum)来展示摄像头位置

示例代码

代码下载地址 here.

#include <opencv2/viz/vizcore.hpp>
#include <iostream>
#include <fstream>

using namespace cv;
using namespace std;

/**
 * @function cvcloud_load
 * @brief load bunny.ply
 */
Mat cvcloud_load()
{
    Mat cloud(1, 1889, CV_32FC3);
    ifstream ifs("bunny.ply");

    string str;
    for(size_t i = 0; i < 12; ++i)
        getline(ifs, str);

    Point3f* data = cloud.ptr<cv::Point3f>();
    float dummy1, dummy2;
    for(size_t i = 0; i < 1889; ++i)
        ifs >> data[i].x >> data[i].y >> data[i].z >> dummy1 >> dummy2;

    cloud *= 5.0f;
    return cloud;
}

/**
 * @function main
 */
int main(int argn, char **argv)
{
    if (argn < 2)
    {
        cout << "Usage: " << endl << "./transformations [ G | C ]" << endl;
        return 1;
    }

    bool camera_pov = (argv[1][0] == 'C');

    /// Create a window
    viz::Viz3d myWindow("Coordinate Frame");

    /// Add coordinate axes
    myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());

    /// Let's assume camera has the following properties
    Point3f cam_pos(3.0f,3.0f,3.0f), cam_focal_point(3.0f,3.0f,2.0f), cam_y_dir(-1.0f,0.0f,0.0f);

    /// We can get the pose of the cam using makeCameraPose
    Affine3f cam_pose = viz::makeCameraPose(cam_pos, cam_focal_point, cam_y_dir);

    /// We can get the transformation matrix from camera coordinate system to global using
    /// - makeTransformToGlobal. We need the axes of the camera
    Affine3f transform = viz::makeTransformToGlobal(Vec3f(0.0f,-1.0f,0.0f), Vec3f(-1.0f,0.0f,0.0f), Vec3f(0.0f,0.0f,-1.0f), cam_pos);

    /// Create a cloud widget.
    Mat bunny_cloud = cvcloud_load();
    viz::WCloud cloud_widget(bunny_cloud, viz::Color::green());

    /// Pose of the widget in camera frame
    Affine3f cloud_pose = Affine3f().translate(Vec3f(0.0f,0.0f,3.0f));
    /// Pose of the widget in global frame
    Affine3f cloud_pose_global = transform * cloud_pose;

    /// Visualize camera frame
    if (!camera_pov)
    {
        viz::WCameraPosition cpw(0.5); // Coordinate axes
        viz::WCameraPosition cpw_frustum(Vec2f(0.889484, 0.523599)); // Camera frustum
        myWindow.showWidget("CPW", cpw, cam_pose);
        myWindow.showWidget("CPW_FRUSTUM", cpw_frustum, cam_pose);
    }

    /// Visualize widget
    myWindow.showWidget("bunny", cloud_widget, cloud_pose_global);

    /// Set the viewer pose to that of camera
    if (camera_pov)
        myWindow.setViewerPose(cam_pose);

    /// Start event loop.
    myWindow.spin();

    return 0;
}
第 1 段(可获 2 积分)

解释

该程序所使用的通用结构:

  • 创建一个可视化窗体:
/// Create a window
viz::Viz3d myWindow("Transformations");
  • 从摄像头位置、聚焦点以及 y 方向获取摄像头构成
/// 假设摄像头使用如下属性
Point3f cam_pos(3.0f,3.0f,3.0f), cam_focal_point(3.0f,3.0f,2.0f), cam_y_dir(-1.0f,0.0f,0.0f);

/// 我们可使用 makeCameraPose 来获取摄像头构成
Affine3f cam_pose = viz::makeCameraPose(cam_pos, cam_focal_point, cam_y_dir);
  • 获取摄像头坐标系的轴变换矩阵
第 2 段(可获 2 积分)
/// 我们可以通过 makeTransformToGlobal 获取摄像头轴系统到全局的变换矩阵. 我们需要摄像头的轴
Affine3f transform = viz::makeTransformToGlobal(Vec3f(0.0f,-1.0f,0.0f), Vec3f(-1.0f,0.0f,0.0f), Vec3f(0.0f,0.0f,-1.0f), cam_pos);
  • 从 bunny.ply 文件创建云部件
/// 创建云部件
Mat bunny_cloud = cvcloud_load();
viz::WCloud cloud_widget(bunny_cloud, viz::Color::green());
  • 在摄像头坐标系统中提供轴信息,模拟全局构成.
/// Pose of the widget in camera frame
Affine3f cloud_pose = Affine3f().translate(Vec3f(0.0f,0.0f,3.0f));
/// Pose of the widget in global frame
Affine3f cloud_pose_global = transform * cloud_pose;
第 3 段(可获 2 积分)
  • 如果视图点被设置为全局的,即可实现可视化的摄像头坐标系以及视锥 viewing frustum.
/// 显示摄像头帧
if (!camera_pov)
{
    viz::WCameraPosition cpw(0.5); // Coordinate axes
    viz::WCameraPosition cpw_frustum(Vec2f(0.889484, 0.523599)); // Camera frustum
    myWindow.showWidget("CPW", cpw, cam_pose);
    myWindow.showWidget("CPW_FRUSTUM", cpw_frustum, cam_pose);
}
  • 显示带全局构成模拟的色彩部件
/// Visualize widget
myWindow.showWidget("bunny", cloud_widget, cloud_pose_global);
  • 如果视图点设置为摄像头的,可设置视图构成为 cam_pose.
第 4 段(可获 2 积分)
/// Set the viewer pose to that of camera
if (camera_pov)
    myWindow.setViewerPose(cam_pose);

运行结果

  1. 下图是摄像头视点结果:

    Camera Viewpoint
  2. 下图是全局视点结果:

    Global Viewpoint
第 5 段(可获 2 积分)

文章评论