ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
image.cc
Go to the documentation of this file.
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 #include "mvs/image.h"
33 
34 #include <Eigen/Core>
35 
36 #include "base/projection.h"
37 #include "util/logging.h"
38 
39 namespace colmap {
40 namespace mvs {
41 
43 
44 Image::Image(const std::string& path, const size_t width, const size_t height,
45  const float* K, const float* R, const float* T)
46  : path_(path), width_(width), height_(height) {
47  memcpy(K_, K, 9 * sizeof(float));
48  memcpy(R_, R, 9 * sizeof(float));
49  memcpy(T_, T, 3 * sizeof(float));
50  ComposeProjectionMatrix(K_, R_, T_, P_);
51  ComposeInverseProjectionMatrix(K_, R_, T_, inv_P_);
52 }
53 
54 void Image::SetBitmap(const Bitmap& bitmap) {
55  bitmap_ = bitmap;
56  CHECK_EQ(width_, bitmap_.Width());
57  CHECK_EQ(height_, bitmap_.Height());
58 }
59 
60 void Image::Rescale(const float factor) { Rescale(factor, factor); }
61 
62 void Image::Rescale(const float factor_x, const float factor_y) {
63  const size_t new_width = std::round(width_ * factor_x);
64  const size_t new_height = std::round(height_ * factor_y);
65 
66  if (bitmap_.Data() != nullptr) {
67  bitmap_.Rescale(new_width, new_height);
68  }
69 
70  const float scale_x = new_width / static_cast<float>(width_);
71  const float scale_y = new_height / static_cast<float>(height_);
72  K_[0] *= scale_x;
73  K_[2] *= scale_x;
74  K_[4] *= scale_y;
75  K_[5] *= scale_y;
76  ComposeProjectionMatrix(K_, R_, T_, P_);
77  ComposeInverseProjectionMatrix(K_, R_, T_, inv_P_);
78 
79  width_ = new_width;
80  height_ = new_height;
81 }
82 
83 void Image::Downsize(const size_t max_width, const size_t max_height) {
84  if (width_ <= max_width && height_ <= max_height) {
85  return;
86  }
87  const float factor_x = static_cast<float>(max_width) / width_;
88  const float factor_y = static_cast<float>(max_height) / height_;
89  Rescale(std::min(factor_x, factor_y));
90 }
91 
92 void ComputeRelativePose(const float R1[9], const float T1[3],
93  const float R2[9], const float T2[3], float R[9],
94  float T[3]) {
95  const Eigen::Map<const Eigen::Matrix<float, 3, 3, Eigen::RowMajor>> R1_m(R1);
96  const Eigen::Map<const Eigen::Matrix<float, 3, 3, Eigen::RowMajor>> R2_m(R2);
97  const Eigen::Map<const Eigen::Matrix<float, 3, 1>> T1_m(T1);
98  const Eigen::Map<const Eigen::Matrix<float, 3, 1>> T2_m(T2);
99  Eigen::Map<Eigen::Matrix<float, 3, 3, Eigen::RowMajor>> R_m(R);
100  Eigen::Map<Eigen::Vector3f> T_m(T);
101 
102  R_m = R2_m * R1_m.transpose();
103  T_m = T2_m - R_m * T1_m;
104 }
105 
106 void ComposeProjectionMatrix(const float K[9], const float R[9],
107  const float T[3], float P[12]) {
108  Eigen::Map<Eigen::Matrix<float, 3, 4, Eigen::RowMajor>> P_m(P);
109  P_m.leftCols<3>() =
110  Eigen::Map<const Eigen::Matrix<float, 3, 3, Eigen::RowMajor>>(R);
111  P_m.rightCols<1>() = Eigen::Map<const Eigen::Vector3f>(T);
112  P_m = Eigen::Map<const Eigen::Matrix<float, 3, 3, Eigen::RowMajor>>(K) * P_m;
113 }
114 
115 void ComposeInverseProjectionMatrix(const float K[9], const float R[9],
116  const float T[3], float inv_P[12]) {
117  Eigen::Matrix<float, 4, 4, Eigen::RowMajor> P;
118  ComposeProjectionMatrix(K, R, T, P.data());
119  P.row(3) = Eigen::Vector4f(0, 0, 0, 1);
120  const Eigen::Matrix4f inv_P_temp = P.inverse();
121  Eigen::Map<Eigen::Matrix<float, 3, 4, Eigen::RowMajor>> inv_P_m(inv_P);
122  inv_P_m = inv_P_temp.topRows<3>();
123 }
124 
125 void ComputeProjectionCenter(const float R[9], const float T[3], float C[3]) {
126  const Eigen::Map<const Eigen::Matrix<float, 3, 3, Eigen::RowMajor>> R_m(R);
127  const Eigen::Map<const Eigen::Matrix<float, 3, 1>> T_m(T);
128  Eigen::Map<Eigen::Vector3f> C_m(C);
129  C_m = -R_m.transpose() * T_m;
130 }
131 
132 void RotatePose(const float RR[9], float R[9], float T[3]) {
133  Eigen::Map<Eigen::Matrix<float, 3, 3, Eigen::RowMajor>> R_m(R);
134  Eigen::Map<Eigen::Matrix<float, 3, 1>> T_m(T);
135  const Eigen::Map<const Eigen::Matrix<float, 3, 3, Eigen::RowMajor>> RR_m(RR);
136  R_m = RR_m * R_m;
137  T_m = RR_m * T_m;
138 }
139 
140 } // namespace mvs
141 } // namespace colmap
int width
int height
void Rescale(const int new_width, const int new_height, const FREE_IMAGE_FILTER filter=FILTER_BILINEAR)
Definition: bitmap.cc:581
int Width() const
Definition: bitmap.h:249
const FIBITMAP * Data() const
Definition: bitmap.h:247
int Height() const
Definition: bitmap.h:250
void SetBitmap(const Bitmap &bitmap)
Definition: image.cc:54
void Rescale(const float factor)
Definition: image.cc:60
void Downsize(const size_t max_width, const size_t max_height)
Definition: image.cc:83
static const std::string path
Definition: PointCloud.cpp:59
void RotatePose(const float RR[9], float R[9], float T[3])
Definition: image.cc:132
void ComposeInverseProjectionMatrix(const float K[9], const float R[9], const float T[3], float inv_P[12])
Definition: image.cc:115
void ComputeProjectionCenter(const float R[9], const float T[3], float C[3])
Definition: image.cc:125
void ComposeProjectionMatrix(const float K[9], const float R[9], const float T[3], float P[12])
Definition: image.cc:106
void ComputeRelativePose(const float R1[9], const float T1[3], const float R2[9], const float T2[3], float R[9], float T[3])
Definition: image.cc:92