Jelajahi Sumber

added post processing filters

Marc Klinge 1 tahun lalu
induk
melakukan
639ac0d164
5 mengubah file dengan 62 tambahan dan 14 penghapusan
  1. 1 0
      .gitignore
  2. 61 12
      3d-scanner.cpp
  3. 0 2
      CMakeLists.txt
  4. TEMPAT SAMPAH
      filtered.ply
  5. TEMPAT SAMPAH
      original.ply

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
+3d-scanner-startup-help.cpp
 .ply
 CmakeFiles/
 .vscode/

+ 61 - 12
3d-scanner.cpp

@@ -1,31 +1,72 @@
 #include <librealsense2/rs.hpp>
 #include <iostream>
 
-int main(int argc, char *argv[])
+// void update_data_and_save(rs2::frame &colorized_depth, rs2::frame data_frame, rs2::points &points, rs2::pointcloud &pc, rs2::colorizer &color_map, rs2::frame &color, const std::string &fileName);
+
+int main()
 try
 {
 
-    rs2::pointcloud pc;
-    rs2::points points;
+    rs2::pointcloud original_pc;
+    rs2::pointcloud filtered_pc;
+    rs2::points original_points;
+    rs2::points filtered_points;
+
     rs2::pipeline pipe;
 
     pipe.start();
 
-    auto frames = pipe.wait_for_frames();
+    // define filters
+    rs2::threshold_filter thr_filter; // Threshold  - removes values outside recommended range
+    rs2::spatial_filter spat_filter;  // Spatial    - edge-preserving spatial smoothing
+    // rs2::hole_filling_filter hole_filter;                  // Hole filling - fills small holes in the depth image
+    const std::string disparity_filter_name = "Disparity"; // Declare disparity transform from depth to disparity and vice versa
+    rs2::disparity_transform depth_to_disparity(true);
+    rs2::disparity_transform disparity_to_depth(false);
 
-    auto color = frames.get_color_frame();
+    // configure filters
+    thr_filter.set_option(RS2_OPTION_MIN_DISTANCE, 0.0);
+    thr_filter.set_option(RS2_OPTION_MAX_DISTANCE, 0.5);
+    spat_filter.set_option(RS2_OPTION_FILTER_MAGNITUDE, 5);
+    spat_filter.set_option(RS2_OPTION_FILTER_SMOOTH_ALPHA, 1);
+    spat_filter.set_option(RS2_OPTION_FILTER_SMOOTH_DELTA, 1);
+    spat_filter.set_option(RS2_OPTION_HOLES_FILL, 5);
+    // hole_filter.set_option(RS2_OPTION_HOLES_FILL, 1);
 
-    // For cameras that don't have RGB sensor, we'll map the pointcloud to infrared instead of color
+    rs2::frameset data = pipe.wait_for_frames();
+    rs2::video_frame color = data.get_color_frame();
     if (!color)
-        color = frames.get_infrared_frame();
+        color = data.get_infrared_frame();
+    original_pc.map_to(color);
+    rs2::depth_frame depth_frame = data.get_depth_frame();
+
+    rs2::depth_frame filtered = depth_frame;
 
-    pc.map_to(color);
+    /* Apply filters.
+    The implemented flow of the filters pipeline is in the following order:
+    1. apply decimation filter
+    2. apply threshold filter
+    3. transform the scene into disparity domain
+    4. apply spatial filter
+    5. apply temporal filter
+    6. revert the results back (if step Disparity filter was applied
+    to depth domain (each post processing block is optional and can be applied independantly).
+    */
 
-    auto depth = frames.get_depth_frame();
+    filtered = thr_filter.process(filtered);
+    filtered = depth_to_disparity.process(filtered);
+    filtered = spat_filter.process(filtered);
+    filtered = disparity_to_depth.process(filtered);
+    // filtered = hole_filter.process(filtered);
 
-    points = pc.calculate(depth);
+    // update_data_and_save(colored_depth, depth_frame, original_points, original_pc, color_map, color, "original");
+    // update_data_and_save(colored_filtered, filtered, filtered_points, filtered_pc, color_map, color, "filtered");
 
-    points.export_to_ply("test.ply", color);
+    original_points = original_pc.calculate(depth_frame); // Generate pointcloud from the depth data
+    filtered_points = filtered_pc.calculate(filtered);    // Generate pointcloud from the depth data
+
+    original_points.export_to_ply("original.ply", color);
+    filtered_points.export_to_ply("filtered.ply", color);
 
     return EXIT_SUCCESS;
 }
@@ -38,4 +79,12 @@ catch (const std::exception &e)
 {
     std::cerr << e.what() << std::endl;
     return EXIT_FAILURE;
-}
+}
+
+// void update_data_and_save(rs2::frame &colorized_depth, rs2::frame data_frame, rs2::points &points, rs2::pointcloud &pc, rs2::colorizer &color_map, rs2::frame &color, const std::string &fileName)
+// {
+//     pc.map_to(color);                  // Map the colored depth to the point cloud
+//     points = pc.calculate(data_frame); // Generate pointcloud from the depth data
+//     // colorized_depth = color_map.process(data_frame); // Colorize the depth frame with a color map (vorher: colorized_depth)
+//     points.export_to_ply(fileName, color);
+// }

+ 0 - 2
CMakeLists.txt

@@ -39,7 +39,6 @@ endif()
 #     message(WARN "Failed to find_library(realsense2)")
 # endif()
 find_package(realsense2 REQUIRED)
-find_package(OpenCV 4 REQUIRED)
 # find_package(GLFW3 REQUIRED)
 
 # set(SOURCES 3d-scanner.cpp ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/openglHelper.hpp)
@@ -49,7 +48,6 @@ include_directories(3d-scanner ${realsense2_INCLUDE_DIR})
 # include_directories(/opt/homebrew/include/)
 # target_link_libraries(3d-scanner ${DEPENDENCIES})
 target_link_libraries(3d-scanner ${realsense2_LIBRARY})
-target_link_libraries(3d-scanner ${OpenCV_LIBS})
 # target_link_libraries(3d-scanner glfw)
 # set_target_properties (3d-scanner PROPERTIES FOLDER Examples)s
 

TEMPAT SAMPAH
filtered.ply


TEMPAT SAMPAH
original.ply