contrib/gel/gevd/gevd_edgel_regions.h
Go to the documentation of this file.
00001 // This is gel/gevd/gevd_edgel_regions.h
00002 #ifndef gevd_edgel_regions_h_
00003 #define gevd_edgel_regions_h_
00004 //:
00005 // \file
00006 // \brief generation of regions bounded by gevd_edgel chains
00007 // \verbatim
00008 //     Inputs:  Image, EdgelGroup
00009 //     Output: CoolListP<IntensityFace*>
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 Face, the
00029 //  IntensityFace.  The IntensityFace maintains a scatter matrix of
00030 //  a planar fit to the intensity distribution over the corresponding
00031 //  region.
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 gevd_region_edge change
00044 // \endverbatim
00045 //
00046 //-----------------------------------------------------------------------------
00047 #include <vcl_vector.h>
00048 #include <vcl_map.h>
00049 
00050 #include <vtol/vtol_vertex_sptr.h>
00051 #include <vtol/vtol_edge_2d_sptr.h>
00052 #include <gevd/gevd_region_edge.h>
00053 #include <gevd/gevd_bufferxy.h>
00054 #include <vtol/vtol_intensity_face_sptr.h>
00055 #include <vil1/vil1_image.h>
00056 
00057 
00058 class gevd_edgel_regions
00059 {
00060  public:
00061   enum RegionLabel {UNLABELED=0, EDGE, LABEL};
00062   //Constructors/Destructors
00063   gevd_edgel_regions(bool debug = false);
00064   ~gevd_edgel_regions();
00065   //Main process method
00066   bool compute_edgel_regions(vil1_image* image,
00067                              vcl_vector<vtol_edge_2d_sptr>& sgrp,
00068                              vcl_vector<vtol_intensity_face_sptr>& faces);
00069 
00070   bool compute_edgel_regions(gevd_bufferxy* buf,
00071                              vcl_vector<vtol_edge_2d_sptr>& sgrp,
00072                              vcl_vector<vtol_intensity_face_sptr>& faces);
00073   //Acessors
00074   void SetVerbose() {verbose_ = true;}
00075   void ClearVerbose() {verbose_ = false;}
00076   void SetDebug() {debug_ = true;}
00077   void ClearDebug() {debug_ = false;}
00078   void set_magnification(float magnification){magnification_=magnification;}
00079   unsigned int BaseLabel(unsigned int label);
00080   unsigned int GetMaxRegionLabel() const {return max_region_label_;}
00081   void SetMaxRegionLabel(unsigned int label){max_region_label_ = label;}
00082   unsigned int** GetRegionArray(){return region_label_array_;}
00083   int GetXSize() const {return xend_ - xo_ + 1;}
00084   int GetYSize() const {return yend_ - yo_ + 1;}
00085   vil1_image* GetEdgeImage(vcl_vector<vtol_edge_2d_sptr>& edgels);
00086 #if 0
00087   topo_debug_data_ref get_topo_debug_data() {return debug_data_;}
00088 #endif
00089   // Utilities (especially for testing)
00090   bool InsertRegionEquivalence(unsigned int label_b, unsigned int label_a);
00091   void GrowEquivalenceClasses();
00092   void PropagateEquivalence();
00093   unsigned int GetLabel(vtol_edge_2d_sptr e, unsigned int nr);
00094   // Debug print methods
00095   void print_region_array();
00096   void print_region_equivalence();
00097   void print_reverse_region_equivalence();
00098   void print_base_equivalence();
00099   void print_intensity_data();
00100  protected:
00101   // Utilities
00102   bool GroupContainsEdges(vcl_vector<vtol_edge_2d_sptr>& sg);
00103   bool InitRegionArray(vcl_vector<vtol_edge_2d_sptr>& sg);
00104   unsigned char label_code(unsigned int label);
00105   bool add_to_forward(unsigned int key, unsigned int value);
00106   bool add_to_reverse(unsigned int key, unsigned int value);
00107   unsigned char EncodeNeighborhood(unsigned int ul, unsigned int ur,
00108                                    unsigned int ll, unsigned int lr);
00109   void UpdateConnectedNeighborhood(unsigned int x, unsigned int y);
00110   void AssignEdgeLabels(unsigned int x, unsigned int y);
00111   void ApplyRegionEquivalence();
00112   bool out_of_bounds(unsigned int x, unsigned int y);
00113   void insert_adjacency(unsigned int region, vtol_edge_2d_sptr e);
00114   void CollectEdges();
00115   void CollectFaceEdges();
00116   void ConstructFaces();
00117   void AccumulateMeans();
00118   void AccumulateRegionData();
00119   void InsertFaceData();
00120   unsigned int X(unsigned int x);
00121   unsigned int Y(unsigned int y);
00122   unsigned int Xf(float x);
00123   unsigned int Yf(float y);
00124   bool insert_edgel(float pre_x, float pre_y, float x, float y,
00125                     gevd_region_edge* e);
00126   void insert_equivalence(unsigned int ll, unsigned int ur, unsigned int& lr);
00127   bool merge_equivalence(vcl_map<unsigned int, vcl_vector<unsigned int>* >& tab,
00128                          unsigned int cur_label,
00129                          unsigned int label);
00130   bool get_next_label(vcl_vector<unsigned int>* labels,
00131                       unsigned int& label);
00132   void print_edge_colis(unsigned int x, unsigned int y,
00133                         gevd_region_edge* r1, gevd_region_edge* r2);
00134   bool corrupt_boundary(vcl_vector<vtol_edge_2d_sptr>& edges,
00135                         vcl_vector<vtol_vertex_sptr>& bad_verts);
00136   bool remove_hairs(vcl_vector<vtol_edge_2d_sptr>& edges);
00137   bool connect_ends(vcl_vector<vtol_edge_2d_sptr>& edges,
00138                     vcl_vector<vtol_vertex_sptr>& bad_verts);
00139   void repair_failed_insertions(vcl_vector<vtol_edge_2d_sptr>& edges,
00140                                 vcl_vector<vtol_vertex_sptr>& bad_verts);
00141   void get_buffer_row(unsigned int row);
00142   void get_image_row(unsigned int row);
00143   unsigned short get_intensity(unsigned int x);
00144   int bytes_per_pix();
00145 
00146   //to be used after image or buf are set
00147   bool compute_edgel_regions(vcl_vector<vtol_edge_2d_sptr>& sgrp,
00148                              vcl_vector<vtol_intensity_face_sptr>& faces);
00149   //members
00150   bool verbose_;
00151   bool debug_;
00152   bool image_source_;
00153   bool buf_source_;
00154   float magnification_;
00155   vil1_image* image_;
00156   gevd_bufferxy* buf_;
00157   gevd_region_edge*** edge_boundary_array_;
00158   unsigned int** region_label_array_;
00159   unsigned int min_region_label_;
00160   unsigned int max_region_label_;
00161   float Xob_;//buffer X origin in original image
00162   float Yob_;//buffer Y origin in original image
00163   unsigned int xo_;                    //X coor of starting x pixel
00164   unsigned int yo_;                    //Y coor of starting y pixel
00165   unsigned int xend_;                  //X coor of ending x pixel
00166   unsigned int yend_;                  //Y coor of ending y pixel
00167   //Region label equivalency hash tables
00168   vcl_map<unsigned int, vcl_vector<unsigned int>* > region_pairs_forward_;
00169   vcl_map<unsigned int, vcl_vector<unsigned int>* > region_pairs_reverse_;
00170   vcl_map<unsigned int, vcl_vector<unsigned int>* > equivalence_set_;
00171   vcl_map<unsigned int, unsigned int > label_map_;
00172   //hash table for Edge<->gevd_region_edge relationship
00173   vcl_map<int, gevd_region_edge*> region_edges_;
00174   vcl_map<unsigned int, vcl_vector<vtol_edge_2d_sptr>* > region_edge_adjacency_;
00175   //Final output vtol_intensity_face(s) and relation to corresponding region label
00176   vcl_vector<vtol_intensity_face_sptr>* faces_;
00177   vtol_intensity_face_sptr* intensity_face_index_;
00178   vcl_vector<vtol_edge_2d_sptr>** face_edge_index_;
00179   vcl_vector<vtol_edge_2d_sptr>* failed_insertions_; //Short edges that fail
00180 #if 0
00181   topo_debug_data_ref debug_data_;
00182 #endif
00183   unsigned char* ubuf_;
00184   unsigned short* sbuf_;
00185 };
00186 
00187 #endif // gevd_edgel_regions_h_