licornea_tools
transform.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 #include <cstdlib>
5 #include <format.h>
6 #include <regex>
7 #include "../lib/camera.h"
8 #include "../lib/border.h"
9 
10 using namespace tlz;
11 
12 [[noreturn]] void usage_fail() {
13  std::cout << "usage: transform in_cameras.json out_cameras.json/none/replace operation\n"
14  << " MPEG2Rt: MPEG convention to standard extrinsic matrix\n"
15  << " flip_t: flip sign of translation vectors\n"
16  << " scale old new: adapt intrinsic matrix for image scale from old to new\n"
17  << " (old/new = pixel length of same segment in old and new image)\n"
18  << " border [old] new: adapt intrinsic matrix for image border change from old to new\n"
19  << " (old/new = border.json file. if no old, assumed old border is zero)\n"
20  << " rename cam_{} [offset=0] [factor=1]: rename according to format, with arg = offset + factor*index\n"
21  << " head n: Only n first cameras\n"
22  << " tail n: Only n last cameras\n"
23  << " nop: No change, just rewrite the cameras file\n"
24  << std::endl;
25  std::exit(1);
26 }
27 
28 int main(int argc, const char* argv[]) {
29  if(argc <= 3) usage_fail();
30  std::string in_cameras_filename = argv[1];
31  std::string out_cameras_filename = argv[2];
32  std::string operation = argv[3];
33 
34  camera_array in_cameras = decode_cameras(import_json_file(in_cameras_filename));
35  camera_array out_cameras;
36 
37  int index = 0;
38  bool skip = false;
39 
40  for(camera& in_cam : in_cameras) {
41  camera cam = in_cam;
42 
43  if(operation == "Rt2MPEG") {
44  cam.translation = -(cam.rotation.t() * cam.translation);
45 
46  } else if(operation == "MPEG2Rt") {
47  cam.translation = -(cam.rotation * cam.translation);
48 
49  } else if(operation == "flip_t") {
50  cam.translation = -cam.translation;
51 
52  } else if(operation == "scale") {
53  if(argc <= 5) usage_fail();
54  int old_sz = std::atoi(argv[4]);
55  int new_sz = std::atoi(argv[5]);
56  float factor = (float)new_sz / old_sz;
57  cam.intrinsic(0,0) *= factor;
58  cam.intrinsic(1,1) *= factor;
59  cam.intrinsic(0,2) *= factor;
60  cam.intrinsic(1,2) *= factor;
61 
62  } else if(operation == "border") {
63  border old_border, new_border;
64  if(argc == 4) {
65  std::string new_border_filename = argv[4];
66  new_border = decode_border(import_json_file(new_border_filename));
67  } else if(argc == 5) {
68  std::string old_border_filename = argv[4];
69  old_border = decode_border(import_json_file(old_border_filename));
70  std::string new_border_filename = argv[5];
71  new_border = decode_border(import_json_file(new_border_filename));
72  } else usage_fail();
73  cam.intrinsic(0,2) += new_border.left - old_border.left;
74  cam.intrinsic(1,2) += new_border.top - old_border.top;
75 
76  } else if(operation == "rename") {
77  if(argc <= 4) usage_fail();
78  std::string format = argv[4];
79  int offset = 0;
80  int factor = 1;
81  if(argc > 5) offset = std::atoi(argv[5]);
82  if(argc > 6) factor = std::atoi(argv[6]);
83  std::string new_name = fmt::format(format, factor*index + offset);
84  std::cout << cam.name << " --> " << new_name << std::endl;
85  cam.name = new_name;
86 
87  } else if(operation == "head") {
88  if(argc <= 4) usage_fail();
89  int n = std::atoi(argv[4]);
90  skip = (index >= n);
91 
92  } else if(operation == "tail") {
93  if(argc <= 4) usage_fail();
94  int n = std::atoi(argv[4]);
95  skip = (index < in_cameras.size() - n);
96 
97  } else if(operation == "nop") {
98  // no change
99 
100  } else {
101  usage_fail();
102  }
103  ++index;
104 
105  if(! skip) out_cameras.push_back(cam);
106  }
107 
108  if(out_cameras_filename == "none") {
109  std::cout << "not writing to output" << std::endl;
110  } else {
111  if(out_cameras_filename == "replace") out_cameras_filename = in_cameras_filename;
112  std::cout << "writing to " << out_cameras_filename << std::endl;
113  std::ofstream output(out_cameras_filename.c_str());
114  export_cameras_file(out_cameras, out_cameras_filename);
115  output.close();
116  }
117 
118  std::cout << "done" << std::endl;
119 }
void export_cameras_file(const camera_array &cameras, const std::string &filename)
Definition: camera.cc:79
camera_array decode_cameras(const json &j_cams)
Definition: camera.cc:60
mat33 intrinsic
Definition: camera.h:15
std::string name
Definition: camera.h:14
border decode_border(const json &j_bord)
Definition: border.cc:15
int left
Definition: border.h:10
mat33 rotation
Definition: camera.h:16
int top
Definition: border.h:9
std::vector< camera > camera_array
Definition: camera.h:26
void usage_fail()
Definition: transform.cc:12
json import_json_file(const std::string &filename)
Definition: json.cc:24
int main(int argc, const char *argv[])
Definition: transform.cc:28
vec3 translation
Definition: camera.h:17