00001 #ifndef vil_blob_finder_h_ 00002 #define vil_blob_finder_h_ 00003 //: 00004 // \file 00005 // \brief Finds connected regions in a boolean image. 00006 // \author Tim Cootes 00007 00008 #include <vil/vil_image_view.h> 00009 #include <vil/vil_chord.h> 00010 #include <vcl_vector.h> 00011 00012 //: Finds connected regions in a boolean image 00013 // Class to iterate through image, finding boundaries of all regions 00014 // (connected true pixels). 00015 // 00016 // Can deal with both 4-connected and 8-connected regions. 00017 // 00018 // Note that the usual use of the class copies the boolean image input, 00019 // then erases the true pixels as it goes through finding the blobs (in 00020 // order to avoid finding the same blob twice). If the image is expendable, 00021 // then the deep copy can be avoided using set_work_image(im). In this 00022 // case the supplied image (im) will be erased as the blob finder goes 00023 // about it's humble task. 00024 // 00025 // Where a blob has holes, only the outside of the blob is located. 00026 // The algorithm will locate nested blobs (ie a blob within a hole 00027 // inside another blob). 00028 // 00029 // \code 00030 // vcl_vector<int> bi,bj; 00031 // vil_blob_finder finder(bool_image); 00032 // while (finder.next_4con_region(bi,bj)) 00033 // { 00034 // vcl_cout<<"Blob boundary length: "<<bi.size()<<vcl_endl; 00035 // } 00036 // \endcode 00037 // 00038 // Alternatively we can get the internal pixels (including the boundary) 00039 // of each blob. A region is represented as a vcl_vector<vil_chord>, 00040 // a set of rows of pixels which make up all the pixels in the region. 00041 // \code 00042 // vcl_vector<vil_chord> region; 00043 // vil_blob_finder finder(bool_image); 00044 // while (finder.next_4con_region(region)) 00045 // { 00046 // vcl_cout<<"Blob area: "<<vil_area(region)<<vcl_endl; 00047 // } 00048 // \endcode 00049 // This is useful when we have holes and wish to represent the region 00050 // more completely than just using its external boundary. 00051 00052 class vil_blob_finder 00053 { 00054 private: 00055 //: Workspace (may be view of external image) 00056 vil_image_view<bool> image_; 00057 00058 //: Current point of interest 00059 unsigned int i_,j_; 00060 00061 public: 00062 //: Default constructor 00063 vil_blob_finder(); 00064 00065 //: Construct to work on image (takes deep copy) 00066 vil_blob_finder(const vil_image_view<bool>& image); 00067 00068 //: Define image to work on (deep copy taken) 00069 void set_image(const vil_image_view<bool>& image); 00070 00071 //: Define image to work on and use as workspace 00072 // Image will be erased during process. 00073 // Avoids the deep copy in set_image() 00074 void set_work_image(vil_image_view<bool>& image); 00075 00076 //: Get boundary pixels of next blob in current image. 00077 // Uses four connected boundary representation. 00078 // Return false if no more regions 00079 bool next_4con_region(vcl_vector<int>& bi, vcl_vector<int>& bj); 00080 00081 //: Get pixels of next blob in current image. 00082 // Uses 4 connected representation. 00083 // Return false if no more regions 00084 bool next_4con_region(vcl_vector<vil_chord>& region); 00085 00086 //: Get longest blob boundary in current image 00087 // Assumes image has been initialised, and that next_4con_region not 00088 // yet called. Erases internal image during this call, so any 00089 // subsequent calls will not work. 00090 // 00091 // bi,bj empty if no blobs found. 00092 void longest_4con_boundary(vcl_vector<int>& bi, vcl_vector<int>& bj); 00093 00094 //: Get largest blob region in current image 00095 // Assumes image has been initialised, and that next_8con_region not 00096 // yet called. Erases internal image during this call, so any 00097 // subsequent calls will not work. 00098 // 00099 // region empty if no blobs found. 00100 // Returns area of largest region 00101 unsigned largest_4con_region(vcl_vector<vil_chord>& region); 00102 00103 //: Get number of blobs in given image 00104 // Overrides any internal state 00105 unsigned n_4con_regions(const vil_image_view<bool>& image); 00106 00107 //: Get boundary pixels of next blob in current image. 00108 // Uses 8 connected boundary representation. 00109 // Return false if no more regions 00110 bool next_8con_region(vcl_vector<int>& bi, vcl_vector<int>& bj); 00111 00112 //: Get pixels of next blob in current image. 00113 // Uses 8 connected representation. 00114 // Return false if no more regions 00115 bool next_8con_region(vcl_vector<vil_chord>& region); 00116 00117 //: Get longest blob boundary in current image 00118 // Assumes image has been initialised, and that next_8con_region not 00119 // yet called. Erases internal image during this call, so any 00120 // subsequent calls will not work. 00121 // 00122 // bi,bj empty if no blobs found. 00123 void longest_8con_boundary(vcl_vector<int>& bi, vcl_vector<int>& bj); 00124 00125 //: Get largest blob region in current image 00126 // Assumes image has been initialised, and that next_8con_region not 00127 // yet called. Erases internal image during this call, so any 00128 // subsequent calls will not work. 00129 // 00130 // region empty if no blobs found. 00131 // Returns area of largest region 00132 unsigned largest_8con_region(vcl_vector<vil_chord>& region); 00133 00134 //: Get number of blobs in given image 00135 // Overrides any internal state 00136 unsigned n_8con_regions(const vil_image_view<bool>& image); 00137 }; 00138 00139 #endif
1.5.1