KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vanishing_point_detector.h
Go to the documentation of this file.
1 /* $Id */
2 
3 /* =========================================================================== *
4 |
5 | Copyright (c) 1994-2008 by Kobus Barnard (author).
6 |
7 | Personal and educational use of this code is granted, provided that this
8 | header is kept intact, and that the authorship is not misrepresented, that
9 | its use is acknowledged in publications, and relevant papers are cited.
10 |
11 | For other use contact the author (kobus AT cs DOT arizona DOT edu).
12 |
13 | Please note that the code in this file has not necessarily been adequately
14 | tested. Naturally, there is no guarantee of performance, support, or fitness
15 | for any particular task. Nonetheless, I am interested in hearing about
16 | problems that you encounter.
17 |
18 | Author: Luca Del Pero
19 * =========================================================================== */
20 
21 #ifndef VANISHING_POINT_DETECTION_H
22 #define VANISHING_POINT_DETECTION_H
23 
24 #include "m/m_incl.h"
25 #include "i/i_float.h"
26 #include <i_cpp/i_image.h>
27 #include "edge/edge_base.h"
29 #include "m_cpp/m_int_vector.h"
31 
32 #include <vector>
33 #include <list>
34 #include <string>
35 
36 #define VPD_VERTICAL_THRESHOLD 0.12
37 #define VPD_VERTICAL_VP_OUTLIER_THRESHOLD 0.05
38 #define VPD_VERTICAL_VP_MAX_OUTLIERS_RATIO 0.6 /*0.4 */
39 
40 #define VPD_MAX_LINE_SEGMENT_FITTING_ERROR 0.5 /* 0.5 */
41 #define VPD_RANSAC_ROBUST_SUCCESS_PROBABILITY 0.998
42 
47 #define VPD_MAX_PRINCIPAL_POINT_POSITION_TOLERANCE 18 /* vero: 30 //16 // 80 */
48 #define VPD_LOOSE_VPTS_TOLERANCE 60
49 
50 namespace kjb
51 {
52 
60 {
61  public:
62 
63  enum {
69 
71  unsigned int img_rows, unsigned int img_cols, double vertical_threshold = VPD_VERTICAL_THRESHOLD,
72  double max_line_segment_fitting_error = VPD_MAX_LINE_SEGMENT_FITTING_ERROR,
73  double ivpts_tolerance = VPD_MAX_PRINCIPAL_POINT_POSITION_TOLERANCE)
74  : _num_rows(img_rows), _num_cols(img_cols), use_relaxed_checks(false),
75  vpts_tolerance(ivpts_tolerance), A(3, 4, 1.0), Atranspose(0),
76  AA(0), absolute_conic(3,3,0.0), E_mp(0), D_vp(0),
77  decomposition(0), inverse(0)
78  {
79  init_from_edge_segment_set(iset, vertical_threshold, max_line_segment_fitting_error );
80  kjb_c::get_target_matrix( &E_mp, 4, 4 );
81  kjb_c::get_target_matrix( &Atranspose, 4, 3 );
82  kjb_c::get_target_matrix( &AA, 4, 4 );
83  kjb_c::get_target_vector( &D_vp, 4);
84  }
85 
87  unsigned int img_rows, unsigned int img_cols, double vertical_threshold = VPD_VERTICAL_THRESHOLD,
88  double ivpts_tolerance = VPD_MAX_PRINCIPAL_POINT_POSITION_TOLERANCE)
89  : _num_rows(img_rows), _num_cols(img_cols), use_relaxed_checks(false),
90  vpts_tolerance(ivpts_tolerance), A(3, 4, 1.0), Atranspose(0),
91  AA(0), absolute_conic(3,3,0.0), E_mp(0), D_vp(0),
92  decomposition(0), inverse(0)
93  {
94  init_from_edge_segment_set(iset, vertical_threshold);
95  kjb_c::get_target_matrix( &E_mp, 4, 4 );
96  kjb_c::get_target_matrix( &Atranspose, 4, 3 );
97  kjb_c::get_target_matrix( &AA, 4, 4 );
98  kjb_c::get_target_vector( &D_vp, 4);
99  }
100 
102  : _vertical_segments(src._vertical_segments), _regular_segments(src._regular_segments),
103  _num_rows(src._num_rows), _num_cols(src._num_cols), use_relaxed_checks(src.use_relaxed_checks),
104  vpts_tolerance(src.vpts_tolerance),
105  A(src.A), Atranspose(0), AA(0), absolute_conic(src.absolute_conic), E_mp(0), D_vp(0),
106  decomposition(0), inverse(0)
107  {
108  kjb_c::copy_matrix( &E_mp, src.E_mp);
109  kjb_c::copy_vector( &D_vp, src.D_vp);
110  kjb_c::copy_matrix( &decomposition, src.decomposition);
111  kjb_c::copy_matrix( &inverse, src.inverse);
112  kjb_c::copy_matrix( &Atranspose, src.Atranspose);
113  kjb_c::copy_matrix( &AA, src.AA);
114 
115  }
116 
118  {
119  _vertical_segments = src._vertical_segments;
120  _regular_segments = src._regular_segments;
121  _num_rows = src._num_rows;
122  _num_cols = src._num_cols;
123  use_relaxed_checks = src.use_relaxed_checks;
124  vpts_tolerance = src.vpts_tolerance;
125  A = src.A;
126  absolute_conic = src.absolute_conic;
127  kjb_c::copy_matrix( &E_mp, src.E_mp);
128  kjb_c::copy_vector( &D_vp, src.D_vp);
129  kjb_c::copy_matrix( &decomposition, src.decomposition);
130  kjb_c::copy_matrix( &inverse, src.inverse);
131  kjb_c::copy_matrix( &Atranspose, src.Atranspose);
132  kjb_c::copy_matrix( &AA, src.AA);
133  return (*this);
134  }
135 
137  {
138  kjb_c::free_matrix(Atranspose);
139  kjb_c::free_matrix(AA);
140  kjb_c::free_matrix(E_mp);
141  kjb_c::free_vector(D_vp);
142  kjb_c::free_matrix(decomposition);
143  kjb_c::free_matrix(inverse);
144  }
145 
147  void init_from_edge_segment_set(const Edge_segment_set & iset, double vertical_threshold,
148  double max_line_segment_fitting_error );
150  void init_from_edge_segment_set(const Line_segment_set & iset, double vertical_threshold);
151 
156  static double compute_alpha(const kjb::Vanishing_point & vp, const Line_segment * line);
157 
158 
163  (
164  double outlier_threshold,
165  unsigned int * total_number_of_outliers,
166  const Vanishing_point & vp1,
167  const Vanishing_point & vp2
168  );
169 
174  (
175  double outlier_threshold,
176  double * total_number_of_outliers,
177  double max_outliers_ratio,
178  const Vanishing_point & vp1,
179  const Vanishing_point & vp2,
180  const Vanishing_point & vp3
181  );
182 
184  (
185  double outlier_threshold,
186  int * total_number_of_outliers,
187  const Line_segment_set & vertical_bucket,
188  const Line_segment_set & horizontal_bucket1,
189  const Line_segment_set & horizontal_bucket2,
190  const Vanishing_point & vp1,
191  const Vanishing_point & vp2,
192  const Vanishing_point & vp3
193  );
194 
199  (
200  double outlier_threshold,
201  unsigned int * total_number_of_outliers,
202  const Vanishing_point & vertical_vp
203  );
204 
215  (
216  const Vanishing_point & vp1,
217  const Vanishing_point & vp2,
218  const Vanishing_point & vertical_vp,
219  double & focal_length
220  );
221 
223  (
224  const Vanishing_point & vp1,
225  const Vanishing_point & vp2,
226  const Vanishing_point & vertical_vp,
227  double & ifocal_length,
228  double & opx,
229  double & opy
230  );
231 
236  (
237  const Vanishing_point & vp1,
238  const Vanishing_point & vp2,
239  const Vanishing_point & vertical_vp,
240  double & focal_length
241  );
242 
244  double find_vpts_with_ransac
245  (
246  double success_probability,
247  double outlier_threshold,
248  double max_outliers_ratio,
249  std::vector<Vanishing_point> & vpts,
250  double vertical_vp_outlier_threshold = VPD_VERTICAL_VP_OUTLIER_THRESHOLD,
251  double vertical_vp_max_outliers_ratio = VPD_VERTICAL_VP_MAX_OUTLIERS_RATIO
252  );
253 
256  (
257  double success_probability,
258  double outlier_threshold,
259  double max_outliers_ratio,
260  Vanishing_point & vp
261  );
262 
268  (
269  double success_probability,
270  double outlier_threshold,
271  double max_outliers_ratio,
272  const Vanishing_point & vertical_vp,
273  Vanishing_point & vp1,
274  Vanishing_point & vp2
275  );
276 
279  (
280  double success_probability,
281  double outlier_threshold,
282  double max_outliers_ratio,
283  std::vector<Vanishing_point> & vpts
284  );
285 
287  (
288  double outlier_threshold,
289  const Line_segment_set & vertical_bucket,
290  const Line_segment_set & horizontal_bucket1,
291  const Line_segment_set & horizontal_bucket2,
292  std::vector<Vanishing_point> & vpts
293  );
294 
296  inline bool get_use_relaxed_checks() const
297  {
298  return use_relaxed_checks;
299  }
300 
302  inline void set_use_relaxed_checks(bool icheck)
303  {
304  use_relaxed_checks = icheck;
305  }
308  static void sample_n_from_m_without_repetitions(unsigned int n, unsigned int m, Int_vector & iv);
309 
311  double compute_focal_length(const std::vector<Vanishing_point> & v_pts);
312 
316 
320 
321  int get_tot_num_segments();
322 
323  void set_vpts_tolerance(double ivpts_tolerance)
324  {
325  vpts_tolerance = ivpts_tolerance;
326  }
327 
329  (
330  double success_probability,
331  double outlier_threshold,
332  double max_outliers_ratio,
333  std::vector<Vanishing_point> & vpts
334  );
335 
336  double get_best_focal()
337  {
338  return best_focal;
339  }
340 
342  {
343  return _vertical_segments.size();
344  }
345 
346 
347  private:
348 
349  void insert_penalty_into_list(double ipenalty);
350 
351  double compute_average_inlier_penalty(double outlier_threshold);
352 
354  std::vector<const Line_segment *> _vertical_segments;
355 
357  std::vector<const Line_segment *> _regular_segments;
358 
360  unsigned int _num_rows;
361 
363  unsigned int _num_cols;
364 
367  bool use_relaxed_checks;
368 
370  std::list<double> penalties;
371 
372  double vpts_tolerance;
373 
374  double best_focal;
375 
379  Matrix A;
380  kjb_c::Matrix * Atranspose;
381  kjb_c::Matrix * AA;
382  Matrix absolute_conic;
383 
384  kjb_c::Matrix * E_mp;
385  kjb_c::Vector * D_vp;
386  kjb_c::Matrix * decomposition;
387  kjb_c::Matrix * inverse;
388 
389 };
390 
399 (
400  std::vector<Vanishing_point> & vpts,
401  double & focal_length,
402  const kjb::Image & img,
403  double success_probability = VPD_RANSAC_ROBUST_SUCCESS_PROBABILITY,
404  bool jointly_estimate = false,
405  std::vector<Vanishing_point> right_ones = std::vector<Vanishing_point>(0)
406 );
407 
409 (
410  Vanishing_point & vertical,
411  const kjb::Image & img,
412  double success_probability = VPD_RANSAC_ROBUST_SUCCESS_PROBABILITY
413 );
414 
421 (
422  std::vector<Vanishing_point> & vpts,
423  double & focal_length,
424  const kjb::Image & img,
425  double success_probability
426 );
427 
440 unsigned int assign_to_vanishing_point
441 (
442  double outlier_threshold,
443  const Line_segment * isegment,
444  const std::vector<Vanishing_point> & ivpts
445 );
446 
448 (
449  std::vector<Vanishing_point> & vpts,
450  double & focal_length,
451  const Image & img,
452  const Line_segment_set & kovesi,
453  double start_threshold = 0.05,
454  double vpts_tolerance = VPD_MAX_PRINCIPAL_POINT_POSITION_TOLERANCE,
455  double success_probability = VPD_RANSAC_ROBUST_SUCCESS_PROBABILITY
456 );
457 
459 (
460  std::vector<Vanishing_point> & vpts,
461  double & focal_length,
462  const std::string & img_path
463 );
464 
465 }
466 #endif /*VANISHING_POINT_DETECTION */
bool check_vpts_consistency(const Vanishing_point &vp1, const Vanishing_point &vp2, const Vanishing_point &vertical_vp, double &focal_length)
Checks that a set of three vanishing points is consistent. It checks that for each pair of vanishing ...
Definition: vanishing_point_detector.cpp:1090
This class computes the position of the three vanishing points from a set of line segments...
Definition: vanishing_point_detector.h:59
void set_use_relaxed_checks(bool icheck)
Returns true if realxed checks are used.
Definition: vanishing_point_detector.h:302
void set_vpts_tolerance(double ivpts_tolerance)
Definition: vanishing_point_detector.h:323
bool groundtruth_check_vpts_consistency(const Vanishing_point &vp1, const Vanishing_point &vp2, const Vanishing_point &vertical_vp, double &ifocal_length, double &opx, double &opy)
Definition: vanishing_point_detector.cpp:1356
double find_vpts_with_ransac(double success_probability, double outlier_threshold, double max_outliers_ratio, std::vector< Vanishing_point > &vpts, double vertical_vp_outlier_threshold=VPD_VERTICAL_VP_OUTLIER_THRESHOLD, double vertical_vp_max_outliers_ratio=VPD_VERTICAL_VP_MAX_OUTLIERS_RATIO)
Finds an estimate for all three vanishing points using RANSAC.
Definition: vanishing_point_detector.cpp:477
bool find_vertical_vp_with_ransac(double success_probability, double outlier_threshold, double max_outliers_ratio, Vanishing_point &vp)
Finds an estimate for the vertical vanishing point.
Definition: vanishing_point_detector.cpp:774
A vanishing point for a set of parallel lines in an image.
Definition: vanishing_point.h:37
bool relaxed_vanishing_point_estimation(std::vector< Vanishing_point > &vpts, double &focal_length, const kjb::Image &img, double success_probability)
This function uses less constraints coming from geometry and uses the data more, relying on the assum...
Definition: vanishing_point_detector.cpp:2439
double get_best_focal()
Definition: vanishing_point_detector.h:336
bool robustly_estimate_vanishing_points(std::vector< Vanishing_point > &vpts, double &focal_length, const kjb::Image &img, double success_probability=VPD_RANSAC_ROBUST_SUCCESS_PROBABILITY, bool jointly_estimate=false, std::vector< Vanishing_point > right_ones=std::vector< Vanishing_point >(0))
Estimates the vanishing points for the three orthogonal directions of a Manhattan world scene (where ...
Definition: vanishing_point_detector.cpp:2199
double compute_focal_length(const std::vector< Vanishing_point > &v_pts)
Computes the focal length from the vanishing points position.
Definition: vanishing_point_detector.cpp:1845
double groundtruth_compute_ransac_penalty(double outlier_threshold, int *total_number_of_outliers, const Line_segment_set &vertical_bucket, const Line_segment_set &horizontal_bucket1, const Line_segment_set &horizontal_bucket2, const Vanishing_point &vp1, const Vanishing_point &vp2, const Vanishing_point &vp3)
Definition: vanishing_point_detector.cpp:329
#define VPD_RANSAC_ROBUST_SUCCESS_PROBABILITY
Definition: vanishing_point_detector.h:41
Definition: vanishing_point_detector.h:64
bool detect_vanishing_points(std::vector< Vanishing_point > &vpts, double &focal_length, const std::string &img_path)
Definition: vanishing_point_detector.cpp:2704
enum kjb::Vanishing_point_detector::@0 Vanishing_point_index
double jointly_find_vpts_with_ransac(double success_probability, double outlier_threshold, double max_outliers_ratio, std::vector< Vanishing_point > &vpts)
Finds an estimate of the three vanishing points at once.
Definition: vanishing_point_detector.cpp:505
void init_from_edge_segment_set(const Edge_segment_set &iset, double vertical_threshold, double max_line_segment_fitting_error)
Inits the edge segment that will be used to estimate the vanishing points.
Definition: vanishing_point_detector.cpp:1884
Class to manipulate a set of line segments.
Definition: line_segment_set.h:28
Declarations for Line segment set classes.
Definition: vanishing_point_detector.h:66
Vanishing_point_detector & operator=(const Vanishing_point_detector &src)
Definition: vanishing_point_detector.h:117
Definition: vanishing_point_detector.h:67
bool get_use_relaxed_checks() const
Sets the type of geometric constraints to be used.
Definition: vanishing_point_detector.h:296
This class implements vectors, in the linear-algebra sense, restricted to integer-valued elements...
Definition: l_int_vector.h:83
Definition: vanishing_point_detector.h:65
bool relaxed_check_vpts_consistency(const Vanishing_point &vp1, const Vanishing_point &vp2, const Vanishing_point &vertical_vp, double &focal_length)
Checks that a set of three vanishing points is consistent, using basic geometric constraints. For a more thourough check use check_vpts_consistency.
Definition: vanishing_point_detector.cpp:1585
Vanishing_point_detector(const Line_segment_set &iset, unsigned int img_rows, unsigned int img_cols, double vertical_threshold=VPD_VERTICAL_THRESHOLD, double ivpts_tolerance=VPD_MAX_PRINCIPAL_POINT_POSITION_TOLERANCE)
Definition: vanishing_point_detector.h:86
~Vanishing_point_detector()
Definition: vanishing_point_detector.h:136
unsigned int assign_to_vanishing_point(double outlier_threshold, const Line_segment *isegment, const std::vector< Vanishing_point > &ivpts)
Assigns an edge segments to the vanishing point that minimizes the angle between the segment and the ...
Definition: vanishing_point_detector.cpp:2524
#define VPD_MAX_LINE_SEGMENT_FITTING_ERROR
Definition: vanishing_point_detector.h:40
double compute_ransac_penalty(double outlier_threshold, unsigned int *total_number_of_outliers, const Vanishing_point &vp1, const Vanishing_point &vp2)
Computes the RANSAC penalty for the horizontal segments given the input horizontal vanishing points...
Definition: vanishing_point_detector.cpp:200
bool guess_horizontal_vpts_from_three_segments(Vanishing_point &vp1, Vanishing_point &vp2)
Definition: vanishing_point_detector.cpp:2005
double jointly_compute_ransac_penalty(double outlier_threshold, double *total_number_of_outliers, double max_outliers_ratio, const Vanishing_point &vp1, const Vanishing_point &vp2, const Vanishing_point &vp3)
Computes the RANSAC penalty for all segments given the three vanishing points.
Definition: vanishing_point_detector.cpp:257
Vanishing_point_detector(const Edge_segment_set &iset, unsigned int img_rows, unsigned int img_cols, double vertical_threshold=VPD_VERTICAL_THRESHOLD, double max_line_segment_fitting_error=VPD_MAX_LINE_SEGMENT_FITTING_ERROR, double ivpts_tolerance=VPD_MAX_PRINCIPAL_POINT_POSITION_TOLERANCE)
Definition: vanishing_point_detector.h:70
Vanishing_point_detector(const Vanishing_point_detector &src)
Definition: vanishing_point_detector.h:101
double find_vpts_from_groundtruth_assignments(double outlier_threshold, const Line_segment_set &vertical_bucket, const Line_segment_set &horizontal_bucket1, const Line_segment_set &horizontal_bucket2, std::vector< Vanishing_point > &vpts)
Definition: vanishing_point_detector.cpp:2769
bool find_horizontal_vanishing_vpts_with_ransac(double success_probability, double outlier_threshold, double max_outliers_ratio, const Vanishing_point &vertical_vp, Vanishing_point &vp1, Vanishing_point &vp2)
Finds an estimate for the two horizontal vanishing points. IMPORTANT: It relies on the current estima...
Definition: vanishing_point_detector.cpp:1707
void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
Definition: nr.cpp:237
static double compute_alpha(const kjb::Vanishing_point &vp, const Line_segment *line)
Compute the angle between the line segment and the line through the vanishing point and the line segm...
Definition: vanishing_point_detector.cpp:126
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
Code for a wrapper class around the C struct KJB_Image.
#define VPD_VERTICAL_VP_OUTLIER_THRESHOLD
Definition: vanishing_point_detector.h:37
int get_tot_num_segments()
Definition: vanishing_point_detector.cpp:397
for m
Definition: APPgetLargeConnectedEdges.m:64
Class to manipulate a set of edge segments.
Definition: line_segment_set.h:128
Wrapped version of the C struct KJB_image.
Definition: i_image.h:76
double robustly_estimate_vanishing_points_Kovesi(std::vector< Vanishing_point > &vpts, double &focal_length, const Image &img, const Line_segment_set &kovesi, double start_threshold=0.05, double vpts_tolerance=VPD_MAX_PRINCIPAL_POINT_POSITION_TOLERANCE, double success_probability=VPD_RANSAC_ROBUST_SUCCESS_PROBABILITY)
Definition: vanishing_point_detector.cpp:2319
#define VPD_MAX_PRINCIPAL_POINT_POSITION_TOLERANCE
Definition: vanishing_point_detector.h:47
bool robustly_estimate_vertical_vanishing_point(Vanishing_point &vertical, const kjb::Image &img, double success_probability=VPD_RANSAC_ROBUST_SUCCESS_PROBABILITY)
Definition: vanishing_point_detector.cpp:2609
int get_num_vertical_segments() const
Definition: vanishing_point_detector.h:341
double geometric_find_vpts_with_ransac(double success_probability, double outlier_threshold, double max_outliers_ratio, std::vector< Vanishing_point > &vpts)
Definition: vanishing_point_detector.cpp:885
static void sample_n_from_m_without_repetitions(unsigned int n, unsigned int m, Int_vector &iv)
samples n integers without repetitions from the set [0,m]
Definition: vanishing_point_detector.cpp:2085
#define VPD_VERTICAL_VP_MAX_OUTLIERS_RATIO
Definition: vanishing_point_detector.h:38
Class to manipulate a line segment The class is parametrized in terms the position of the centre...
Definition: gr_line_segment.h:62
double compute_vertical_ransac_penalty(double outlier_threshold, unsigned int *total_number_of_outliers, const Vanishing_point &vertical_vp)
Computes the RANSAC penalty for the vertical segments given the input vertical vanishing point...
Definition: vanishing_point_detector.cpp:414
bool guess_horizontal_vpts_from_two_segments(Vanishing_point &vp1, Vanishing_point &vp2)
Definition: vanishing_point_detector.cpp:1979
#define VPD_VERTICAL_THRESHOLD
Definition: vanishing_point_detector.h:36