1 #include "../lib/common.h" 2 #include "../lib/assert.h" 3 #include "../lib/args.h" 4 #include "../lib/dataset.h" 5 #include "../lib/camera.h" 6 #include "../lib/image_io.h" 7 #include "../lib/opencv.h" 8 #include "../lib/viewer.h" 14 const cv::Mat_<cv::Vec3b>& ref_img,
15 const cv::Mat_<cv::Vec3b>& tg_img,
const cv::Mat_<ushort>& tg_depth,
20 cv::Size sz = ref_img.size();
21 Assert(tg_img.size() == sz);
22 Assert(tg_depth.size() == sz);
25 real z_diff = z_far - z_near;
27 real alpha = -z_near / z_diff;
28 real beta = z_near*z_far / z_diff;
33 0.0, 0.0, alpha, beta,
39 0.0, 0.0, alpha, beta,
45 cv::Mat_<cv::Vec3b> out_img(sz);
46 ref_img.copyTo(out_img);
47 out_img *= darken_background;
49 cv::Mat_<real> out_img_zbuffer(sz);
50 out_img_zbuffer.setTo(0.0);
52 #pragma omp parallel shared(out_img, ref_img, tg_img, tg_depth) 54 cv::Mat_<real> local_out_img_zbuffer(sz);
55 local_out_img_zbuffer.setTo(0.0);
57 cv::Mat_<cv::Vec3b> local_out_img(sz);
60 for(
int x = 0; x < sz.width; ++x)
for(
int y = 0; y < sz.height; ++y) {
61 cv::Vec3b tg_col = tg_img(y, x);
62 ushort d_int = tg_depth(y, x);
63 if(d_int == 0)
continue;
66 real disp = alpha + beta/d;
68 vec4 tg_pos_h(x, y, disp, 1.0);
69 vec4 ref_pos_h = H * tg_pos_h;
71 vec2 ref_pos(ref_pos_h[0]/ref_pos_h[3], ref_pos_h[1]/ref_pos_h[3]);
72 real ref_disp = ref_pos_h[2]/ref_pos_h[3];
73 int ref_x = ref_pos[0], ref_y = ref_pos[1];
74 if(ref_x < 0 || ref_x >= sz.width || ref_y < 0 || ref_y >= sz.height)
continue;
76 real& out_z = local_out_img_zbuffer(ref_y, ref_x);
77 cv::Vec3b& out_col = local_out_img(ref_y, ref_x);
79 cv::Vec3b ref_col = ref_img(ref_y, ref_x);
80 if(out_z < ref_disp) {
82 out_col = (1-opacity)*ref_col + opacity*tg_col;
88 cv::Mat_<uchar> mask = (local_out_img_zbuffer > out_img_zbuffer);
89 local_out_img.copyTo(out_img, mask);
90 local_out_img_zbuffer.copyTo(out_img_zbuffer, mask);
99 int main(
int argc,
const char* argv[]) {
100 get_args(argc, argv,
"dataset_parameters.json cameras.json [dataset_group]");
111 auto& slider_ref_y = view.add_int_slider(
"ref Y", datas.
y_mid(), datas.
y_min(), datas.
y_max(), datas.
y_step());
112 auto& slider_tg_x = view.add_int_slider(
"tg X", datas.
x_mid(), datas.
x_min(), datas.
x_max(), datas.
x_step());
113 auto& slider_tg_y = view.add_int_slider(
"tg Y", datas.
y_mid(), datas.
y_min(), datas.
y_max(), datas.
y_step());
114 auto& slider_opacity = view.add_real_slider(
"opacity", 0.5, 0.0, 1.0);
115 auto& slider_darken_background = view.add_real_slider(
"darken", 0.3, 0.0, 1.0);
120 view.update_callback = [&]() {
123 view_index ref_idx(slider_ref_x, slider_ref_y);
125 if(! datas.
valid(ref_idx) || ! datas.
valid(tg_idx))
return;
134 ref_image, tg_image, tg_depth, ref_cam, tg_cam, z_near, z_far, slider_opacity, 1.0-slider_darken_background);
136 view.draw(cv::Point(0,0), out_image);
137 }
catch(
const std::exception&) { }
int main(int argc, const char *argv[])
cv::Size image_size_with_border() const
std::string image_filename() const
std::map< std::string, camera > cameras_map(const camera_array &arr)
cv::Mat_< ushort > load_depth(const std::string &filename)
mat44 extrinsic_inv() const
dataset_view view(int x) const
cv::Mat_< cv::Vec3b > load_texture(const std::string &filename)
camera_array cameras_arg()
dataset_view view(int x) const
bool valid(view_index) const
std::string depth_filename() const
std::string camera_name() const
int_slider & add_int_slider(const std::string &caption, int default_val, int min_val, int max_val, int step=1)
std::vector< camera > camera_array
dataset_group group(const std::string &grp) const
cv::Mat_< cv::Vec3b > view_synthesis(const cv::Mat_< cv::Vec3b > &ref_img, const cv::Mat_< cv::Vec3b > &tg_img, const cv::Mat_< ushort > &tg_depth, const camera &ref_cam, const camera &tg_cam, real z_near, real z_far, real opacity, real darken_background)
cv::Matx< real, 4, 4 > mat44
std::string string_opt_arg(const std::string &def="")
void get_args(int argc, const char *argv[], const std::string &usage)