stereo-vision
All Data Structures Namespaces Functions Modules Pages
cvgui.cpp
1 
2 #include "cvgui.h"
3 
4 #define CVUI_IMPLEMENTATION
5 #include "cvui.h"
6 
7 const char * WINDOW_NAME = "DisparityModule Parameters";
8 
9 
10 GUI::GUI()
11 {
12 
13 }
14 
15 
16 bool GUI::isUpdated()
17 {
18  return this->updated;
19 }
20 
21 
22 void GUI::initializeGUI(int &minDisparity, int &numberOfDisparities, int &SADWindowSize,
23  int &disp12MaxDiff, int &preFilterCap, int &uniquenessRatio,
24  int &speckleWindowSize, int &speckleRange, double &sigmaColorBLF,
25  double &sigmaSpaceBLF, double &wls_lambda, double &wls_sigma,
26  SM_BLF_FILTER &BLFfiltering, SM_WLS_FILTER &WLSfiltering,
27  SM_MATCHING_ALG &stereo_matching)
28 {
29  this->params.minDisparity = minDisparity;
30  this->params.numberOfDisparities = numberOfDisparities;
31  this->params.SADWindowSize = SADWindowSize;
32  this->params.disp12MaxDiff = disp12MaxDiff;
33  this->params.preFilterCap = preFilterCap;
34  this->params.uniquenessRatio = uniquenessRatio;
35  this->params.speckleWindowSize = speckleWindowSize;
36  this->params.speckleRange = speckleRange;
37  this->params.sigmaColorBLF = sigmaColorBLF;
38  this->params.sigmaSpaceBLF = sigmaSpaceBLF;
39  this->params.wls_lambda = wls_lambda;
40  this->params.wls_sigma = wls_sigma;
41  this->params.BLFfiltering = BLFfiltering;
42  this->params.WLSfiltering = WLSfiltering;
43  this->params.stereo_matching = stereo_matching;
44 
45  this->refine_th = 0;
46 
47  this->convertEnumToID();
48  this->initializeGUI();
49 }
50 
51 
52 void GUI::initializeGUI()
53 {
54  // initializes the GUI internal states
55 
56  this->resetState();
57 
58  // creates the window on which to plot the GUI
59 
60  int gui_width = 450; // separate only for the sake of clarity in the initialization
61 
62  frame = cv::Mat(cv::Size(gui_width, cvuiw::estimateHeight(12,4,2)), CV_8UC3);
63  cvui::init(WINDOW_NAME, 3);
64 }
65 
66 
67 void GUI::killGUI()
68 {
69 
70 }
71 
72 
73 void GUI::updateGUI()
74 {
75  // if the window has been closed by the user,
76  // terminate the execution of the GUI
77 
78  if(cv::getWindowProperty(WINDOW_NAME, 0) < 0)
79  {
80  this->done = false;
81  return;
82  }
83 
84  // set the background color for the GUI frame
85 
86  frame = cv::Scalar(20, 22, 23);
87 
88  // reset the local state of the GUI
89 
90  cvuiw::reset();
91 
92  this->updated = false;
93 
94  // GUI definition
95 
96  // 1. Stereo Matching and filtering parameters
97 
98  this->updated |= cvuiw::trackbar<int>("refineTh", frame, &(this->refine_th), 0, 50, 1, "%1.Lf", cvui::TRACKBAR_HIDE_LABELS);
99  this->updated |= cvuiw::trackbar<int>("minDisparity", frame, &(this->params.minDisparity), 0, 20, 1, "%1.Lf", cvui::TRACKBAR_HIDE_LABELS);
100  this->updated |= cvuiw::trackbar<int>("SADWindowSize", frame, &(this->params.SADWindowSize), 3, 31, 1, "%1.Lf", cvui::TRACKBAR_HIDE_LABELS);
101  this->updated |= cvuiw::trackbar<int>("disp12MaxDiff", frame, &(this->params.disp12MaxDiff), 0, 30, 1, "%1.Lf", cvui::TRACKBAR_HIDE_LABELS);
102  this->updated |= cvuiw::trackbar<int>("preFilterCap", frame, &(this->params.preFilterCap), 0, 100, 1, "%1.Lf", cvui::TRACKBAR_HIDE_LABELS);
103  this->updated |= cvuiw::trackbar<int>("uniquenessRatio", frame, &(this->params.uniquenessRatio), 5, 20, 1, "%1.Lf", cvui::TRACKBAR_HIDE_LABELS);
104  this->updated |= cvuiw::trackbar<int>("speckleWindowSize", frame, &(this->params.speckleWindowSize), 0, 200, 1, "%1.Lf", cvui::TRACKBAR_HIDE_LABELS);
105  this->updated |= cvuiw::trackbar<int>("speckleRange", frame, &(this->params.speckleRange), 1, 16, 1, "%1.Lf", cvui::TRACKBAR_HIDE_LABELS);
106  this->updated |= cvuiw::trackbar<double>("sigmaColorBLF", frame, &(this->params.sigmaColorBLF), 1.0, 30.0, 1, "%.1Lf", cvui::TRACKBAR_HIDE_LABELS);
107  this->updated |= cvuiw::trackbar<double>("sigmaSpaceBLF", frame, &(this->params.sigmaSpaceBLF), 1.0, 30.0, 1, "%.1Lf", cvui::TRACKBAR_HIDE_LABELS);
108  this->updated |= cvuiw::trackbar<double>("WLS lambda", frame, &(this->params.wls_lambda), 500.0, 30000.0, 1, "%1.Lf", cvui::TRACKBAR_HIDE_LABELS | cvui::TRACKBAR_DISCRETE, 500.0);
109  this->updated |= cvuiw::trackbar<double>("WLS sigma", frame, &(this->params.wls_sigma), 0.1, 10.0, 1, "%.1Lf", cvui::TRACKBAR_HIDE_LABELS);
110 
111  // 2. Multiple choice controls
112 
113  this->updated |= cvuiw::radioButtons(frame, "Num. of Disparities:", {"32", "64", "96", "128"}, {20, 65, 110, 155}, this->num_disparities_id);
114  this->updated |= cvuiw::radioButtons(frame, "Stereo Matching Alg.:", {"SGBM", "SGBM_CUDA", "LibElas"}, {20, 90, 190}, this->stereo_matching_id);
115  this->updated |= cvuiw::radioButtons(frame, "Bilateral Filtering:", {"No BLF", "Original BLF", "CUDA BLF"}, {20, 90, 190}, this->BLFfiltering_id);
116  this->updated |= cvuiw::radioButtons(frame, "Weigthed LS Filtering:", {"No WLS", "WLS", "WLS w/ lr cons."}, {20, 100, 155}, this->WLSfiltering_id);
117 
118  // 3. Button to trigger the recalibration of the system
119 
120  this->recalibrate = cvuiw::button(frame, "&Recalibrate", 10, 0);
121  this->updated |= this->recalibrate;
122 
123  // 4. Button to save the calibration parameters to the local file
124 
125  this->save_calibration = cvuiw::button(frame, "&Save Calibration", 130, 0);
126  this->updated |= this->save_calibration;
127 
128  // 5. Button to close the GUI
129 
130  if(cvuiw::button(frame, "&Quit", 282, 0))
131  {
132  this->done = true;
133  cv::destroyWindow(WINDOW_NAME);
134  return;
135  }
136 
137  // 6. Button to set the default parameters
138 
139  this->load_parameters = cvuiw::button(frame, "&Default Param.", 10, 1);
140  this->updated |= this->load_parameters;
141 
142  // 7. Save the matching/filtering parameters to a local configuration file
143 
144  this->save_parameters = cvuiw::button(frame, "Save &Parameters", 155, 1);
145  this->updated |= this->save_parameters;
146 
147  // 8. Acquire the selected radiobuttons indexes..
148 
149  this->num_disparities_id = cvuiw::getRadioIndex(0);
150  this->stereo_matching_id = cvuiw::getRadioIndex(1);
151  this->BLFfiltering_id = cvuiw::getRadioIndex(2);
152  this->WLSfiltering_id = cvuiw::getRadioIndex(3);
153 
154  // 9. ..then convert it to the corresponding ENUM elements
155 
156  this->convertIDToEnum();
157 
158  // Since cvui::init() received a param regarding waitKey,
159  // there is no need to call cv::waitKey() anymore. cvui::update()
160  // will do it automatically.
161 
162  cvui::update();
163 
164  cv::imshow(WINDOW_NAME, frame);
165 
166  return;
167 }
168 
169 
170 void GUI::convertIDToEnum()
171 {
172  switch(this->stereo_matching_id)
173  {
174  case 0:
175  this->params.stereo_matching = SM_MATCHING_ALG::SGBM_OPENCV;
176  break;
177  case 1:
178  this->params.stereo_matching = SM_MATCHING_ALG::SGBM_CUDA;
179  break;
180  case 2:
181  this->params.stereo_matching = SM_MATCHING_ALG::LIBELAS;
182  break;
183  }
184 
185  switch(this->BLFfiltering_id)
186  {
187  case 0:
188  this->params.BLFfiltering = SM_BLF_FILTER::BLF_DISABLED;
189  break;
190  case 1:
191  this->params.BLFfiltering = SM_BLF_FILTER::BLF_ORIGINAL;
192  break;
193  case 2:
194  this->params.BLFfiltering = SM_BLF_FILTER::BLF_CUDA;
195  break;
196  }
197 
198  switch(this->WLSfiltering_id)
199  {
200  case 0:
201  this->params.WLSfiltering = SM_WLS_FILTER::WLS_DISABLED;
202  break;
203  case 1:
204  this->params.WLSfiltering = SM_WLS_FILTER::WLS_ENABLED;
205  break;
206  case 2:
207  this->params.WLSfiltering = SM_WLS_FILTER::WLS_LRCHECK;
208  break;
209  }
210 
211  this->params.numberOfDisparities = 32 * (this->num_disparities_id+1);
212 }
213 
214 
215 void GUI::convertEnumToID()
216 {
217  switch(this->params.stereo_matching)
218  {
219  case SM_MATCHING_ALG::SGBM_OPENCV:
220  this->stereo_matching_id = 0;
221  break;
222  case SM_MATCHING_ALG::SGBM_CUDA:
223  this->stereo_matching_id = 1;
224  break;
225  case SM_MATCHING_ALG::LIBELAS:
226  this->stereo_matching_id = 2;
227  break;
228 
229  }
230 
231  switch(this->params.BLFfiltering)
232  {
233  case SM_BLF_FILTER::BLF_DISABLED:
234  this->BLFfiltering_id = 0;
235  break;
236  case SM_BLF_FILTER::BLF_ORIGINAL:
237  this->BLFfiltering_id = 1;
238  break;
239  case SM_BLF_FILTER::BLF_CUDA:
240  this->BLFfiltering_id = 2;
241  break;
242 
243  }
244 
245  switch(this->params.WLSfiltering)
246  {
247  case SM_WLS_FILTER::WLS_DISABLED:
248  this->WLSfiltering_id = 0;
249  break;
250  case SM_WLS_FILTER::WLS_ENABLED:
251  this->WLSfiltering_id = 1;
252  break;
253  case SM_WLS_FILTER::WLS_LRCHECK:
254  this->WLSfiltering_id = 2;
255  break;
256 
257  }
258 
259  this->num_disparities_id = (this->params.numberOfDisparities/32)-1;
260 }
261 
262 
263 void GUI::getParameters(int& minDisparity, int& numberOfDisparities, int& SADWindowSize,
264  int& disp12MaxDiff, int& preFilterCap, int& uniquenessRatio,
265  int& speckleWindowSize, int& speckleRange, double& sigmaColorBLF,
266  double& sigmaSpaceBLF, double& wls_lambda, double& wls_sigma,
267  SM_BLF_FILTER& BLFfiltering, SM_WLS_FILTER& WLSfiltering,
268  SM_MATCHING_ALG& stereo_matching)
269 {
270  minDisparity = this->params.minDisparity;
271  numberOfDisparities = this->params.numberOfDisparities;
272  SADWindowSize = this->params.SADWindowSize;
273  disp12MaxDiff = this->params.disp12MaxDiff;
274  preFilterCap = this->params.preFilterCap;
275  uniquenessRatio = this->params.uniquenessRatio;
276  speckleWindowSize = this->params.speckleWindowSize;
277  speckleRange = this->params.speckleRange;
278  sigmaColorBLF = this->params.sigmaColorBLF;
279  sigmaSpaceBLF = this->params.sigmaSpaceBLF;
280  wls_lambda = this->params.wls_lambda;
281  wls_sigma = this->params.wls_sigma;
282  BLFfiltering = this->params.BLFfiltering;
283  WLSfiltering = this->params.WLSfiltering;
284  stereo_matching = this->params.stereo_matching;
285 }
286 
287 
288 void GUI::setParameters(int& minDisparity, int& numberOfDisparities, int& SADWindowSize,
289  int& disp12MaxDiff, int& preFilterCap, int& uniquenessRatio,
290  int& speckleWindowSize, int& speckleRange, double& sigmaColorBLF,
291  double& sigmaSpaceBLF, double& wls_lambda, double& wls_sigma,
292  SM_BLF_FILTER& BLFfiltering, SM_WLS_FILTER& WLSfiltering,
293  SM_MATCHING_ALG& stereo_matching)
294 {
295  this->params.minDisparity = minDisparity;
296  this->params.numberOfDisparities = numberOfDisparities;
297  this->params.SADWindowSize = SADWindowSize;
298  this->params.disp12MaxDiff = disp12MaxDiff;
299  this->params.preFilterCap = preFilterCap;
300  this->params.uniquenessRatio = uniquenessRatio;
301  this->params.speckleWindowSize = speckleWindowSize;
302  this->params.speckleRange = speckleRange;
303  this->params.sigmaColorBLF = sigmaColorBLF;
304  this->params.sigmaSpaceBLF = sigmaSpaceBLF;
305  this->params.wls_lambda = wls_lambda;
306  this->params.wls_sigma = wls_sigma;
307  this->params.BLFfiltering = BLFfiltering;
308  this->params.WLSfiltering = WLSfiltering;
309  this->params.stereo_matching = stereo_matching;
310 }
311 
312 
313 bool GUI::isDone()
314 {
315  return this->done;
316 }
317 
318 
319 GUI::~GUI()
320 {
321  this->killGUI();
322 }
323 
324 
325 bool GUI::toRecalibrate()
326 {
327  return this->recalibrate;
328 }
329 
330 
331 bool GUI::toSaveCalibration()
332 {
333  return this->save_calibration;
334 }
335 
336 
337 bool GUI::toLoadParameters()
338 {
339  return this->load_parameters;
340 }
341 
342 
343 bool GUI::toSaveParameters()
344 {
345  return this->save_parameters;
346 }
347 
348 
349 void GUI::resetState()
350 {
351  this->done = false;
352  this->updated = false;
353  this->recalibrate = false;
354  this->save_calibration = false;
355  this->load_parameters = false;
356  this->save_parameters = false;
357 }
358 
359 
360 int GUI::getRefineTh()
361 {
362  return refine_th;
363 }
364 
365 
366 bool GUI::toRefine()
367 {
368  return refine_th > 0;
369 }