3d-scanner.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <librealsense2/rs.hpp>
  2. #include <iostream>
  3. void update_data_and_save(rs2::frame data_frame, rs2::points &points, rs2::pointcloud &pc, rs2::frame &color, const std::string &fileName);
  4. int main()
  5. try
  6. {
  7. rs2::pointcloud original_pc;
  8. rs2::pointcloud filtered_pc;
  9. rs2::points original_points;
  10. rs2::points filtered_points;
  11. rs2::pipeline pipe;
  12. pipe.start();
  13. // define filters
  14. rs2::threshold_filter thr_filter; // Threshold - removes values outside recommended range
  15. rs2::spatial_filter spat_filter; // Spatial - edge-preserving spatial smoothing
  16. // rs2::hole_filling_filter hole_filter; // Hole filling - fills small holes in the depth image
  17. const std::string disparity_filter_name = "Disparity"; // Declare disparity transform from depth to disparity and vice versa
  18. rs2::disparity_transform depth_to_disparity(true);
  19. rs2::disparity_transform disparity_to_depth(false);
  20. // configure filters
  21. thr_filter.set_option(RS2_OPTION_MIN_DISTANCE, 0.0);
  22. thr_filter.set_option(RS2_OPTION_MAX_DISTANCE, 0.5);
  23. spat_filter.set_option(RS2_OPTION_FILTER_MAGNITUDE, 5);
  24. spat_filter.set_option(RS2_OPTION_FILTER_SMOOTH_ALPHA, 1);
  25. spat_filter.set_option(RS2_OPTION_FILTER_SMOOTH_DELTA, 1);
  26. spat_filter.set_option(RS2_OPTION_HOLES_FILL, 5);
  27. // hole_filter.set_option(RS2_OPTION_HOLES_FILL, 1);
  28. rs2::frameset data = pipe.wait_for_frames();
  29. rs2::video_frame color = data.get_color_frame();
  30. if (!color)
  31. color = data.get_infrared_frame();
  32. rs2::depth_frame depth_frame = data.get_depth_frame();
  33. rs2::depth_frame filtered = depth_frame;
  34. /* Apply filters.
  35. The implemented flow of the filters pipeline is in the following order:
  36. 1. apply decimation filter
  37. 2. apply threshold filter
  38. 3. transform the scene into disparity domain
  39. 4. apply spatial filter
  40. 5. apply temporal filter
  41. 6. revert the results back (if step Disparity filter was applied
  42. to depth domain (each post processing block is optional and can be applied independantly).
  43. */
  44. filtered = thr_filter.process(filtered);
  45. filtered = depth_to_disparity.process(filtered);
  46. filtered = spat_filter.process(filtered);
  47. filtered = disparity_to_depth.process(filtered);
  48. // filtered = hole_filter.process(filtered);
  49. update_data_and_save(depth_frame, original_points, original_pc, color, "original");
  50. update_data_and_save(filtered, filtered_points, filtered_pc, color, "filtered");
  51. return EXIT_SUCCESS;
  52. }
  53. catch (const rs2::error &e)
  54. {
  55. std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
  56. return EXIT_FAILURE;
  57. }
  58. catch (const std::exception &e)
  59. {
  60. std::cerr << e.what() << std::endl;
  61. return EXIT_FAILURE;
  62. }
  63. void update_data_and_save(rs2::frame data_frame, rs2::points &points, rs2::pointcloud &pc, rs2::frame &color, const std::string &fileName)
  64. {
  65. pc.map_to(color); // Map the colored image to the point cloud
  66. points = pc.calculate(data_frame); // Generate pointcloud from the depth data
  67. points.export_to_ply(fileName + ".ply", color); // export pointcloud to .ply file
  68. }