licornea_tools
read_feature_depths.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <utility>
3 #include <vector>
4 #include <cmath>
5 #include <algorithm>
6 #include <fstream>
7 #include <set>
8 #include <map>
9 #include <atomic>
10 #include <limits>
12 #include "../lib/args.h"
13 #include "../lib/misc.h"
14 #include "../lib/dataset.h"
15 #include "../lib/image_io.h"
16 #include "../lib/json.h"
17 #include "../lib/opencv.h"
18 #include "../lib/filesystem.h"
19 
20 using namespace tlz;
21 
22 int main(int argc, const char* argv[]) {
23  get_args(argc, argv, "dataset_parameters.json in_cors.json out_cors.json [xy_outreach=0] [step=1]");
24  dataset datas = dataset_arg();
26  std::string out_cors_filename = out_filename_arg();
27  int xy_outreach = int_opt_arg(0);
28  int step = int_opt_arg(1);
29 
30  std::cout << "for each view, reading feature depths" << std::endl;
31 
32  auto views = get_all_views(cors);
33 
34  std::atomic<int> counter(0);
35  #pragma omp parallel for
36  for(std::ptrdiff_t i = 0; i < views.size(); ++i) {
37  ++counter;
38  if(i % step != 0) continue;
39 
40  const view_index& view_idx = views[i];
41 
42  std::string depth_filename = datas.view(view_idx).depth_filename();
43  if(! file_exists(depth_filename)) continue;
44  cv::Mat_<ushort> depth = load_depth(depth_filename);
45 
46 
47  for(auto& kv : cors.features) {
48  const std::string& feature_name = kv.first;
49  auto& fpoints = kv.second.points;
50  if(fpoints.find(view_idx) == fpoints.end()) continue;
51 
52  feature_point& fpoint = fpoints.at(view_idx);
53  int x = fpoint.position[0], y = fpoint.position[1];
54 
55  if(xy_outreach == 0) {
56  if(x < 0 || x >= depth.cols || y < 0 || y >= depth.rows) continue;
57  ushort depth_value = depth(y, x);
58  if(depth_value != 0) fpoint.depth = depth_value;
59 
60  } else {
61  ushort min_depth_value = std::numeric_limits<ushort>::max();
62  for(int y_ = y - xy_outreach; y_ <= y + xy_outreach; y_++)
63  for(int x_ = x - xy_outreach; x_ <= x + xy_outreach; x_++) {
64  if(x_ < 0 || x_ >= depth.cols || y_ < 0 || y_ >= depth.rows) continue;
65  ushort depth_value = depth(y, x);
66  if(depth_value < min_depth_value) min_depth_value = depth_value;
67  }
68  if(min_depth_value != 0) fpoint.depth = min_depth_value;
69 
70  }
71  }
72 
73  std::cout << '.' << std::flush;
74 
75  if(counter/step % 1000 == 0) {
76  #pragma omp critical
77  std::cout << '\n' << counter << " of " << views.size() << std::endl;
78  }
79  }
80 
81  std::cout << "saving correspondences with depths" << std::endl;
82  export_image_corresponcences(cors, out_cors_filename);
83 }
84 
long int_opt_arg(long def)
Definition: args.h:50
bool file_exists(const std::string &filename)
std::vector< view_index > get_all_views(const image_correspondences &cors)
Set of features, each on set of views.
cv::Mat_< ushort > load_depth(const std::string &filename)
Definition: image_io.cc:35
dataset_view view(int x) const
Definition: dataset.cc:243
std::string depth_filename() const
Definition: dataset.cc:68
std::map< std::string, image_correspondence_feature > features
int main(int argc, const char *argv[])
dataset dataset_arg()
Definition: dataset.cc:297
image_correspondences image_correspondences_arg()
std::string out_filename_arg()
Definition: args.cc:104
void export_image_corresponcences(const image_correspondences &cors, const std::string &filename)
void get_args(int argc, const char *argv[], const std::string &usage)
Definition: args.cc:49