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
warp.h
colmap::PrintHeading1
void PrintHeading1(const std::string &heading)
Definition: misc.cc:225
colmap::UndistortCameraOptions::roi_max_y
double roi_max_y
Definition: undistortion.h:38
QtCompat::endl
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
colmap::image_t
uint32_t image_t
Definition: types.h:61
colmap::Camera::FocalLengthY
double FocalLengthY() const
Definition: camera.cc:121
colmap::Camera::SetFocalLengthX
void SetFocalLengthX(const double focal_length_x)
Definition: camera.cc:134
colmap::CreateDirIfNotExists
void CreateDirIfNotExists(const std::string &path)
Definition: misc.cc:112
x
normal_z x
Definition: my_point_types.h:165
Eigen::Matrix3x4d
Matrix< double, 3, 4 > Matrix3x4d
Definition: types.h:39
camera_models.h
colmap::Camera::PrincipalPointX
double PrincipalPointX() const
Definition: camera.cc:146
colmap::Thread::GetTimer
const Timer & GetTimer() const
Definition: threading.cc:154
colmap::UndistortCameraOptions::min_scale
double min_scale
Definition: undistortion.h:24
colmap::Bitmap::IsRGB
bool IsRGB() const
Definition: bitmap.h:261
colmap::ExistsFile
bool ExistsFile(const std::string &path)
Definition: misc.cc:100
bitmap.h
colmap::Camera::WorldToImage
Eigen::Vector2d WorldToImage(const Eigen::Vector2d &world_point) const
Definition: camera.cc:230
colmap::UndistortImage
void UndistortImage(const UndistortCameraOptions &options, const Bitmap &distorted_bitmap, const Camera &distorted_camera, Bitmap *undistorted_bitmap, Camera *undistorted_camera)
Definition: undistortion.cc:939
colmap::ThreadPool::AddTask
auto AddTask(func_t &&f, args_t &&... args) -> std::future< typename std::result_of< func_t(args_t...)>::type >
Definition: threading.h:299
colmap::RectifyAndUndistortStereoImages
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)
Definition: undistortion.cc:1039
colmap::JoinPaths
std::string JoinPaths(T const &... paths)
Definition: misc.h:128
alignment.h
colmap::point2D_t
uint32_t point2D_t
Definition: types.h:67
colmap::Bitmap
Definition: bitmap.h:51
colmap::UndistortCameraOptions::max_scale
double max_scale
Definition: undistortion.h:25
triangulation.h
colmap::Thread
Definition: threading.h:74
colmap::CMPMVSUndistorter::CMPMVSUndistorter
CMPMVSUndistorter(const UndistortCameraOptions &options, Reconstruction *reconstruction, const std::string &image_path, const std::string &output_path)
Definition: undistortion.cc:536
colmap::UndistortCamera
Camera UndistortCamera(const UndistortCameraOptions &options, const Camera &camera)
Definition: undistortion.cc:754
colmap::Reconstruction::CreateImageDirs
void CreateImageDirs(const std::string &path) const
Definition: reconstruction.cc:1447
colmap::Camera::PrincipalPointY
double PrincipalPointY() const
Definition: camera.cc:152
colmap::PMVSUndistorter
Definition: undistortion.h:73
colmap::CalculateTriangulationAngle
double CalculateTriangulationAngle(const Eigen::Vector3d &proj_center1, const Eigen::Vector3d &proj_center2, const Eigen::Vector3d &point3D)
Definition: triangulation.cc:122
colmap::Bitmap::Allocate
bool Allocate(const int width, const int height, const bool as_rgb)
Definition: bitmap.cc:104
colmap::Camera::SetPrincipalPointY
void SetPrincipalPointY(const double ppy)
Definition: camera.cc:164
colmap::UndistortCameraOptions::blank_pixels
double blank_pixels
Definition: undistortion.h:20
colmap::Camera::SetPrincipalPointX
void SetPrincipalPointX(const double ppx)
Definition: camera.cc:158
colmap::UndistortCameraOptions::roi_min_x
double roi_min_x
Definition: undistortion.h:35
testing.h
undistortion.h
colmap::Reconstruction
Definition: reconstruction.h:40
colmap::Reconstruction::Write
void Write(const std::string &path) const
Definition: reconstruction.cc:787
colmap::SimilarityTransform3::Matrix
Eigen::Matrix4d Matrix() const
Definition: similarity_transform.cc:241
colmap::WarpImageBetweenCameras
void WarpImageBetweenCameras(const Camera &source_camera, const Camera &target_camera, const Bitmap &source_image, Bitmap *target_image)
Definition: warp.cc:51
colmap::Camera::ImageToWorld
Eigen::Vector2d ImageToWorld(const Eigen::Vector2d &image_point) const
Definition: camera.cc:219
colmap::UndistortCameraOptions::roi_max_x
double roi_max_x
Definition: undistortion.h:37
y
normal_z y
Definition: my_point_types.h:168
colmap::Camera::FocalLengthX
double FocalLengthX() const
Definition: camera.cc:115
colmap::Camera::CalibrationMatrix
Eigen::Matrix3d CalibrationMatrix() const
Definition: camera.cc:75
colmap::Bitmap::Width
int Width() const
Definition: bitmap.h:249
colmap::StereoImageRectifier::StereoImageRectifier
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)
Definition: undistortion.cc:658
cloudViewer::t::geometry::path
static const std::string path
Definition: PointCloud.cpp:59
e
const double * e
Definition: essential_matrix_poly.h:10
misc.h
colmap::Reconstruction::RegImageIds
const std::vector< image_t > & RegImageIds() const
Definition: reconstruction.h:504
cloudViewer::t::geometry::kernel::voxel_grid::index_t
int index_t
Definition: VoxelBlockGrid.h:23
Point3D
Definition: Common.h:117
colmap::Bitmap::Height
int Height() const
Definition: bitmap.h:250
colmap::FileCopy
void FileCopy(const std::string &src_path, const std::string &dst_path, CopyType type)
Definition: misc.cc:85
colmap::PMVSUndistorter::PMVSUndistorter
PMVSUndistorter(const UndistortCameraOptions &options, Reconstruction *reconstruction, const std::string &image_path, const std::string &output_path)
Definition: undistortion.cc:305
colmap::Camera::Width
size_t Width() const
Definition: camera.h:160
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(TestTriangulatePoint)
Definition: triangulation_test.cc:42
colmap::UndistortReconstruction
void UndistortReconstruction(const UndistortCameraOptions &options, Reconstruction *reconstruction)
Definition: undistortion.cc:956
colmap::CalculateTriangulationAngles
std::vector< double > CalculateTriangulationAngles(const Eigen::Vector3d &proj_center1, const Eigen::Vector3d &proj_center2, const std::vector< Eigen::Vector3d > &points3D)
Definition: triangulation.cc:147
knncpp::Index
Eigen::MatrixXd::Index Index
Definition: knncpp.h:26
reconstruction.h
colmap::Camera::SetHeight
void SetHeight(const size_t height)
Definition: camera.h:166
colmap::CopyType
CopyType
Definition: misc.h:27
image
std::shared_ptr< core::Tensor > image
Definition: FilamentRenderer.cpp:184
colmap::PinholeCameraModel::model_id
static const int model_id
Definition: camera_models.h:180
colmap::Reconstruction::Cameras
const std::unordered_map< camera_t, class Camera > & Cameras() const
Definition: reconstruction.h:496
colmap::CMPMVSUndistorter
Definition: undistortion.h:98
colmap::Reconstruction::Images
const std::unordered_map< image_t, class Image > & Images() const
Definition: reconstruction.h:500
colmap::SimplePinholeCameraModel::model_id
static const int model_id
Definition: camera_models.h:167
colmap::Reconstruction::ExportBundler
bool ExportBundler(const std::string &path, const std::string &list_path, bool skip_distortion=false) const
Definition: reconstruction.cc:1137
colmap::ComputeRelativePose
void ComputeRelativePose(const Eigen::Vector4d &qvec1, const Eigen::Vector3d &tvec1, const Eigen::Vector4d &qvec2, const Eigen::Vector3d &tvec2, Eigen::Vector4d *qvec12, Eigen::Vector3d *tvec12)
Definition: pose.cc:173
colmap::WarpImageWithHomographyBetweenCameras
void WarpImageWithHomographyBetweenCameras(const Eigen::Matrix3d &H, const Camera &source_camera, const Camera &target_camera, const Bitmap &source_image, Bitmap *target_image)
Definition: warp.cc:124
colmap::StereoImageRectifier
Definition: undistortion.h:139
colmap::Reconstruction::NumRegImages
size_t NumRegImages() const
Definition: reconstruction.h:440
cloudViewer::visualization::rendering::EntityType::Camera
@ Camera
colmap::ThreadPool::Stop
void Stop()
Definition: threading.cc:198
colmap::Camera::ModelId
int ModelId() const
Definition: camera.h:158
colmap::Camera::Rescale
void Rescale(const double scale)
Definition: camera.cc:237
colmap::Clip
T Clip(const T &value, const T &low, const T &high)
Definition: math.h:167
threading.h
colmap::Camera::MeanFocalLength
double MeanFocalLength() const
Definition: camera.cc:100
colmap::SimilarityTransform3
Definition: similarity_transform.h:24
colmap::UndistortCameraOptions::max_image_size
int max_image_size
Definition: undistortion.h:28
colmap::TriangulatePoint
Eigen::Vector3d TriangulatePoint(const Eigen::Matrix3x4d &proj_matrix1, const Eigen::Matrix3x4d &proj_matrix2, const Eigen::Vector2d &point1, const Eigen::Vector2d &point2)
Definition: triangulation.cc:39
colmap::Camera::FocalLength
double FocalLength() const
Definition: camera.cc:109
colmap::Image::Name
const std::string & Name() const
Definition: image.h:242
pose.h
logging.h
colmap::Camera::FocalLengthIdxs
const std::vector< size_t > & FocalLengthIdxs() const
Definition: camera.cc:63
colmap::Camera::SetFocalLengthY
void SetFocalLengthY(const double focal_length_y)
Definition: camera.cc:140
colmap::CopyType::COPY
@ COPY
colmap::Bitmap::CloneMetadata
void CloneMetadata(Bitmap *target) const
Definition: bitmap.cc:604
colmap::Camera::SetModelId
void SetModelId(const int model_id)
Definition: camera.cc:51
colmap::UndistortCameraOptions
Definition: undistortion.h:18
colmap
Definition: AutomaticReconstructionController.h:17
colmap::PureImageUndistorter::PureImageUndistorter
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)
Definition: undistortion.cc:598
colmap::StringReplace
std::string StringReplace(const std::string &str, const std::string &old_str, const std::string &new_str)
Definition: string.cc:140
colmap::Thread::IsStopped
bool IsStopped()
Definition: threading.cc:97
colmap::UndistortCameraOptions::roi_min_y
double roi_min_y
Definition: undistortion.h:36
colmap::RectifyStereoCameras
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)
Definition: undistortion.cc:977
colmap::Camera::Height
size_t Height() const
Definition: camera.h:162
colmap::Timer::PrintMinutes
void PrintMinutes() const
Definition: timer.cc:93
colmap::Camera::SetWidth
void SetWidth(const size_t width)
Definition: camera.h:164
similarity_transform.h
colmap::COLMAPUndistorter::COLMAPUndistorter
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 >())
Definition: undistortion.cc:154
colmap::COLMAPUndistorter
Definition: undistortion.h:43
colmap::StringPrintf
std::string StringPrintf(const char *format,...)
Definition: string.cc:131
colmap::ThreadPool
Definition: threading.h:171
colmap::Reconstruction::Camera
const class Camera & Camera(const camera_t camera_id) const
Definition: reconstruction.h:450
colmap::Camera
Definition: camera.h:20
colmap::Reconstruction::Point3D
const class Point3D & Point3D(const point3D_t point3D_id) const
Definition: reconstruction.h:458
colmap::PureImageUndistorter
Definition: undistortion.h:119
colmap::Reconstruction::Image
const class Image & Image(const image_t image_id) const
Definition: reconstruction.h:454