licornea_tools
vsrs_disparity.cc
Go to the documentation of this file.
1 #include "../lib/common.h"
2 #include "../lib/args.h"
3 #include <opencv2/opencv.hpp>
4 #include <cstdlib>
5 #include <cstdint>
6 #include <cstring>
7 #include <iostream>
8 #include <fstream>
9 #include <stdexcept>
10 
11 using namespace tlz;
12 
13 void append_null_tail(std::ostream& output, std::streamsize length) {
14  while(length--) output.put(0);
15 }
16 
17 
18 void export_yuv_disparity(std::string filename, const cv::Mat_<uchar>& mat) {
19  std::ofstream output(filename, std::ios::binary);
20  std::streamsize sz = mat.cols * mat.rows;
21  output.write(reinterpret_cast<const std::ofstream::char_type*>(mat.data), sz);
22  append_null_tail(output, sz);
23 }
24 void export_yuv_disparity(std::string filename, const cv::Mat_<ushort>& mat) {
25  std::ofstream output(filename, std::ios::binary);
26  std::streamsize sz = mat.cols * mat.rows * 2;
27  output.write(reinterpret_cast<const std::ofstream::char_type*>(mat.data), sz);
28  append_null_tail(output, sz);
29 }
30 
31 
32 template<typename Distance, typename Depth>
34  const cv::Mat_<Distance>& in_z,
35  cv::Mat_<Depth>& out_d,
36  real d_near, real d_far,
37  real z_near, real z_far
38 ) {
39  const real z_diff = z_far - z_near;
40  const real offset = ((d_far * z_far) - (d_near * z_near)) / z_diff;
41  const real factor = ((d_near - d_far) * z_near * z_far) / z_diff;
42 
43  cv::Mat_<real> z_real = in_z;
44  cv::Mat_<real> d_real = offset + factor / z_real;
45  out_d = d_real;
46 }
47 
48 
49 
50 int main(int argc, const char* argv[]) {
51  get_args(argc, argv, "depth.png out_disparity.yuv z_near z_far [8/16]");
52  std::string input_filename = in_filename_arg();
53  std::string output_filename = out_filename_arg();
54  ushort z_near = int_arg();
55  ushort z_far = int_arg();
56  bool output_disparity_16bit = (enum_opt_arg({"8", "16"}, "8") == "16");
57 
58  cv::Mat_<ushort> depth;
59  {
60  cv::Mat depth_ = cv::imread(input_filename, CV_LOAD_IMAGE_ANYDEPTH);
61  if(depth_.depth() != CV_16U) throw std::runtime_error("input depth map: must be 16 bit");
62  depth = depth_;
63  }
64 
65  if(output_disparity_16bit) {
66  cv::Mat_<ushort> disparity;
67  orthogonal_distance_to_depth<ushort, ushort>(depth, disparity, 0xffff, 0, z_near, z_far);
68  disparity.setTo(0, depth == 0);
69 
70  export_yuv_disparity(output_filename, disparity);
71  } else {
72  cv::Mat_<uchar> disparity;
73  orthogonal_distance_to_depth<ushort, uchar>(depth, disparity, 0xff, 0, z_near, z_far);
74  disparity.setTo(0, depth == 0);
75 
76  export_yuv_disparity(output_filename, disparity);
77  }
78 
79 }
void orthogonal_distance_to_depth(const cv::Mat_< Distance > &in_z, cv::Mat_< Depth > &out_d, real d_near, real d_far, real z_near, real z_far)
std::string in_filename_arg()
Definition: args.cc:98
int main(int argc, const char *argv[])
void export_yuv_disparity(std::string filename, const cv::Mat_< uchar > &mat)
std::string enum_opt_arg(const std::vector< std::string > &options, const std::string &def)
Definition: args.h:58
double real
Definition: common.h:16
void append_null_tail(std::ostream &output, std::streamsize length)
long int_arg()
Definition: args.cc:138
std::string out_filename_arg()
Definition: args.cc:104
void get_args(int argc, const char *argv[], const std::string &usage)
Definition: args.cc:49