截至目前为止,在下使用opencv4年有余,曾在读研期间写了一个viz模块显示slam的3D轨迹教学,
链接:https://www.cnblogs.com/winslam/p/9244598.html
前几天在度娘搜索opencv viz居然发现有个老哥的blog引用了我的blog,
链接:https://blog.csdn.net/wq1psa78/article/details/103773469
初看代码,感觉很熟悉,翻到最底下,居然还附上了我博客链接,这年头。。。这样有素质的人不多了。
中文博客里面有关opencv viz模块的详细教程几乎没有。我这里给出一个详细教程。
opencv viz模块其实依赖VTK。
点云库也可以显示以上控件(点、线、面、球、箭头、2D文本、3D文本、点云等等,其余的请参考相关手册,查看构造函数)
1 #include <iostream> 2 #include <opencv2/viz.hpp> 3 #include <opencv2/highgui.hpp> 4 #include <opencv2/calib3d.hpp> 5 6 using namespace std; 7 using namespace cv; 8 9 int main() 10 { 11 // window 12 viz::Viz3d window("window"); 13 window.showWidget("Coordinate", viz::WCoordinateSystem()); 14 window.setBackgroundColor(viz::Color(186, 36, 1)); 15 16 // plane 17 viz::WPlane plane(Size2d(2, 2), viz::Color(13, 255, 39)); 18 plane.setRenderingProperty(viz::LINE_WIDTH, 5); 19 plane.setPose(Affine3f()); 20 window.showWidget("plane", plane); 21 22 // line 23 viz::WLine line(Point3d(0.0, 0.0, 0.0), Point3d(10.0, 10.0, 10.0), viz::Color::yellow()); 24 line.setRenderingProperty(viz::LINE_WIDTH, 2); 25 //line.setPose(Affine3f()); 26 window.showWidget("line", line); 27 28 // WSphere 29 viz::WSphere sphere(Point3d(1, 4, 3), 2, 1, viz::Color::blue()); 30 sphere.setRenderingProperty(viz::LINE_WIDTH, 1); 31 window.showWidget("sphere", sphere); 32 33 // WArrow 34 viz::WArrow warrow(Point3d(1, 1, 1), Point3d(-2, 8, -2), 0.03, viz::Color::red()); 35 warrow.setRenderingProperty(viz::LINE_WIDTH, 2); 36 window.showWidget("warrow", warrow); 37 38 // WCircle 39 viz::WCircle wcricle(3.0, 0.02, viz::Color::navy()); 40 wcricle.setRenderingProperty(viz::LINE_WIDTH, 2); 41 window.showWidget("wcricle", wcricle); 42 43 // WPolyLine (将点集自动转为一根线显示,适合SLAM显示轨迹) 44 vector<Point3d> points3d; 45 for (int i = 0; i < 5; i++) 46 { 47 points3d.push_back(Point3d(i, i*i, 2 * i)); 48 } 49 50 viz::WPolyLine wpolyLine(points3d, viz::Color::rose()); 51 wpolyLine.setRenderingProperty(viz::LINE_WIDTH, 2); 52 window.showWidget("wpolyLine", wpolyLine); 53 54 // WPaintedCloud 55 vector<Point3d> pointCloud; 56 // 创建点云 57 pointCloud.resize(points3d.size()); 58 for (size_t i = 0; i < points3d.size(); i++) 59 { 60 pointCloud[i] = points3d[i] * 3; 61 } 62 viz::WPaintedCloud wpaintedCloud(pointCloud); 63 wpaintedCloud.setRenderingProperty(viz::POINT_SIZE, 4); 64 window.showWidget("wpaintedCloud", wpaintedCloud); 65 66 // WGrid 67 viz::WGrid wgrid(Vec2i::all(10), Vec2d::all(1.0), viz::Color::white()); 68 window.showWidget("wgrid", wgrid); 69 70 // WCube 71 viz::WCube wcube(Vec3d::all(-5), Vec3d::all(5), true, viz::Color::cherry()); 72 wcube.setRenderingProperty(viz::LINE_WIDTH, 3); 73 window.showWidget("wcube", wcube); 74 75 // WText(2D) 76 viz::WText wtext("OpenCV", Point2i(100, 100), 20, viz::Color::green()); 77 window.showWidget("wtext", wtext); 78 79 // WText3D 80 viz::WText3D wtext3d("OpenCV", Point3d(6, 5, 5), 4.1, true, viz::Color::white()); 81 window.showWidget("wtext3d", wtext3d); 82 83 // WTrajectory 84 // Displays a poly line that represents the path. 先要将点进行拟合?? 85 /*viz::WTrajectory wtrajectory(points3d, viz::WTrajectory::PATH, 1.0, viz::Color::green()); 86 wtrajectory.setRenderingProperty(viz::LINE_WIDTH, 3); 87 window.showWidget("wtrajectory", wtrajectory);*/ 88 89 Mat rvec = Mat::zeros(1, 3, CV_32F); 90 while (!window.wasStopped()) 91 { 92 rvec.at<float>(0, 0) = 0.f; 93 rvec.at<float>(0, 1) += CV_PI * 0.01f; 94 rvec.at<float>(0, 2) = 0.f; 95 Mat rmat; 96 Rodrigues(rvec, rmat); 97 Affine3f pose(rmat, Vec3f(0, 0, 0)); 98 99 window.setWidgetPose("plane", pose); 100 101 window.showWidget("line", line); 102 103 window.showWidget("sphere", sphere); 104 105 window.showWidget("warrow", warrow); 106 107 window.showWidget("wcricle", wcricle); 108 109 window.showWidget("wpolyLine", wpolyLine); 110 111 window.showWidget("wpaintedCloud", wpaintedCloud); 112 113 //window.showWidget("wtrajectory", wtrajectory); 114 115 window.showWidget("wgrid", wgrid); 116 117 window.showWidget("wcube", wcube); 118 119 window.showWidget("wtext", wtext); 120 121 window.showWidget("wtext3d", wtext3d); 122 123 window.spinOnce(1, true); 124 } 125 126 window.removeAllWidgets(); 127 return 1; 128 }