herrDeng網內搜尋

自訂搜尋

Ads

2021年12月21日 星期二

realsense2+openGL顯示點雲

  1. //修改realsense2 rs-pointcloud.cpp範例,加上按esc可存ply 3D影像
  2. #include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
  3. #include "example.hpp" // Include short list of convenience functions for rendering
  4. using namespace std;
  5. using namespace rs2;
  6. // Helper functions
  7. void register_glfw_callbacks(window& app, glfw_state& app_state);
  8.  
  9. int main()
  10. try
  11. {
  12. // Create a simple OpenGL window for rendering:
  13. window app(1280, 720, "RealSense Pointcloud Example");
  14. //老師加上key_listener
  15. window_key_listener key_listener(app);
  16.  
  17. // Construct an object to manage view state
  18. glfw_state app_state;
  19. // register callbacks to allow manipulation of the pointcloud
  20. register_glfw_callbacks(app, app_state);
  21.  
  22. // Declare pointcloud object, for calculating pointclouds and texture mappings
  23. pointcloud pc;
  24. // We want the points object to be persistent so we can display the last cloud when a frame drops
  25. points points;
  26.  
  27. // Declare RealSense pipeline, encapsulating the actual device and sensors
  28. pipeline pipe;
  29. // Start streaming with default recommended configuration
  30. pipe.start();
  31.  
  32. while (app) // Application still alive?
  33. {
  34. // Wait for the next set of frames from the camera
  35. frameset frames = pipe.wait_for_frames();
  36. frame color = frames.get_color_frame();
  37.  
  38. // Tell pointcloud object to map to this color frame
  39. pc.map_to(color);
  40.  
  41. frame depth = frames.get_depth_frame();
  42.  
  43. // Generate the pointcloud and texture mappings
  44. points = pc.calculate(depth);
  45.  
  46. // Upload the color frame to OpenGL
  47. app_state.tex.upload(color);
  48.  
  49. // Draw the pointcloud
  50. draw_pointcloud(app.width(), app.height(), app_state, points);
  51.  
  52. //如果按esc就存檔
  53. if (key_listener.get_key()==27)
  54. points.export_to_ply("point_cloud.ply", color);
  55. }
  56. return 0;
  57. }
  58. catch (rs2::error & e){
  59. cerr << "RealSense error calling "
  60. << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << endl;
  61. return 1;
  62. }
  63. catch (exception & e){
  64. cerr << e.what() << endl;
  65. return 1;
  66. }

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章