OpenCV Viz转换
2018-10-27 10:47 更新
目标
在本教程中,您将学习如何
- 如何使用makeTransformToGlobal来计算pose
- 如何使用makeCameraPose和Viz3d :: setViewerPose
如何用轴和视锥显示相机位置
Code
你可以从这里下载代码。
#include <opencv2/viz.hpp>
#include <iostream>
#include <fstream>
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;
}
static 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;
}
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 = cvcloud_load();
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.889484, 0.523599)); // 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 :: 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);
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);
- 从bunny.ply文件创建一个云小部件
Mat bunny_cloud = cvcloud_load();
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.889484, 0.523599)); // Camera frustum
myWindow.showWidget("CPW", cpw, cam_pose);
myWindow.showWidget("CPW_FRUSTUM", cpw_frustum, cam_pose);
}
- 使用估计的全局姿态可视化云小部件
myWindow.showWidget(“bunny”,cloud_widget,cloud_pose_global);
- 如果视点设置为相机,请将观看者姿态设置为cam_pose。
if (camera_pov)
myWindow.setViewerPose(cam_pose);
结果
这是从相机视图点的结果。
这是全局视野的结果。
以上内容是否对您有帮助:
更多建议: