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/depth_map.h"
33 
34 #include "base/warp.h"
35 #include "util/math.h"
36 
37 namespace colmap {
38 namespace mvs {
39 
DepthMap()40 DepthMap::DepthMap() : DepthMap(0, 0, -1.0f, -1.0f) {}
41 
DepthMap(const size_t width,const size_t height,const float depth_min,const float depth_max)42 DepthMap::DepthMap(const size_t width, const size_t height,
43                    const float depth_min, const float depth_max)
44     : Mat<float>(width, height, 1),
45       depth_min_(depth_min),
46       depth_max_(depth_max) {}
47 
DepthMap(const Mat<float> & mat,const float depth_min,const float depth_max)48 DepthMap::DepthMap(const Mat<float>& mat, const float depth_min,
49                    const float depth_max)
50     : Mat<float>(mat.GetWidth(), mat.GetHeight(), mat.GetDepth()),
51       depth_min_(depth_min),
52       depth_max_(depth_max) {
53   CHECK_EQ(mat.GetDepth(), 1);
54   data_ = mat.GetData();
55 }
56 
Rescale(const float factor)57 void DepthMap::Rescale(const float factor) {
58   if (width_ * height_ == 0) {
59     return;
60   }
61 
62   const size_t new_width = std::round(width_ * factor);
63   const size_t new_height = std::round(height_ * factor);
64   std::vector<float> new_data(new_width * new_height);
65   DownsampleImage(data_.data(), height_, width_, new_height, new_width,
66                   new_data.data());
67 
68   data_ = new_data;
69   width_ = new_width;
70   height_ = new_height;
71 
72   data_.shrink_to_fit();
73 }
74 
Downsize(const size_t max_width,const size_t max_height)75 void DepthMap::Downsize(const size_t max_width, const size_t max_height) {
76   if (height_ <= max_height && width_ <= max_width) {
77     return;
78   }
79   const float factor_x = static_cast<float>(max_width) / width_;
80   const float factor_y = static_cast<float>(max_height) / height_;
81   Rescale(std::min(factor_x, factor_y));
82 }
83 
ToBitmap(const float min_percentile,const float max_percentile) const84 Bitmap DepthMap::ToBitmap(const float min_percentile,
85                           const float max_percentile) const {
86   CHECK_GT(width_, 0);
87   CHECK_GT(height_, 0);
88 
89   Bitmap bitmap;
90   bitmap.Allocate(width_, height_, true);
91 
92   std::vector<float> valid_depths;
93   valid_depths.reserve(data_.size());
94   for (const float depth : data_) {
95     if (depth > 0) {
96       valid_depths.push_back(depth);
97     }
98   }
99 
100   if (valid_depths.empty()) {
101     bitmap.Fill(BitmapColor<uint8_t>(0));
102     return bitmap;
103   }
104 
105   const float robust_depth_min = Percentile(valid_depths, min_percentile);
106   const float robust_depth_max = Percentile(valid_depths, max_percentile);
107 
108   const float robust_depth_range = robust_depth_max - robust_depth_min;
109   for (size_t y = 0; y < height_; ++y) {
110     for (size_t x = 0; x < width_; ++x) {
111       const float depth = Get(y, x);
112       if (depth > 0) {
113         const float robust_depth =
114             std::max(robust_depth_min, std::min(robust_depth_max, depth));
115         const float gray =
116             (robust_depth - robust_depth_min) / robust_depth_range;
117         const BitmapColor<float> color(255 * JetColormap::Red(gray),
118                                        255 * JetColormap::Green(gray),
119                                        255 * JetColormap::Blue(gray));
120         bitmap.SetPixel(x, y, color.Cast<uint8_t>());
121       } else {
122         bitmap.SetPixel(x, y, BitmapColor<uint8_t>(0));
123       }
124     }
125   }
126 
127   return bitmap;
128 }
129 
130 }  // namespace mvs
131 }  // namespace colmap
132