1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 //       notice, this list of conditions and the following disclaimer.
9 //
10 //     * Redistributions in binary form must reproduce the above copyright
11 //       notice, this list of conditions and the following disclaimer in the
12 //       documentation and/or other materials provided with the distribution.
13 //
14 //     * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 //       its contributors may be used to endorse or promote products derived
16 //       from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #ifndef COLMAP_SRC_BASE_IMAGE_READER_H_
33 #define COLMAP_SRC_BASE_IMAGE_READER_H_
34 
35 #include <unordered_set>
36 
37 #include "base/database.h"
38 #include "util/bitmap.h"
39 #include "util/threading.h"
40 
41 namespace colmap {
42 
43 struct ImageReaderOptions {
44   // Path to database in which to store the extracted data.
45   std::string database_path = "";
46 
47   // Root path to folder which contains the images.
48   std::string image_path = "";
49 
50   // Optional root path to folder which contains image masks. For a given image,
51   // the corresponding mask must have the same sub-path below this root as the
52   // image has below image_path. The filename must be equal, aside from the
53   // added extension .png. For example, for an image image_path/abc/012.jpg, the
54   // mask would be mask_path/abc/012.jpg.png. No features will be extracted in
55   // regions where the mask image is black (pixel intensity value 0 in
56   // grayscale).
57   std::string mask_path = "";
58 
59   // Optional list of images to read. The list must contain the relative path
60   // of the images with respect to the image_path.
61   std::vector<std::string> image_list;
62 
63   // Name of the camera model.
64   std::string camera_model = "SIMPLE_RADIAL";
65 
66   // Whether to use the same camera for all images.
67   bool single_camera = false;
68 
69   // Whether to use the same camera for all images in the same sub-folder.
70   bool single_camera_per_folder = false;
71 
72   // Whether to explicitly use an existing camera for all images. Note that in
73   // this case the specified camera model and parameters are ignored.
74   int existing_camera_id = kInvalidCameraId;
75 
76   // Manual specification of camera parameters. If empty, camera parameters
77   // will be extracted from EXIF, i.e. principal point and focal length.
78   std::string camera_params = "";
79 
80   // If camera parameters are not specified manually and the image does not
81   // have focal length EXIF information, the focal length is set to the
82   // value `default_focal_length_factor * max(width, height)`.
83   double default_focal_length_factor = 1.2;
84 
85   // Optional path to an image file specifying a mask for all images. No
86   // features will be extracted in regions where the mask is black (pixel
87   // intensity value 0 in grayscale).
88   std::string camera_mask_path = "";
89 
90   bool Check() const;
91 };
92 
93 // Recursively iterate over the images in a directory. Skips an image if it
94 // already exists in the database. Extracts the camera intrinsics from EXIF and
95 // writes the camera information to the database.
96 class ImageReader {
97  public:
98   enum class Status {
99     FAILURE,
100     SUCCESS,
101     IMAGE_EXISTS,
102     BITMAP_ERROR,
103     CAMERA_SINGLE_DIM_ERROR,
104     CAMERA_EXIST_DIM_ERROR,
105     CAMERA_PARAM_ERROR
106   };
107 
108   ImageReader(const ImageReaderOptions& options, Database* database);
109 
110   Status Next(Camera* camera, Image* image, Bitmap* bitmap, Bitmap* mask);
111   size_t NextIndex() const;
112   size_t NumImages() const;
113 
114  private:
115   // Image reader options.
116   ImageReaderOptions options_;
117   Database* database_;
118   // Index of previously processed image.
119   size_t image_index_;
120   // Previously processed camera.
121   Camera prev_camera_;
122   // Names of image sub-folders.
123   std::string prev_image_folder_;
124   std::unordered_set<std::string> image_folders_;
125 };
126 
127 }  // namespace colmap
128 
129 #endif  // COLMAP_SRC_BASE_IMAGE_READER_H_
130