3d-scanner.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <librealsense2/rs.hpp>
  2. #include <iostream>
  3. #include <pcl/io/ply_io.h>
  4. #include <pcl/point_types.h>
  5. #include "pcl-converter.hpp"
  6. struct ColoredPoints
  7. {
  8. rs2::points points;
  9. rs2::video_frame color;
  10. ColoredPoints(rs2::points p, rs2::video_frame c) : points(p), color(c) {}
  11. };
  12. void configure_filters(rs2::threshold_filter &thr_filter, rs2::spatial_filter &spat_filter, float min_dist, float max_dist, int magnitude, int smooth_alpha, int smooth_delta, int holes_fill);
  13. void apply_filters(rs2::threshold_filter &thr_filter, rs2::spatial_filter &spat_filter, rs2::disparity_transform &depth_to_disparity, rs2::disparity_transform &disparity_to_depth, rs2::depth_frame &depth_frame);
  14. ColoredPoints scan(rs2::threshold_filter &thr_filter, rs2::spatial_filter &spat_filter, rs2::disparity_transform &depth_to_disparity, rs2::disparity_transform &disparity_to_depth, rs2::pipeline &pipe);
  15. int main()
  16. try
  17. {
  18. rs2::pipeline pipe;
  19. pipe.start();
  20. // define filters
  21. rs2::threshold_filter thr_filter; // Threshold - removes values outside recommended range
  22. rs2::spatial_filter spat_filter; // Spatial - edge-preserving spatial smoothing
  23. // rs2::hole_filling_filter hole_filter; // Hole filling - fills small holes in the depth image
  24. const std::string disparity_filter_name = "Disparity"; // Declare disparity transform from depth to disparity and vice versa
  25. rs2::disparity_transform depth_to_disparity(true);
  26. rs2::disparity_transform disparity_to_depth(false);
  27. configure_filters(thr_filter, spat_filter, 0.0, 0.5, 5, 1, 1, 5);
  28. ColoredPoints points = scan(thr_filter, spat_filter, depth_to_disparity, disparity_to_depth, pipe);
  29. PCLConverter::convert_and_save_ply("output.ply", points.points, points.color);
  30. return EXIT_SUCCESS;
  31. }
  32. catch (const rs2::error &e)
  33. {
  34. std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
  35. return EXIT_FAILURE;
  36. }
  37. catch (const std::exception &e)
  38. {
  39. std::cerr << e.what() << std::endl;
  40. return EXIT_FAILURE;
  41. }
  42. void configure_filters(rs2::threshold_filter &thr_filter, rs2::spatial_filter &spat_filter, float min_dist, float max_dist, int magnitude, int smooth_alpha, int smooth_delta, int holes_fill)
  43. {
  44. thr_filter.set_option(RS2_OPTION_MIN_DISTANCE, min_dist); // 0.0
  45. thr_filter.set_option(RS2_OPTION_MAX_DISTANCE, max_dist); // 0.5
  46. spat_filter.set_option(RS2_OPTION_FILTER_MAGNITUDE, magnitude); // 5
  47. spat_filter.set_option(RS2_OPTION_FILTER_SMOOTH_ALPHA, smooth_alpha); // 1
  48. spat_filter.set_option(RS2_OPTION_FILTER_SMOOTH_DELTA, smooth_delta); // 1
  49. spat_filter.set_option(RS2_OPTION_HOLES_FILL, holes_fill); // 5
  50. // hole_filter.set_option(RS2_OPTION_HOLES_FILL, 1); //1
  51. }
  52. /* Apply filters.
  53. The implemented flow of the filters pipeline is in the following order:
  54. 1. apply decimation filter
  55. 2. apply threshold filter
  56. 3. transform the scene into disparity domain
  57. 4. apply spatial filter
  58. 5. apply temporal filter
  59. 6. revert the results back (if step Disparity filter was applied
  60. to depth domain (each post processing block is optional and can be applied independantly).
  61. */
  62. void apply_filters(rs2::threshold_filter &thr_filter, rs2::spatial_filter &spat_filter, rs2::disparity_transform &depth_to_disparity, rs2::disparity_transform &disparity_to_depth, rs2::depth_frame &depth_frame)
  63. {
  64. depth_frame = thr_filter.process(depth_frame);
  65. depth_frame = depth_to_disparity.process(depth_frame);
  66. depth_frame = spat_filter.process(depth_frame);
  67. depth_frame = disparity_to_depth.process(depth_frame);
  68. // filtered = hole_filter.process(depth_frame);
  69. }
  70. ColoredPoints scan(rs2::threshold_filter &thr_filter, rs2::spatial_filter &spat_filter, rs2::disparity_transform &depth_to_disparity, rs2::disparity_transform &disparity_to_depth, rs2::pipeline &pipe)
  71. {
  72. rs2::pointcloud pointcloud;
  73. rs2::points points;
  74. // let the auto exposure settle
  75. for (auto i = 0; i < 30; ++i)
  76. pipe.wait_for_frames();
  77. rs2::frameset data = pipe.wait_for_frames();
  78. rs2::video_frame color = data.get_color_frame();
  79. if (!color)
  80. color = data.get_infrared_frame();
  81. rs2::depth_frame depth_frame = data.get_depth_frame();
  82. apply_filters(thr_filter, spat_filter, depth_to_disparity, disparity_to_depth, depth_frame);
  83. pointcloud.map_to(color);
  84. points = pointcloud.calculate(depth_frame);
  85. return ColoredPoints(points, color);
  86. }