00001 #ifndef _IMAGESTOOLSFILTRE_H_
00002 #define _IMAGESTOOLSFILTRE_H_
00003
00004 #ifdef WIN32
00005 #include <iostream>
00006 #else
00007 #include <iostream.h>
00008 #endif
00009 using namespace std;
00010
00011
00012 #include <vector>
00013 #include <algorithm>
00014 #include <list>
00015 #include <iterator>
00016
00017 #include <math.h>
00018
00019
00036 template <class T>
00037 class ImageToolsFiltre
00038 {
00039 public:
00045 static Image<T> Filtre_Median(const Image<T> &image,const int w_size);
00046
00051 static Image<double> gradient_spatial(const Image<T> &image);
00052
00058 static Image<T> Variance(const Image<T> &image,const int w_size);
00059
00065 static Image<T> Filtre_Min(const Image<T> &image,const int w_size);
00066
00067 };
00069
00070 template <class T>
00071 Image<T> ImageToolsFiltre<T>::Filtre_Median(const Image<T> &image1,const int w_size)
00072 {
00073
00074 int nbl=image1.NbRow();
00075 int nbc=image1.NbCol();
00076 Image<T> image(nbl,nbc);
00077 int taille= (w_size*2+1)*(w_size*2+1);
00078 vector<T> tab(taille);
00079 vector<T> tab_trie(taille);
00080 list<T> liste;
00081
00082
00083 int i,j,u,v,ind,med,half,deb_i=0,deb_j=0,fin_i=0,fin_j=0;
00084 med=(int)((taille-1)/2);
00085 for(i=0;i<nbc;i++)
00086 {
00087 for(j=0;j<nbl;j++)
00088 {
00089 if((i>=w_size)&&(i<nbc-1-w_size)&&(j>=w_size)&&(j<nbl-1-w_size))
00090 {
00091
00092 ind=0;
00093 for(u=i-w_size;u<=i+w_size;u++)
00094 {
00095 for(v=j-w_size;v<=j+w_size;v++)
00096 {
00097 tab[ind]=image1(v,u);
00098 ind++;
00099 }
00100 }
00101 tab_trie=tab;
00102
00103 sort(tab_trie.begin(),tab_trie.end());
00104
00105 image(j,i)= (T)tab_trie[med];
00106
00107 }
00108 else
00109 {
00110
00111 if((i<w_size)&&(j<w_size))
00112 {
00113 deb_i=0;
00114 deb_j=0;
00115 fin_i=w_size;
00116 fin_j=w_size;
00117 }
00118
00119 if((i<w_size)&&(j>=w_size)&&(j<nbl-1-w_size))
00120 {
00121 deb_i=0;
00122 deb_j=j-w_size;
00123 fin_i=w_size;
00124 fin_j=j+w_size;
00125 }
00126
00127
00128 if((i<w_size)&&(j>=nbl-1-w_size))
00129 {
00130 deb_i=0;
00131 deb_j=j-w_size;
00132 fin_i=w_size;
00133 fin_j=nbl-1;
00134 }
00135
00136
00137 if((i>=nbc-1-w_size)&&(j<w_size))
00138 {
00139 deb_i=i-w_size;
00140 deb_j=0;
00141 fin_i=nbc-1;
00142 fin_j=j+w_size;
00143 }
00144
00145
00146 if((i>=nbc-1-w_size)&&(j>=w_size)&&(j<nbl-1-w_size))
00147 {
00148 deb_i=i-w_size;
00149 deb_j=j-w_size;
00150 fin_i=nbc-1;
00151 fin_j=j+w_size;
00152 }
00153
00154
00155 if((i>=nbc-1-w_size)&&(j>=nbl-1-w_size))
00156 {
00157 deb_i=i-w_size;
00158 deb_j=j-w_size;
00159 fin_i=nbc-1;
00160 fin_j=nbl-1;
00161 }
00162
00163
00164 if((i<nbc-1-w_size)&&(j<w_size)&&(i>=w_size))
00165 {
00166 deb_i=i-w_size;
00167 deb_j=0;
00168 fin_i=i+w_size;
00169 fin_j=w_size;
00170 }
00171
00172
00173 if((i<nbc-1-w_size)&&(j>=nbl-1-w_size)&&(i>=w_size))
00174 {
00175 deb_i=i-w_size;
00176 deb_j=j-w_size;
00177 fin_i=i+w_size;
00178 fin_j=nbl-1;
00179 }
00180 if(!liste.empty())
00181 liste.clear();
00182
00183
00184 for(u=deb_i;u<=fin_i;u++)
00185 {
00186 for(v=deb_j;v<=fin_j;v++)
00187 {
00188 liste.push_back(image1(v,u));
00189 }
00190 }
00191
00192 if(liste.empty())
00193 cout<<"probleme"<<endl;
00194 liste.sort();
00195 half = (int)(liste.size()/2.0)+1;
00196 for(u=1;u<half;u++)
00197 {
00198 liste.erase(liste.begin());
00199 }
00200
00201 image(j,i)=*liste.begin();
00202 }
00203 }
00204 }
00205 return image;
00206 }
00207
00208
00209 template <class T>
00210 Image<double> ImageToolsFiltre<T>::gradient_spatial(const Image<T> &image1)
00211 {
00212 int u,v;
00213 int nb_row,nb_col;
00214 int filtre_1[3];
00215 int filtre_2[3];
00216 nb_col=image1.NbCol();
00217 nb_row=image1.NbRow();
00218
00219 Image<double> sortie_x1(nb_row,nb_col);
00220 Image<double> sortie_x2(nb_row,nb_col);
00221 Image<double> sortie_y1(nb_row,nb_col);
00222 Image<double> sortie_y2(nb_row,nb_col);
00223
00224 Image<double> sortie(nb_row,nb_col);
00225
00226 filtre_1[0]=1;
00227 filtre_1[1]=2;
00228 filtre_1[2]=1;
00229
00230 for(u=1;u<nb_col-1;u++)
00231 {
00232 for(v=1;v<nb_row-1;v++)
00233 {
00234 sortie_x1(v,u)=(double)(filtre_1[0]*(int)image1(v-1,u)+filtre_1[1]*(int)image1(v,u)+filtre_1[2]*(int)image1(v+1,u));
00235
00236 sortie_y1(v,u)=(double)(filtre_1[0]*(int)image1(v,u-1)+filtre_1[1]*(int)image1(v,u)+filtre_1[2]*(int)image1(v,u+1));
00237 }
00238 }
00239
00240
00241 for(u=1;u<nb_col-1;u++)
00242 {
00243 for(v=1;v<nb_row-1;v++)
00244 {
00245 sortie_x2(v,u)=(double)(filtre_1[0]*(int)sortie_x1(v,u-1)+filtre_1[1]*(int)sortie_x1(v,u)+filtre_1[2]*(int)sortie_x1(v,u+1));
00246
00247 sortie_y2(v,u)=(double)(filtre_1[0]*(int)sortie_y1(v-1,u)+filtre_1[1]*(int)sortie_y1(v,u)+filtre_1[2]*(int)sortie_y1(v+1,u));
00248 }
00249 }
00250
00251
00252 filtre_2[0]=-1;
00253 filtre_2[1]=0;
00254 filtre_2[2]=1;
00255
00256 for(u=1;u<nb_col-1;u++)
00257 {
00258 for(v=1;v<nb_row-1;v++)
00259 {
00260 sortie(v,u)=sqrt((filtre_2[0]*sortie_x2(v,u-1)+filtre_2[1]*sortie_x2(v,u)+filtre_2[2]*sortie_x2(v,u+1))*(filtre_2[0]*sortie_x1(v,u-1)+filtre_2[1]*sortie_x1(v,u)+filtre_2[2]*sortie_x1(v,u+1)) +
00261 (filtre_2[0]*sortie_y1(v-1,u)+filtre_2[1]*sortie_y1(v,u)+filtre_2[2]*sortie_y1(v+1,u))*(filtre_2[0]*sortie_y1(v-1,u)+filtre_2[1]*sortie_y1(v,u)+filtre_2[2]*sortie_y1(v+1,u)));
00262 }
00263 }
00264 return sortie;
00265 }
00266
00267
00268 template <class T>
00269 Image<T> ImageToolsFiltre<T>::Variance(const Image<T> &image1,const int w_size)
00270 {
00271
00272 int nbl=image1.NbRow();
00273 int nbc=image1.NbCol();
00274 Image<T> image(nbl,nbc);
00275
00276 double moyenne, variance, diff;
00277 int nbelem;
00278
00279 for (int y=0,i=0; y<nbl;y++) {
00280 for (int x=0; x<nbc; x++, i++) {
00281 moyenne = 0;
00282 variance = 0;
00283 nbelem = 0;
00284
00285
00286 int v,u;
00287 for ( v=y-w_size; v<=y+w_size; v++) {
00288 for ( u=x-w_size; u<=x+w_size; u++) {
00289 if ((v>=0) && (v<nbl) && (u>=0) && (u<nbc)) {
00290 moyenne += image(v,u);
00291 nbelem++;
00292 }
00293 }
00294 }
00295
00296 moyenne /= nbelem;
00297
00298
00299 for ( v=y-w_size; v<=y+w_size; v++) {
00300 for ( u=x-w_size; u<=x+w_size; u++) {
00301 if ((v>=0) && (v<nbl) && (u>=0) && (u<nbc)) {
00302 diff = image(v,u) - moyenne;
00303 variance = diff*diff;
00304 }
00305 }
00306 }
00307
00308 image(y,x)= variance/nbelem;
00309 }
00310 }
00311
00312
00313 return image;
00314 }
00315
00316
00317
00318 template <class T>
00319 Image<T> ImageToolsFiltre<T>::Filtre_Min(const Image<T> &image1,const int w_size)
00320 {
00321
00322 int nbl=image1.NbRow();
00323 int nbc=image1.NbCol();
00324 Image<T> image(nbl,nbc);
00325
00326 double valMin;
00327 bool first;
00328
00329 for (int y=0,i=0; y<nbl;y++) {
00330 for (int x=0; x<nbc; x++, i++) {
00331
00332 first = true;
00333
00334
00335 int v,u;
00336 for ( v=y-w_size; v<=y+w_size; v++)
00337 for ( u=x-w_size; u<=x+w_size; u++)
00338 if ((v>=0) && (v<nbl) && (u>=0) && (u<nbc)) {
00339 if (first) {
00340 valMin = image(v,u);
00341 first = false;
00342 }
00343 else
00344 if (image(v,u)<valMin) valMin = image(v,u);
00345 }
00346
00347
00348 image (y,x) = valMin;
00349 }
00350 }
00351
00352 return image;
00353 }
00354
00355 #endif