ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
image_reader.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_set>
11 
12 #include "base/database.h"
13 #include "util/bitmap.h"
14 #include "util/threading.h"
15 
16 namespace colmap {
17 
19  // Path to database in which to store the extracted data.
20  std::string database_path = "";
21 
22  // Root path to folder which contains the images.
23  std::string image_path = "";
24 
25  // Optional root path to folder which contains image masks. For a given
26  // image, the corresponding mask must have the same sub-path below this root
27  // as the image has below image_path. The filename must be equal, aside from
28  // the added extension .png. For example, for an image
29  // image_path/abc/012.jpg, the mask would be mask_path/abc/012.jpg.png. No
30  // features will be extracted in regions where the mask image is black
31  // (pixel intensity value 0 in grayscale).
32  std::string mask_path = "";
33 
34  // Optional list of images to read. The list must contain the relative path
35  // of the images with respect to the image_path.
36  std::vector<std::string> image_list;
37 
38  // Name of the camera model.
39  std::string camera_model = "SIMPLE_RADIAL";
40 
41  // Whether to use the same camera for all images.
42  bool single_camera = false;
43 
44  // Whether to use the same camera for all images in the same sub-folder.
46 
47  // Whether to use a different camera for each image.
49 
50  // Whether to explicitly use an existing camera for all images. Note that in
51  // this case the specified camera model and parameters are ignored.
53 
54  // Manual specification of camera parameters. If empty, camera parameters
55  // will be extracted from EXIF, i.e. principal point and focal length.
56  std::string camera_params = "";
57 
58  // If camera parameters are not specified manually and the image does not
59  // have focal length EXIF information, the focal length is set to the
60  // value `default_focal_length_factor * max(width, height)`.
62 
63  // Optional path to an image file specifying a mask for all images. No
64  // features will be extracted in regions where the mask is black (pixel
65  // intensity value 0 in grayscale).
66  std::string camera_mask_path = "";
67 
68  bool Check() const;
69 };
70 
71 // Recursively iterate over the images in a directory. Skips an image if it
72 // already exists in the database. Extracts the camera intrinsics from EXIF and
73 // writes the camera information to the database.
74 class ImageReader {
75 public:
76  enum class Status {
77  FAILURE,
78  SUCCESS,
84  };
85 
86  ImageReader(const ImageReaderOptions& options, Database* database);
87 
88  Status Next(Camera* camera, Image* image, Bitmap* bitmap, Bitmap* mask);
89  size_t NextIndex() const;
90  size_t NumImages() const;
91 
92 private:
93  // Image reader options.
94  ImageReaderOptions options_;
95  Database* database_;
96  // Index of previously processed image.
97  size_t image_index_;
98  // Previously processed camera.
99  Camera prev_camera_;
100  std::unordered_map<std::string, camera_t> camera_model_to_id_;
101  // Names of image sub-folders.
102  std::string prev_image_folder_;
103  std::unordered_set<std::string> image_folders_;
104 };
105 
106 } // namespace colmap
colmap::ImageReader::Status::CAMERA_SINGLE_DIM_ERROR
@ CAMERA_SINGLE_DIM_ERROR
colmap::ImageReader::Status::CAMERA_EXIST_DIM_ERROR
@ CAMERA_EXIST_DIM_ERROR
colmap::ImageReaderOptions::image_path
std::string image_path
Definition: image_reader.h:23
colmap::Bitmap::ExifCameraModel
bool ExifCameraModel(std::string *camera_model) const
Definition: bitmap.cc:306
colmap::ImageReader::Status::IMAGE_EXISTS
@ IMAGE_EXISTS
camera_models.h
colmap::ExistsFile
bool ExistsFile(const std::string &path)
Definition: misc.cc:100
colmap::ImageReader::ImageReader
ImageReader(const ImageReaderOptions &options, Database *database)
Definition: image_reader.cc:50
bitmap.h
colmap::Database::ReadCamera
Camera ReadCamera(const camera_t camera_id) const
Definition: database.cc:365
colmap::ImageReader::Status::FAILURE
@ FAILURE
colmap::JoinPaths
std::string JoinPaths(T const &... paths)
Definition: misc.h:128
colmap::Bitmap
Definition: bitmap.h:51
colmap::GetRelativePath
std::string GetRelativePath(const std::string &from, const std::string &to)
Definition: misc.cc:132
colmap::ImageReaderOptions::mask_path
std::string mask_path
Definition: image_reader.h:32
colmap::Bitmap::ExifFocalLength
bool ExifFocalLength(double *focal_length) const
Definition: bitmap.cc:336
colmap::Bitmap::ExifLongitude
bool ExifLongitude(double *longitude) const
Definition: bitmap.cc:444
colmap::GetRecursiveFileList
std::vector< std::string > GetRecursiveFileList(const std::string &path)
Definition: misc.cc:183
colmap::ImageReaderOptions::image_list
std::vector< std::string > image_list
Definition: image_reader.h:36
colmap::Camera::SetModelIdFromName
void SetModelIdFromName(const std::string &model_name)
Definition: camera.cc:57
colmap::Database
Definition: database.h:29
colmap::ImageReaderOptions::camera_model
std::string camera_model
Definition: image_reader.h:39
colmap::Camera::CameraId
camera_t CameraId() const
Definition: camera.h:154
colmap::ExistsCameraModelWithName
bool ExistsCameraModelWithName(const std::string &model_name)
Definition: camera_models.cc:94
colmap::ImageReader::NextIndex
size_t NextIndex() const
Definition: image_reader.cc:260
colmap::ImageReaderOptions::single_camera_per_folder
bool single_camera_per_folder
Definition: image_reader.h:45
colmap::Camera::SetPriorFocalLength
void SetPriorFocalLength(const bool prior)
Definition: camera.h:170
colmap::Database::ExistsKeypoints
bool ExistsKeypoints(const image_t image_id) const
Definition: database.cc:307
colmap::Database::ExistsCamera
bool ExistsCamera(const camera_t camera_id) const
Definition: database.cc:295
CHECK_OPTION
#define CHECK_OPTION(expr)
Definition: logging.h:20
colmap::ImageReaderOptions::existing_camera_id
int existing_camera_id
Definition: image_reader.h:52
colmap::Bitmap::Width
int Width() const
Definition: bitmap.h:249
colmap::DatabaseTransaction
Definition: database.h:323
image_reader.h
misc.h
database.h
colmap::ImageReaderOptions::database_path
std::string database_path
Definition: image_reader.h:20
colmap::Database::WriteCamera
camera_t WriteCamera(const Camera &camera, const bool use_camera_id=false) const
Definition: database.cc:603
colmap::Bitmap::Height
int Height() const
Definition: bitmap.h:250
colmap::ImageReader::Status
Status
Definition: image_reader.h:76
colmap::CameraModelVerifyParams
bool CameraModelVerifyParams(const int model_id, const std::vector< double > &params)
Definition: camera_models.cc:215
colmap::Camera::Width
size_t Width() const
Definition: camera.h:160
colmap::ImageReaderOptions::single_camera
bool single_camera
Definition: image_reader.h:42
colmap::Bitmap::ExifAltitude
bool ExifAltitude(double *altitude) const
Definition: bitmap.cc:472
colmap::Database::ExistsImageWithName
bool ExistsImageWithName(std::string name) const
Definition: database.cc:303
colmap::ImageReaderOptions
Definition: image_reader.h:18
colmap::ImageReader::Next
Status Next(Camera *camera, Image *image, Bitmap *bitmap, Bitmap *mask)
Definition: image_reader.cc:86
colmap::Camera::SetHeight
void SetHeight(const size_t height)
Definition: camera.h:166
colmap::Database::ReadImageWithName
Image ReadImageWithName(const std::string &name) const
Definition: database.cc:407
colmap::ImageReader::Status::BITMAP_ERROR
@ BITMAP_ERROR
image
std::shared_ptr< core::Tensor > image
Definition: FilamentRenderer.cpp:184
colmap::Image
Definition: image.h:29
colmap::ImageReaderOptions::default_focal_length_factor
double default_focal_length_factor
Definition: image_reader.h:61
colmap::ImageReader::Status::SUCCESS
@ SUCCESS
colmap::ImageReader
Definition: image_reader.h:74
colmap::ImageReader::Status::CAMERA_PARAM_ERROR
@ CAMERA_PARAM_ERROR
colmap::Database::ExistsDescriptors
bool ExistsDescriptors(const image_t image_id) const
Definition: database.cc:311
colmap::kInvalidCameraId
const camera_t kInvalidCameraId
Definition: types.h:75
colmap::Camera::ModelId
int ModelId() const
Definition: camera.h:158
threading.h
colmap::ImageReader::NumImages
size_t NumImages() const
Definition: image_reader.cc:262
colmap::Bitmap::Read
bool Read(const std::string &path, const bool as_rgb=true)
Definition: bitmap.cc:485
colmap::GetParentDir
std::string GetParentDir(const std::string &path)
Definition: misc.cc:128
colmap::EnsureTrailingSlash
std::string EnsureTrailingSlash(const std::string &str)
Definition: misc.cc:40
colmap::Bitmap::ExifLatitude
bool ExifLatitude(double *latitude) const
Definition: bitmap.cc:416
colmap::ImageReaderOptions::single_camera_per_image
bool single_camera_per_image
Definition: image_reader.h:48
colmap::CameraModelNameToId
int CameraModelNameToId(const std::string &model_name)
Definition: camera_models.cc:102
colmap
Definition: AutomaticReconstructionController.h:17
colmap::StringReplace
std::string StringReplace(const std::string &str, const std::string &old_str, const std::string &new_str)
Definition: string.cc:140
colmap::ImageReaderOptions::Check
bool Check() const
Definition: image_reader.cc:39
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::ImageReaderOptions::camera_params
std::string camera_params
Definition: image_reader.h:56
colmap::camera_t
uint32_t camera_t
Definition: types.h:58
CHECK_OPTION_GT
#define CHECK_OPTION_GT(val1, val2)
Definition: logging.h:34
colmap::ImageReaderOptions::camera_mask_path
std::string camera_mask_path
Definition: image_reader.h:66
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::Camera::SetParamsFromString
bool SetParamsFromString(const std::string &string)
Definition: camera.cc:172