阅读(4304) (11)

Pose of a widget

2017-10-27 10:22:57 更新

目标

在本教程中,您将学习如何

  • 将窗口小部件添加到可视化窗口
  • 使用Affine3设置窗口Pose of a widget
  • 旋转和平移沿轴部件

Code

你可以从这里下载代码。


#include <opencv2/viz.hpp>
#include <opencv2/calib3d.hpp>
#include <iostream>
using namespace cv;
using namespace std;
static void help()
{
    cout
    << "--------------------------------------------------------------------------"   << endl
    << "This program shows how to visualize a cube rotated around (1,1,1) and shifted "
    << "using Rodrigues vector."                                                      << endl
    << "Usage:"                                                                       << endl
    << "./widget_pose"                                                                << endl
    << endl;
}
int main()
{
    help();
    viz::Viz3d myWindow("Coordinate Frame");
    myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());
    viz::WLine axis(Point3f(-1.0f,-1.0f,-1.0f), Point3f(1.0f,1.0f,1.0f));
    axis.setRenderingProperty(viz::LINE_WIDTH, 4.0);
    myWindow.showWidget("Line Widget", axis);
    viz::WCube cube_widget(Point3f(0.5,0.5,0.0), Point3f(0.0,0.0,-0.5), true, viz::Color::blue());
    cube_widget.setRenderingProperty(viz::LINE_WIDTH, 4.0);
    myWindow.showWidget("Cube Widget", cube_widget);
    Mat rot_vec = Mat::zeros(1,3,CV_32F);
    float translation_phase = 0.0, translation = 0.0;
    while(!myWindow.wasStopped())
    {
        /* Rotation using rodrigues */
        rot_vec.at<float>(0,0) += CV_PI * 0.01f;
        rot_vec.at<float>(0,1) += CV_PI * 0.01f;
        rot_vec.at<float>(0,2) += CV_PI * 0.01f;
        translation_phase += CV_PI * 0.01f;
        translation = sin(translation_phase);
        Mat rot_mat;
        Rodrigues(rot_vec, rot_mat);
        Affine3f pose(rot_mat, Vec3f(translation, translation, translation));
        myWindow.setWidgetPose("Cube Widget", pose);
        myWindow.spinOnce(1, true);
    }
    return 0;
}

说明

这是程序的一般结构:

  • 创建一个可视化窗口。
viz :: Viz3d myWindow(“Coordinate Frame”);

  • 使用CoordinateSystemWidget在窗口中显示坐标轴。

myWindow.showWidget(“Coordinate Widget”,viz :: WCoordinateSystem());

  • 显示一条表示轴(1,1,1)的线。

viz :: WLine axis(Point3f(-1.0f,-1.0f,-1.0f),Point3f(1.0f,1.0f,1.0f));
axis.setRenderingProperty(viz :: LINE_WIDTH,4.0);
myWindow.showWidget(“Line Widget”,axis);

  • 建立方体

viz :: WCube cube_widget(Point3f(0.5,0.5,0.0),Point3f(0.0,0.0,-0.5),true,viz :: Color :: blue());
cube_widget.setRenderingProperty(viz :: LINE_WIDTH,4.0);
myWindow.showWidget(“Cube Widget”,cube_widget);

从rodrigues向量创建旋转矩阵

rot_vec.at<float>(0,0) += CV_PI * 0.01f;
rot_vec.at<float>(0,1) += CV_PI * 0.01f;
rot_vec.at<float>(0,2) += CV_PI * 0.01f;
...
Mat rot_mat;
Rodrigues(rot_vec, rot_mat);

  • 使用Affine3f设置立方体的形态。

Affine3f pose(rot_mat, Vec3f(translation, translation, translation));
myWindow.setWidgetPose("Cube Widget", pose);

  • 使用wasStopped和spinOnce动画旋转

while(!myWindow.wasStopped())
{
    ...
    myWindow.spinOnce(1,true);
}