ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
correspondence_graph.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 <unordered_map>
11 #include <vector>
12 
13 #include "base/database.h"
14 #include "util/types.h"
15 
16 namespace colmap {
17 
18 // Scene graph represents the graph of image to image and feature to feature
19 // correspondences of a dataset. It should be accessed from the DatabaseCache.
21 public:
22  struct Correspondence {
27 
28  // The identifier of the corresponding image.
30 
31  // The index of the corresponding point in the corresponding image.
33  };
34 
36 
37  // Number of added images.
38  inline size_t NumImages() const;
39 
40  // Number of added images.
41  inline size_t NumImagePairs() const;
42 
43  // Check whether image exists.
44  inline bool ExistsImage(const image_t image_id) const;
45 
46  // Get the number of observations in an image. An observation is an image
47  // point that has at least one correspondence.
48  inline point2D_t NumObservationsForImage(const image_t image_id) const;
49 
50  // Get the number of correspondences per image.
51  inline point2D_t NumCorrespondencesForImage(const image_t image_id) const;
52 
53  // Get the number of correspondences between a pair of images.
55  const image_t image_id1, const image_t image_id2) const;
56 
57  // Get the number of correspondences between all images.
58  std::unordered_map<image_pair_t, point2D_t>
60 
61  // Finalize the database manager.
62  //
63  // - Calculates the number of observations per image by counting the number
64  // of image points that have at least one correspondence.
65  // - Deletes images without observations, as they are useless for SfM.
66  // - Shrinks the correspondence vectors to their size to save memory.
67  void Finalize();
68 
69  // Add new image to the correspondence graph.
70  void AddImage(const image_t image_id, const size_t num_points2D);
71 
72  // Add correspondences between images. This function ignores invalid
73  // correspondences where the point indices are out of bounds or duplicate
74  // correspondences between the same image points. Whenever either of the two
75  // cases occur this function prints a warning to the standard output.
76  void AddCorrespondences(const image_t image_id1,
77  const image_t image_id2,
78  const FeatureMatches& matches);
79 
80  // Find the correspondence of an image observation to all other images.
81  inline const std::vector<Correspondence>& FindCorrespondences(
82  const image_t image_id, const point2D_t point2D_idx) const;
83 
84  // Find correspondences to the given observation.
85  //
86  // Transitively collects correspondences to the given observation by first
87  // finding correspondences to the given observation, then looking for
88  // correspondences to the collected correspondences in the first step, and
89  // so forth until the transitivity is exhausted or no more correspondences
90  // are found. The returned list does not contain duplicates and contains the
91  // given observation.
92  std::vector<Correspondence> FindTransitiveCorrespondences(
93  const image_t image_id,
94  const point2D_t point2D_idx,
95  const size_t transitivity) const;
96 
97  // Find all correspondences between two images.
99  const image_t image_id1, const image_t image_id2) const;
100 
101  // Check whether the image point has correspondences.
102  inline bool HasCorrespondences(const image_t image_id,
103  const point2D_t point2D_idx) const;
104 
105  // Check whether the given observation is part of a two-view track, i.e.
106  // it only has one correspondence and that correspondence has the given
107  // observation as its only correspondence.
108  bool IsTwoViewObservation(const image_t image_id,
109  const point2D_t point2D_idx) const;
110 
111 private:
112  struct Image {
113  // Number of 2D points with at least one correspondence to another
114  // image.
115  point2D_t num_observations = 0;
116 
117  // Total number of correspondences to other images. This measure is
118  // useful to find a good initial pair, that is connected to many images.
119  point2D_t num_correspondences = 0;
120 
121  // Correspondences to other images per image point.
122  std::vector<std::vector<Correspondence>> corrs;
123  };
124 
125  struct ImagePair {
126  // The number of correspondences between pairs of images.
127  point2D_t num_correspondences = 0;
128  };
129 
130  std::unordered_map<image_t, Image> images_;
131  std::unordered_map<image_pair_t, ImagePair> image_pairs_;
132 };
133 
135 // Implementation
137 
138 size_t CorrespondenceGraph::NumImages() const { return images_.size(); }
139 
141  return image_pairs_.size();
142 }
143 
144 bool CorrespondenceGraph::ExistsImage(const image_t image_id) const {
145  return images_.find(image_id) != images_.end();
146 }
147 
149  const image_t image_id) const {
150  return images_.at(image_id).num_observations;
151 }
152 
154  const image_t image_id) const {
155  return images_.at(image_id).num_correspondences;
156 }
157 
159  const image_t image_id1, const image_t image_id2) const {
160  const image_pair_t pair_id =
161  Database::ImagePairToPairId(image_id1, image_id2);
162  const auto it = image_pairs_.find(pair_id);
163  if (it == image_pairs_.end()) {
164  return 0;
165  } else {
166  return static_cast<point2D_t>(it->second.num_correspondences);
167  }
168 }
169 
170 const std::vector<CorrespondenceGraph::Correspondence>&
172  const point2D_t point2D_idx) const {
173  return images_.at(image_id).corrs.at(point2D_idx);
174 }
175 
177  const image_t image_id, const point2D_t point2D_idx) const {
178  return !images_.at(image_id).corrs.at(point2D_idx).empty();
179 }
180 
181 } // namespace colmap
QtCompat::endl
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
colmap::OpenCVCameraModel::model_id
static const int model_id
Definition: camera_models.h:226
colmap::image_t
uint32_t image_t
Definition: types.h:61
colmap::image_pair_t
uint64_t image_pair_t
Definition: types.h:64
colmap::Camera::FocalLengthY
double FocalLengthY() const
Definition: camera.cc:121
types.h
colmap::FullOpenCVCameraModel::model_id
static const int model_id
Definition: camera_models.h:258
colmap::Camera::SetFocalLengthX
void SetFocalLengthX(const double focal_length_x)
Definition: camera.cc:134
colmap::Image::SetImageId
void SetImageId(const image_t image_id)
Definition: image.h:240
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(TestEmpty)
Definition: camera_rig_test.cc:39
colmap::CameraRig::NumSnapshots
size_t NumSnapshots() const
Definition: camera_rig.cc:42
colmap::CorrespondenceGraph::CorrespondenceGraph
CorrespondenceGraph()
Definition: correspondence_graph.cc:41
colmap::CorrespondenceGraph::Correspondence::Correspondence
Correspondence(const image_t image_id, const point2D_t point2D_idx)
Definition: correspondence_graph.h:25
camera_models.h
colmap::Camera::PrincipalPointX
double PrincipalPointX() const
Definition: camera.cc:146
colmap::Camera::HasBogusParams
bool HasBogusParams(const double min_focal_length_ratio, const double max_focal_length_ratio, const double max_extra_param) const
Definition: camera.cc:186
colmap::Camera::WorldToImage
Eigen::Vector2d WorldToImage(const Eigen::Vector2d &world_point) const
Definition: camera.cc:230
colmap::CorrespondenceGraph::NumImages
size_t NumImages() const
Definition: correspondence_graph.h:138
colmap::Camera::ParamsToString
std::string ParamsToString() const
Definition: camera.cc:170
colmap::Camera::ParamsInfo
std::string ParamsInfo() const
Definition: camera.cc:96
string.h
colmap::Camera::SetFocalLength
void SetFocalLength(const double focal_length)
Definition: camera.cc:127
colmap::point2D_t
uint32_t point2D_t
Definition: types.h:67
colmap::Camera::ModelName
std::string ModelName() const
Definition: camera.cc:49
colmap::ComposeIdentityQuaternion
Eigen::Vector4d ComposeIdentityQuaternion()
Definition: pose.h:209
colmap::Camera::Params
const std::vector< double > & Params() const
Definition: camera.h:176
colmap::Camera::InitializeWithName
void InitializeWithName(const std::string &model_name, const double focal_length, const size_t width, const size_t height)
Definition: camera.cc:212
colmap::CameraRig::ComputeRelativePoses
bool ComputeRelativePoses(const Reconstruction &reconstruction)
Definition: camera_rig.cc:167
colmap::CorrespondenceGraph::NumObservationsForImage
point2D_t NumObservationsForImage(const image_t image_id) const
Definition: correspondence_graph.h:148
colmap::Camera::SetModelIdFromName
void SetModelIdFromName(const std::string &model_name)
Definition: camera.cc:57
colmap::Camera::PrincipalPointY
double PrincipalPointY() const
Definition: camera.cc:152
colmap::kInvalidCameraModelId
static const int kInvalidCameraModelId
Definition: camera_models.h:55
colmap::Camera::SetPrincipalPointY
void SetPrincipalPointY(const double ppy)
Definition: camera.cc:164
colmap::CameraRig::HasCamera
bool HasCamera(const camera_t camera_id) const
Definition: camera_rig.cc:44
colmap::Camera::SetPrincipalPointX
void SetPrincipalPointX(const double ppx)
Definition: camera.cc:158
colmap::Camera::CameraId
camera_t CameraId() const
Definition: camera.h:154
colmap::CorrespondenceGraph::NumImagePairs
size_t NumImagePairs() const
Definition: correspondence_graph.h:140
colmap::CorrespondenceGraph::FindTransitiveCorrespondences
std::vector< Correspondence > FindTransitiveCorrespondences(const image_t image_id, const point2D_t point2D_idx, const size_t transitivity) const
Definition: correspondence_graph.cc:164
testing.h
colmap::Reconstruction
Definition: reconstruction.h:40
colmap::Camera::ImageToWorldThreshold
double ImageToWorldThreshold(const double threshold) const
Definition: camera.cc:226
colmap::CameraRig::NumCameras
size_t NumCameras() const
Definition: camera_rig.cc:40
colmap::SimpleRadialCameraModel::model_id
static const int model_id
Definition: camera_models.h:196
colmap::Camera::SetPriorFocalLength
void SetPriorFocalLength(const bool prior)
Definition: camera.h:170
colmap::Camera::ImageToWorld
Eigen::Vector2d ImageToWorld(const Eigen::Vector2d &image_point) const
Definition: camera.cc:219
colmap::CorrespondenceGraph
Definition: correspondence_graph.h:20
colmap::Image::SetQvec
void SetQvec(const Eigen::Vector4d &qvec)
Definition: image.h:309
colmap::CameraRig::AddSnapshot
void AddSnapshot(const std::vector< image_t > &image_ids)
Definition: camera_rig.cc:79
colmap::Image::SetCameraId
void SetCameraId(const camera_t camera_id)
Definition: image.h:250
colmap::IsNaN
bool IsNaN(const float x)
Definition: math.h:160
colmap::CorrespondenceGraph::FindCorrespondencesBetweenImages
FeatureMatches FindCorrespondencesBetweenImages(const image_t image_id1, const image_t image_id2) const
Definition: correspondence_graph.cc:224
colmap::CorrespondenceGraph::AddCorrespondences
void AddCorrespondences(const image_t image_id1, const image_t image_id2, const FeatureMatches &matches)
Definition: correspondence_graph.cc:77
colmap::Reconstruction::AddImage
void AddImage(const class Image &image)
Definition: reconstruction.cc:184
colmap::Camera::FocalLengthX
double FocalLengthX() const
Definition: camera.cc:115
colmap::Camera::ExtraParamsIdxs
const std::vector< size_t > & ExtraParamsIdxs() const
Definition: camera.cc:71
colmap::Camera::CalibrationMatrix
Eigen::Matrix3d CalibrationMatrix() const
Definition: camera.cc:75
colmap::kInvalidImageId
const image_t kInvalidImageId
Definition: types.h:76
database.h
colmap::CameraRig::SetRefCameraId
void SetRefCameraId(const camera_t camera_id)
Definition: camera_rig.cc:50
colmap::Database::ImagePairToPairId
static image_pair_t ImagePairToPairId(const image_t image_id1, const image_t image_id2)
Definition: database.h:339
colmap::CameraRig
Definition: camera_rig.h:25
colmap::Camera::Width
size_t Width() const
Definition: camera.h:160
colmap::CameraRig::RelativeQvec
Eigen::Vector4d & RelativeQvec(const camera_t camera_id)
Definition: camera_rig.cc:112
colmap::CorrespondenceGraph::Correspondence::point2D_idx
point2D_t point2D_idx
Definition: correspondence_graph.h:32
colmap::CameraRig::ComputeScale
double ComputeScale(const Reconstruction &reconstruction) const
Definition: camera_rig.cc:128
colmap::CameraRig::Snapshots
const std::vector< std::vector< image_t > > & Snapshots() const
Definition: camera_rig.cc:64
colmap::Camera::SetHeight
void SetHeight(const size_t height)
Definition: camera.h:166
image
std::shared_ptr< core::Tensor > image
Definition: FilamentRenderer.cpp:184
colmap::CameraRig::Check
void Check(const Reconstruction &reconstruction) const
Definition: camera_rig.cc:86
colmap::PinholeCameraModel::model_id
static const int model_id
Definition: camera_models.h:180
colmap::Image
Definition: image.h:29
colmap::Reconstruction::AddCamera
void AddCamera(const class Camera &camera)
Definition: reconstruction.cc:178
colmap::SimplePinholeCameraModel::model_id
static const int model_id
Definition: camera_models.h:167
colmap::CameraRig::RelativeTvec
Eigen::Vector3d & RelativeTvec(const camera_t camera_id)
Definition: camera_rig.cc:120
camera.h
colmap::CorrespondenceGraph::HasCorrespondences
bool HasCorrespondences(const image_t image_id, const point2D_t point2D_idx) const
Definition: correspondence_graph.h:176
colmap::Camera::ParamsData
const double * ParamsData() const
Definition: camera.h:184
colmap::kInvalidCameraId
const camera_t kInvalidCameraId
Definition: types.h:75
colmap::Camera::ModelId
int ModelId() const
Definition: camera.h:158
colmap::Camera::Rescale
void Rescale(const double scale)
Definition: camera.cc:237
colmap::Camera::HasPriorFocalLength
bool HasPriorFocalLength() const
Definition: camera.h:168
colmap::RadialCameraModel::model_id
static const int model_id
Definition: camera_models.h:210
colmap::Camera::PrincipalPointIdxs
const std::vector< size_t > & PrincipalPointIdxs() const
Definition: camera.cc:67
colmap::Image::SetTvec
void SetTvec(const Eigen::Vector3d &tvec)
Definition: image.h:333
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(TestEmpty)
Definition: camera_test.cc:40
colmap::Camera::FocalLength
double FocalLength() const
Definition: camera.cc:109
pose.h
colmap::Camera::FocalLengthIdxs
const std::vector< size_t > & FocalLengthIdxs() const
Definition: camera.cc:63
correspondence_graph.h
colmap::CameraRig::GetCameraIds
std::vector< camera_t > GetCameraIds() const
Definition: camera_rig.cc:55
colmap::Camera::SetFocalLengthY
void SetFocalLengthY(const double focal_length_y)
Definition: camera.cc:140
colmap::CorrespondenceGraph::Correspondence::image_id
image_t image_id
Definition: correspondence_graph.h:29
colmap::CorrespondenceGraph::IsTwoViewObservation
bool IsTwoViewObservation(const image_t image_id, const point2D_t point2D_idx) const
Definition: correspondence_graph.cc:250
colmap::Camera::SetModelId
void SetModelId(const int model_id)
Definition: camera.cc:51
colmap::SimplePinholeCameraModel::num_params
static const size_t num_params
Definition: camera_models.h:167
colmap::CorrespondenceGraph::FindCorrespondences
const std::vector< Correspondence > & FindCorrespondences(const image_t image_id, const point2D_t point2D_idx) const
Definition: correspondence_graph.h:171
colmap
Definition: AutomaticReconstructionController.h:17
colmap::CorrespondenceGraph::NumCorrespondencesBetweenImages
std::unordered_map< image_pair_t, point2D_t > NumCorrespondencesBetweenImages() const
Definition: correspondence_graph.cc:44
colmap::Camera::IsUndistorted
bool IsUndistorted() const
Definition: camera.cc:194
colmap::CorrespondenceGraph::NumCorrespondencesForImage
point2D_t NumCorrespondencesForImage(const image_t image_id) const
Definition: correspondence_graph.h:153
colmap::Camera::Height
size_t Height() const
Definition: camera.h:162
colmap::Camera::SetWidth
void SetWidth(const size_t width)
Definition: camera.h:164
colmap::Camera::NumParams
size_t NumParams() const
Definition: camera.h:174
colmap::kInvalidPoint2DIdx
const point2D_t kInvalidPoint2DIdx
Definition: types.h:79
colmap::FeatureMatches
std::vector< FeatureMatch > FeatureMatches
Definition: types.h:80
colmap::SimpleRadialCameraModel::num_params
static const size_t num_params
Definition: camera_models.h:196
colmap::StringPrintf
std::string StringPrintf(const char *format,...)
Definition: string.cc:131
colmap::CameraRig::ComputeAbsolutePose
void ComputeAbsolutePose(const size_t snapshot_idx, const Reconstruction &reconstruction, Eigen::Vector4d *abs_qvec, Eigen::Vector3d *abs_tvec) const
Definition: camera_rig.cc:230
colmap::CorrespondenceGraph::Finalize
void Finalize()
Definition: correspondence_graph.cc:54
colmap::CorrespondenceGraph::Correspondence
Definition: correspondence_graph.h:22
colmap::CorrespondenceGraph::AddImage
void AddImage(const image_t image_id, const size_t num_points2D)
Definition: correspondence_graph.cc:71
colmap::Camera::SetParams
void SetParams(const std::vector< double > &params)
Definition: camera.h:188
colmap::CorrespondenceGraph::ExistsImage
bool ExistsImage(const image_t image_id) const
Definition: correspondence_graph.h:144
colmap::CorrespondenceGraph::Correspondence::Correspondence
Correspondence()
Definition: correspondence_graph.h:23
colmap::Camera
Definition: camera.h:20
colmap::Camera::SetCameraId
void SetCameraId(const camera_t camera_id)
Definition: camera.h:156
colmap::Camera::InitializeWithId
void InitializeWithId(const int model_id, const double focal_length, const size_t width, const size_t height)
Definition: camera.cc:203
colmap::Camera::VerifyParams
bool VerifyParams() const
Definition: camera.cc:182
colmap::Reconstruction::Image
const class Image & Image(const image_t image_id) const
Definition: reconstruction.h:454
colmap::Camera::SetParamsFromString
bool SetParamsFromString(const std::string &string)
Definition: camera.cc:172
colmap::CameraRig::AddCamera
void AddCamera(const camera_t camera_id, const Eigen::Vector4d &rel_qvec, const Eigen::Vector3d &rel_tvec)
Definition: camera_rig.cc:68
camera_rig.h