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_MVS_IMAGE_H_
33 #define COLMAP_SRC_MVS_IMAGE_H_
34 
35 #include <cstdint>
36 #include <fstream>
37 #include <set>
38 #include <string>
39 #include <unordered_map>
40 #include <vector>
41 
42 #include "util/bitmap.h"
43 
44 namespace colmap {
45 namespace mvs {
46 
47 class Image {
48  public:
49   Image();
50   Image(const std::string& path, const size_t width, const size_t height,
51         const float* K, const float* R, const float* T);
52 
53   inline size_t GetWidth() const;
54   inline size_t GetHeight() const;
55 
56   void SetBitmap(const Bitmap& bitmap);
57   inline const Bitmap& GetBitmap() const;
58 
59   inline const std::string& GetPath() const;
60   inline const float* GetR() const;
61   inline const float* GetT() const;
62   inline const float* GetK() const;
63   inline const float* GetP() const;
64   inline const float* GetInvP() const;
65   inline const float* GetViewingDirection() const;
66 
67   void Rescale(const float factor);
68   void Rescale(const float factor_x, const float factor_y);
69   void Downsize(const size_t max_width, const size_t max_height);
70 
71  private:
72   std::string path_;
73   size_t width_;
74   size_t height_;
75   float K_[9];
76   float R_[9];
77   float T_[3];
78   float P_[12];
79   float inv_P_[12];
80   Bitmap bitmap_;
81 };
82 
83 void ComputeRelativePose(const float R1[9], const float T1[3],
84                          const float R2[9], const float T2[3], float R[9],
85                          float T[3]);
86 
87 void ComposeProjectionMatrix(const float K[9], const float R[9],
88                              const float T[3], float P[12]);
89 
90 void ComposeInverseProjectionMatrix(const float K[9], const float R[9],
91                                     const float T[3], float inv_P[12]);
92 
93 void ComputeProjectionCenter(const float R[9], const float T[3], float C[3]);
94 
95 void RotatePose(const float RR[9], float R[9], float T[3]);
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 // Implementation
99 ////////////////////////////////////////////////////////////////////////////////
100 
GetWidth()101 size_t Image::GetWidth() const { return width_; }
102 
GetHeight()103 size_t Image::GetHeight() const { return height_; }
104 
GetBitmap()105 const Bitmap& Image::GetBitmap() const { return bitmap_; }
106 
GetPath()107 const std::string& Image::GetPath() const { return path_; }
108 
GetR()109 const float* Image::GetR() const { return R_; }
110 
GetT()111 const float* Image::GetT() const { return T_; }
112 
GetK()113 const float* Image::GetK() const { return K_; }
114 
GetP()115 const float* Image::GetP() const { return P_; }
116 
GetInvP()117 const float* Image::GetInvP() const { return inv_P_; }
118 
GetViewingDirection()119 const float* Image::GetViewingDirection() const { return &R_[6]; }
120 
121 }  // namespace mvs
122 }  // namespace colmap
123 
124 #endif  // COLMAP_SRC_MVS_IMAGE_H_
125