licornea_tools
depth_densify_mine.cc
Go to the documentation of this file.
1 #include "depth_densify_mine.h"
2 #include "../common.h"
3 #include <vector>
4 #include <algorithm>
5 
6 namespace tlz {
7 
8 
9 void depth_densify_mine::densify(const std::vector<sample>& orig_samples, cv::Mat_<real>& out_, cv::Mat_<uchar>& out_mask) {
10  out_ = cv::Mat_<real>(texture_height, texture_width);
11  out_mask = cv::Mat_<uchar>(texture_height, texture_width);
12 
13  std::vector<sample> samples = orig_samples;
14  auto cmp = [](const sample& a, const sample& b) { return (a.color_depth > b.color_depth); };
15  std::sort(samples.begin(), samples.end(), cmp);
16 
17  cv::Mat_<float> out(out_.size());
18 
19  cv::Mat_<uchar> sparse_mask(texture_height, texture_width);
20  cv::Mat_<real> sparse(texture_height, texture_width);
21  sparse_mask.setTo(0);
22 
23  int shadow_width = 3;
24  real shadow_min_depth_diff = 100.0;
25  for(const sample& samp : samples) {
26  int sx = samp.color_coordinates[0], sy = samp.color_coordinates[1];
27  if(sx < 0 || sx >= texture_width || sy < 0 || sy >= texture_height) continue;
28 
29  real new_d = samp.color_depth;
30 
31  // cast shadow
32  int min_x = std::max(sx - shadow_width, 0), max_x = std::min(sx + shadow_width, (int)texture_width-1);
33  int min_y = std::max(sy - shadow_width, 0), max_y = std::min(sy + shadow_width, (int)texture_height-1);
34  for(int x = min_x; x <= max_x; ++x) if(x != sx)
35  for(int y = min_y; y <= max_y; ++y) if(y != sy) {
36  if(! sparse_mask(y, x)) continue;
37  if(sparse(y, x) - new_d > shadow_min_depth_diff) sparse_mask(y, x) = 0;
38  }
39 
40  sparse(sy, sx) = new_d;
41  sparse_mask(sy, sx) = 0xff;
42  }
43 
44  out.setTo(0.0);
45  out_mask.setTo(0);
46 
47  /*
48  out=sparse;
49  out.setTo(0, sparse_mask==0);
50  out_mask=sparse_mask;
51  return;
52  */
53 
54  int rad = 5;
55  int rad_sq = rad*rad;
56 
57  #pragma omp parallel for
58  for(int px = 0; px < texture_width; ++px)
59  for(int py = 0; py < texture_height; ++py) {
60  uchar& mask = out_mask(py, px);
61  float& d = out(py, px);
62 
63  if(sparse_mask(py, px)) {
64  d = sparse(py, px);
65  mask = 0xff;
66  //continue;
67  }
68 
69  int min_dist = rad*rad;
70  real min_dist_d = 0.0;
71 
72  real max_d = 0.0;
73 
74  int samples_count = 0;
75 
76  int accept_dist = 3;
77 
78  int min_x = std::max(px - rad, 0), max_x = std::min(px + rad, (int)texture_width-1);
79  int min_y = std::max(py - rad, 0), max_y = std::min(py + rad, (int)texture_height-1);
80  for(int sx = min_x; sx <= max_x; ++sx) if(sx != px)
81  for(int sy = min_y; sy <= max_y; ++sy) if(sy != py) {
82  if(! sparse_mask(sy, sx)) continue;
83 
84  real sd = sparse(sy, sx);
85 
86  int off_x = sx - px, off_y = sy - py;
87 
88  int dist = std::max(std::abs(off_x), std::abs(off_y));
89  int euc_sq_dist = off_x*off_x + off_y*off_y;
90 
91  if(euc_sq_dist > rad_sq) continue;
92 
93  samples_count++;
94 
95  //if(sx % 100 != 0 || sy % 100 != 0) continue;
96 
97  if(dist < min_dist) {
98  min_dist = dist;
99  min_dist_d = sd;
100  }
101 
102  if(sd > max_d) {
103  max_d = sd;
104  }
105  }
106 
107  if(samples_count > 0) {
108  mask = 0xff;
109  if(min_dist < accept_dist) d = min_dist_d;
110  else d = max_d;
111  }
112  }
113 
114  cv::medianBlur(out, out, 3);
115 
116  out_ = out;
117 }
118 
119 }
constexpr std::size_t texture_height
Definition: common.h:13
void densify(const std::vector< sample > &samples, cv::Mat_< real > &out, cv::Mat_< uchar > &out_mask) override
constexpr std::size_t texture_width
Definition: common.h:12
double real
Definition: common.h:16