codecamp

Pose of a widget

目标

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

  • 将窗口小部件添加到可视化窗口
  • 使用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);
}
启动Viz
OpenCV Viz转换
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

OpenCV教程

OpenCV高级GUI和媒体(highgui模块)

OpenCV图像输入和输出(imgcodecs模块)

对象检测(objdetect模块)

计算摄影(照片模块)

图像拼接(拼接模块)

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }