00001 #ifndef vil_flood_fill_h_
00002 #define vil_flood_fill_h_
00003
00004
00005
00006
00007
00008 #include <vil/vil_image_view.h>
00009 #include <vcl_vector.h>
00010 #include <vcl_utility.h>
00011 #include <vil/vil_chord.h>
00012
00013
00014
00015
00016 template<class T>
00017 inline void vil_flood_fill_row(vil_image_view<T>& image,
00018 unsigned i, unsigned j,
00019 T v, T new_v,
00020 unsigned& ilo, unsigned& ihi)
00021 {
00022 T* row=image.top_left_ptr() + j*image.jstep();
00023 unsigned ni1=image.ni()-1;
00024 vcl_ptrdiff_t istep=image.istep();
00025 ilo=i;
00026 T* p=row+(i-1)*istep;
00027 while (ilo>0 && *p==v) { ilo--; *p=new_v; p-=istep; }
00028 ihi=i;
00029 p=row+(i+1)*istep;
00030 while (ihi<ni1 && *p==v) { ihi++; *p=new_v; p+=istep;}
00031 }
00032
00033
00034
00035
00036
00037
00038
00039
00040 template<class T>
00041 void vil_flood_fill4(vil_image_view<T>& image,
00042 unsigned seed_i, unsigned seed_j,
00043 T v, T new_v)
00044 {
00045 unsigned ni1=image.ni()-1;
00046 unsigned nj1=image.nj()-1;
00047 vcl_vector<vcl_pair<unsigned,unsigned> > q;
00048 if (seed_i>ni1 || seed_j>nj1) return;
00049
00050
00051 q.push_back(vcl_pair<unsigned,unsigned>(seed_i,seed_j));
00052
00053 unsigned k=0;
00054 while (k<q.size())
00055 {
00056 unsigned i=q[k].first, j=q[k].second;
00057 if (image(i,j)==v)
00058 {
00059 image(i,j)=new_v;
00060
00061 unsigned ilo,ihi;
00062 vil_flood_fill_row(image,i,j,v,new_v,ilo,ihi);
00063
00064 if (j>0)
00065 {
00066
00067 for (unsigned i1=ilo;i1<=ihi;++i1)
00068 if (image(i1,j-1)==v)
00069 q.push_back(vcl_pair<unsigned,unsigned>(i1,j-1));
00070 }
00071 if (j<nj1)
00072 {
00073
00074 for (unsigned i1=ilo;i1<=ihi;++i1)
00075 if (image(i1,j+1)==v)
00076 q.push_back(vcl_pair<unsigned,unsigned>(i1,j+1));
00077 }
00078 }
00079 k++;
00080 }
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 template<class T>
00094 void vil_flood_fill4(vil_image_view<T>& image,
00095 unsigned seed_i, unsigned seed_j,
00096 T v, T new_v,
00097 vcl_vector<vil_chord>& region)
00098 {
00099 region.resize(0);
00100 unsigned ni1=image.ni()-1;
00101 unsigned nj1=image.nj()-1;
00102 vcl_vector<vcl_pair<unsigned,unsigned> > q;
00103 if (seed_i>ni1 || seed_j>nj1) return;
00104
00105
00106 q.push_back(vcl_pair<unsigned,unsigned>(seed_i,seed_j));
00107
00108 unsigned k=0;
00109 while (k<q.size())
00110 {
00111 unsigned i=q[k].first, j=q[k].second;
00112 if (image(i,j)==v)
00113 {
00114 image(i,j)=new_v;
00115
00116 unsigned ilo,ihi;
00117 vil_flood_fill_row(image,i,j,v,new_v,ilo,ihi);
00118
00119 region.push_back(vil_chord(ilo,ihi,j));
00120
00121 if (j>0)
00122 {
00123
00124 for (unsigned i1=ilo;i1<=ihi;++i1)
00125 if (image(i1,j-1)==v)
00126 q.push_back(vcl_pair<unsigned,unsigned>(i1,j-1));
00127 }
00128 if (j<nj1)
00129 {
00130
00131 for (unsigned i1=ilo;i1<=ihi;++i1)
00132 if (image(i1,j+1)==v)
00133 q.push_back(vcl_pair<unsigned,unsigned>(i1,j+1));
00134 }
00135 }
00136 k++;
00137 }
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 template<class T>
00149 void vil_flood_fill8(vil_image_view<T>& image,
00150 unsigned seed_i, unsigned seed_j,
00151 T v, T new_v)
00152 {
00153 unsigned ni1=image.ni()-1;
00154 unsigned nj1=image.nj()-1;
00155 vcl_vector<vcl_pair<unsigned,unsigned> > q;
00156 if (seed_i>ni1 || seed_j>nj1) return;
00157
00158
00159 q.push_back(vcl_pair<unsigned,unsigned>(seed_i,seed_j));
00160
00161 unsigned k=0;
00162 while (k<q.size())
00163 {
00164 unsigned i=q[k].first, j=q[k].second;
00165 if (image(i,j)==v)
00166 {
00167 image(i,j)=new_v;
00168
00169 unsigned ilo,ihi;
00170 vil_flood_fill_row(image,i,j,v,new_v,ilo,ihi);
00171
00172
00173 if (ilo>0) ilo--;
00174 if (ihi<ni1) ihi++;
00175 if (j>0)
00176 {
00177
00178 for (unsigned i1=ilo;i1<=ihi;++i1)
00179 if (image(i1,j-1)==v)
00180 q.push_back(vcl_pair<unsigned,unsigned>(i1,j-1));
00181 }
00182 if (j<nj1)
00183 {
00184
00185 for (unsigned i1=ilo;i1<=ihi;++i1)
00186 if (image(i1,j+1)==v)
00187 q.push_back(vcl_pair<unsigned,unsigned>(i1,j+1));
00188 }
00189 }
00190 k++;
00191 }
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 template<class T>
00205 void vil_flood_fill8(vil_image_view<T>& image,
00206 unsigned seed_i, unsigned seed_j,
00207 T v, T new_v,
00208 vcl_vector<vil_chord>& region)
00209 {
00210 region.resize(0);
00211 unsigned ni1=image.ni()-1;
00212 unsigned nj1=image.nj()-1;
00213 vcl_vector<vcl_pair<unsigned,unsigned> > q;
00214 if (seed_i>ni1 || seed_j>nj1) return;
00215
00216
00217 q.push_back(vcl_pair<unsigned,unsigned>(seed_i,seed_j));
00218
00219 unsigned k=0;
00220 while (k<q.size())
00221 {
00222 unsigned i=q[k].first, j=q[k].second;
00223 if (image(i,j)==v)
00224 {
00225 image(i,j)=new_v;
00226
00227 unsigned ilo,ihi;
00228 vil_flood_fill_row(image,i,j,v,new_v,ilo,ihi);
00229
00230 region.push_back(vil_chord(ilo,ihi,j));
00231
00232
00233 if (ilo>0) ilo--;
00234 if (ihi<ni1) ihi++;
00235 if (j>0)
00236 {
00237
00238 for (unsigned i1=ilo;i1<=ihi;++i1)
00239 if (image(i1,j-1)==v)
00240 q.push_back(vcl_pair<unsigned,unsigned>(i1,j-1));
00241 }
00242 if (j<nj1)
00243 {
00244
00245 for (unsigned i1=ilo;i1<=ihi;++i1)
00246 if (image(i1,j+1)==v)
00247 q.push_back(vcl_pair<unsigned,unsigned>(i1,j+1));
00248 }
00249 }
00250 k++;
00251 }
00252 }
00253
00254 #endif // vil_flood_fill_h_