ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
undistortion.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 "base/reconstruction.h"
11 #include "util/alignment.h"
12 #include "util/bitmap.h"
13 #include "util/misc.h"
14 #include "util/threading.h"
15 
16 namespace colmap {
17 
19  // The amount of blank pixels in the undistorted image in the range [0, 1].
20  double blank_pixels = 0.0;
21 
22  // Minimum and maximum scale change of camera used to satisfy the blank
23  // pixel constraint.
24  double min_scale = 0.2;
25  double max_scale = 2.0;
26 
27  // Maximum image size in terms of width or height of the undistorted camera.
28  int max_image_size = -1;
29 
30  // The 4 factors in the range [0, 1] that define the ROI (region of
31  // interest) in original image. The bounding box pixel coordinates are
32  // calculated as
33  // (roi_min_x * Width, roi_min_y * Height) and
34  // (roi_max_x * Width, roi_max_y * Height).
35  double roi_min_x = 0.0;
36  double roi_min_y = 0.0;
37  double roi_max_x = 1.0;
38  double roi_max_y = 1.0;
39 };
40 
41 // Undistort images and export undistorted cameras, as required by the
42 // mvs::PatchMatchController class.
43 class COLMAPUndistorter : public Thread {
44 public:
46  const UndistortCameraOptions& options,
47  Reconstruction* reconstruction,
48  const std::string& image_path,
49  const std::string& output_path,
50  const int num_related_images = 20,
51  const CopyType copy_type = CopyType::COPY,
52  const std::vector<image_t>& image_ids = std::vector<image_t>());
53 
54 private:
55  void Run();
56 
57  bool Undistort(const image_t image_id) const;
58  void WritePatchMatchConfig() const;
59  void WriteFusionConfig() const;
60  void WriteScript(const bool geometric) const;
61 
62  UndistortCameraOptions options_;
63  const std::string image_path_;
64  const std::string output_path_;
65  const CopyType copy_type_;
66  const int num_patch_match_src_images_;
67  Reconstruction* reconstruction_;
68  const std::vector<image_t> image_ids_;
69  std::vector<std::string> image_names_;
70 };
71 
72 // Undistort images and prepare data for CMVS/PMVS.
73 class PMVSUndistorter : public Thread {
74 public:
76  Reconstruction* reconstruction,
77  const std::string& image_path,
78  const std::string& output_path);
79 
80 private:
81  void Run();
82 
83  bool Undistort(const size_t reg_image_idx) const;
84  void WriteVisibilityData() const;
85  void WriteOptionFile() const;
86  void WritePMVSScript() const;
87  void WriteCMVSPMVSScript() const;
88  void WriteCOLMAPScript(const bool geometric) const;
89  void WriteCMVSCOLMAPScript(const bool geometric) const;
90 
91  UndistortCameraOptions options_;
92  std::string image_path_;
93  std::string output_path_;
94  Reconstruction* reconstruction_;
95 };
96 
97 // Undistort images and prepare data for CMP-MVS.
98 class CMPMVSUndistorter : public Thread {
99 public:
101  Reconstruction* reconstruction,
102  const std::string& image_path,
103  const std::string& output_path);
104 
105 private:
106  void Run();
107 
108  bool Undistort(const size_t reg_image_idx) const;
109 
110  UndistortCameraOptions options_;
111  std::string image_path_;
112  std::string output_path_;
113  Reconstruction* reconstruction_;
114 };
115 
116 // Undistort images and export undistorted cameras without the need for a
117 // reconstruction. Instead, the image names and camera model information are
118 // read from a text file.
119 class PureImageUndistorter : public Thread {
120 public:
122  const std::string& image_path,
123  const std::string& output_path,
124  const std::vector<std::pair<std::string, Camera>>&
125  image_names_and_cameras);
126 
127 private:
128  void Run();
129 
130  bool Undistort(const size_t reg_image_idx) const;
131 
132  UndistortCameraOptions options_;
133  std::string image_path_;
134  std::string output_path_;
135  const std::vector<std::pair<std::string, Camera>>& image_names_and_cameras_;
136 };
137 
138 // Rectify stereo image pairs.
139 class StereoImageRectifier : public Thread {
140 public:
142  const UndistortCameraOptions& options,
143  Reconstruction* reconstruction,
144  const std::string& image_path,
145  const std::string& output_path,
146  const std::vector<std::pair<image_t, image_t>>& stereo_pairs);
147 
148 private:
149  void Run();
150 
151  void Rectify(const image_t image_id1, const image_t image_id2) const;
152 
153  UndistortCameraOptions options_;
154  std::string image_path_;
155  std::string output_path_;
156  const std::vector<std::pair<image_t, image_t>>& stereo_pairs_;
157  Reconstruction* reconstruction_;
158 };
159 
160 // Undistort camera by resizing the image and shifting the principal point.
161 //
162 // The scaling factor is computed such that no blank pixels are in the
163 // undistorted image (blank_pixels=0) or all pixels in distorted image are
164 // contained in output image (blank_pixels=1).
165 //
166 // The focal length of the image is preserved and the dimensions of the
167 // undistorted pinhole camera are adjusted such that either all pixels in
168 // the undistorted image have a corresponding pixel in the distorted image
169 // (i.e. no blank pixels at the borders, for `blank_pixels=0`), or all pixels
170 // in the distorted image project have a corresponding pixel in the undistorted
171 // image (i.e. blank pixels at the borders, for `blank_pixels=1`). Intermediate
172 // states can be achieved by setting `blank_pixels` between 0 and 1.
173 //
174 // The relative location of the principal point of the distorted camera is
175 // preserved. The scaling of the image dimensions is subject to the `min_scale`,
176 // `max_scale`, and `max_image_size` constraints.
178  const Camera& camera);
179 
180 // Undistort image such that the viewing geometry of the undistorted image
181 // follows a pinhole camera model. See `UndistortCamera` for more details
182 // on the undistortion conventions.
183 void UndistortImage(const UndistortCameraOptions& options,
184  const Bitmap& distorted_image,
185  const Camera& distorted_camera,
186  Bitmap* undistorted_image,
187  Camera* undistorted_camera);
188 
189 // Undistort all cameras in the reconstruction and accordingly all
190 // observations in their corresponding images.
192  Reconstruction* reconstruction);
193 
194 // Compute stereo rectification homographies that transform two images,
195 // such that corresponding pixels in one image lie on the same scanline in the
196 // other image. The matrix Q transforms disparity values to world coordinates
197 // as [x, y, disparity, 1] * Q = [X, Y, Z, 1] * w. Note that this function
198 // assumes that the two cameras are already undistorted.
199 void RectifyStereoCameras(const Camera& camera1,
200  const Camera& camera2,
201  const Eigen::Vector4d& qvec,
202  const Eigen::Vector3d& tvec,
203  Eigen::Matrix3d* H1,
204  Eigen::Matrix3d* H2,
205  Eigen::Matrix4d* Q);
206 
207 // Rectify and undistort the stereo image pair using the given geometry.
209  const Bitmap& distorted_image1,
210  const Bitmap& distorted_image2,
211  const Camera& distorted_camera1,
212  const Camera& distorted_camera2,
213  const Eigen::Vector4d& qvec,
214  const Eigen::Vector3d& tvec,
215  Bitmap* undistorted_image1,
216  Bitmap* undistorted_image2,
217  Camera* undistorted_camera,
218  Eigen::Matrix4d* Q);
219 
220 } // namespace colmap
CMPMVSUndistorter(const UndistortCameraOptions &options, Reconstruction *reconstruction, const std::string &image_path, const std::string &output_path)
COLMAPUndistorter(const UndistortCameraOptions &options, Reconstruction *reconstruction, const std::string &image_path, const std::string &output_path, const int num_related_images=20, const CopyType copy_type=CopyType::COPY, const std::vector< image_t > &image_ids=std::vector< image_t >())
PMVSUndistorter(const UndistortCameraOptions &options, Reconstruction *reconstruction, const std::string &image_path, const std::string &output_path)
PureImageUndistorter(const UndistortCameraOptions &options, const std::string &image_path, const std::string &output_path, const std::vector< std::pair< std::string, Camera >> &image_names_and_cameras)
StereoImageRectifier(const UndistortCameraOptions &options, Reconstruction *reconstruction, const std::string &image_path, const std::string &output_path, const std::vector< std::pair< image_t, image_t >> &stereo_pairs)
void RectifyStereoCameras(const Camera &camera1, const Camera &camera2, const Eigen::Vector4d &qvec, const Eigen::Vector3d &tvec, Eigen::Matrix3d *H1, Eigen::Matrix3d *H2, Eigen::Matrix4d *Q)
CopyType
Definition: misc.h:27
void UndistortImage(const UndistortCameraOptions &options, const Bitmap &distorted_bitmap, const Camera &distorted_camera, Bitmap *undistorted_bitmap, Camera *undistorted_camera)
void UndistortReconstruction(const UndistortCameraOptions &options, Reconstruction *reconstruction)
Camera UndistortCamera(const UndistortCameraOptions &options, const Camera &camera)
uint32_t image_t
Definition: types.h:61
void RectifyAndUndistortStereoImages(const UndistortCameraOptions &options, const Bitmap &distorted_image1, const Bitmap &distorted_image2, const Camera &distorted_camera1, const Camera &distorted_camera2, const Eigen::Vector4d &qvec, const Eigen::Vector3d &tvec, Bitmap *undistorted_image1, Bitmap *undistorted_image2, Camera *undistorted_camera, Eigen::Matrix4d *Q)