Balaji_R

OpenCV Viz Camera Movement

May 27th, 2017
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1.  
  2. #include <opencv2/opencv.hpp>
  3. #include <opencv2/viz.hpp>
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8.  
  9. using namespace std;
  10. using namespace cv;
  11.  
  12. bool bCamView = false;
  13.  
  14.  
  15. void keyboard_callback(const viz::KeyboardEvent &event, void* cookie)
  16. {
  17.     if (event.action == 0 && !event.symbol.compare("s"))
  18.         bCamView = !bCamView;
  19. }
  20.  
  21. int _tmain(int argc, _TCHAR* argv[])
  22. {
  23.     /// Create 3D windows
  24.     viz::Viz3d Window_3D("Estimation Coordinate Frame");
  25.     Window_3D.setBackgroundColor(); // black by default
  26.     Window_3D.registerKeyboardCallback(&keyboard_callback);
  27.  
  28.     // Create the pointcloud
  29.     cout << "Reading the 3D points  ... ";
  30.  
  31.     const int NoOfCamera = 10;
  32.     const double    fx = 1239.911,
  33.                     fy = 1239.911,
  34.                     cx = 519.909,
  35.                     cy = 246.656;
  36.    
  37.     Matx33d K = Matx33d(fx, 0, cx,
  38.                         0, fy, cy,
  39.                         0, 0, 1);
  40.  
  41.     vector<Affine3d> Cam_Trajectory;
  42.     Mat RotationVector(1, 3, CV_64FC1, Scalar(0.0));
  43.     Mat TranslationVector(3, 1, CV_64FC1, Scalar(1.0));
  44.     for (size_t i = 0; i < NoOfCamera; ++i)
  45.     {
  46.         Cam_Trajectory.push_back(Affine3d(RotationVector, TranslationVector));
  47.         TranslationVector.at<double>(2, 0) += 1.0;
  48.     }
  49.  
  50.     Vec3d Pt3d_Object(0.10,0.10,15.0);
  51.     /// Wait for key 'q' to close the window
  52.     cout << endl << "Press:                       " << endl;
  53.     cout << " 's' to switch the camera view" << endl;
  54.     cout << " 'q' to close the windows    " << endl;
  55.  
  56.  
  57.     if (Cam_Trajectory.size() > 0)
  58.     {
  59.         // animated trajectory
  60.         int idx = 0, forw = -1, n = static_cast<int>(Cam_Trajectory.size());
  61.  
  62.         while (!Window_3D.wasStopped())
  63.         {
  64.             /// Render a 3D cube
  65.             Affine3d point_pose(Mat::eye(3, 3, CV_64F), Pt3d_Object);
  66.             viz::WCube cube_widget(Point3f(0.1, 0.1, 0.0), Point3f(0.0, 0.0, -0.1), true, viz::Color::blue());
  67.             cube_widget.setRenderingProperty(viz::LINE_WIDTH, 2.0);
  68.             Window_3D.showWidget("Cube1", cube_widget, point_pose);
  69.  
  70.             Affine3d cam_pose = Cam_Trajectory[idx];
  71.  
  72.             viz::WCameraPosition cpw(0.25); // Coordinate axes
  73.             viz::WCameraPosition cpw_frustum(K, 0.3, viz::Color::yellow()); // Camera frustum
  74.             if (bCamView)
  75.             {
  76.                 Window_3D.setViewerPose(cam_pose);
  77.             }
  78.             else
  79.             {
  80.                 // render complete trajectory
  81.                 Window_3D.showWidget("cameras_frames_and_lines_est", viz::WTrajectory(Cam_Trajectory, viz::WTrajectory::PATH, 1.0, viz::Color::green()));
  82.  
  83.                 Window_3D.showWidget("CPW", cpw, cam_pose);
  84.                 Window_3D.showWidget("CPW_FRUSTUM", cpw_frustum, cam_pose);
  85.             }
  86.            
  87.             // update trajectory index (spring effect)
  88.             forw *= (idx == n || idx == 0) ? -1 : 1;
  89.             idx += forw;
  90.  
  91.             // frame rate 1s
  92.             Window_3D.spinOnce(100, true);
  93.             Window_3D.removeAllWidgets();
  94.  
  95.         }
  96.  
  97.     }
  98.     Window_3D.close();
  99.     return 0;
  100. }
Add Comment
Please, Sign In to add comment