ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
fusion.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Eigen/Core>
11 #include <cfloat>
12 #include <unordered_set>
13 #include <vector>
14 
15 #include "mvs/depth_map.h"
16 #include "mvs/image.h"
17 #include "mvs/mat.h"
18 #include "mvs/model.h"
19 #include "mvs/normal_map.h"
20 #include "mvs/workspace.h"
21 #include "util/alignment.h"
22 #include "util/cache.h"
23 #include "util/math.h"
24 #include "util/ply.h"
25 #include "util/threading.h"
26 
27 namespace colmap {
28 namespace mvs {
29 
31  // Path for PNG masks. Same format expected as ImageReaderOptions.
32  std::string mask_path = "";
33 
34  // The number of threads to use during fusion.
35  int num_threads = -1;
36 
37  // Maximum image size in either dimension.
38  int max_image_size = -1;
39 
40  // Minimum number of fused pixels to produce a point.
41  int min_num_pixels = 5;
42 
43  // Maximum number of pixels to fuse into a single point.
44  int max_num_pixels = 10000;
45 
46  // Maximum depth in consistency graph traversal.
48 
49  // Maximum relative difference between measured and projected pixel.
50  double max_reproj_error = 2.0f;
51 
52  // Maximum relative difference between measured and projected depth.
53  double max_depth_error = 0.01f;
54 
55  // Maximum angular difference in degrees of normals of pixels to be fused.
56  double max_normal_error = 10.0f;
57 
58  // Number of overlapping images to transitively check for fusing points.
59  int check_num_images = 50;
60 
61  // Flag indicating whether to use LRU cache or pre-load all data
62  bool use_cache = false;
63 
64  // Cache size in gigabytes for fusion. The fusion keeps the bitmaps, depth
65  // maps, normal maps, and consistency graphs of this number of images in
66  // memory. A higher value leads to less disk access and faster fusion, while
67  // a lower value leads to reduced memory usage. Note that a single image can
68  // consume a lot of memory, if the consistency graph is dense.
69  double cache_size = 32.0;
70 
71  std::pair<Eigen::Vector3f, Eigen::Vector3f> bounding_box =
72  std::make_pair(Eigen::Vector3f(-FLT_MAX, -FLT_MAX, -FLT_MAX),
73  Eigen::Vector3f(FLT_MAX, FLT_MAX, FLT_MAX));
74 
75  // Check the options for validity.
76  bool Check() const;
77 
78  // Print the options to stdout.
79  void Print() const;
80 };
81 
82 class StereoFusion : public Thread {
83 public:
84  StereoFusion(const StereoFusionOptions& options,
85  const std::string& workspace_path,
86  const std::string& workspace_format,
87  const std::string& pmvs_option_name,
88  const std::string& input_type);
89 
90  const std::vector<PlyPoint>& GetFusedPoints() const;
91  const std::vector<std::vector<int>>& GetFusedPointsVisibility() const;
92 
93 private:
94  void Run();
95  void InitFusedPixelMask(int image_idx, size_t width, size_t height);
96  void Fuse(const int thread_id,
97  const int image_idx,
98  const int row,
99  const int col);
100 
101  const StereoFusionOptions options_;
102  const std::string workspace_path_;
103  const std::string workspace_format_;
104  const std::string pmvs_option_name_;
105  const std::string input_type_;
106  const float max_squared_reproj_error_;
107  const float min_cos_normal_error_;
108 
109  std::unique_ptr<Workspace> workspace_;
110  std::vector<char> used_images_;
111  std::vector<char> fused_images_;
112  std::vector<std::vector<int>> overlapping_images_;
113  // Contains image masks of pre-masked and already fused pixels.
114  // Initialized from image masks if provided in StereoFusionOptions.
115  std::vector<Mat<char>> fused_pixel_masks_;
116  std::vector<std::pair<int, int>> depth_map_sizes_;
117  std::vector<std::pair<float, float>> bitmap_scales_;
118  std::vector<Eigen::Matrix<float, 3, 4, Eigen::RowMajor>> P_;
119  std::vector<Eigen::Matrix<float, 3, 4, Eigen::RowMajor>> inv_P_;
120  std::vector<Eigen::Matrix<float, 3, 3, Eigen::RowMajor>> inv_R_;
121 
122  struct FusionData {
123  int image_idx = kInvalidImageId;
124  int row = 0;
125  int col = 0;
126  int traversal_depth = -1;
127  FusionData(int image_idx, int row, int col, int traversal_depth)
128  : image_idx(image_idx),
129  row(row),
130  col(col),
131  traversal_depth(traversal_depth) {}
132  bool operator()(const FusionData& data1, const FusionData& data2) {
133  return data1.image_idx > data2.image_idx;
134  }
135  };
136 
137  // Already fused points.
138  std::vector<PlyPoint> fused_points_;
139  std::vector<std::vector<int>> fused_points_visibility_;
140 
141  std::vector<std::vector<PlyPoint>> task_fused_points_;
142  std::vector<std::vector<std::vector<int>>> task_fused_points_visibility_;
143 };
144 
145 // Write the visiblity information into a binary file of the following format:
146 //
147 // <num_points : uint64_t>
148 // <num_visible_images_for_point1 : uint32_t>
149 // <point1_image_idx1 : uint32_t><point1_image_idx2 : uint32_t> ...
150 // <num_visible_images_for_point2 : uint32_t>
151 // <point2_image_idx2 : uint32_t><point2_image_idx2 : uint32_t> ...
152 // ...
153 //
154 // Note that an image_idx in the case of the mvs::StereoFuser does not
155 // correspond to the image_id of a Reconstruction, but the index of the image in
156 // the mvs::Model, which is the location of the image in the images.bin/.txt.
158  const std::string& path,
159  const std::vector<std::vector<int>>& points_visibility);
160 
161 } // namespace mvs
162 } // namespace colmap
colmap::mvs::Mat::GetWidth
size_t GetWidth() const
Definition: mat.h:71
colmap::mvs::Workspace::Options::stereo_folder
std::string stereo_folder
Definition: workspace.h:40
QtCompat::endl
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
fusion.h
cloudViewer::core::make_pair
CLOUDVIEWER_HOST_DEVICE Pair< First, Second > make_pair(const First &_first, const Second &_second)
Definition: SlabTraits.h:49
colmap::mvs::DepthMap::GetDepthMax
float GetDepthMax() const
Definition: depth_map.h:52
colmap::mvs::ComposeInverseProjectionMatrix
void ComposeInverseProjectionMatrix(const float K[9], const float R[9], const float T[3], float inv_P[12])
Definition: image.cc:115
colmap::mvs::StereoFusionOptions::Check
bool Check() const
Definition: fusion.cc:110
colmap::Thread::GetTimer
const Timer & GetTimer() const
Definition: threading.cc:154
colmap::ReadTextFileLines
std::vector< std::string > ReadTextFileLines(const std::string &path)
Definition: misc.cc:308
colmap::Bitmap::IsRGB
bool IsRGB() const
Definition: bitmap.h:261
colmap::ExistsFile
bool ExistsFile(const std::string &path)
Definition: misc.cc:100
normal
double normal[3]
Definition: FileIOFactory.cpp:53
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(TestEmpty)
Definition: depth_map_test.cc:40
data
GraphType data
Definition: graph_cut.cc:138
PrintOption
#define PrintOption(option)
colmap::mvs::StereoFusion
Definition: fusion.h:82
colmap::JoinPaths
std::string JoinPaths(T const &... paths)
Definition: misc.h:128
alignment.h
colmap::Bitmap
Definition: bitmap.h:51
colmap::mvs::StereoFusionOptions::check_num_images
int check_num_images
Definition: fusion.h:59
colmap::Thread
Definition: threading.h:74
workspace.h
colmap::mvs::StereoFusionOptions::bounding_box
std::pair< Eigen::Vector3f, Eigen::Vector3f > bounding_box
Definition: fusion.h:71
colmap::mvs::Mat::GetDepth
size_t GetDepth() const
Definition: mat.h:81
colmap::mvs::internal::Median
float Median(std::vector< T > *elems)
Definition: fusion.cc:41
ply.h
color
math::float4 color
Definition: LineSetBuffers.cpp:46
CHECK_OPTION_GE
#define CHECK_OPTION_GE(val1, val2)
Definition: logging.h:33
mat.h
colmap::mvs::Workspace::Options::image_as_rgb
bool image_as_rgb
Definition: workspace.h:34
colmap::mvs::ComposeProjectionMatrix
void ComposeProjectionMatrix(const float K[9], const float R[9], const float T[3], float P[12])
Definition: image.cc:106
colmap::mvs::internal::FindNextImage
int FindNextImage(const std::vector< std::vector< int >> &overlapping_images, const std::vector< char > &used_images, const std::vector< char > &fused_images, const int prev_image_idx)
Definition: fusion.cc:58
colmap::mvs::Mat::Fill
void Fill(const T value)
Definition: mat.h:131
testing.h
CHECK_OPTION_LE
#define CHECK_OPTION_LE(val1, val2)
Definition: logging.h:31
colmap::mvs::DepthMap::Downsize
void Downsize(const size_t max_width, const size_t max_height)
Definition: depth_map.cc:75
colmap::DegToRad
float DegToRad(const float deg)
Definition: math.h:171
colmap::mvs::StereoFusionOptions::num_threads
int num_threads
Definition: fusion.h:35
colmap::mvs
Definition: consistency_graph.cc:41
width
int width
Definition: FileIOFactory.cpp:142
colmap::mvs::Workspace::Options::workspace_format
std::string workspace_format
Definition: workspace.h:38
colmap::mvs::StereoFusionOptions::mask_path
std::string mask_path
Definition: fusion.h:32
colmap::mvs::DepthMap::ToBitmap
Bitmap ToBitmap(const float min_percentile, const float max_percentile) const
Definition: depth_map.cc:84
colmap::mvs::StereoFusionOptions::use_cache
bool use_cache
Definition: fusion.h:62
height
int height
Definition: FileIOFactory.cpp:143
math.h
colmap::PrintHeading2
void PrintHeading2(const std::string &heading)
Definition: misc.cc:231
normal_map.h
depth_map.h
colmap::mvs::DepthMap::GetDepthMin
float GetDepthMin() const
Definition: depth_map.h:50
colmap::Bitmap::Width
int Width() const
Definition: bitmap.h:249
colmap::kInvalidImageId
const image_t kInvalidImageId
Definition: types.h:76
cloudViewer::t::geometry::path
static const std::string path
Definition: PointCloud.cpp:59
misc.h
colmap::mvs::StereoFusionOptions::max_normal_error
double max_normal_error
Definition: fusion.h:56
colmap::Bitmap::Height
int Height() const
Definition: bitmap.h:250
colmap::mvs::Mat::Set
void Set(const size_t row, const size_t col, const T value)
Definition: mat.h:118
colmap::mvs::Workspace::Options::input_type
std::string input_type
Definition: workspace.h:39
colmap::mvs::StereoFusion::GetFusedPointsVisibility
const std::vector< std::vector< int > > & GetFusedPointsVisibility() const
Definition: fusion.cc:142
colmap::mvs::Workspace::Options::max_image_size
int max_image_size
Definition: workspace.h:31
colmap::mvs::StereoFusionOptions::max_reproj_error
double max_reproj_error
Definition: fusion.h:50
colmap::mvs::DepthMap::Rescale
void Rescale(const float factor)
Definition: depth_map.cc:57
colmap::StringToLower
void StringToLower(std::string *str)
Definition: string.cc:193
image
std::shared_ptr< core::Tensor > image
Definition: FilamentRenderer.cpp:184
colmap::mvs::WritePointsVisibility
void WritePointsVisibility(const std::string &path, const std::vector< std::vector< int >> &points_visibility)
Definition: fusion.cc:635
colmap::mvs::Workspace::Options::num_threads
int num_threads
Definition: workspace.h:28
colmap::BitmapColor
Definition: bitmap.h:30
colmap::mvs::StereoFusionOptions::max_depth_error
double max_depth_error
Definition: fusion.h:53
colmap::mvs::StereoFusionOptions::max_traversal_depth
int max_traversal_depth
Definition: fusion.h:47
colmap::mvs::Workspace::Options::workspace_path
std::string workspace_path
Definition: workspace.h:37
cache.h
model.h
threading.h
colmap::mvs::Workspace::Options::cache_size
double cache_size
Definition: workspace.h:25
colmap::GetEffectiveNumThreads
int GetEffectiveNumThreads(const int num_threads)
Definition: threading.cc:269
colmap::mvs::DepthMap
Definition: depth_map.h:19
std
Definition: Eigen.h:85
colmap::mvs::StereoFusionOptions::Print
void Print() const
Definition: fusion.cc:89
colmap::mvs::StereoFusionOptions::max_image_size
int max_image_size
Definition: fusion.h:38
colmap::mvs::StereoFusionOptions::cache_size
double cache_size
Definition: fusion.h:69
image.h
colmap::mvs::StereoFusion::GetFusedPoints
const std::vector< PlyPoint > & GetFusedPoints() const
Definition: fusion.cc:138
colmap
Definition: AutomaticReconstructionController.h:17
colmap::mvs::StereoFusionOptions::max_num_pixels
int max_num_pixels
Definition: fusion.h:44
colmap::Bitmap::GetPixel
bool GetPixel(const int x, const int y, BitmapColor< uint8_t > *color) const
Definition: bitmap.cc:178
colmap::Thread::IsStopped
bool IsStopped()
Definition: threading.cc:97
colmap::mvs::Workspace::Options
Definition: workspace.h:23
colmap::Timer::PrintMinutes
void PrintMinutes() const
Definition: timer.cc:93
CHECK_OPTION_GT
#define CHECK_OPTION_GT(val1, val2)
Definition: logging.h:34
colmap::mvs::StereoFusion::StereoFusion
StereoFusion(const StereoFusionOptions &options, const std::string &workspace_path, const std::string &workspace_format, const std::string &pmvs_option_name, const std::string &input_type)
Definition: fusion.cc:122
colmap::StringPrintf
std::string StringPrintf(const char *format,...)
Definition: string.cc:131
colmap::mvs::Mat::GetHeight
size_t GetHeight() const
Definition: mat.h:76
colmap::mvs::StereoFusionOptions::min_num_pixels
int min_num_pixels
Definition: fusion.h:41
colmap::mvs::StereoFusionOptions
Definition: fusion.h:30