licornea_tools
apply_homography.cc
Go to the documentation of this file.
1 #include "../lib/common.h"
2 #include "../lib/args.h"
3 #include "../lib/image_io.h"
4 #include "../lib/opencv.h"
5 #include "../lib/json.h"
6 #include "../lib/border.h"
7 #include "../lib/view_homography.h"
8 
9 using namespace tlz;
10 
11 const cv::Vec3b background_color(0, 0, 0);
12 
13 int main(int argc, const char* argv[]) {
14  get_args(argc, argv, "homography.json in_image.png out_image.png texture/depth [border.json]");
15  view_homography homography = homography_arg();
16  std::string in_image_filename = in_filename_arg();
17  std::string out_image_filename = out_filename_arg();
18  std::string image_type = enum_arg({ "texture", "depth" });
19  std::string border_filename = in_filename_opt_arg();
20  border bord;
21  if(! border_filename.empty()) bord = decode_border(import_json_file(border_filename));
22 
23 
24  mat33 offset_mat(
25  1, 0, bord.left,
26  0, 1, bord.top,
27  0, 0, 1
28  );
29  mat33 H = offset_mat * homography.mat;
30 
31  if(image_type == "texture") {
32  cv::Mat_<cv::Vec3b> in_image = load_texture(in_image_filename);
33  cv::Mat_<cv::Vec3b> out_image;
34  cv::Size dsize = add_border(bord, in_image.size());
35  cv::warpPerspective(in_image, out_image, H, dsize, cv::INTER_CUBIC, cv::BORDER_CONSTANT, cv::Scalar(background_color));
36  save_texture(out_image_filename, out_image);
37 
38  } else if(image_type == "depth") {
39  cv::Mat_<ushort> in_image = load_depth(in_image_filename);
40  cv::Mat_<ushort> out_image;
41  cv::Size dsize = add_border(bord, in_image.size());
42  cv::warpPerspective(in_image, out_image, H, dsize, cv::INTER_NEAREST, cv::BORDER_CONSTANT, 0);
43  save_depth(out_image_filename, out_image);
44 
45  }
46 }
void save_depth(const std::string &filename, const cv::Mat_< ushort > &depth)
Definition: image_io.cc:43
void save_texture(const std::string &filename, const cv::Mat_< cv::Vec3b > &texture)
Definition: image_io.cc:13
std::string in_filename_arg()
Definition: args.cc:98
std::string enum_arg(const std::vector< std::string > &options)
Definition: args.cc:154
cv::Mat_< ushort > load_depth(const std::string &filename)
Definition: image_io.cc:35
border decode_border(const json &j_bord)
Definition: border.cc:15
int left
Definition: border.h:10
const cv::Vec3b background_color(0, 0, 0)
cv::Mat_< cv::Vec3b > load_texture(const std::string &filename)
Definition: image_io.cc:6
int main(int argc, const char *argv[])
cv::Matx< real, 3, 3 > mat33
Definition: common.h:26
view_homography homography_arg()
int top
Definition: border.h:9
cv::Size add_border(const border &bord, const cv::Size &sz)
Definition: border.cc:24
std::string out_filename_arg()
Definition: args.cc:104
json import_json_file(const std::string &filename)
Definition: json.cc:24
std::string in_filename_opt_arg(const std::string &def="")
Definition: args.h:40
void get_args(int argc, const char *argv[], const std::string &usage)
Definition: args.cc:49