herrDeng網內搜尋

自訂搜尋

Ads

2021年12月7日 星期二

柴犬3D ply圖檔點雲擷取realsense C++ opencv程式

 
 這類程式不難,要訣就是要「對齊」『深度攝影處理C++ realsense2加上影格對齊align功能』https://youtu.be/GP88KJzUxgY 

柴犬3D ply圖檔點雲擷取realsense C++ opencv程式。relsense/ Kinect之類的深度攝影機原來是可以這樣用,程式產生的點雲ply圖檔用MeshLab自由軟體開啟,效果超乎意料之外的好,再多加些程式設計自製的3D Scanner軟體就快完成了。
  1. //本程式測試於D435i
  2. #include <librealsense2/rs.hpp>
  3. #include <librealsense2/rs_advanced_mode.hpp>//進階設定
  4. #include <opencv2/opencv.hpp>
  5. #include <iostream>
  6. #include <fstream>
  7. using namespace std;
  8. using namespace cv;
  9. using namespace rs2;
  10. using namespace rs400;//進階設定
  11. int main() try
  12. {
  13. cout << "存點雲到point_cloud.ply\n";
  14. colorizer color_map(3);
  15. //0 - Jet, 1 - Classic 2 - WhiteToBlack 3 - BlackToWhite 4 - Bio 5 - Cold
  16. //6 - Warm 7 - Quantized 8 - Pattern 9 - Hue
  17.  
  18. rs2::align align2(RS2_STREAM_COLOR);
  19.  
  20. pipeline pipe;
  21. //多加幾行就能錄製整個深度攝影機的錄製資訊
  22. config cfg;
  23. int w = 1280, h = 720, FPS = 30;
  24. cfg.enable_record_to_file("test001.bag");//錄製
  25. cfg.enable_stream(RS2_STREAM_COLOR);
  26. cfg.enable_stream(RS2_STREAM_DEPTH);
  27. cfg.enable_stream(RS2_STREAM_INFRARED, 1, w, h, RS2_FORMAT_Y8, FPS);
  28. cfg.enable_stream(RS2_STREAM_INFRARED, 2, w, h, RS2_FORMAT_Y8, FPS);
  29.  
  30. auto profile = pipe.start(cfg);
  31.  
  32. //參考https://github.com/IntelRealSense/librealsense/issues/3067
  33. auto sensor = profile.get_device().first<depth_sensor>();
  34. sensor.set_option(rs2_option::RS2_OPTION_VISUAL_PRESET,
  35. rs2_rs400_visual_preset::RS2_RS400_VISUAL_PRESET_HIGH_DENSITY);
  36.  
  37. pointcloud pc;
  38. points points;
  39. hole_filling_filter holeFilter;//補洞filter
  40. while (1)
  41. {
  42. frameset data = pipe.wait_for_frames();//
  43. data.apply_filter(holeFilter);//點雲補洞
  44. data = align2.process(data);
  45.  
  46. frame depth = data.get_depth_frame();
  47. frame color = data.get_color_frame();
  48. if (depth)
  49. {
  50. points = pc.calculate(depth);
  51. pc.map_to(color);
  52. Mat pc_mat(720, 1280, CV_64FC3, (void*)points.get_data());
  53. Mat depth_mat(h, w, CV_8UC3, (void*)depth.apply_filter(color_map).get_data());
  54. Mat color_mat(h, w, CV_8UC3, (void*)color.get_data()), color_mat2;
  55. // display depth_mat
  56. imshow("depth_mat", depth_mat);
  57. dilate(depth_mat, depth_mat, 1);
  58. erode(depth_mat, depth_mat, 1);
  59. threshold(depth_mat, depth_mat, 180, 255, THRESH_BINARY);
  60. cvtColor(color_mat, color_mat2, COLOR_RGB2BGR);
  61. color_mat2=depth_mat | color_mat2;
  62. imshow("color_mat & depth_mat", color_mat2);
  63.  
  64. int k = waitKey(1);
  65. if (k == 27) {
  66. points.export_to_ply("point_cloud.ply", color);
  67. break;
  68. }
  69. }
  70. }
  71. fstream ff("point_cloud.ply");
  72. string ss;
  73. do{
  74. getline(ff, ss);
  75. cout << ss << endl;
  76. } while (ss != "end_header");
  77. return 0;
  78. }
  79. catch (const rs2::error& e)
  80. {
  81. cerr << "RealSense error calling " << e.get_failed_function()
  82. << "(" << e.get_failed_args() << "):\n "
  83. << e.what();
  84. return 1;
  85. }
  86. catch (exception& e) {
  87. cerr << e.what();
  88. return 1;
  89. }

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章