1 #ifndef _RSLF_FINE_TO_COARSE 2 #define _RSLF_FINE_TO_COARSE 8 #define _MIN_SPATIAL_DIM 10 26 template<
typename DataType>
36 float epi_scale_factor = -1,
38 int max_pyr_depth = -1,
39 bool accept_all_last_scale =
true 79 bool m_accept_all_last_scale;
102 template<
typename DataType>
109 float epi_scale_factor,
112 bool accept_all_last_scale
117 int start_dim_v = tmp_epis.size();
118 int start_dim_u = tmp_epis[0].cols;
120 int dim_v = tmp_epis.size();
121 int dim_u = tmp_epis[0].cols;
123 if (max_pyr_depth < 1)
124 max_pyr_depth = std::numeric_limits<int>::max();
128 while (dim_v > _MIN_SPATIAL_DIM && dim_u > _MIN_SPATIAL_DIM && counter < max_pyr_depth)
132 std::cout <<
"Creating Depth2DComputer with sizes (" << dim_v <<
", " << dim_u <<
")" << std::endl;
137 new_parameters->par_slope_factor = (0.0 + dim_u) / start_dim_u;
145 tmp_epis = downsampled_epis;
147 dim_v = tmp_epis.size();
148 dim_u = tmp_epis[0].cols;
150 m_computers.push_back(computer);
151 m_parameter_instances.push_back(new_parameters);
155 if (accept_all_last_scale)
156 m_computers.back()->set_accept_all(
true);
159 template<
typename DataType>
162 for (
int p=0; p<m_computers.size(); p++)
164 delete m_computers[p];
165 delete m_parameter_instances[p];
169 template<
typename DataType>
172 for (
int p=0; p<m_computers.size(); p++)
174 m_computers[p]->run();
175 if (p < m_computers.size() - 1)
177 std::cout <<
"Setting new depth bounds..." << std::endl;
180 Vec<Mat> depth_map_up = m_computers[p]->get_depths_s_v_u();
181 Vec<Mat> depth_map_down = m_computers[p+1]->get_depths_s_v_u();
183 Vec<Mat> mask_up = m_computers[p]->get_valid_depths_mask_s_v_u();
184 Vec<Mat> mask_down = m_computers[p+1]->get_valid_depths_mask_s_v_u();
187 Vec<Mat> dmin_map = m_computers[p+1]->edit_dmin();
189 Vec<Mat> dmax_map = m_computers[p+1]->edit_dmax();
191 int dim_s = depth_map_up.size();
193 int dim_v_up = depth_map_up[0].rows;
194 int dim_u_up = depth_map_up[0].cols;
196 int dim_v_down = depth_map_down[0].rows;
197 int dim_u_down = depth_map_down[0].cols;
200 #pragma omp parallel for 201 for (
int s=0; s<dim_s; s++)
203 for (
int v=0; v<dim_v_down; v++)
205 for (
int u=0; u<dim_u_down; u++)
210 int v_up = std::min(2 * v, dim_v_up - 1);
211 int u_up = std::min(2 * u, dim_u_up - 1);
214 float d_left = nan_type<float>();
218 if (mask_up[s].at<uchar>(v_up, u_left) > 0)
220 d_left = depth_map_up[s].at<
float>(v_up, u_left);
225 float d_right = nan_type<float>();
226 while (u_right < dim_u_up-1)
229 if (mask_up[s].at<uchar>(v_up, u_right) > 0)
231 d_right = depth_map_up[s].at<
float>(v_up, u_right);
240 if (!is_nan_type<float>(d_left) && !is_nan_type<float>(d_right))
242 candidate_ds.push_back(d_left);
243 candidate_ds.push_back(d_right);
247 if (v_up+1 < dim_v_up)
251 int u_up = std::min(2 * u, dim_u_up - 1);
254 float d_left = nan_type<float>();
258 if (mask_up[s].at<uchar>(v_up, u_left) > 0)
260 d_left = depth_map_up[s].at<
float>(v_up, u_left);
265 float d_right = nan_type<float>();
266 while (u_right < dim_u_up-1)
269 if (mask_up[s].at<uchar>(v_up, u_right) > 0)
271 d_right = depth_map_up[s].at<
float>(v_up, u_right);
275 if (!is_nan_type<float>(d_left) && !is_nan_type<float>(d_right))
277 candidate_ds.push_back(d_left);
278 candidate_ds.push_back(d_right);
281 if (candidate_ds.size() > 1)
283 std::sort(candidate_ds.begin(), candidate_ds.end());
284 int nb_candidates = candidate_ds.size();
287 dmin_map[s].at<
float>(v, u) = candidate_ds[0];
288 dmax_map[s].at<
float>(v, u) = candidate_ds.back();
299 template<
typename DataType>
302 std::cout <<
"Getting results..." << std::endl;
307 for (
int p=0; p<m_computers.size(); p++)
309 disp_pyr_p_s_v_u_.push_back(m_computers[p]->get_depths_s_v_u());
310 validity_indicators_p_s_v_u_.push_back(m_computers[p]->get_valid_depths_mask_s_v_u());
316 validity_indicators_p_s_v_u_,
322 template<
typename DataType>
325 std::cout <<
"Plot depth results..." << std::endl;
336 int dim_s = out_map_s_v_u_.size();
337 int dim_v = out_map_s_v_u_[0].rows;
338 int dim_u = out_map_s_v_u_[0].cols;
341 image_converter.
fit(out_map_s_v_u_[(
int)std::round(dim_s/2.0)], saturate);
343 for (
int s=0; s<dim_s; s++)
348 cv::applyColorMap(disparity_map, disparity_map, cv_colormap);
350 int dim_v = disparity_map.rows;
351 int dim_u = disparity_map.cols;
354 disparity_map.setTo(0.0, out_validity_s_v_u_[s] == 0);
357 Mat im_norm =
Mat(dim_v, dim_u, CV_32FC1);
358 for (
int v=0; v<dim_v; v++)
360 for (
int u=0; u<dim_u; u++)
362 Mat tmp = m_computers[0]->get_epis()[v];
363 im_norm.at<
float>(v, u) = norm<DataType>(tmp.at<DataType>(s, u));
366 disparity_map.setTo(0.0, im_norm < _SHADOW_NORMALIZED_LEVEL);
368 out_plot_depth_s_v_u_.push_back(disparity_map);
373 template<
typename DataType>
378 Vec<Mat> epis = m_computers[0]->get_epis();
380 int dim_s = tmp.size();
381 int dim_v = tmp[0].rows;
382 int dim_u = tmp[0].cols;
384 out_plot_depth_s_v_u_.clear();
388 for (
int s=0; s<dim_s; s++)
391 Mat img =
Mat(dim_v, dim_u, epis[0].type());
392 for (
int v=0; v<dim_v; v++)
394 epis[v].row(s).copyTo(img.row(v));
398 converter.
fit(img,
false);
402 if (img.channels() == 1)
404 cv::cvtColor(img, img, CV_GRAY2RGB);
406 std::cout <<
"img: " << img.size() <<
", " <<
rslf::type2str(img.type()) << std::endl;
407 std::cout <<
"tmp[s]: " << tmp[s].size() <<
", " <<
rslf::type2str(tmp[s].type()) << std::endl;
413 cv::vconcat(img, tmp[s], img);
418 cv::hconcat(img, tmp[s], img);
421 out_plot_depth_s_v_u_.push_back(img);
425 template<
typename DataType>
429 int m_pyr_size_ = m_computers.size();
430 int m_dim_v_orig_ = m_computers[0]->get_depths_s_v_u()[0].rows;
433 v = (int)std::round(m_dim_v_orig_/2.0);
437 out_plot_epi_pyr_p_s_u_.clear();
438 for (
int p=0; p<m_pyr_size_; p++)
440 int dim_s = m_computers[p]->get_depths_s_v_u().size();
441 int dim_v = m_computers[p]->get_depths_s_v_u()[0].rows;
442 int dim_u = m_computers[p]->get_depths_s_v_u()[0].cols;
444 Mat tmp(dim_s, dim_u, CV_32FC1);
446 int v_scaled = (int)std::round(1.0 * v * dim_v / m_dim_v_orig_);
448 Vec<Mat> masks = m_computers[p]->get_valid_depths_mask_s_v_u();
450 for (
int s=0; s<dim_s; s++)
452 m_computers[p]->get_depths_s_v_u()[s].row(v_scaled).copyTo(tmp.row(s));
453 tmp.row(s).setTo(0.0, masks[s].row(v_scaled) == 0);
457 image_converter.
fit(tmp, saturate);
460 cv::applyColorMap(tmp, tmp, cv_colormap);
463 Mat epi_norm =
Mat(dim_s, dim_u, CV_32FC1);
464 for (
int s=0; s<dim_s; s++)
466 for (
int u=0; u<dim_u; u++)
468 Mat tmp = m_computers[0]->get_epis()[v];
469 epi_norm.at<
float>(s, u) = norm<DataType>(tmp.at<DataType>(s, u));
472 tmp.setTo(0.0, epi_norm < _SHADOW_NORMALIZED_LEVEL);
474 out_plot_epi_pyr_p_s_u_.push_back(tmp);
480 template<
typename DataType>
483 int m_pyr_size_ = m_computers.size();
484 int dim_s = m_computers[0]->get_depths_s_v_u().size();
487 s = (int)std::round(dim_s/2.0);
491 out_plot_depth_pyr_p_v_u_.clear();
492 for (
int p=0; p<m_pyr_size_; p++)
494 Mat tmp = m_computers[p]->get_depths_s_v_u()[s].clone();
497 image_converter.
fit(tmp, saturate);
500 cv::applyColorMap(tmp, tmp, cv_colormap);
502 Vec<Mat> masks = m_computers[p]->get_valid_depths_mask_s_v_u();
504 tmp.setTo(0.0, masks[s] == 0);
506 out_plot_depth_pyr_p_v_u_.push_back(tmp);
Impelment a structure containing all the algorithm parameters.
Definition: rslf_depth_computation_core.hpp:65
Definition: rslf_fine_to_coarse.hpp:27
std::string type2str(int type)
Get an explicit form of an OpenCV type.
Definition: rslf_types.cpp:52
void fuse_disp_maps(const Vec< Vec< Mat >> &in_disp_pyr_p_s_v_u, const Vec< Vec< Mat > > &in_validity_indicators_p_s_v_u, Vec< Mat > &out_map_s_v_u, Vec< Mat > &out_validity_s_v_u)
Fuse a pyramid of depths into one depth map and apply a median filter on top of it.
Definition: rslf_fine_to_coarse_core.cpp:70
std::vector< T > Vec
RSLF Vector class (std::vector)
Definition: rslf_types.hpp:32
Image normalizer to be fitted on a given image, that will further scale images given the preceding fi...
Definition: rslf_plot.hpp:45
Definition: rslf_depth_computation.hpp:14
void downsample_EPIs(const Vec< Mat > &in_epis, Vec< Mat > &out_epis)
Downsample given EPIs by a factor 2 in the spatial dimensions.
Definition: rslf_fine_to_coarse_core.cpp:14
void get_coloured_depth_pyr(Vec< Mat > &out_plot_depth_pyr_p_v_u_, int s=-1, int cv_colormap=cv::COLORMAP_JET, bool saturate=true)
Get the disparity map pyramid with colors corresponding to computed disparities.
Definition: rslf_fine_to_coarse.hpp:481
Implement low-level fine-to-coarse functions.
void get_coloured_depth_maps_and_imgs(Vec< Mat > &out_plot_depth_s_v_u_, int cv_colormap=cv::COLORMAP_JET, bool saturate=true)
Get disparity maps with colors corresponding to the computed disparities, juxtaposed with the origina...
Definition: rslf_fine_to_coarse.hpp:374
cv::Mat Mat
RSLF Matrix class (cv::Mat)
Definition: rslf_types.hpp:26
void copy_and_scale(const Mat &src, Mat &dst)
Scales the input image to uchar and return a copy.
Definition: rslf_plot.cpp:100
void run()
Runs the algorithm.
Definition: rslf_fine_to_coarse.hpp:170
void get_coloured_depth_maps(Vec< Mat > &out_plot_depth_s_v_u_, int cv_colormap=cv::COLORMAP_JET, bool saturate=true)
Get disparity maps with colors corresponding to the computed disparities.
Definition: rslf_fine_to_coarse.hpp:323
void get_coloured_epi_pyr(Vec< Mat > &out_plot_epi_pyr_p_s_u_, int v=-1, int cv_colormap=cv::COLORMAP_JET, bool saturate=true)
Get the EPI pyramid with colors corresponding to computed slopes.
Definition: rslf_fine_to_coarse.hpp:426
void fit(const Mat &img, bool saturate=true)
Given an image, fit the internal normalization parameters.
Definition: rslf_plot.cpp:66
void get_results(Vec< Mat > &out_map_s_v_u_, Vec< Mat > &out_validity_s_v_u_)
Get the resulting disparity maps at the finest scale and the corresponding validity mask...
Definition: rslf_fine_to_coarse.hpp:300
Definition: rslf_depth_computation.hpp:167