licornea_tools
undistort_image.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 #include <cstdlib>
5 #include <stdexcept>
6 #include "../lib/args.h"
7 #include "../lib/json.h"
8 #include "../lib/image_io.h"
9 #include "../lib/opencv.h"
10 #include "../lib/intrinsics.h"
11 
12 using namespace tlz;
13 
14 
15 int main(int argc, const char* argv[]) {
16  get_args(argc, argv, "in_image.png out_image.json intrinsics.json texture/depth");
17  std::string in_image_filename = in_filename_arg();
18  std::string out_image_filename = out_filename_arg();
19  intrinsics intr = intrinsics_arg();
20  std::string mode = enum_arg({ "texture", "depth" });
21 
22  if(mode == "texture") {
23  cv::Mat_<cv::Vec3b> in_image = load_texture(in_image_filename);
24  cv::Mat_<cv::Vec3b> out_image;
25  cv::undistort(in_image, out_image, intr.K, intr.distortion.cv_coeffs());
26  save_texture(out_image_filename, out_image);
27 
28  } else if(mode == "depth") {
29  cv::Mat_<ushort> in_image = load_depth(in_image_filename);
30  cv::Mat_<ushort> out_image;
31  cv::Mat map1, map2;
32  cv::initUndistortRectifyMap(intr.K, intr.distortion.cv_coeffs(), cv::Mat::eye(3, 3, CV_32F), intr.K, in_image.size(), CV_32FC1, map1, map2);
33  cv::remap(in_image, out_image, map1, map2, cv::INTER_NEAREST);
34  save_depth(out_image_filename, out_image);
35 
36  }
37 }
38 
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
cv::Mat_< cv::Vec3b > load_texture(const std::string &filename)
Definition: image_io.cc:6
intrinsics intrinsics_arg()
Definition: intrinsics.cc:119
distortion_parameters distortion
Definition: intrinsics.h:30
feature_points undistort(const feature_points &dist_fpoints, const intrinsics &intr)
std::string out_filename_arg()
Definition: args.cc:104
int main(int argc, const char *argv[])
std::vector< real > cv_coeffs() const
Definition: intrinsics.h:17
std::string mode
void get_args(int argc, const char *argv[], const std::string &usage)
Definition: args.cc:49