opencv在viz有4个例程
Launching Viz
Pose of a widget
Transformations
Creating Widgets
其中前3个比较通用
下面就逐一介绍一下
有些地方写的不太好 我已经修改了代码
1. Launching Viz
知识点
#include
#include
using namespace cv;
using namespace std;
static void help()
{
cout
<< "--------------------------------------------------------------------------" << endl
<< "This program shows how to launch a 3D visualization window. You can stop event loop to continue executing. "
<< "You can access the same window via its name. You can run event loop for a given period of time. " << endl
<< "Usage:" << endl
<< "./launching_viz" << endl
<< endl;
}
int main()
{
help();
viz::Viz3d myWindow("Viz Demo");
cout << "This event loop will run until user terminates it by pressing e, E, q, Q." << endl;
myWindow.spin();
cout << "First event loop is over" << endl;
viz::Viz3d sameWindow = viz::getWindowByName("Viz Demo");
sameWindow.spin();
cout << "Second event loop is over" << endl;
sameWindow.spinOnce(1, true);
while (!sameWindow.wasStopped())
{
sameWindow.spinOnce(1, true);
}
cout << "Last event loop is over" << endl;
return 0;
}
这里的注意点是
viz::Viz3d 窗口的关闭 使用按键e,E,q,Q
2. Pose of a widget
知识点
#include
// #include
#include
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(0, 0) += CV_PI * 0.01f;
rot_vec.at(0, 1) += CV_PI * 0.01f;
rot_vec.at(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));
Affine3f pose(rot_vec, Vec3f(translation, translation, translation));
myWindow.setWidgetPose("Cube Widget", pose);
myWindow.spinOnce(1, true);
}
return 0;
}
因为这里使用 cv::Affine3f 这个类型作为非齐次的转换矩阵Trasform 3X4 传入到setWidgetPose
由于cv::Affine3f初始化是可以直接使用旋转向量和平移向量的 所以我这里去除了Rodrigues方程 也去除了包含
3. Transformations
知识点
#include
#include
using namespace cv;
using namespace std;
static void help()
{
cout
<< "--------------------------------------------------------------------------" << endl
<< "This program shows how to use makeTransformToGlobal() to compute required pose,"
<< "how to use makeCameraPose and Viz3d::setViewerPose. You can observe the scene "
<< "from camera point of view (C) or global point of view (G)" << endl
<< "Usage:" << endl
<< "./transformations [ G | C ]" << endl
<< endl;
}
int main(int argn, char **argv)
{
help();
if (argn < 2)
{
cout << "Missing arguments." << endl;
return 1;
}
bool camera_pov = (argv[1][0] == 'C');
viz::Viz3d myWindow("Coordinate Frame");
myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());
Vec3f 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);
Affine3f cam_pose = viz::makeCameraPose(cam_pos, cam_focal_point, cam_y_dir);
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);
Mat bunny_cloud = viz::readCloud("bunny.ply");
bunny_cloud *= 5.f;
viz::WCloud cloud_widget(bunny_cloud, viz::Color::green());
Affine3f cloud_pose = Affine3f().translate(Vec3f(0.0f, 0.0f, 3.0f));
Affine3f cloud_pose_global = transform * cloud_pose;
if (!camera_pov)
{
viz::WCameraPosition cpw(0.5); // Coordinate axes
viz::WCameraPosition cpw_frustum(Vec2f(0.889484f, 0.523599f)); // Camera frustum
myWindow.showWidget("CPW", cpw, cam_pose);
myWindow.showWidget("CPW_FRUSTUM", cpw_frustum, cam_pose);
}
myWindow.showWidget("bunny", cloud_widget, cloud_pose_global);
if (camera_pov)
myWindow.setViewerPose(cam_pose);
myWindow.spin();
return 0;
}
原文中使用了自己写的函数读取点云
我这里使用了原生的 viz::readCloud