licornea_tools
cg_measure_optical_flow_slopes.cc
Go to the documentation of this file.
1 #include <string>
2 #include <map>
3 #include <cmath>
6 #include "../lib/misc.h"
7 #include "../lib/json.h"
8 #include "../lib/dataset.h"
9 #include "../lib/opencv.h"
10 #include "../lib/intrinsics.h"
11 
12 using namespace tlz;
13 
15  std::vector<cv::Vec2f> points;
16  for(const auto& kv : feature.points) {
17  const view_index& idx = kv.first;
18  const feature_point& fpoint = kv.second;
19  if(std::abs(idx.y - feature.reference_view.y) <= y_outreach)
20  points.push_back(fpoint.position);
21  }
22 
23  cv::Vec4f line_parameters;
24  cv::fitLine(points, line_parameters, CV_DIST_L2, 0.0, 0.01, 0.01);
25 
26  return line_parameters[1] / line_parameters[0];
27 }
28 
30  std::vector<cv::Vec2f> points;
31  for(const auto& kv : feature.points) {
32  const view_index& idx = kv.first;
33  const feature_point& fpoint = kv.second;
34  if(idx.x == feature.reference_view.x)
35  points.push_back(fpoint.position);
36  }
37 
38  cv::Vec4f line_parameters;
39  cv::fitLine(points, line_parameters, CV_DIST_L2, 0.0, 0.01, 0.01);
40 
41  return line_parameters[0] / line_parameters[1];
42 }
43 
44 
45 int main(int argc, const char* argv[]) {
46  get_args(argc, argv, "dataset_parameters.json image_correspondences.json intrinsics.json out_slopes.json");
47  dataset datas = dataset_arg();
49  intrinsics intr = intrinsics_arg();
50  std::string out_slopes_filename = out_filename_arg();
51  int y_outreach = 3;
52 
53  std::cout << "undistorting image correspondences (if applicable)" << std::endl;
54  image_correspondences cors = undistort(dist_cors, intr);
55 
56  std::cout << "measuring slopes" << std::endl;
57  std::map<std::string, real> feature_horizontal_slopes, feature_vertical_slopes;
58  for(const auto& kv : cors.features) {
59  const std::string& feature_name = kv.first;
60  const image_correspondence_feature& feature = kv.second;
61 
62  feature_horizontal_slopes[feature_name] = measure_horizontal_slope(feature, y_outreach);
63  feature_vertical_slopes[feature_name] = measure_vertical_slope(feature);
64 
65  std::cout << '.' << std::flush;
66  }
67  std::cout << std::endl;
68 
69 
70  std::cout << "saving slopes" << std::endl;
71  auto ref_views = get_reference_views(cors);
72  feature_slopes fslopes;
73  for(const view_index& ref_idx : ref_views) {
75  feature_points ref_fpoints = undistorted_feature_points_for_view(ref_cors, ref_idx, intr);
76  feature_slopes ref_fslopes(ref_fpoints);
77  for(auto& kv : ref_cors.features) {
78  const std::string& feature_name = kv.first;
79  feature_slope& fslope = ref_fslopes.slopes[feature_name];
80  fslope.horizontal = feature_horizontal_slopes.at(feature_name);
81  fslope.vertical = feature_vertical_slopes.at(feature_name);
82  fslopes = merge_multiview_feature_slopes(fslopes, ref_fslopes);
83  }
84  }
85  export_json_file(encode_feature_slopes(fslopes), out_slopes_filename);
86 }
json encode_feature_slopes(const feature_slopes &fslopes)
real measure_horizontal_slope(const image_correspondence_feature &feature, int y_outreach)
int main(int argc, const char *argv[])
feature_points undistorted_feature_points_for_view(const image_correspondences &cors, view_index idx, const intrinsics &intr)
image_correspondences image_correspondences_with_reference(const image_correspondences &cors, const view_index &reference_view)
Set of features, each on set of views.
Points of different features, on one view.
std::map< std::string, image_correspondence_feature > features
intrinsics intrinsics_arg()
Definition: intrinsics.cc:119
std::vector< view_index > get_reference_views(const relative_camera_positions &rcpos)
dataset dataset_arg()
Definition: dataset.cc:297
double real
Definition: common.h:16
void export_json_file(const json &j, const std::string &filename, bool compact)
Definition: json.cc:9
image_correspondences image_correspondences_arg()
feature_points undistort(const feature_points &dist_fpoints, const intrinsics &intr)
std::string out_filename_arg()
Definition: args.cc:104
std::map< std::string, feature_slope > slopes
Feature on set of views. Optionally one view is "reference".
feature_slopes merge_multiview_feature_slopes(const feature_slopes &a, const feature_slopes &b)
std::map< view_index, feature_point > points
real measure_vertical_slope(const image_correspondence_feature &feature)
void get_args(int argc, const char *argv[], const std::string &usage)
Definition: args.cc:49