00001 // <begin copyright notice> 00002 // --------------------------------------------------------------------------- 00003 // 00004 // Copyright (c) 1997 TargetJr Consortium 00005 // GE Corporate Research and Development (GE CRD) 00006 // 1 Research Circle 00007 // Niskayuna, NY 12309 00008 // All Rights Reserved 00009 // Reproduction rights limited as described below. 00010 // 00011 // Permission to use, copy, modify, distribute, and sell this software 00012 // and its documentation for any purpose is hereby granted without fee, 00013 // provided that (i) the above copyright notice and this permission 00014 // notice appear in all copies of the software and related documentation, 00015 // (ii) the name TargetJr Consortium (represented by GE CRD), may not be 00016 // used in any advertising or publicity relating to the software without 00017 // the specific, prior written permission of GE CRD, and (iii) any 00018 // modifications are clearly marked and summarized in a change history 00019 // log. 00020 // 00021 // THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND, 00022 // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 00023 // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 00024 // IN NO EVENT SHALL THE TARGETJR CONSORTIUM BE LIABLE FOR ANY SPECIAL, 00025 // INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND OR ANY 00026 // DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 00027 // WHETHER OR NOT ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, OR ON 00028 // ANY THEORY OF LIABILITY ARISING OUT OF OR IN CONNECTION WITH THE 00029 // USE OR PERFORMANCE OF THIS SOFTWARE. 00030 // 00031 // --------------------------------------------------------------------------- 00032 // <end copyright notice> 00033 #ifndef bsol_hough_line_index_h_ 00034 #define bsol_hough_line_index_h_ 00035 //----------------------------------------------------------------------------- 00036 //: 00037 // \file 00038 // \brief Hough transform for fast queries on line sets 00039 // 00040 // bsol_hough_line_index is a class for indexing 2-d lines and 00041 // tangent vectors in the space of orientation and distance to the origin. 00042 // 00043 // The line index space is defined as follows: 00044 // \verbatim 00045 // o 00046 // / 00047 // line --> /\ . 00048 // / |theta 00049 // <-------------------o--/-----------------> 00050 // \endverbatim 00051 // 00052 // The angle, theta, corresponding to the line tangent direction 00053 // ranges from 0 to 180 degrees. The perpendicular distance 00054 // to the origin, r, is defined as: 00055 // \verbatim 00056 // . / 00057 // . / Line 00058 // Line / . / 00059 // Normal /^ . 00060 // / r 00061 // / . 00062 // x Origin 00063 // \endverbatim 00064 // r can be expressed as r = N.(M - O) where N is the line normal, 00065 // O is the origin position vector and M is the line midpoint. 00066 // Note that N = (-Sin(theta), Cos(theta)). 00067 // The values of theta and r for a vsol_line_2d are computed by 00068 // the method ::array_loc(..). 00069 // If the line is translated by (tx, ty), then the r for the translated 00070 // line is given by r' = r - Sin(theta)tx + Cos(theta)ty. 00071 // The value of r for a given translation is computed by ::trans_loc(..). 00072 // 00073 // The lines are stored in a two dimensional array of r_dim_ by th_dim_ bins 00074 // of vector<vsol_line_2d_sptr>. The theta dimension of the array 00075 // is typically 180/5, or 36. The r dimension is defined by the diagonal 00076 // of the bounding box of the space containing the lines. The resolution in 00077 // r is assumed to be 1. It is necessary to specify the bounding box 00078 // of the line coordinate space at construction time, so that new lines 00079 // entered into the index do not exceed array bounds. 00080 // 00081 // Internally, the r space is defined on an origin at the center of the 00082 // bounding box diagonal. This choice minimizes the error in normal distance 00083 // due to variations in line orientation. 00084 // 00085 // Typical usage: 00086 // - Create a new index for a 100x100 coordinate space with 5 degree angular res. 00087 // \code 00088 // index = new bsol_hough_line_index(0.0, 0.0, 100.0, 100.0, 180.0, 5.0) 00089 // \endcode 00090 // - Add a line to the index 00091 // \code 00092 // ... 00093 // vsol_line_2d_sptr l(p1, p2); 00094 // index->index(l); 00095 // \endcode 00096 // - find collinear lines (lines in a 2-d region in Hough space centered on the 00097 // parameters defined by line l) 00098 // \code 00099 // ... 00100 // vcl_vector<vsol_line_2d_sptr> lines; 00101 // index->lines_in_interval(lines, l, 1.0, 5.0);//dr = 1.0, dtheta = 5.0 00102 // //i.e. +- 1.0 and +- 5.0 00103 // \endcode 00104 // - find lines at a particular orientation 00105 // \code 00106 // index->parallel_lines(lines, 45.0, 5.0); //Lines parallel to 45 deg. 00107 // //+- 5 deg. 00108 // \endcode 00109 // 00110 // \author J.L. Mundy December 1997 00111 // \date ported to VXL April 11, 2003 00112 // 00113 // \verbatim 00114 // Modifications 00115 // 10-sep-2004 Peter Vanroose Added copy ctor with explicit vbl_ref_count init 00116 // \endverbatim 00117 //----------------------------------------------------------------------------- 00118 #include <vcl_vector.h> 00119 #include <vbl/vbl_ref_count.h> 00120 #include <vbl/vbl_bounding_box.h> 00121 #include <vbl/vbl_array_2d.h> 00122 #include <vsol/vsol_line_2d_sptr.h> 00123 00124 class bsol_hough_line_index : public vbl_ref_count 00125 { 00126 // PUBLIC INTERFACE---------------------------------------------------------- 00127 00128 public: 00129 00130 // Constructors/initializers/Destructors------------------------------------- 00131 00132 bsol_hough_line_index(const int r_dimension, const int theta_dimension); 00133 bsol_hough_line_index(const float x0, const float y0, 00134 const float xsize, const float ysize, 00135 const float angle_range=180.0, 00136 const float angle_increment=5.0); 00137 00138 bsol_hough_line_index(vbl_bounding_box<double, 2> const & box, 00139 const float angle_range=180.0, 00140 const float angle_increment=5.0); 00141 00142 bsol_hough_line_index(bsol_hough_line_index const& i) 00143 : vbl_ref_count(), xo_(i.xo_), yo_(i.yo_), 00144 xsize_(i.xsize_), ysize_(i.ysize_), 00145 angle_range_(i.angle_range_), angle_increment_(i.angle_increment_), 00146 r_dim_(i.r_dim_), th_dim_(i.th_dim_), index_(i.index_) {} 00147 00148 ~bsol_hough_line_index(); 00149 00150 // Data Access--------------------------------------------------------------- 00151 00152 float getxsize_() const {return xsize_;} 00153 float getysize_() const {return ysize_;} 00154 00155 int get_r_dimension() const {return r_dim_;} 00156 int get_theta_dimension() const {return th_dim_;} 00157 00158 //: Get the bsol_hough_line_index array location of a line segment 00159 void array_loc(vsol_line_2d_sptr const& line, float& r, float& theta); 00160 void array_loc(vsol_line_2d_sptr const& line, int& r, int& theta); 00161 00162 //: r Location for a translated line position 00163 int trans_loc(const int transx, const int transy, 00164 const int ry, const int theta); 00165 00166 //: Get line count at a particular location in bsol_hough_line_index space 00167 int count(const int r, const int theta); 00168 00169 //: Insert a new line into the index 00170 bool index(vsol_line_2d_sptr const& line); 00171 00172 //: Insert a unique new line into the index 00173 bool index_new(vsol_line_2d_sptr const& line); 00174 00175 //: find if a line is in the index 00176 bool find(vsol_line_2d_sptr const& line); 00177 00178 //: remove a line 00179 bool remove(vsol_line_2d_sptr const& line); 00180 00181 //: Lines in a line index bin at integer r and theta bin indices. 00182 void lines_at_index(const int r, const int theta, 00183 vcl_vector<vsol_line_2d_sptr>& lines); 00184 00185 vcl_vector<vsol_line_2d_sptr > lines_at_index(const int r, 00186 const int theta); 00187 00188 //: Lines in a tolerance box around the r and theta of a given line. 00189 // r is in distance units and theta is in degrees. 00190 void lines_in_interval(vsol_line_2d_sptr const& l, 00191 const float r_dist, const float theta_dist, 00192 vcl_vector<vsol_line_2d_sptr>& lines); 00193 00194 vcl_vector<vsol_line_2d_sptr> 00195 lines_in_interval(vsol_line_2d_sptr const & l, 00196 const float r_dist, 00197 const float theta_dist); 00198 00199 //:Lines parallel to a given angle in degrees 00200 void parallel_lines(const float angle, 00201 const float angle_dist, 00202 vcl_vector<vsol_line_2d_sptr>& lines); 00203 00204 vcl_vector<vsol_line_2d_sptr> parallel_lines(const float angle, 00205 const float angle_dist); 00206 00207 //: Lines at an angle to a given line (angle is in degrees) 00208 void lines_at_angle(vsol_line_2d_sptr const &l, 00209 const float angle, const float angle_dist, 00210 vcl_vector<vsol_line_2d_sptr >& lines); 00211 00212 vcl_vector<vsol_line_2d_sptr> 00213 lines_at_angle(vsol_line_2d_sptr const &l, 00214 const float angle, const float angle_dist); 00215 00216 //: Lines parallel to a given line with angle_dist in degrees 00217 void parallel_lines(vsol_line_2d_sptr const &l, 00218 const float angle_dist, 00219 vcl_vector<vsol_line_2d_sptr>& lines); 00220 00221 vcl_vector<vsol_line_2d_sptr> 00222 parallel_lines(vsol_line_2d_sptr const &l, 00223 const float angle_dist); 00224 00225 //: Angle histogram - projection of hough space onto theta axis 00226 vcl_vector<int> angle_histogram(); 00227 00228 //: Dominant line directions found by non-maximum suppression above thresh 00229 int dominant_directions(const int thresh, const float angle_tol, 00230 vcl_vector<int>& dirs); 00231 00232 //: Dominant parallel line groups 00233 int dominant_line_groups(const int thresh, const float angle_tol, 00234 vcl_vector<vcl_vector<vsol_line_2d_sptr> >& groups); 00235 00236 //: An image of the hough space 00237 vbl_array_2d<unsigned char> get_hough_image(); 00238 00239 // Data Control-------------------------------------------------------------- 00240 00241 void clear_index(); 00242 00243 // INTERNALS----------------------------------------------------------------- 00244 00245 protected: 00246 //internal functions 00247 void init(const int r_dimension, const int theta_dimension); 00248 vcl_vector<int> non_maximum_suppress(const int radius, 00249 vcl_vector<int> const & bins); 00250 00251 // Data Members-------------------------------------------------------------- 00252 00253 private: 00254 00255 float xo_; //!< X Origin of the Cartesian Space 00256 float yo_; //!< Y Origin of the Cartesian Space 00257 00258 float xsize_; //!< Dimensions of the Cartesian space 00259 float ysize_; 00260 00261 float angle_range_; //!< Granularity of the line index 00262 float angle_increment_; 00263 00264 int r_dim_; //!< The dimensions of the index space 00265 int th_dim_; 00266 00267 //: The index space for lines. An array of vectors of line indices 00268 vbl_array_2d<vcl_vector<vsol_line_2d_sptr>* > index_; 00269 }; 00270 00271 #endif