00001 // This is brl/bseg/brip/brip_watershed.h 00002 #ifndef brip_watershed_h_ 00003 #define brip_watershed_h_ 00004 //----------------------------------------------------------------------------- 00005 //: 00006 // \file 00007 // \author J.L. Mundy 00008 // \brief Computes the seeded watershed algorithm 00009 // 00010 // The watershed algorithm proceeds from a set of seeds that are 00011 // defined by some other process. A typical seed generator is to 00012 // find local minima of gradient magnitude. A cost function is defined that 00013 // provides a measure on pixels in the image. A typical cost is based on 00014 // the gradient magnitude. The initial seed is extended to a region by adding 00015 // the lowest cost pixel. The region continues to grow by adding lowest cost 00016 // unlabeled pixels to each pixel in the region. 00017 // 00018 // Each seed defines a unique label which is propagated to each pixel in the 00019 // associated region. Regions are isolated by a boundary with a unique (0) 00020 // label. 00021 // 00022 // Region growth is managed by a priority queue. Neighboring pixels to each 00023 // seed are inserted in the queue to get the process started. When a new pixel 00024 // is added to the region, its unlabeled neighbors are added to the queue. 00025 // 00026 // \verbatim 00027 // Modifications 00028 // Initial version June 18, 2004 00029 // \endverbatim 00030 // 00031 //----------------------------------------------------------------------------- 00032 #include <vcl_vector.h> 00033 #include <vcl_map.h> 00034 #include <vcl_queue.h> 00035 #include <vbl/vbl_array_2d.h> 00036 #include <vil1/vil1_image.h> 00037 #include <vil1/vil1_memory_image_of.h> 00038 #include <brip/brip_region_pixel.h> 00039 #include <brip/brip_watershed_params.h> 00040 00041 class brip_watershed : public brip_watershed_params 00042 { 00043 public: 00044 enum label {UNLABELED = 0, BOUNDARY=1}; 00045 //:Constructors/destructor 00046 brip_watershed(brip_watershed_params const& bwp); 00047 ~brip_watershed(); 00048 //: Accessors/Mutators 00049 void set_image(vil1_memory_image_of<float> const& image); 00050 static unsigned int min_region_label() {return BOUNDARY + 1;} 00051 unsigned int max_region_label() const {return max_region_label_;} 00052 vil1_image overlay_image(); 00053 vbl_array_2d<unsigned int>& region_label_array(){return region_label_array_;} 00054 bool adjacent_regions(const unsigned int region, 00055 vcl_vector<unsigned int>& adj_regs); 00056 //: Main process method 00057 bool compute_regions(); 00058 //: Debug methods 00059 void print_region_array(); 00060 void print_adjacency_map(); 00061 protected: 00062 //: internal methods 00063 void print_neighborhood(int col, int row, unsigned int lab); 00064 bool add_adjacency(const unsigned int reg, const unsigned int adj_reg); 00065 bool compute_seeds(); 00066 bool initialize_queue(); 00067 bool grow_regions(); 00068 //: members 00069 brip_watershed();//don't use this constructor 00070 int width_; 00071 int height_; 00072 unsigned int max_region_label_; 00073 vbl_array_2d<unsigned int> region_label_array_; 00074 vil1_memory_image_of<float> image_; 00075 vil1_memory_image_of<float> gradient_mag_image_; 00076 vcl_priority_queue<brip_region_pixel_sptr, vcl_vector<brip_region_pixel_sptr>, 00077 brip_region_pixel::compare> priority_queue_; 00078 vcl_map<unsigned int, vcl_vector<unsigned int>* > region_adjacency_; 00079 }; 00080 00081 #endif // brip_watershed_h_