stereo-vision
All Data Structures Namespaces Functions Modules Pages
elasWrapper.cpp
1 /*
2  * Copyright (C) 2015 iCub Facility - Istituto Italiano di Tecnologia
3  * Authors: Giulia Pasquale
4  * email: giulia.pasquale@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txtd
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 
19 #include <opencv2/core/core_c.h>
20 #include <iCub/stereoVision/elasWrapper.h>
21 
22 int64 elasWrapper::workBegin()
23 {
24  return getTickCount();
25 }
26 
27 double elasWrapper::workEnd(int64 work_begin)
28 {
29  int64 d = getTickCount() - work_begin;
30  double f = getTickFrequency();
31  double work_time = d / f;
32  return work_time;
33 }
34 
35 elasWrapper::elasWrapper(double _io_scaling_factor, string elas_setting) : Elas(parameters(( elas_setting == "MIDDLEBURY") ? MIDDLEBURY : ROBOTICS))
36 {
37  param.postprocess_only_left = true;
38 
39  io_scaling_factor = _io_scaling_factor;
40 }
41 
42 elasWrapper::elasWrapper() : Elas(parameters(ROBOTICS))
43 {
44  param.postprocess_only_left = true;
45 
46  io_scaling_factor = 1.0;
47 }
48 
49 bool elasWrapper::compute_disparity(const cv::Mat &imL, const cv::Mat &imR, cv::Mat &dispL, int num_disparities)
50 {
51 
52  // check for correct size
53  if (imL.cols <= 0 || imL.rows <= 0 || imR.cols <= 0 || imR.rows <= 0
54  || imL.cols != imR.cols || imL.rows != imR.rows)
55  {
56  cout << "ERROR: Images must be of same size, but" << endl;
57  cout << " I1: " << imL.cols << " x " << imL.rows << ", I2: "
58  << imR.cols << " x " << imR.rows << endl;
59  return -1;
60  }
61 
62  Size im_size = imL.size();
63 
64  param.disp_max = num_disparities - 1;
65 
66  Mat imR_scaled, imL_scaled;
67  if (io_scaling_factor!=1.0)
68  {
69  resize(imR, imR_scaled, Size(), io_scaling_factor, io_scaling_factor);
70  resize(imL, imL_scaled, Size(), io_scaling_factor, io_scaling_factor);
71  } else
72  {
73  imR_scaled = imR.clone();
74  imL_scaled = imL.clone();
75  }
76  int width = imL_scaled.cols;
77  int height = imL_scaled.rows;
78 
79  int width_disp_data = param.subsampling ? width>>1 : width;
80  int height_disp_data = param.subsampling ? height>>1 : height;
81 
82  float *dispL_data = (float*) malloc(width_disp_data * height_disp_data * sizeof(float));
83  float *dispR_data = (float*) malloc(width_disp_data * height_disp_data * sizeof(float));
84 
85  // prepare input images
86  if (imL_scaled.channels() == 3)
87  {
88  cv::cvtColor(imL_scaled, imL_scaled, cv::COLOR_BGR2GRAY);
89  cv::cvtColor(imR_scaled, imR_scaled, cv::COLOR_BGR2GRAY);
90  }
91  if (!imL_scaled.isContinuous())
92  imL_scaled = imL_scaled.clone();
93  if (!imR_scaled.isContinuous())
94  imR_scaled = imR_scaled.clone();
95 
96  // compute disparity
97  const int32_t dims[3] = {width,height,width}; // bytes per line = width
98 
99  bool success = process((unsigned char*)imL_scaled.data,(unsigned char*)imR_scaled.data, dispL_data, dispR_data, dims);
100 
101  if (success)
102  {
103  Mat dispL_scaled = Mat(height_disp_data, width_disp_data, CV_32FC1, dispL_data);
104 
105  if (io_scaling_factor!=1.0 || param.subsampling==true)
106  resize(dispL_scaled, dispL, im_size);
107  else
108  dispL = dispL_scaled.clone();
109  }
110 
111  free(dispL_data);
112  free(dispR_data);
113 
114  return success;
115 }
116 
117 int elasWrapper::get_disp_min()
118 {
119  return param.disp_min;
120 }
121 int elasWrapper::get_disp_max()
122 {
123  return param.disp_max;
124 }
125 float elasWrapper::get_support_threshold()
126 {
127  return param.support_threshold;
128 }
129 int elasWrapper::get_support_texture()
130 {
131  return param.support_texture;
132 }
133 int elasWrapper::get_candidate_stepsize()
134 {
135  return param.candidate_stepsize;
136 }
137 int elasWrapper::get_incon_window_size()
138 {
139  return param.incon_window_size;
140 }
141 int elasWrapper::get_incon_threshold()
142 {
143  return param.incon_threshold;
144 }
145 int elasWrapper::get_incon_min_support()
146 {
147  return param.incon_min_support;
148 }
149 bool elasWrapper::get_add_corners()
150 {
151  return param.add_corners;
152 }
153 int elasWrapper::get_grid_size()
154 {
155  return param.grid_size;
156 }
157 float elasWrapper::get_beta()
158 {
159  return param.beta;
160 }
161 float elasWrapper::get_gamma()
162 {
163  return param.gamma;
164 }
165 float elasWrapper::get_sigma()
166 {
167  return param.sigma;
168 }
169 float elasWrapper::get_sradius()
170 {
171  return param.sradius;
172 }
173 int elasWrapper::get_match_texture()
174 {
175  return param.match_texture;
176 }
177 int elasWrapper::get_lr_threshold()
178 {
179  return param.lr_threshold;
180 }
181 float elasWrapper::get_speckle_sim_threshold()
182 {
183  return param.speckle_sim_threshold;
184 }
185 int elasWrapper::get_speckle_size()
186 {
187  return param.speckle_size;
188 }
189 int elasWrapper::get_ipol_gap_width()
190 {
191  return param.ipol_gap_width;
192 }
193 bool elasWrapper::get_filter_median()
194 {
195  return param.filter_median;
196 }
197 bool elasWrapper::get_filter_adaptive_mean()
198 {
199  return param.filter_adaptive_mean;
200 }
201 bool elasWrapper::get_postprocess_only_left()
202 {
203  return param.postprocess_only_left;
204 }
205 bool elasWrapper::get_subsampling()
206 {
207  return param.subsampling;
208 }
209 
210 
211 void elasWrapper::set_disp_min(int param_value)
212 {
213  param.disp_min = param_value;
214 }
215 void elasWrapper::set_disp_max(int param_value)
216 {
217  param.disp_max = param_value;
218 }
219 void elasWrapper::set_support_threshold(float param_value)
220 {
221  param.support_threshold = param_value;
222 }
223 void elasWrapper::set_support_texture(int param_value)
224 {
225  param.support_texture = param_value;
226 }
227 void elasWrapper::set_candidate_stepsize(int param_value)
228 {
229  param.candidate_stepsize = param_value;
230 }
231 void elasWrapper::set_incon_window_size(int param_value)
232 {
233  param.incon_window_size = param_value;
234 }
235 void elasWrapper::set_incon_threshold(int param_value)
236 {
237  param.incon_threshold = param_value;
238 }
239 void elasWrapper::set_incon_min_support(int param_value)
240 {
241  param.incon_min_support = param_value;
242 }
243 void elasWrapper::set_add_corners(bool param_value)
244 {
245  param.add_corners = param_value;
246 }
247 void elasWrapper::set_grid_size(int param_value)
248 {
249  param.grid_size = param_value;
250 }
251 void elasWrapper::set_beta(float param_value)
252 {
253  param.beta = param_value;
254 }
255 void elasWrapper::set_gamma(float param_value)
256 {
257  param.gamma = param_value;
258 }
259 void elasWrapper::set_sigma(float param_value)
260 {
261  param.sigma = param_value;
262 }
263 void elasWrapper::set_sradius(float param_value)
264 {
265  param.sradius = param_value;
266 }
267 void elasWrapper::set_match_texture(int param_value)
268 {
269  param.match_texture = param_value;
270 }
271 void elasWrapper::set_lr_threshold(int param_value)
272 {
273  param.lr_threshold = param_value;
274 }
275 void elasWrapper::set_speckle_sim_threshold(float param_value)
276 {
277  param.speckle_sim_threshold = param_value;
278 }
279 void elasWrapper::set_speckle_size(int param_value)
280 {
281  param.speckle_size = param_value;
282 }
283 void elasWrapper::set_ipol_gap_width(int param_value)
284 {
285  param.ipol_gap_width = param_value;
286 }
287 void elasWrapper::set_filter_median(bool param_value)
288 {
289  param.filter_median = param_value;
290 }
291 void elasWrapper::set_filter_adaptive_mean(bool param_value)
292 {
293  param.filter_adaptive_mean = param_value;
294 }
295 void elasWrapper::set_postprocess_only_left(bool param_value)
296 {
297  param.postprocess_only_left = param_value;
298 }
299 void elasWrapper::set_subsampling(bool param_value)
300 {
301  param.subsampling = param_value;
302 }