contrib/brl/bseg/sdet/sdet_edgel_regions.h
Go to the documentation of this file.
00001 // This is brl/bseg/sdet/sdet_edgel_regions.h
00002 #ifndef sdet_edgel_regions_h_
00003 #define sdet_edgel_regions_h_
00004 //:
00005 // \file
00006 // \brief generation of regions bounded by sdet_edgel chains
00007 // \verbatim
00008 //     Inputs:  vil1_image, vector<vtol_edge_2d_sptr>
00009 //     Output: vector<gevd_intensity_face_sptr>
00010 // \endverbatim
00011 //  The idea is to generate regions by inserting boundaries into an
00012 //  array.  The boundaries are defined by a set of edgel chains and
00013 //  a boundary location is inserted at each edgel.  The array is
00014 //  assumed to have a boundary at the perimeter of the ROI.
00015 //
00016 //  The array is scanned with a 2x2 neighborhood to form connected
00017 //  components in the usual way. Each of the connected component labels are
00018 //  reduced to the lowest equivalent label id by a transitive closure
00019 //  on the equivalence table.
00020 //
00021 // A second scan of the label array is made to determine adjacency to
00022 //  a given edge.  This process is enabled by a companion class,
00023 //  the gevd_region_edge which has storage for two labels, corresponding to
00024 //  each side of the edge.
00025 //
00026 //  The region edges are then used to collect the input edges which are
00027 //  adjacent to a given region.  The set of edges adjacent to a region
00028 //  are used to construct a multiply-connected sub-class of vtol_face_2d, the
00029 //  vtol_intensity_face.  The vtol_intensity_face contains a
00030 //  vdgl_digtial_region which holds arrays of pixel coordinates and values
00031 //  within the face boundaries.
00032 //
00033 // \author Author J. L. Mundy - January 14, 1999
00034 // \verbatim
00035 // Modifications
00036 //  25 April 2000 - collinsr@cs.rpi.edu - switched region_edges_
00037 //                  to key on the Id() rather than the pointer value
00038 //                  to avoid different hash tables (and different
00039 //                  segmentations) for identical inputs (leaving the
00040 //                  question of why the hash table order matters for
00041 //                  another day)
00042 // 24 April 2002 - Peter Vanroose - replaced GetLeftLabel and GetRightLabel
00043 //                  by GetLabel, in accordance with sdet_region_edge change
00044 //
00045 // 19 November 2002 - J.L. Mundy moved to brl for further evolution
00046 // 09 January 2003  - J.L. Mundy implemented expanded array resolution
00047 //                    for more accurate connection neighborhoods
00048 // \endverbatim
00049 //
00050 //-----------------------------------------------------------------------------
00051 #include <vcl_vector.h>
00052 #include <vcl_map.h>
00053 #include <vbl/vbl_array_2d.h>
00054 #include <vil1/vil1_image.h>
00055 #include <vil/vil_image_resource.h>
00056 #include <vtol/vtol_edge_2d_sptr.h>
00057 #include <gevd/gevd_bufferxy.h>
00058 #include <vtol/vtol_intensity_face_sptr.h>
00059 #include <sdet/sdet_region_edge_sptr.h>
00060 
00061 class sdet_edgel_regions
00062 {
00063  public:
00064   enum RegionLabel {UNLABELED=0, EDGE, LABEL};
00065   //Constructors/Destructors
00066   sdet_edgel_regions(int array_scale=2, bool verbose = false,
00067                      bool debug = false);
00068   ~sdet_edgel_regions();
00069   //Main process method
00070   bool compute_edgel_regions(vil1_image const& image,
00071                              vcl_vector<vtol_edge_2d_sptr>& sgrp,
00072                              vcl_vector<vtol_intensity_face_sptr>& faces);
00073 
00074   bool compute_edgel_regions(vil_image_resource_sptr const& image,
00075                              vcl_vector<vtol_edge_2d_sptr>& sgrp,
00076                              vcl_vector<vtol_intensity_face_sptr>& faces);
00077 
00078 
00079   bool compute_edgel_regions(gevd_bufferxy* buf,
00080                              vcl_vector<vtol_edge_2d_sptr>& sgrp,
00081                              vcl_vector<vtol_intensity_face_sptr>& faces);
00082   //Acessors
00083   void SetVerbose() { verbose_ = true; }
00084   void ClearVerbose() { verbose_ = false; }
00085   void SetDebug() { debug_ = true; }
00086   void ClearDebug() { debug_ = false; }
00087 #if 0
00088   void set_magnification(float magnification) { magnification_=magnification; }
00089 #endif // 0
00090   unsigned int BaseLabel(unsigned int label);
00091   unsigned int GetMaxRegionLabel() const { return max_region_label_; }
00092   void SetMaxRegionLabel(unsigned int label) { max_region_label_ = label; }
00093   vbl_array_2d<unsigned int> const& GetRegionArray() const { return region_label_array_; }
00094   int GetXSize() const { return xend_ - xo_ + 1; }
00095   int GetYSize() const { return yend_ - yo_ + 1; }
00096 
00097   vil1_image GetEdgeImage(vcl_vector<vtol_edge_2d_sptr>& edgels);
00098 #ifdef DEBUG
00099   topo_debug_data_ref get_topo_debug_data() { return debug_data_; }
00100 #endif
00101   // Utilities (especially for testing)
00102   bool InsertRegionEquivalence(unsigned int label_b, unsigned int label_a);
00103   void GrowEquivalenceClasses();
00104   void PropagateEquivalence();
00105   unsigned int GetLabel(vtol_edge_2d_sptr e, unsigned int nr) const;
00106   // Debug print methods
00107   void print_region_array();
00108   void print_region_equivalence();
00109   void print_reverse_region_equivalence();
00110   void print_base_equivalence();
00111   void print_intensity_data();
00112  protected:
00113   // Utilities
00114   bool GroupContainsEdges(vcl_vector<vtol_edge_2d_sptr>& sg);
00115   bool InitRegionArray(vcl_vector<vtol_edge_2d_sptr>& sg);
00116   unsigned char label_code(unsigned int label);
00117   bool add_to_forward(unsigned int key, unsigned int value);
00118   bool add_to_reverse(unsigned int key, unsigned int value);
00119   unsigned char EncodeNeighborhood(unsigned int ul, unsigned int ur,
00120                                    unsigned int ll, unsigned int lr);
00121   void UpdateConnectedNeighborhood(unsigned int x, unsigned int y);
00122   void AssignEdgeLabels(unsigned int x, unsigned int y);
00123   void ApplyRegionEquivalence();
00124   bool out_of_bounds(unsigned int x, unsigned int y);
00125   void insert_adjacency(unsigned int region, vtol_edge_2d_sptr e);
00126   void CollectEdges();
00127   void CollectFaceEdges();
00128   void ConstructFaces();
00129   void AccumulateMeans();
00130   void AccumulateRegionData();
00131   void InsertFaceData();
00132   unsigned int X(unsigned int x);
00133   unsigned int Y(unsigned int y);
00134   float Xf(float x);
00135   float Yf(float y);
00136   bool insert_edgel(float pre_x, float pre_y, float x, float y,
00137                     sdet_region_edge_sptr const& e);
00138   void insert_equivalence(unsigned int ll, unsigned int ur, unsigned int& lr);
00139   bool merge_equivalence(vcl_map<unsigned int, vcl_vector<unsigned int>* >& tab,
00140                          unsigned int cur_label,
00141                          unsigned int label);
00142   bool get_next_label(vcl_vector<unsigned int>* labels,
00143                       unsigned int& label);
00144   void print_edge_colis(unsigned int x, unsigned int y,
00145                         sdet_region_edge_sptr const & r1,
00146                         sdet_region_edge_sptr const & r2);
00147 
00148   bool remove_hairs(vcl_vector<vtol_edge_2d_sptr>& edges);
00149 
00150   void get_buffer_row(unsigned int row);
00151   void get_image_row(unsigned int row);
00152   unsigned short get_intensity(unsigned int x);
00153   int bytes_per_pix();
00154 
00155   //to be used after image or buf are set
00156   bool compute_edgel_regions(vcl_vector<vtol_edge_2d_sptr>& sgrp,
00157                              vcl_vector<vtol_intensity_face_sptr>& faces);
00158 
00159   //members
00160   bool verbose_;
00161   bool debug_;
00162   bool image_source_;
00163   bool buf_source_;
00164   int s_;//resolution factor for label, edge arrays
00165   vil1_image image_;
00166   gevd_bufferxy* buf_;
00167   vbl_array_2d<sdet_region_edge_sptr> edge_boundary_array_;
00168   vbl_array_2d<unsigned int> region_label_array_;
00169   unsigned int min_region_label_;
00170   unsigned int max_region_label_;
00171   float Xob_;//buffer X origin in original image
00172   float Yob_;//buffer Y origin in original image
00173   unsigned int xo_;                    //X index of starting array element
00174   unsigned int yo_;                    //Y index of starting array element
00175   unsigned int xend_;                  //X index of ending array element
00176   unsigned int yend_;                  //Y index of ending array element
00177   unsigned int xs_;
00178   unsigned int ys_;
00179   //Region label equivalency hash tables
00180   vcl_map<unsigned int, vcl_vector<unsigned int>* > region_pairs_forward_;
00181   vcl_map<unsigned int, vcl_vector<unsigned int>* > region_pairs_reverse_;
00182   vcl_map<unsigned int, vcl_vector<unsigned int>* > equivalence_set_;
00183   vcl_map<unsigned int, unsigned int > label_map_;
00184   //hash table for Edge<->sdet_region_edge relationship
00185   vcl_map<int, sdet_region_edge_sptr> region_edges_;
00186   vcl_map<unsigned int, vcl_vector<vtol_edge_2d_sptr>* > region_edge_adjacency_;
00187   //Final output vtol_intensity_face(s) and relation to corresponding region label
00188   vcl_vector<vtol_intensity_face_sptr>* faces_;
00189   vtol_intensity_face_sptr* intensity_face_index_;
00190   vcl_vector<vtol_edge_2d_sptr>** face_edge_index_;
00191   vcl_vector<vtol_edge_2d_sptr>* failed_insertions_; //Short edges that fail
00192 #ifdef DEBUG
00193   topo_debug_data_ref debug_data_;
00194 #endif
00195   unsigned char* ubuf_;
00196   unsigned short* sbuf_;
00197 };
00198 
00199 #endif // sdet_edgel_regions_h_