libelas
elas.h
Go to the documentation of this file.
1 /*
2 Copyright 2011. All rights reserved.
3 Institute of Measurement and Control Systems
4 Karlsruhe Institute of Technology, Germany
5 
6 This file is part of libelas.
7 Authors: Andreas Geiger
8 
9 libelas is free software; you can redistribute it and/or modify it under the
10 terms of the GNU General Public License as published by the Free Software
11 Foundation; either version 3 of the License, or any later version.
12 
13 libelas is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 libelas; if not, write to the Free Software Foundation, Inc., 51 Franklin
19 Street, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21 
22 // Main header file. Include this to use libelas in your code.
23 
24 #ifndef __ELAS_H__
25 #define __ELAS_H__
26 
27 #include <iostream>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <vector>
32 #include <emmintrin.h>
33 
34 // define fixed-width datatypes for Visual Studio projects
35 #ifndef _MSC_VER
36  #include <stdint.h>
37 #else
38  typedef __int8 int8_t;
39  typedef __int16 int16_t;
40  typedef __int32 int32_t;
41  typedef __int64 int64_t;
42  typedef unsigned __int8 uint8_t;
43  typedef unsigned __int16 uint16_t;
44  typedef unsigned __int32 uint32_t;
45  typedef unsigned __int64 uint64_t;
46 #endif
47 
48 #ifdef PROFILE
49 #include "timer.h"
50 #endif
51 
52 class Elas {
53 
54 public:
55 
57 
58  // parameter settings
59  struct parameters {
60  int32_t disp_min; // min disparity
61  int32_t disp_max; // max disparity
62  float support_threshold; // max. uniqueness ratio (best vs. second best support match)
63  int32_t support_texture; // min texture for support points
64  int32_t candidate_stepsize; // step size of regular grid on which support points are matched
65  int32_t incon_window_size; // window size of inconsistent support point check
66  int32_t incon_threshold; // disparity similarity threshold for support point to be considered consistent
67  int32_t incon_min_support; // minimum number of consistent support points
68  bool add_corners; // add support points at image corners with nearest neighbor disparities
69  int32_t grid_size; // size of neighborhood for additional support point extrapolation
70  float beta; // image likelihood parameter
71  float gamma; // prior constant
72  float sigma; // prior sigma
73  float sradius; // prior sigma radius
74  int32_t match_texture; // min texture for dense matching
75  int32_t lr_threshold; // disparity threshold for left/right consistency check
76  float speckle_sim_threshold; // similarity threshold for speckle segmentation
77  int32_t speckle_size; // maximal size of a speckle (small speckles get removed)
78  int32_t ipol_gap_width; // interpolate small gaps (left<->right, top<->bottom)
79  bool filter_median; // optional median filter (approximated)
80  bool filter_adaptive_mean; // optional adaptive mean filter (approximated)
81  bool postprocess_only_left; // saves time by not postprocessing the right image
82  bool subsampling; // saves time by only computing disparities for each 2nd pixel
83  // note: for this option D1 and D2 must be passed with size
84  // width/2 x height/2 (rounded towards zero)
85 
86  // constructor
88 
89  // default settings in a robotics environment
90  // (do not produce results in half-occluded areas
91  // and are a bit more robust towards lighting etc.)
92  if (s==ROBOTICS) {
93  disp_min = 0;
94  disp_max = 255;
95  support_threshold = 0.85;
96  support_texture = 10;
97  candidate_stepsize = 5;
98  incon_window_size = 5;
99  incon_threshold = 5;
100  incon_min_support = 5;
101  add_corners = 0;
102  grid_size = 20;
103  beta = 0.02;
104  gamma = 3;
105  sigma = 1;
106  sradius = 2;
107  match_texture = 1;
108  lr_threshold = 2;
109  speckle_sim_threshold = 1;
110  speckle_size = 200;
111  ipol_gap_width = 3;
112  filter_median = 0;
113  filter_adaptive_mean = 1;
114  postprocess_only_left = 1;
115  subsampling = 0;
116 
117  // default settings for middlebury benchmark
118  // (interpolate all missing disparities)
119  } else {
120  disp_min = 0;
121  disp_max = 255;
122  support_threshold = 0.95;
123  support_texture = 10;
124  candidate_stepsize = 5;
125  incon_window_size = 5;
126  incon_threshold = 5;
127  incon_min_support = 5;
128  add_corners = 1;
129  grid_size = 20;
130  beta = 0.02;
131  gamma = 5;
132  sigma = 1;
133  sradius = 3;
134  match_texture = 0;
135  lr_threshold = 2;
136  speckle_sim_threshold = 1;
137  speckle_size = 200;
138  ipol_gap_width = 5000;
139  filter_median = 1;
140  filter_adaptive_mean = 0;
141  postprocess_only_left = 0;
142  subsampling = 0;
143  }
144  }
145  };
146 
147  // constructor, input: parameters
148  Elas (parameters param) : param(param) {}
149 
150  // deconstructor
151  ~Elas () {}
152 
153  // matching function
154  // inputs: pointers to left (I1) and right (I2) intensity image (uint8, input)
155  // pointers to left (D1) and right (D2) disparity image (float, output)
156  // dims[0] = width of I1 and I2
157  // dims[1] = height of I1 and I2
158  // dims[2] = bytes per line (often equal to width, but allowed to differ)
159  // note: D1 and D2 must be allocated before (bytes per line = width)
160  // if subsampling is not active their size is width x height,
161  // otherwise width/2 x height/2 (rounded towards zero)
162  void process (uint8_t* I1,uint8_t* I2,float* D1,float* D2,const int32_t* dims);
163 
164 private:
165 
166  struct support_pt {
167  int32_t u;
168  int32_t v;
169  int32_t d;
170  support_pt(int32_t u,int32_t v,int32_t d):u(u),v(v),d(d){}
171  };
172 
173  struct triangle {
174  int32_t c1,c2,c3;
175  float t1a,t1b,t1c;
176  float t2a,t2b,t2c;
177  triangle(int32_t c1,int32_t c2,int32_t c3):c1(c1),c2(c2),c3(c3){}
178  };
179 
180  inline uint32_t getAddressOffsetImage (const int32_t& u,const int32_t& v,const int32_t& width) {
181  return v*width+u;
182  }
183 
184  inline uint32_t getAddressOffsetGrid (const int32_t& x,const int32_t& y,const int32_t& d,const int32_t& width,const int32_t& disp_num) {
185  return (y*width+x)*disp_num+d;
186  }
187 
188  // support point functions
189  void removeInconsistentSupportPoints (int16_t* D_can,int32_t D_can_width,int32_t D_can_height);
190  void removeRedundantSupportPoints (int16_t* D_can,int32_t D_can_width,int32_t D_can_height,
191  int32_t redun_max_dist, int32_t redun_threshold, bool vertical);
192  void addCornerSupportPoints (std::vector<support_pt> &p_support);
193  inline int16_t computeMatchingDisparity (const int32_t &u,const int32_t &v,uint8_t* I1_desc,uint8_t* I2_desc,const bool &right_image);
194  std::vector<support_pt> computeSupportMatches (uint8_t* I1_desc,uint8_t* I2_desc);
195 
196  // triangulation & grid
197  std::vector<triangle> computeDelaunayTriangulation (std::vector<support_pt> p_support,int32_t right_image);
198  void computeDisparityPlanes (std::vector<support_pt> p_support,std::vector<triangle> &tri,int32_t right_image);
199  void createGrid (std::vector<support_pt> p_support,int32_t* disparity_grid,int32_t* grid_dims,bool right_image);
200 
201  // matching
202  inline void updatePosteriorMinimum (__m128i* I2_block_addr,const int32_t &d,const int32_t &w,
203  const __m128i &xmm1,__m128i &xmm2,int32_t &val,int32_t &min_val,int32_t &min_d);
204  inline void updatePosteriorMinimum (__m128i* I2_block_addr,const int32_t &d,
205  const __m128i &xmm1,__m128i &xmm2,int32_t &val,int32_t &min_val,int32_t &min_d);
206  inline void findMatch (int32_t &u,int32_t &v,float &plane_a,float &plane_b,float &plane_c,
207  int32_t* disparity_grid,int32_t *grid_dims,uint8_t* I1_desc,uint8_t* I2_desc,
208  int32_t *P,int32_t &plane_radius,bool &valid,bool &right_image,float* D);
209  void computeDisparity (std::vector<support_pt> p_support,std::vector<triangle> tri,int32_t* disparity_grid,int32_t* grid_dims,
210  uint8_t* I1_desc,uint8_t* I2_desc,bool right_image,float* D);
211 
212  // L/R consistency check
213  void leftRightConsistencyCheck (float* D1,float* D2);
214 
215  // postprocessing
216  void removeSmallSegments (float* D);
217  void gapInterpolation (float* D);
218 
219  // optional postprocessing
220  void adaptiveMean (float* D);
221  void median (float* D);
222 
223  // parameter set
224  parameters param;
225 
226  // memory aligned input images + dimensions
227  uint8_t *I1,*I2;
228  int32_t width,height,bpl;
229 
230  // profiling timer
231 #ifdef PROFILE
232  Timer timer;
233 #endif
234 };
235 
236 #endif
float ** triangle
Definition: triangle.cpp:404
int32_t incon_min_support
Definition: elas.h:67
parameters(setting s=ROBOTICS)
Definition: elas.h:87
bool postprocess_only_left
Definition: elas.h:81
float sigma
Definition: elas.h:72
bool add_corners
Definition: elas.h:68
Elas(parameters param)
Definition: elas.h:148
float support_threshold
Definition: elas.h:62
int32_t disp_min
Definition: elas.h:60
int32_t incon_threshold
Definition: elas.h:66
int32_t speckle_size
Definition: elas.h:77
int32_t support_texture
Definition: elas.h:63
int32_t disp_max
Definition: elas.h:61
float speckle_sim_threshold
Definition: elas.h:76
int32_t incon_window_size
Definition: elas.h:65
bool filter_adaptive_mean
Definition: elas.h:80
int32_t lr_threshold
Definition: elas.h:75
int32_t ipol_gap_width
Definition: elas.h:78
Definition: elas.h:56
float beta
Definition: elas.h:70
int32_t grid_size
Definition: elas.h:69
float gamma
Definition: elas.h:71
setting
Definition: elas.h:56
Definition: elas.h:59
float sradius
Definition: elas.h:73
bool subsampling
Definition: elas.h:82
bool filter_median
Definition: elas.h:79
Definition: timer.h:48
Definition: elas.h:56
int32_t candidate_stepsize
Definition: elas.h:64
void process(uint8_t *I1, uint8_t *I2, float *D1, float *D2, const int32_t *dims)
Definition: elas.cpp:31
Definition: elas.h:52
~Elas()
Definition: elas.h:151
int32_t match_texture
Definition: elas.h:74