00001 #ifndef _IMAGESTOOLS_MISC_H_
00002 #define _IMAGESTOOLS_MISC_H_
00003 #include <vector>
00004 #include <algorithm>
00005 #include <list>
00006 #include <iterator>
00007 #include <iostream>
00008 #include <math.h>
00009 using namespace std;
00010
00027 template <class T>
00028 class ImageToolsMisc
00029 {
00030 public:
00038 static vector<unsigned int> Histogramme(const Image<T> &image,const unsigned int nb_classes=256,const T min=0,const T max=255);
00039
00045 static Image<T> Partition_connexe(const Image<T> &image1,int &nb_classes);
00046
00047 };
00049
00050
00051 template <class T>
00052 vector<unsigned int> ImageToolsMisc<T>::Histogramme(const Image<T> &image,const unsigned int nb_classe,const T min,const T max)
00053 {
00054 double pas;
00055 unsigned int k;
00056 int i,j;
00057 vector <unsigned int> histo(nb_classe+1);
00058 for(i=0;i<nb_classe;i++)
00059 histo[i]=0;
00060
00061 pas=(double)(max-min)/(double)(nb_classe );
00062 for(i=0;i<image.NbCol();i++)
00063 {
00064 for(j=0;j<image.NbRow();j++)
00065 {
00066 k=(unsigned int)((image(j,i) -min)/pas);
00067
00068 if(k>=nb_classe)
00069 k=nb_classe-1;
00070 if(k<0)
00071 k=0;
00072 histo[k]++;
00073 }
00074 }
00075 return histo;
00076 }
00077
00078
00079 template <class T>
00080 Image<T> ImageToolsMisc<T>::Partition_connexe(const Image<T> &image1,int &nb_cl)
00081 {
00082 int nbl=image1.NbRow();
00083 int nbc=image1.NbCol();
00084 Image<T> image(nbl,nbc);
00085 int courant=0;
00086 int centre=0;
00087
00088 int i,j,k,u,l=1,i1,j1;
00089 bool test=false;
00090
00091 image.InitTo(0);
00092 vector<list<int> > classes(256);
00093 vector<list<int> > classes_epure(256);
00094 list<int>::iterator iter,iter1;
00095 int classe_cible;
00096 int nb_classes=0;
00097
00098
00099 for(i=0;i<nbl;i++)
00100 {
00101 for(j=0;j<nbc;j++)
00102 {
00103
00104 if(courant>=250)
00105 {
00106 cout<<"plus de 250 classes"<<endl;
00107 l=1;
00108 nb_classes=0;
00109 for(u=1;u<=courant;u++)
00110 {
00111 if(!classes[u].empty())
00112 {
00113 classes_epure[l].clear();
00114 classes_epure[l]=classes[u];
00115 classes[u].clear();
00116 l++;
00117 nb_classes++;
00118 }
00119 }
00120
00121 cout<<"nombre de classes apres epuration"<<nb_classes<<endl;
00122 for(u=1;u<=nb_classes;u++)
00123 {
00124 classes[u]=classes_epure[u];
00125 for(i1=0;i1<nbl;i1++)
00126 {
00127 for(j1=0;j1<nbc;j1++)
00128 {
00129 if(image(i1,j1)!=0)
00130 {
00131 for(k=1;k<=nb_classes;k++)
00132 {
00133 iter=find(classes_epure[k].begin(),classes_epure[k].end(),image(i1,j1));
00134 if(iter!=classes_epure[k].end())
00135 {
00136 image(i1,j1)=k;
00137 }
00138 }
00139 }
00140 }
00141 }
00142 }
00143 courant=nb_cl-1;
00144 }
00145
00146
00147
00148
00149
00150
00151 test=false;
00152 centre=0;
00153 classe_cible=0;
00154
00155 if(image1(i,j)!=0)
00156 {
00157 if(i==0&&j==0)
00158 {
00159 courant++;
00160 classes[courant].push_back(courant);
00161 image(i,j)=courant;
00162 }
00163
00164 if(i==0&&j!=0)
00165 {
00166 if(image(i,j-1)!=0)
00167 image(i,j)=image(i,j-1);
00168
00169 else
00170 {
00171 courant++;
00172 classes[courant].push_back(courant);
00173 image(i,j)=courant;
00174 }
00175 }
00176
00177
00178 if(j==0&&i!=0)
00179 {
00180 if(image(i-1,j)!=0)
00181 image(i,j)=image(i-1,j);
00182
00183 else
00184 {
00185 courant++;
00186 classes[courant].push_back(courant);
00187 image(i,j)=courant;
00188 }
00189 }
00190
00191
00192
00193
00194 if(i!=0&&j!=0)
00195 {
00196
00197 if((image(i-1,j)!=0)&&(image(i,j-1)==0))
00198 {
00199 test=true;
00200 centre=image(i-1,j);
00201 }
00202
00203 if((image(i-1,j)==0)&&(image(i,j-1)!=0))
00204 {
00205 test=true;
00206 centre=image(i,j-1);
00207 }
00208
00209
00210 if((image(i,j-1)!=0)&&(image(i-1,j)!=0))
00211 {
00212 test=true;
00213 centre=image(i-1,j);
00214 for(k=1;k<=courant;k++)
00215 {
00216 iter=find(classes[k].begin(),classes[k].end(),image(i-1,j));
00217 if(iter!=classes[k].end())
00218 {
00219 classe_cible=k;
00220 break;
00221 }
00222 }
00223
00224 for(k=1;k<=courant;k++)
00225 {
00226 iter=find(classes[k].begin(),classes[k].end(),image(i,j-1));
00227
00228 if(iter!=classes[k].end()&&(k!=classe_cible))
00229 {
00230 iter1=classes[k].begin();
00231
00232 while(iter1!=classes[k].end())
00233 {
00234
00235 classes[classe_cible].push_back(*iter1);
00236 iter1++;
00237 }
00238 classes[k].clear();
00239 break;
00240 }
00241 }
00242
00243
00244 }
00245
00246
00247 if(!test)
00248 {
00249 courant++;
00250 classes[courant].push_back(courant);
00251 image(i,j)=courant;
00252 }
00253
00254 if(test)
00255 {
00256 image(i,j)=centre;
00257 }
00258
00259 }
00260
00261
00262 }
00263
00264 }
00265 }
00266
00267 nb_classes=0;
00268 l=1;
00269 for(i=1;i<=courant;i++)
00270 {
00271 if(!classes[i].empty())
00272 {
00273 classes_epure[l].clear();
00274 classes_epure[l]=classes[i];
00275 l++;
00276 nb_classes++;
00277 }
00278 }
00279
00280
00281
00282 for(i=0;i<nbl;i++)
00283 {
00284 for(j=0;j<nbc;j++)
00285 {
00286 if(image(i,j)!=0)
00287 {
00288 for(k=1;k<=nb_classes;k++)
00289 {
00290 iter=find(classes_epure[k].begin(),classes_epure[k].end(),image(i,j));
00291 if(iter!=classes_epure[k].end())
00292 {
00293 image(i,j)=k;
00294 }
00295 }
00296 }
00297 }
00298 }
00299 nb_cl=nb_classes;
00300 return image;
00301 }
00302
00303
00304 #endif