| [658] | 1 | #include "machdefs.h" | 
|---|
|  | 2 | #include <stdio.h> | 
|---|
|  | 3 | #include <stdlib.h> | 
|---|
|  | 4 |  | 
|---|
|  | 5 | #include "simplesort.h" | 
|---|
|  | 6 | #include "nbmath.h" | 
|---|
|  | 7 | #include "histos.h" | 
|---|
|  | 8 | #include "datatypes.h" | 
|---|
|  | 9 | #include "imageop.h" | 
|---|
|  | 10 | #include "nbtri.h" | 
|---|
|  | 11 |  | 
|---|
|  | 12 |  | 
|---|
|  | 13 | //++ | 
|---|
|  | 14 | //  Module      Operations sur les images (C++) | 
|---|
|  | 15 | //  Lib Images++ | 
|---|
|  | 16 | //  include     imageop.h | 
|---|
|  | 17 | // | 
|---|
|  | 18 | //      Operations sur les images. | 
|---|
|  | 19 | //-- | 
|---|
|  | 20 |  | 
|---|
|  | 21 |  | 
|---|
|  | 22 | // Outils prives. | 
|---|
|  | 23 | template <class T> | 
|---|
|  | 24 | float RzCmvSigmaCiel(T const * pix,int nx,int ny,int ndata,int bin, | 
|---|
|  | 25 | int taille,float frac,int deg, | 
|---|
|  | 26 | float low,float high,int deb); | 
|---|
|  | 27 |  | 
|---|
|  | 28 | #ifdef IMGRGCHECK | 
|---|
|  | 29 | #define CHECKIMG2(_img_,_x_,_y_)                                  \ | 
|---|
|  | 30 | if ((_x_ >= (_img_).siz_x) || (_y_ >= (_img_).siz_y) ||              \ | 
|---|
|  | 31 | (_x_ < 0) || (_y_ < 0)) THROW(rangeCheckErr);  \ | 
|---|
|  | 32 | if (!(_img_).ImagePtr()) THROW(nullPtrErr) | 
|---|
|  | 33 | #else | 
|---|
|  | 34 | #if defined(IMGVOIDCHECK) | 
|---|
|  | 35 | #define CHECKIMG2(_img_,_x_,_y_) \ | 
|---|
|  | 36 | if (!(_img_).ImagePtr()) THROW(NullPtrErr) | 
|---|
|  | 37 | #else | 
|---|
|  | 38 | #define CHECKIMG2(_img_,_x_,_y_) | 
|---|
|  | 39 | #endif | 
|---|
|  | 40 | #endif /* IMGRGCHECK */ | 
|---|
|  | 41 |  | 
|---|
|  | 42 | // Les fonctions | 
|---|
|  | 43 |  | 
|---|
|  | 44 | /* Nouvelle-Fonction */ | 
|---|
|  | 45 |  | 
|---|
|  | 46 | template <class T> | 
|---|
|  | 47 | void CleanImagesOld(int nImg, Image<T>** imgTab, double nSig, | 
|---|
|  | 48 | double lowbad, double highbad) | 
|---|
|  | 49 |  | 
|---|
|  | 50 | /*  Nettoie une serie d'image a partir de l'histo de la  */ | 
|---|
|  | 51 | /* serie de valeurs pour chaque pixel (Voir E. Aubourg)  */ | 
|---|
|  | 52 |  | 
|---|
|  | 53 | { | 
|---|
|  | 54 | if (lowbad < MinRange((T)0)) | 
|---|
|  | 55 | lowbad = MinRange((T)0); | 
|---|
|  | 56 |  | 
|---|
|  | 57 | if (highbad > MaxRange((T)0)) | 
|---|
|  | 58 | highbad = MaxRange((T)0); | 
|---|
|  | 59 |  | 
|---|
|  | 60 | if (lowbad >= highbad) | 
|---|
|  | 61 | {lowbad = MinRange((T)0); highbad = MaxRange((T)0);} | 
|---|
|  | 62 | Histo h(lowbad, highbad, 500); | 
|---|
|  | 63 |  | 
|---|
|  | 64 | for (int i=0; i<imgTab[0]->XSize(); i++) | 
|---|
|  | 65 | for (int j=0; j<imgTab[0]->YSize(); j++) { | 
|---|
|  | 66 | h.Zero(); | 
|---|
|  | 67 | for (int k=0; k<nImg; k++) | 
|---|
|  | 68 | h.Add((*imgTab[k])(i,j)); | 
|---|
|  | 69 | float x0, s0=0; | 
|---|
|  | 70 | x0 = h.CleanedMean(s0); | 
|---|
|  | 71 | float low = x0 - s0*nSig; if (low < lowbad) low = lowbad; | 
|---|
|  | 72 | float hi  = x0 + s0*nSig; // if (hi  > highbad) hi = highbad; | 
|---|
|  | 73 | for (k=0; k<nImg; k++) | 
|---|
|  | 74 | if ((*imgTab[k])(i,j) > hi || (*imgTab[k])(i,j) < low) | 
|---|
|  | 75 | (*imgTab[k])(i,j) = 0; | 
|---|
|  | 76 | } | 
|---|
|  | 77 | } | 
|---|
|  | 78 |  | 
|---|
|  | 79 | /* Nouvelle-Fonction */ | 
|---|
|  | 80 |  | 
|---|
|  | 81 | template <class T> | 
|---|
|  | 82 | //++ | 
|---|
|  | 83 | void CleanImages(int nImg, Image<T>** imgTab, double nSig, | 
|---|
|  | 84 | double lowbad, double highbad) | 
|---|
|  | 85 | // | 
|---|
|  | 86 | //      Nettoie une serie de `nImg' images `imgTab' a partir | 
|---|
|  | 87 | //      d'une moyenne tronquee a `nSig' sigma | 
|---|
|  | 88 | //      de la serie de valeurs pour chaque pixel. | 
|---|
|  | 89 | //      Les valeurs des pixels sont prises entre `lowbad' et `highbad'. | 
|---|
|  | 90 | //-- | 
|---|
|  | 91 | { | 
|---|
|  | 92 | if (lowbad < MinRange((T)0)) | 
|---|
|  | 93 | lowbad = MinRange((T)0); | 
|---|
|  | 94 |  | 
|---|
|  | 95 | if (highbad > MaxRange((T)0)) | 
|---|
|  | 96 | highbad = MaxRange((T)0); | 
|---|
|  | 97 |  | 
|---|
|  | 98 | if (lowbad >= highbad) | 
|---|
|  | 99 | {lowbad = MinRange((T)0); highbad = MaxRange((T)0);} | 
|---|
|  | 100 |  | 
|---|
|  | 101 | double sx, sx2, sig, mean; | 
|---|
|  | 102 | int n; | 
|---|
|  | 103 |  | 
|---|
|  | 104 | for (int i=0; i<imgTab[0]->XSize(); i++) | 
|---|
|  | 105 | for (int j=0; j<imgTab[0]->YSize(); j++) { | 
|---|
|  | 106 | sig = 1e20; mean = 0; | 
|---|
|  | 107 | for (int l = 0; l<3; l++) { | 
|---|
|  | 108 | double low = mean - nSig*sig; | 
|---|
|  | 109 | if (low < lowbad) low = lowbad; | 
|---|
|  | 110 | double high = mean + nSig*sig; | 
|---|
|  | 111 | if (high > highbad) high = highbad; | 
|---|
|  | 112 | sx = sx2 = n = 0; | 
|---|
|  | 113 | for (int k=0; k<nImg; k++) { | 
|---|
|  | 114 | double p = (*imgTab[k])(i,j); | 
|---|
|  | 115 | if (p > low && p < high) { | 
|---|
|  | 116 | sx += p; | 
|---|
|  | 117 | sx2 += p*p; | 
|---|
|  | 118 | n++; | 
|---|
|  | 119 | } | 
|---|
|  | 120 | } | 
|---|
|  | 121 | if (n>0) { | 
|---|
|  | 122 | mean = sx / n; | 
|---|
|  | 123 | sig  = sqrt( sx2/n - (sx/n)*(sx/n) ); | 
|---|
|  | 124 | } // sinon on laisse tel quel, ca sera coupe... | 
|---|
|  | 125 | } | 
|---|
|  | 126 | double low = mean - nSig*sig; | 
|---|
|  | 127 | if (low < lowbad) low = lowbad; | 
|---|
|  | 128 | double high = mean + nSig*sig; | 
|---|
|  | 129 | if (high > highbad) high = highbad; | 
|---|
|  | 130 | for (int k=0; k<nImg; k++) | 
|---|
|  | 131 | if ((*imgTab[k])(i,j) > high || (*imgTab[k])(i,j) < low) | 
|---|
|  | 132 | (*imgTab[k])(i,j) = 0; | 
|---|
|  | 133 | } | 
|---|
|  | 134 | } | 
|---|
|  | 135 |  | 
|---|
|  | 136 | /* Nouvelle-Fonction */ | 
|---|
|  | 137 |  | 
|---|
|  | 138 | template <class T> | 
|---|
|  | 139 | //++ | 
|---|
|  | 140 | float ImgFondCielHisto(Image<T> const& img, float binWidth, int degree, float frac, | 
|---|
|  | 141 | float low, float high, int debug) | 
|---|
|  | 142 | // | 
|---|
|  | 143 | //      Calcul du fond de ciel par histogrammation. L'histogramme | 
|---|
|  | 144 | //      va de `low' a `high' avec une largeur de bin `binWidth'. | 
|---|
|  | 145 | //      La valeur la plus probalbe est fittee par un polynome de degre | 
|---|
|  | 146 | //      `degree' en utilisant les bins entourant le maximum | 
|---|
|  | 147 | //      a `frac' pourcent de sa hauteur. | 
|---|
|  | 148 | //-- | 
|---|
|  | 149 | { | 
|---|
|  | 150 | // CHECKIMG2(img,0,0); | 
|---|
|  | 151 |  | 
|---|
|  | 152 | if (degree < 2 || degree > 3) | 
|---|
|  | 153 | degree = 3; | 
|---|
|  | 154 |  | 
|---|
|  | 155 | if (frac <= 0 || frac >= 1) | 
|---|
|  | 156 | frac = 0.5; | 
|---|
|  | 157 |  | 
|---|
|  | 158 | if (low < MinRange((T)0)+2) | 
|---|
|  | 159 | low = MinRange((T)0)+2; | 
|---|
|  | 160 |  | 
|---|
|  | 161 | if (high > MaxRange((T)0)-2) | 
|---|
|  | 162 | high = MaxRange((T)0)-2; | 
|---|
|  | 163 |  | 
|---|
|  | 164 | if (low >= high) | 
|---|
|  | 165 | {low = MinRange((T)0); high = MaxRange((T)0);} | 
|---|
|  | 166 |  | 
|---|
|  | 167 | // verifier binwidth | 
|---|
|  | 168 |  | 
|---|
|  | 169 | //  if ( binWidth < 65536.0/img.XSize()*img.YSize() ) | 
|---|
|  | 170 | //    binWidth = 65536.0/img.XSize()*img.YSize(); | 
|---|
|  | 171 |  | 
|---|
|  | 172 | if (binWidth<0) | 
|---|
|  | 173 | binWidth = 1; | 
|---|
|  | 174 |  | 
|---|
|  | 175 | if (debug > 1) | 
|---|
|  | 176 | cout << __FUNCTION__ << " binWidth =" << binWidth << | 
|---|
|  | 177 | ", degree = " << degree << | 
|---|
|  | 178 | ", frac = "   << frac   << | 
|---|
|  | 179 | ", low = "    << low    << | 
|---|
|  | 180 | ", high = "   << high   << endl; | 
|---|
|  | 181 |  | 
|---|
|  | 182 | int nBins = int(((double)high-(double)low)/(double)binWidth + 1); | 
|---|
|  | 183 | Histo hist(low, high, nBins); | 
|---|
|  | 184 |  | 
|---|
|  | 185 | int nPix = img.XSize()*img.YSize(); | 
|---|
|  | 186 | for (int i=0; i<nPix; i++) | 
|---|
|  | 187 | hist.Add(img[i]); | 
|---|
|  | 188 |  | 
|---|
|  | 189 | hist(0) = 0; | 
|---|
|  | 190 |  | 
|---|
|  | 191 | float fd = hist.FitMax(degree, frac, debug); | 
|---|
|  | 192 |  | 
|---|
|  | 193 | (float&) img.fond = fd; | 
|---|
|  | 194 |  | 
|---|
|  | 195 | return fd; | 
|---|
|  | 196 | } | 
|---|
|  | 197 |  | 
|---|
|  | 198 |  | 
|---|
|  | 199 | /* Nouvelle-Fonction */ | 
|---|
|  | 200 |  | 
|---|
|  | 201 | template <class T> | 
|---|
|  | 202 | float ImgSigmaCiel(Image<T> const& img, int ndata, int binWidth, | 
|---|
|  | 203 | int taille, float frac, int degree, | 
|---|
|  | 204 | float low, float high, int debug) | 
|---|
|  | 205 | { | 
|---|
|  | 206 | (float&) img.sigmaFond = RzCmvSigmaCiel(img.ImagePtr(), img.XSize(), img.YSize(), | 
|---|
|  | 207 | ndata, binWidth, taille, frac, | 
|---|
|  | 208 | degree, low, high, debug); | 
|---|
|  | 209 | return img.sigmaFond; | 
|---|
|  | 210 | } | 
|---|
|  | 211 |  | 
|---|
|  | 212 |  | 
|---|
|  | 213 | /* Nouvelle-Fonction */ | 
|---|
|  | 214 |  | 
|---|
|  | 215 | template <class T> | 
|---|
|  | 216 | //++ | 
|---|
|  | 217 | float RzCmvSigmaCiel(T const* pix,int nx,int ny,int ndata,int bin, | 
|---|
|  | 218 | int taille,float frac,int deg, | 
|---|
|  | 219 | float low,float high,int deb) | 
|---|
|  | 220 | // | 
|---|
|  | 221 | //      Calcul du fond de ciel par histogrammation | 
|---|
|  | 222 | // | 
|---|
|  | 223 | //| INPUT: | 
|---|
|  | 224 | //|   - pix   : tableau pix[nx][ny] | 
|---|
|  | 225 | //|   - nx,ny : dimensions du tableau pix | 
|---|
|  | 226 | //|   - ndata : nombre de donnees a utiliser | 
|---|
|  | 227 | //|   - bin   : largeur du bin en ADU | 
|---|
|  | 228 | //|   - taille: 1/2 dimension de l'histogramme en ADU | 
|---|
|  | 229 | //|   - frac  : fraction du maximum au dessus de laquelle on fite | 
|---|
|  | 230 | //|   - deg   : degre fit polynome sous la gaussienne (0 ou 2) | 
|---|
|  | 231 | //|   - low   : valeur basse limite en ADU | 
|---|
|  | 232 | //|   - high  : valeur haute limite en ADU | 
|---|
|  | 233 | //|   - deb   : flag de debug | 
|---|
|  | 234 | //-- | 
|---|
|  | 235 | //++ | 
|---|
|  | 236 | //| OUTPUT: | 
|---|
|  | 237 | //|   - retourne : le sigma du fond de ciel si OK | 
|---|
|  | 238 | //|                -1 si probleme definition | 
|---|
|  | 239 | //|                -2 si probleme allocation | 
|---|
|  | 240 | //|                -3 si probleme remplissage histogramme | 
|---|
|  | 241 | //|                -4 si probleme recherche maximum | 
|---|
|  | 242 | //|                -5 si probleme calcul fraction du maximum | 
|---|
|  | 243 | //|                -6 si probleme nombre de bin | 
|---|
|  | 244 | //|                -7 si probleme nombre de fit | 
|---|
|  | 245 | //| | 
|---|
|  | 246 | //-- | 
|---|
|  | 247 | //                // EA seul changement par rapport a la version C : | 
|---|
|  | 248 | //                // pix est un T*, en template. -> C++ necessaire. | 
|---|
|  | 249 | { | 
|---|
|  | 250 | FLOATDOUBLE X,Y,EY; | 
|---|
|  | 251 | double (*Fun) (double ,double *,double *); | 
|---|
|  | 252 | int_4 npar; | 
|---|
|  | 253 | double par[6], epar[6],minpar[6],maxpar[6],stepar[6],stochi2; | 
|---|
|  | 254 | int_4 i,j,i1,j1,j2,n,ip,nbin,inc,ibin0,max,imax,lmax,b1,b2; | 
|---|
|  | 255 | float *histo,*xbin,*ehisto,fwhm,rc; | 
|---|
|  | 256 | double vp,pxv; | 
|---|
|  | 257 |  | 
|---|
|  | 258 | /* set reasonable parameters */ | 
|---|
|  | 259 | Fun = Gauss1DPolF ; | 
|---|
|  | 260 | if ( deg<=1 ) deg=0;  else deg=2; | 
|---|
|  | 261 | low = ( low < 0 ) ? 0 : low; | 
|---|
|  | 262 | high = ( high > 65530 ) ? 65530 : high; | 
|---|
|  | 263 | if ( low >= high ) { low=0; high = 65530;} | 
|---|
|  | 264 | bin = ( bin <= 0 ) ? 1 : bin; | 
|---|
|  | 265 | nbin = int(2.*taille /bin + 1); | 
|---|
|  | 266 | if ( nbin < 20 ) nbin = 20; | 
|---|
|  | 267 | if ( nbin >= 65530 ) nbin = 65530; | 
|---|
|  | 268 | ibin0 = int(-nbin*bin/2.); | 
|---|
|  | 269 | inc = int(nx*ny / ndata); inc = int(sqrt( (double) inc )); | 
|---|
|  | 270 | inc = (inc<1) ? 1 : inc; | 
|---|
|  | 271 | if(deb>1) | 
|---|
|  | 272 | { printf("Sigma_Ciel: pix[%d,%d] ndata=%d bin=%d taille=%d" | 
|---|
|  | 273 | ,nx,ny,ndata,bin,taille); | 
|---|
|  | 274 | printf(" frac=%f low=%g high=%g nbin=%d ibin0=%d inc=%d\n" | 
|---|
|  | 275 | ,frac,low,high,nbin,ibin0,inc); } | 
|---|
|  | 276 | if ( nx < 5 || ny < 5 ) return (-1.); | 
|---|
|  | 277 |  | 
|---|
|  | 278 | /* allocate place for histogramme */ | 
|---|
|  | 279 | if ( (histo  = (float *) calloc(nbin, sizeof(float)) ) == NULL ) | 
|---|
|  | 280 | return(-2.); | 
|---|
|  | 281 | if ( (xbin   = (float *) calloc(nbin, sizeof(float)) ) == NULL ) | 
|---|
|  | 282 | {free(histo); return(-2.);} | 
|---|
|  | 283 | if ( (ehisto = (float *) calloc(nbin, sizeof(float)) ) == NULL ) | 
|---|
|  | 284 | {free(histo); free(xbin); return(-2.);} | 
|---|
|  | 285 |  | 
|---|
|  | 286 | /* fill histogramme */ | 
|---|
|  | 287 | ndata=0; | 
|---|
|  | 288 | for (j=1;j<ny-1;j=j+inc) { for (i=1;i<nx-1;i=i+inc) { | 
|---|
|  | 289 | ip = (int)(*(pix+j*nx+i)); | 
|---|
|  | 290 | if ( ip < low || ip > high ) continue; | 
|---|
|  | 291 | vp = (double) ip; | 
|---|
|  | 292 | pxv = 0.; n=0; | 
|---|
|  | 293 | for(j1=j-1;j1<=j+1;j1++) { for(i1=i-1;i1<=i+1;i1++) { | 
|---|
|  | 294 | if ( i1 == i && j1 == j ) continue; | 
|---|
|  | 295 | ip = (int)(*(pix+j1*nx+i1)); | 
|---|
|  | 296 | if ( ip < low || ip > high ) continue; | 
|---|
|  | 297 | pxv += (double) ip; | 
|---|
|  | 298 | n++; | 
|---|
|  | 299 | } } | 
|---|
|  | 300 | if ( n != 8 ) continue; | 
|---|
|  | 301 | vp = vp - pxv/n; | 
|---|
|  | 302 | ip = int((vp-ibin0)/bin); | 
|---|
|  | 303 | if( ip<0 || ip >= nbin ) continue; | 
|---|
|  | 304 | histo[ip]++; | 
|---|
|  | 305 | ndata++; | 
|---|
|  | 306 | } } | 
|---|
|  | 307 | for(i=0;i<nbin;i++) | 
|---|
|  | 308 | { xbin[i] = (float) ibin0 + ( (float) i + 0.5 ) * (float) bin; | 
|---|
|  | 309 | ehisto[i] = (histo[i]<1.) ? -1. : sqrt((double) histo[i]);} | 
|---|
|  | 310 |  | 
|---|
|  | 311 | if(deb>1) printf("     nombre de data corrects = %d\n",ndata); | 
|---|
|  | 312 | if( ndata < 1 ) {rc= -3.; goto END;} | 
|---|
|  | 313 |  | 
|---|
|  | 314 | /* find maximum */ | 
|---|
|  | 315 | max = -1; imax = -1; | 
|---|
|  | 316 | for(i=nbin-1;i>=0;i--) if(histo[i]>max) { max=int(histo[i]); imax=i;} | 
|---|
|  | 317 | if(deb>1) printf("     maxi histo = %d (bin=%d)\n",max,imax); | 
|---|
|  | 318 | if ( imax == -1 || imax == 0 || imax == nbin-1 ) {rc= -4.; goto END;} | 
|---|
|  | 319 | if(deb>2) { | 
|---|
|  | 320 | for(i=imax-10;i<=imax+10;i++) if(i>=0 && i<nbin) printf(" %5f",xbin[i]); | 
|---|
|  | 321 | printf("\n"); | 
|---|
|  | 322 | for(i=imax-10;i<=imax+10;i++) if(i>=0 && i<nbin) printf(" %5d",(int) histo[i]); | 
|---|
|  | 323 | printf("\n"); } | 
|---|
|  | 324 |  | 
|---|
|  | 325 | /* find limites a half maximum */ | 
|---|
|  | 326 | lmax = (int) ( (float) max * 0.5 ); | 
|---|
|  | 327 | for(b1=imax;b1>0;b1--)    if(histo[b1]<=lmax) break; | 
|---|
|  | 328 | for(b2=imax;b2<nbin;b2++)  if(histo[b2]<=lmax) break; | 
|---|
|  | 329 | fwhm = (xbin[b2]-xbin[b1]); | 
|---|
|  | 330 | if(deb>1) printf("     fwhm = %f (%d,%d)\n",fwhm,b1,b2); | 
|---|
|  | 331 |  | 
|---|
|  | 332 | /* find limites a frac*max */ | 
|---|
|  | 333 | lmax = (int) ( (float) max * frac ) + 1; | 
|---|
|  | 334 | if( lmax < 1 ) | 
|---|
|  | 335 | {if(deb>1) printf("     lmax=%d\n",lmax); rc= -5.; goto END;} | 
|---|
|  | 336 | for(b1=imax;b1>0;b1--)    if(histo[b1]<=lmax) break; | 
|---|
|  | 337 | for(b2=imax;b2<nbin;b2++)  if(histo[b2]<=lmax) break; | 
|---|
|  | 338 |  | 
|---|
|  | 339 | n=0; | 
|---|
|  | 340 | for(i=b1;i<=b2;i++) if( histo[i] > 0 ) n++; | 
|---|
|  | 341 | if(deb>1) printf("     limites bin %d %d (v=%d) bin non nuls=%d\n" | 
|---|
|  | 342 | ,b1,b2,lmax,n); | 
|---|
|  | 343 | /* pas assez de bin, on en ajoute artificiellement */ | 
|---|
|  | 344 | /* 8 est une borne large pour fiter gauss + parab (3+3) */ | 
|---|
|  | 345 | if( n < 8 ) { | 
|---|
|  | 346 | j1 = b1; j2 = b2; | 
|---|
|  | 347 | for(i=1;i<nbin;i++) { | 
|---|
|  | 348 | if ( b1-i >= 0 ) j1=b1-i; | 
|---|
|  | 349 | if ( b2+i < nbin ) j2=b2+i; | 
|---|
|  | 350 | if ( histo[j1] > 0 ) n++; | 
|---|
|  | 351 | if ( histo[j2] > 0 ) n++; | 
|---|
|  | 352 | if( n >= 6 ) break; | 
|---|
|  | 353 | } | 
|---|
|  | 354 | b1 = j1; b2 = j2; | 
|---|
|  | 355 | if(deb>1) printf("     Re-update des limites bin %d %d (v=%d) bin non nuls=%d\n" | 
|---|
|  | 356 | ,b1,b2,lmax,n); | 
|---|
|  | 357 | } | 
|---|
|  | 358 | if( n < 4 ) {rc= -6.; goto END;} | 
|---|
|  | 359 | ndata = n; | 
|---|
|  | 360 |  | 
|---|
|  | 361 | /* fit dans les limites */ | 
|---|
|  | 362 | npar = 3+deg+1; Set_FitFunDPol(deg); | 
|---|
|  | 363 | if(ndata<8) {npar=4; Set_FitFunDPol(0);} | 
|---|
|  | 364 | if(ndata<5) {npar=3; Set_FitFunDPol(-1);} | 
|---|
|  | 365 | X.fx  = xbin; | 
|---|
|  | 366 | Y.fx  = histo; | 
|---|
|  | 367 | EY.fx = ehisto; | 
|---|
|  | 368 | stochi2 = 0.0001; | 
|---|
|  | 369 | n = nbin; | 
|---|
|  | 370 | par[0] = max; | 
|---|
|  | 371 | par[1] = histo[imax-1]*xbin[imax-1]+histo[imax]*xbin[imax]+histo[imax+1]*xbin[imax+1]; | 
|---|
|  | 372 | par[1] = par[1]/(histo[imax-1]+histo[imax]+histo[imax+1]); | 
|---|
|  | 373 | par[2] = (fwhm<1.) ? 1. : fwhm/2.36; | 
|---|
|  | 374 | par[3] = par[4] = par[5] = 0.; | 
|---|
|  | 375 | stepar[0] = par[0]/20.; stepar[1] = 1.; stepar[2] = par[2]/5.; | 
|---|
|  | 376 | stepar[3] = 1.; stepar[5] = 0.1; | 
|---|
|  | 377 | stepar[4] =  0.; | 
|---|
|  | 378 | for(i=0;i<6;i++) {minpar[i] = 1.; maxpar[i] = -1.;} | 
|---|
|  | 379 | minpar[2] = 0.1; maxpar[2] = 65530.; | 
|---|
|  | 380 | if(deb>1) printf("     choix de fit a %d parametres b1=%d,b2=%d,ndata=%d)\n" | 
|---|
|  | 381 | ,npar,b1,b2,ndata); | 
|---|
|  | 382 |  | 
|---|
|  | 383 | i = (deb>=3) ? deb-2 : 0 ; | 
|---|
|  | 384 | vp = FitFun(Fun,FITFUN_FLOAT,X,Y,EY,&n,b1,b2,par | 
|---|
|  | 385 | ,epar,stepar,minpar,maxpar,npar,stochi2,50,i); | 
|---|
|  | 386 |  | 
|---|
|  | 387 | if( vp<=0. && vp != -4. ) {rc= -7.; goto END;} | 
|---|
|  | 388 | rc = par[2]; | 
|---|
|  | 389 | if(deb>1) printf("     sigma=%f (h=%f centrage=%f) xi2=%f (%f pour %d)\n" | 
|---|
|  | 390 | ,rc,par[0],par[1],vp,vp/(n-npar),npar); | 
|---|
|  | 391 |  | 
|---|
|  | 392 | END: | 
|---|
|  | 393 | free(histo); free(xbin); free(ehisto); | 
|---|
|  | 394 | return(rc); | 
|---|
|  | 395 | } | 
|---|
|  | 396 |  | 
|---|
|  | 397 | /* ................................................................................. */ | 
|---|
|  | 398 | /*                  Filtrage ( Convolution ) d' images                               */ | 
|---|
|  | 399 | /* ................................................................................. */ | 
|---|
|  | 400 |  | 
|---|
|  | 401 | /* Nouvelle-Fonction */ | 
|---|
|  | 402 | template <class T> | 
|---|
|  | 403 | //++ | 
|---|
|  | 404 | Image<T> * FilterImage(Image<T> const * pim, Image<T> * pom, ImageR4 *matx) | 
|---|
|  | 405 | // | 
|---|
|  | 406 | //      Filtrage ( Convolution ) d'une image `pim' par une matrice carree | 
|---|
|  | 407 | //      representee par l'ImageR4 `matx'. | 
|---|
|  | 408 | //      Le resultat se retrouve dans `pom'. | 
|---|
|  | 409 | //      Si `pom' = NULL, elle est cree. | 
|---|
|  | 410 | //      `pim' et `pom' doivent avoir la meme taille. | 
|---|
|  | 411 | //-- | 
|---|
|  | 412 | { | 
|---|
|  | 413 |  | 
|---|
|  | 414 | float vpix, *mxe; | 
|---|
|  | 415 | bool fgcr; | 
|---|
|  | 416 | int i,j,k,l,m,n; | 
|---|
|  | 417 | int ndim, nd2; | 
|---|
|  | 418 |  | 
|---|
|  | 419 | fgcr = false; | 
|---|
|  | 420 |  | 
|---|
|  | 421 | if ((pim == NULL) || (matx == NULL)) | 
|---|
|  | 422 | { printf(" FilterImage_Erreur : pim ou matx = NULL ! \n"); | 
|---|
|  | 423 | return(NULL);  } | 
|---|
|  | 424 | if (matx->XSize() != matx->YSize()) | 
|---|
|  | 425 | { printf("FilterImage_Erreur: Filtrage par matrice carre uniquement (%d,%d)\n", | 
|---|
|  | 426 | matx->XSize(), matx->YSize());  return(NULL); } | 
|---|
|  | 427 | ndim = matx->XSize(); | 
|---|
|  | 428 | if ( (ndim < 3) || ((ndim%2) == 0) || (ndim > 25)) | 
|---|
|  | 429 | { printf("FilterImage_Erreur: Pb dimension matrice (%d) (Impair, entre 3 et 25)\n", ndim); | 
|---|
|  | 430 | return(NULL); } | 
|---|
|  | 431 | if (pom == NULL) | 
|---|
|  | 432 | {  pom = new Image<T>(pim->XSize(), pim->YSize(), false);   fgcr = true; } | 
|---|
|  | 433 |  | 
|---|
|  | 434 | if ( (pim->XSize() != pom->XSize()) || (pim->YSize() != pom->YSize()) ) | 
|---|
|  | 435 | { printf("FilterImage_Erreur : Taille pim et pom differentes ! \n") ; | 
|---|
|  | 436 | if (fgcr) delete pom;   return(NULL) ;} | 
|---|
|  | 437 |  | 
|---|
|  | 438 | nd2 = ndim/2; | 
|---|
|  | 439 |  | 
|---|
|  | 440 | /*  Traitement de la partie centrale */ | 
|---|
|  | 441 | for(j=nd2; j<(pom->YSize()-nd2); j++) | 
|---|
|  | 442 | { | 
|---|
|  | 443 | for(i=nd2; i<(pom->XSize()-nd2); i++) | 
|---|
|  | 444 | { | 
|---|
|  | 445 | vpix = 0.;   mxe = matx->ImagePtr(); | 
|---|
|  | 446 | for(l=-nd2; l<=nd2; l++) | 
|---|
|  | 447 | for(k=-nd2; k<=nd2; k++) | 
|---|
|  | 448 | { vpix += (float)((*pim)((i+k), (j+l))) * (*mxe); | 
|---|
|  | 449 | mxe++; } | 
|---|
|  | 450 | (*pom)(i, j) = (T)vpix; | 
|---|
|  | 451 | } | 
|---|
|  | 452 | } | 
|---|
|  | 453 |  | 
|---|
|  | 454 | /*  Traitement bord en Y  en haut */ | 
|---|
|  | 455 | for(j=0; j<nd2; j++) | 
|---|
|  | 456 | { | 
|---|
|  | 457 | for(i=nd2; i<(pom->XSize()-nd2); i++) | 
|---|
|  | 458 | { | 
|---|
|  | 459 | vpix = 0.;  mxe = matx->ImagePtr(); | 
|---|
|  | 460 | for(l=-nd2; l<=nd2; l++) | 
|---|
|  | 461 |  | 
|---|
|  | 462 | for(k=-nd2; k<=nd2; k++) | 
|---|
|  | 463 | { | 
|---|
|  | 464 | m = i+k;  n = j+l; | 
|---|
|  | 465 | if (n < 0)  n = 0; | 
|---|
|  | 466 | vpix += (float)((*pim)(m, n)) * (*mxe); | 
|---|
|  | 467 | mxe++; | 
|---|
|  | 468 | } | 
|---|
|  | 469 | (*pom)(i, j) = (T)vpix; | 
|---|
|  | 470 | } | 
|---|
|  | 471 | } | 
|---|
|  | 472 |  | 
|---|
|  | 473 | /* Traitement bord en Y en bas  */ | 
|---|
|  | 474 | for(j=(pom->YSize()-nd2); j<pom->YSize(); j++) | 
|---|
|  | 475 | { | 
|---|
|  | 476 | for(i=nd2; i<(pom->XSize()-nd2); i++) | 
|---|
|  | 477 | { | 
|---|
|  | 478 | vpix = 0.;  mxe = matx->ImagePtr(); | 
|---|
|  | 479 | for(l=-nd2; l<=nd2; l++) | 
|---|
|  | 480 | for(k=-nd2; k<=nd2; k++) | 
|---|
|  | 481 | { | 
|---|
|  | 482 | m = i+k;  n = j+l; | 
|---|
|  | 483 | if (n >= pim->YSize())  n = (pim->YSize()-1); | 
|---|
|  | 484 | vpix += (float)((*pim)(m, n)) * (*mxe); | 
|---|
|  | 485 | mxe++; | 
|---|
|  | 486 | } | 
|---|
|  | 487 | (*pom)(i, j) = (T)vpix; | 
|---|
|  | 488 | } | 
|---|
|  | 489 | } | 
|---|
|  | 490 |  | 
|---|
|  | 491 | /*  traitement bords en X et coins */ | 
|---|
|  | 492 | for(j=0; j<pom->YSize(); j++) | 
|---|
|  | 493 | { | 
|---|
|  | 494 | for(i=0; i<nd2; i++) | 
|---|
|  | 495 | { | 
|---|
|  | 496 | vpix = 0.;  mxe = matx->ImagePtr(); | 
|---|
|  | 497 | for(l=-nd2; l<=nd2; l++) | 
|---|
|  | 498 | for(k=-nd2; k<=nd2; k++) | 
|---|
|  | 499 | { | 
|---|
|  | 500 | m = i+k;  n = j+l; | 
|---|
|  | 501 | if (m < 0)  m = 0; | 
|---|
|  | 502 | if (n < 0)  n = 0; | 
|---|
|  | 503 | if (n >= pim->YSize())  n = (pim->YSize()-1); | 
|---|
|  | 504 | vpix += (float)((*pim)(m, n)) * (*mxe); | 
|---|
|  | 505 | mxe++; | 
|---|
|  | 506 | } | 
|---|
|  | 507 | (*pom)(i, j) = (T)vpix; | 
|---|
|  | 508 | } | 
|---|
|  | 509 | for(i=(pom->XSize()-nd2); i<pom->XSize(); i++) | 
|---|
|  | 510 | { | 
|---|
|  | 511 | vpix = 0.;  mxe = matx->ImagePtr(); | 
|---|
|  | 512 | for(l=-nd2; l<=nd2; l++) | 
|---|
|  | 513 | for(k=-nd2; k<=nd2; k++) | 
|---|
|  | 514 | { | 
|---|
|  | 515 | m = i+k;  n = j+l; | 
|---|
|  | 516 | if (m >= pim->XSize())  m = (pim->XSize()-1); | 
|---|
|  | 517 | if (n < 0)  n = 0; | 
|---|
|  | 518 | if (n >= pim->YSize())  n = (pim->YSize()-1); | 
|---|
|  | 519 | vpix += (float)((*pim)(m, n)) * (*mxe); | 
|---|
|  | 520 | mxe++; | 
|---|
|  | 521 | } | 
|---|
|  | 522 | (*pom)(i, j) = (T)vpix; | 
|---|
|  | 523 | } | 
|---|
|  | 524 | } | 
|---|
|  | 525 |  | 
|---|
|  | 526 |  | 
|---|
|  | 527 | return(pom); | 
|---|
|  | 528 |  | 
|---|
|  | 529 | } | 
|---|
|  | 530 |  | 
|---|
|  | 531 | /* Nouvelle-Fonction */ | 
|---|
|  | 532 | //++ | 
|---|
|  | 533 | ImageR4* NoiseFiltImage(ImageR4& img, ImageR4& filtre, DynCCD& dynccd) | 
|---|
|  | 534 | //      Calcule une image de bruit, a partir de "img" et de "dynccd" | 
|---|
|  | 535 | //      prenant en compte le filtre de convolution "filtre". | 
|---|
|  | 536 | //      L'image de bruit renvoyee contient les correlations de bruit entre | 
|---|
|  | 537 | //      pixels, du au filtrage (convolution) de l'image "img" par le | 
|---|
|  | 538 | //      filtre "filtre". | 
|---|
|  | 539 | //-- | 
|---|
|  | 540 | { | 
|---|
|  | 541 | ImageR4* ImgBrt = NoiseImage(&img,&dynccd); | 
|---|
|  | 542 |  | 
|---|
|  | 543 | int i,j; | 
|---|
|  | 544 | for (i=0; i<ImgBrt->XSize(); i++) | 
|---|
|  | 545 | for (j=0; j<ImgBrt->YSize(); j++)  (*ImgBrt)(i,j) =(*ImgBrt)(i,j) *(*ImgBrt)(i,j); | 
|---|
|  | 546 |  | 
|---|
|  | 547 | ImageR4 Filter2(filtre.XSize(), filtre.YSize()) ; | 
|---|
|  | 548 | for (i=0; i<filtre.XSize(); i++) | 
|---|
|  | 549 | for (j=0; j<filtre.YSize(); j++) (Filter2)(i,j) = filtre(i,j) * filtre(i,j); | 
|---|
|  | 550 |  | 
|---|
|  | 551 | ImageR4* out2; | 
|---|
|  | 552 | out2 = new ImageR4(ImgBrt->XSize(), ImgBrt->YSize()); | 
|---|
|  | 553 | FilterImage(ImgBrt, out2, &Filter2); | 
|---|
|  | 554 | for (i=0; i<out2->XSize(); i++) | 
|---|
|  | 555 | for (j=0; j<out2->YSize(); j++)  (*out2)(i,j) =sqrt((*out2)(i,j)); | 
|---|
|  | 556 | delete ImgBrt; | 
|---|
|  | 557 | return(out2); | 
|---|
|  | 558 | } | 
|---|
|  | 559 |  | 
|---|
|  | 560 |  | 
|---|
|  | 561 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 562 | /* Nouvelle-Fonction    CMV 14/10/97 */ | 
|---|
|  | 563 | template <class T> | 
|---|
|  | 564 | //++ | 
|---|
|  | 565 | Image<T> * FilterStat(Image<T> const * pim, Image<T> * pom | 
|---|
|  | 566 | ,int N,int M,float lowbad,float highbad) | 
|---|
|  | 567 | // | 
|---|
|  | 568 | //      Filtrage median d'une image `(*pim)'. | 
|---|
|  | 569 | //      Le resultat se retrouve dans `(*pom)'. | 
|---|
|  | 570 | //      Si pom = NULL l'image de sortie est cree automatiquement. | 
|---|
|  | 571 | //      pim et pom doivent avoir la meme taille. | 
|---|
|  | 572 | //      Le masque de calcul du filtre median est un rectangle `N'x`M' | 
|---|
|  | 573 | //      La dynamique de l'image est `lowbad,highbad'. | 
|---|
|  | 574 | //      Si lowbad>=highbad dynamique infinie. | 
|---|
|  | 575 | //-- | 
|---|
|  | 576 | { | 
|---|
|  | 577 | if(pim == NULL) | 
|---|
|  | 578 | {printf(" FilterStat_Erreur : pim = NULL ! \n"); | 
|---|
|  | 579 | return(NULL);} | 
|---|
|  | 580 | if( N<=0 || M<=0 ) | 
|---|
|  | 581 | {printf("FilterStat_Erreur: Filtrage par matrice (%d,%d)\n",N,M); | 
|---|
|  | 582 | return(NULL);} | 
|---|
|  | 583 | bool fgcr = false; | 
|---|
|  | 584 | if(pom == NULL) | 
|---|
|  | 585 | {pom = new Image<T>(pim->XSize(), pim->YSize(), false);   fgcr = true;} | 
|---|
|  | 586 | if( (pim->XSize() != pom->XSize()) || (pim->YSize() != pom->YSize()) ) | 
|---|
|  | 587 | { printf("FilterImage_Erreur : Taille pim et pom differentes ! \n"); | 
|---|
|  | 588 | if(fgcr) delete pom;   return(NULL);} | 
|---|
|  | 589 |  | 
|---|
|  | 590 | T low  = (T) lowbad; | 
|---|
|  | 591 | T high = (T) highbad; | 
|---|
|  | 592 | if(high>low) fgcr = true; else fgcr = false; | 
|---|
|  | 593 |  | 
|---|
|  | 594 | N /= 2; M /= 2; | 
|---|
|  | 595 | double * tab = new double[(2*N+1)*(2*M+1)]; | 
|---|
|  | 596 |  | 
|---|
|  | 597 | for(int j=0;j<pom->YSize();j++) { | 
|---|
|  | 598 | for(int i=0;i<pom->XSize();i++) { | 
|---|
|  | 599 | int n=0; | 
|---|
|  | 600 | for(int jj=j-M;jj<=j+M;jj++) { | 
|---|
|  | 601 | if( jj<0 || jj >= pom->YSize() ) continue; | 
|---|
|  | 602 | for(int ii=i-N;ii<=i+N;ii++) { | 
|---|
|  | 603 | if( ii<0 || ii >= pom->XSize() ) continue; | 
|---|
|  | 604 | if( fgcr && ( (*pim)(ii,jj)<=low || high<=(*pim)(ii,jj) ) ) continue; | 
|---|
|  | 605 | tab[n] = (double) (*pim)(ii,jj); | 
|---|
|  | 606 | n++; | 
|---|
|  | 607 | } | 
|---|
|  | 608 | } | 
|---|
|  | 609 | if(n==0) (*pom)(i,j) = 0; | 
|---|
|  | 610 | else if(n==1) (*pom)(i,j) = (T) tab[0]; | 
|---|
|  | 611 | else { | 
|---|
|  | 612 | HeapSort(n,tab); | 
|---|
|  | 613 | if(n%2==1) (*pom)(i,j) = (T) tab[(n-1)/2]; | 
|---|
|  | 614 | else (*pom)(i,j) = (T) (tab[n/2]+tab[n/2-1])/2.; | 
|---|
|  | 615 | } | 
|---|
|  | 616 | } | 
|---|
|  | 617 | } | 
|---|
|  | 618 |  | 
|---|
|  | 619 | delete [] tab; | 
|---|
|  | 620 | return(pom); | 
|---|
|  | 621 | } | 
|---|
|  | 622 |  | 
|---|
|  | 623 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 624 | /* Nouvelle-Fonction    CMV 7/8/98 */ | 
|---|
|  | 625 | template <class T> | 
|---|
|  | 626 | //++ | 
|---|
|  | 627 | Image<T> * CSplineImage(Image<T> const & f1,double pxsz,double x0,double y0,int natural) | 
|---|
|  | 628 | // | 
|---|
|  | 629 | //      Fonction retournant un pointeur sur une image qui contient | 
|---|
|  | 630 | //      le Spline 3 a 2 dimensions de l'images f1 (NULL est retourne si probleme). | 
|---|
|  | 631 | //      `pxsz' est la taille du pixel de l'image retournee en considerant | 
|---|
|  | 632 | //      que l'image `f1' en entree a une taille de pixel egale a 1. | 
|---|
|  | 633 | //      `x0,y0' est le decalage eventuel a appliquer de l'image origine a | 
|---|
|  | 634 | //      l'image retournee, ce decalage etant mesure en unites de pixels | 
|---|
|  | 635 | //      de l'image origine (1). | 
|---|
|  | 636 | //      `natural' a la signification decrite dans la classe `CSpline2'. | 
|---|
|  | 637 | //      Attention, la taille de l'image retournee sera `nx/pxsz' et `ny/pxsz'. | 
|---|
|  | 638 | //      Pour eviter les problemes d'extrapolation sur les bords de l'image | 
|---|
|  | 639 | //      d'origine, il leurs est assigne la valeur du centre du pixel auquel ils | 
|---|
|  | 640 | //      appartiennent. | 
|---|
|  | 641 | //-- | 
|---|
|  | 642 | { | 
|---|
|  | 643 | //// open for result | 
|---|
|  | 644 | int nx = f1.XSize(); | 
|---|
|  | 645 | int ny = f1.YSize(); | 
|---|
|  | 646 | float dum; | 
|---|
|  | 647 | dum = nx/pxsz; | 
|---|
|  | 648 | int dx = (int) (dum+0.5); | 
|---|
|  | 649 | dum = ny/pxsz; | 
|---|
|  | 650 | int dy = (int) (dum+0.5); | 
|---|
|  | 651 | cout<<"Taille image en entree: "<<nx<<" "<<ny<<endl | 
|---|
|  | 652 | <<"Taille image en sortie: "<<dx<<" "<<dy<<" (pix size="<<pxsz<<")"<<endl | 
|---|
|  | 653 | <<"Decalage en x="<<x0<<" en y="<<y0<<endl; | 
|---|
|  | 654 | if(nx<=1 || ny<=1) return NULL; | 
|---|
|  | 655 | if(dx<=0 || dy<=0) return NULL; | 
|---|
|  | 656 |  | 
|---|
|  | 657 | Image<T> *f2 = NULL; | 
|---|
|  | 658 | TRY { | 
|---|
|  | 659 | f2 = new Image<T>(dx,dy); | 
|---|
|  | 660 | } CATCHALL { | 
|---|
|  | 661 | cout<<"Impossible d'ouvrir le fichier resultat"<<endl; | 
|---|
|  | 662 | return NULL; | 
|---|
|  | 663 | } ENDTRY | 
|---|
|  | 664 |  | 
|---|
|  | 665 | //// Spline 3 2D ... nx+2 pour tenir compte des effets de bords ! | 
|---|
|  | 666 | double *x = new double[nx+2]; | 
|---|
|  | 667 | double *y = new double[ny+2]; | 
|---|
|  | 668 | double *z = new double[(nx+2)*(ny+2)]; | 
|---|
|  | 669 | { | 
|---|
|  | 670 | int k; | 
|---|
|  | 671 | // Centre pixel (0,0) en (0.5,0.5) et on ajoute les coordonnees | 
|---|
|  | 672 | // des bords pour extrapolation sur tout l'intervalle. | 
|---|
|  | 673 | // Les coordonnees vont de [0,nx] et [0,ny] (valeurs extremes comprises). | 
|---|
|  | 674 | x[0] = 0.; x[nx+1] = nx; | 
|---|
|  | 675 | y[0] = 0.; y[ny+1] = ny; | 
|---|
|  | 676 | for(k=1;k<=nx;k++) x[k] = k-0.5; | 
|---|
|  | 677 | for(k=1;k<=ny;k++) y[k] = k-0.5; | 
|---|
|  | 678 | // Remplissage des valeurs avec effet de bord [0] et [nx+1],[ny+1]. | 
|---|
|  | 679 | k = 0; | 
|---|
|  | 680 | int ii,jj; | 
|---|
|  | 681 | for(int j=0;j<ny+2;j++) { | 
|---|
|  | 682 | jj = j-1; | 
|---|
|  | 683 | if(jj<0) jj=0; else if(jj>=ny) jj=ny-1; | 
|---|
|  | 684 | for(int i=0;i<nx+2;i++) { | 
|---|
|  | 685 | ii = i-1; | 
|---|
|  | 686 | if(ii<0) ii=0; else if(ii>=nx) ii=nx-1; | 
|---|
|  | 687 | z[k] = (double) f1(ii,jj); | 
|---|
|  | 688 | k++; | 
|---|
|  | 689 | } | 
|---|
|  | 690 | } | 
|---|
|  | 691 | } | 
|---|
|  | 692 |  | 
|---|
|  | 693 | CSpline2 spl(nx+2,x,ny+2,y,z,natural,true); | 
|---|
|  | 694 | spl.ComputeCSpline(); | 
|---|
|  | 695 |  | 
|---|
|  | 696 | *f2 = 0.; | 
|---|
|  | 697 | { | 
|---|
|  | 698 | // Attention au decalage possible ! | 
|---|
|  | 699 | // Les valeurs vont de [0.5*pxsz,(dx-0.5)*pxsz] idem pour y... | 
|---|
|  | 700 | // a corriger eventuellement du decalage x0,y0. | 
|---|
|  | 701 | double xmin = -pxsz/10000., xmax = nx+pxsz/10000.; | 
|---|
|  | 702 | double ymin = -pxsz/10000., ymax = ny+pxsz/10000.; | 
|---|
|  | 703 | for(int j=0;j<dy;j++) { | 
|---|
|  | 704 | double yy = (j+0.5)*pxsz - y0; | 
|---|
|  | 705 | if( yy<=ymin || yy>=ymax ) continue; | 
|---|
|  | 706 | for(int i=0;i<dx;i++) { | 
|---|
|  | 707 | double xx = (i+0.5)*pxsz - x0; | 
|---|
|  | 708 | if( xx<=xmin || xx>=xmax ) continue; | 
|---|
|  | 709 | (*f2)(i,j) = (T) spl.CSplineInt(xx,yy); | 
|---|
|  | 710 | }}} | 
|---|
|  | 711 |  | 
|---|
|  | 712 | //// nettoyage | 
|---|
|  | 713 | delete [] x; | 
|---|
|  | 714 | delete [] y; | 
|---|
|  | 715 | delete [] z; | 
|---|
|  | 716 |  | 
|---|
|  | 717 | return f2; | 
|---|
|  | 718 | } | 
|---|
|  | 719 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 720 |  | 
|---|
|  | 721 |  | 
|---|
|  | 722 |  | 
|---|
|  | 723 | /* Nouvelle-Fonction */ | 
|---|
|  | 724 | int FondSigmaCiel(RzImage *pim, float low, float high,int pvsz | 
|---|
|  | 725 | ,float nbsig1,float nbsig2,float nbsig3 | 
|---|
|  | 726 | ,float binsg,float frac,int modu,int deb) | 
|---|
|  | 727 | { | 
|---|
|  | 728 | int rc; | 
|---|
|  | 729 |  | 
|---|
|  | 730 | switch (pim->PixelType()) | 
|---|
|  | 731 | { | 
|---|
|  | 732 | case kuint_2: | 
|---|
|  | 733 | { | 
|---|
|  | 734 | ImageU2 imt((RzImage&)(*pim)); | 
|---|
|  | 735 | rc = imt.FondSigmaCiel(low, high, pvsz, nbsig1, nbsig2, nbsig3, binsg, frac, modu, deb); | 
|---|
|  | 736 | pim->fond = imt.fond; | 
|---|
|  | 737 | pim->sigmaFond = imt.sigmaFond; | 
|---|
|  | 738 | break; | 
|---|
|  | 739 | } | 
|---|
|  | 740 | case kint_2: | 
|---|
|  | 741 | { | 
|---|
|  | 742 | ImageI2 imt((RzImage&)(*pim)); | 
|---|
|  | 743 | rc = imt.FondSigmaCiel(low, high, pvsz, nbsig1, nbsig2, nbsig3, binsg, frac, modu, deb); | 
|---|
|  | 744 | pim->fond = imt.fond; | 
|---|
|  | 745 | pim->sigmaFond = imt.sigmaFond; | 
|---|
|  | 746 | break; | 
|---|
|  | 747 | } | 
|---|
|  | 748 | case kint_4: | 
|---|
|  | 749 | { | 
|---|
|  | 750 | ImageI4 imt((RzImage&)(*pim)); | 
|---|
|  | 751 | rc = imt.FondSigmaCiel(low, high, pvsz, nbsig1, nbsig2, nbsig3, binsg, frac, modu, deb); | 
|---|
|  | 752 | pim->fond = imt.fond; | 
|---|
|  | 753 | pim->sigmaFond = imt.sigmaFond; | 
|---|
|  | 754 | break; | 
|---|
|  | 755 | } | 
|---|
|  | 756 | case kr_4: | 
|---|
|  | 757 | { | 
|---|
|  | 758 | ImageR4 imt((RzImage&)(*pim)); | 
|---|
|  | 759 | rc = imt.FondSigmaCiel(low, high, pvsz, nbsig1, nbsig2, nbsig3, binsg, frac, modu, deb); | 
|---|
|  | 760 | pim->fond = imt.fond; | 
|---|
|  | 761 | pim->sigmaFond = imt.sigmaFond; | 
|---|
|  | 762 | break; | 
|---|
|  | 763 | } | 
|---|
|  | 764 | default: | 
|---|
|  | 765 | { | 
|---|
|  | 766 | cout<<"FindSigmaCiel: type d'image non-prevu "<<(int) pim->PixelType()<<endl; | 
|---|
|  | 767 | exit(-1); | 
|---|
|  | 768 | } | 
|---|
|  | 769 | } | 
|---|
|  | 770 |  | 
|---|
|  | 771 | return(rc); | 
|---|
|  | 772 | } | 
|---|
|  | 773 |  | 
|---|
|  | 774 | /* Nouvelle-Fonction */ | 
|---|
|  | 775 | int CalFondImage(RzImage *pim, double low, double high, int action) | 
|---|
|  | 776 |  | 
|---|
|  | 777 | /*  ATTENTION: Adaptation a partir d'une version C              */ | 
|---|
|  | 778 | /*             CalculFond() de imgstat.c                        */ | 
|---|
|  | 779 |  | 
|---|
|  | 780 | /*  Cette fonction calcule le fond et le sigma du ciel a la CMV */ | 
|---|
|  | 781 | /*  CMV / E.Aubourg / Reza                                      */ | 
|---|
|  | 782 | /*  action Bit 0  CheckDyn()                                    */ | 
|---|
|  | 783 | /*         Bit 1  ImgFondCielHisto()                            */ | 
|---|
|  | 784 | /*         Bit 2  ImgSigmaCiel                                  */ | 
|---|
|  | 785 | /*         Bit 3  FondSig2Ciel()                                */ | 
|---|
|  | 786 | /*         Bit 1+2+3 (=14)   Fond_Ciel() + Sigma_Ciel() Si OK   */ | 
|---|
|  | 787 | /*                            FondSig2Ciel()  sinon             */ | 
|---|
|  | 788 | /*  action = 0  ou  >= 15  CheckDyn + Bit 1+2+3 (=14)           */ | 
|---|
|  | 789 | /*  Rc=0 OK  Erreur Sinon                                       */ | 
|---|
|  | 790 | /*  Sortie : Mise a jour de la struct RzImage                   */ | 
|---|
|  | 791 |  | 
|---|
|  | 792 | { | 
|---|
|  | 793 | int n,ok,rc,fga; | 
|---|
|  | 794 | float fnd, sg; | 
|---|
|  | 795 |  | 
|---|
|  | 796 | int nbsig, vitesse, nbin; | 
|---|
|  | 797 | float minaff, maxaff, minpix, maxpix; | 
|---|
|  | 798 | float fracmax,fracnul,fracsat; | 
|---|
|  | 799 | char *meth; | 
|---|
|  | 800 |  | 
|---|
|  | 801 | #define DBG  0 | 
|---|
|  | 802 |  | 
|---|
|  | 803 | if (action >= 15)   action = 15; | 
|---|
|  | 804 | if (action <= 0)  action = 15; | 
|---|
|  | 805 | fga = action; | 
|---|
|  | 806 | fnd = sg = -1.; | 
|---|
|  | 807 | ok = 1; | 
|---|
|  | 808 |  | 
|---|
|  | 809 | if (action & 1) | 
|---|
|  | 810 | { pim->CheckDyn(low, high); | 
|---|
|  | 811 | action--;  } | 
|---|
|  | 812 |  | 
|---|
|  | 813 | n = pim->XSize()*pim->YSize(); | 
|---|
|  | 814 |  | 
|---|
|  | 815 | if (action & 6) | 
|---|
|  | 816 | { | 
|---|
|  | 817 | int npu = (n/4 < 5000) ? 5000 : n/4; | 
|---|
|  | 818 | meth = (char *)"Fond/Sig"; | 
|---|
|  | 819 | switch (pim->PixelType()) | 
|---|
|  | 820 | { | 
|---|
|  | 821 | case kuint_2: | 
|---|
|  | 822 | { | 
|---|
|  | 823 | ImageU2 imt((RzImage&)(*pim)); | 
|---|
|  | 824 | if (action & 2) | 
|---|
|  | 825 | fnd = ImgFondCielHisto(imt, 1.0f, 2, 0.5f, (float)low, (float)high, DBG); | 
|---|
|  | 826 | if (action & 4) | 
|---|
|  | 827 | {  sg = ImgSigmaCiel(imt, npu, 1, 200, 0.1f, 2, | 
|---|
|  | 828 | (float)low, (float)high, DBG); | 
|---|
|  | 829 | if (sg < -1.e-3 )  ok = 0; } | 
|---|
|  | 830 | break; | 
|---|
|  | 831 | } | 
|---|
|  | 832 | case kint_2: | 
|---|
|  | 833 | { | 
|---|
|  | 834 | ImageI2 imt((RzImage&)(*pim)); | 
|---|
|  | 835 | if (action & 2) | 
|---|
|  | 836 | fnd = ImgFondCielHisto(imt, 1.0f, 2, 0.5f, (float)low, (float)high, DBG); | 
|---|
|  | 837 | if (action & 4) | 
|---|
|  | 838 | {  sg = ImgSigmaCiel(imt, npu, 1, 200, 0.1f, 2, | 
|---|
|  | 839 | (float)low, (float)high, DBG); | 
|---|
|  | 840 | if (sg < -1.e-3 )  ok = 0; } | 
|---|
|  | 841 | break; | 
|---|
|  | 842 | } | 
|---|
|  | 843 | case kint_4: | 
|---|
|  | 844 | { | 
|---|
|  | 845 | ImageI4 imt((RzImage&)(*pim)); | 
|---|
|  | 846 | if (action & 2) | 
|---|
|  | 847 | fnd = ImgFondCielHisto(imt, 1.0f, 2, 0.5f, (float)low, (float)high, DBG); | 
|---|
|  | 848 | if (action & 4) | 
|---|
|  | 849 | {  sg = ImgSigmaCiel(imt, npu, 1, 200, 0.1f, 2, | 
|---|
|  | 850 | (float)low, (float)high, DBG); | 
|---|
|  | 851 | if (sg < -1.e-3 )  ok = 0; } | 
|---|
|  | 852 | break; | 
|---|
|  | 853 | } | 
|---|
|  | 854 | case kr_4: | 
|---|
|  | 855 | { | 
|---|
|  | 856 | ImageR4 imt((RzImage&)(*pim)); | 
|---|
|  | 857 | if (action & 2) | 
|---|
|  | 858 | fnd = ImgFondCielHisto(imt, 1.0f, 2, 0.5f, (float)low, (float)high, DBG); | 
|---|
|  | 859 | if (action & 4) | 
|---|
|  | 860 | {  sg = ImgSigmaCiel(imt, npu, 1, 200, 0.1f, 2, | 
|---|
|  | 861 | (float)low,(float) high, DBG); | 
|---|
|  | 862 | if (sg < -1.e-3 )  ok = 0; } | 
|---|
|  | 863 | break; | 
|---|
|  | 864 | } | 
|---|
|  | 865 |  | 
|---|
|  | 866 | case kuint_4: | 
|---|
|  | 867 | case kr_8: | 
|---|
|  | 868 | cerr << "CalFondImage(RzImage *, ...) Unsupported image type (kuint_4/kr_8) " | 
|---|
|  | 869 | << (int) pim->PixelType() << "\n" ; | 
|---|
|  | 870 | break; | 
|---|
|  | 871 | default: | 
|---|
|  | 872 | cerr << "CalFondImage(RzImage *, ...) Unknown image type !! " << (int) pim->PixelType() << "\n" ; | 
|---|
|  | 873 | break; | 
|---|
|  | 874 | } | 
|---|
|  | 875 |  | 
|---|
|  | 876 | } | 
|---|
|  | 877 |  | 
|---|
|  | 878 | if ( (action == 8) || (!ok && (action & 8)) ) | 
|---|
|  | 879 | { | 
|---|
|  | 880 | fracmax = 0.85;   nbsig = 1;     vitesse = 3; | 
|---|
|  | 881 | minpix = low;   maxpix = high;  nbin = 10000; | 
|---|
|  | 882 | rc = RzFondSig2Ciel(pim, vitesse, fracmax, nbsig, nbin, | 
|---|
|  | 883 | &minpix, &maxpix, &fnd, &sg, | 
|---|
|  | 884 | &minaff, &maxaff, &fracnul, &fracsat); | 
|---|
|  | 885 | if (rc != 0)   ok = 0; | 
|---|
|  | 886 | meth = (char *)"FondSig2"; | 
|---|
|  | 887 | } | 
|---|
|  | 888 |  | 
|---|
|  | 889 | if (ok > 0)     /*  Mise a jour */ | 
|---|
|  | 890 | { | 
|---|
|  | 891 | if (fnd > -1.)  pim->fond = fnd; | 
|---|
|  | 892 | if (sg > -1. )  pim->sigmaFond = sg; | 
|---|
|  | 893 | printf("CalculFond_Info: Meth=%s (%d) Fond/Sig= %5g %4g Nul/Sat=%d %d\n", | 
|---|
|  | 894 | meth, fga, fnd, sg, pim->nbNul, pim->nbSat); | 
|---|
|  | 895 | return(0); | 
|---|
|  | 896 | } | 
|---|
|  | 897 | else | 
|---|
|  | 898 | { | 
|---|
|  | 899 | printf("CalculFond_Erreur: Action=%d  NbNul/Sat=%d %d\n",fga, | 
|---|
|  | 900 | pim->nbNul, pim->nbSat); | 
|---|
|  | 901 | return(1); | 
|---|
|  | 902 | } | 
|---|
|  | 903 |  | 
|---|
|  | 904 | } | 
|---|
|  | 905 |  | 
|---|
|  | 906 |  | 
|---|
|  | 907 |  | 
|---|
|  | 908 | /* Nouvelle-Fonction */ | 
|---|
|  | 909 |  | 
|---|
|  | 910 | int RzFondSig2Ciel(RzImage * pim, int vitesse, float FracMax, int NbSigmas, int NBin, | 
|---|
|  | 911 | float *MinPix, float *MaxPix, float *Fond, float *SigFond, | 
|---|
|  | 912 | float *MinAff, float *MaxAff, float *FracNul, float *FracSature) | 
|---|
|  | 913 |  | 
|---|
|  | 914 | /*   ATTENTION : Cette fonction a ete mis en C++, pour traiter des   */ | 
|---|
|  | 915 | /*               RzImage a partir de la fonction FondSig2Ciel()      */ | 
|---|
|  | 916 | /*               de imgstat.c                                        */ | 
|---|
|  | 917 | /*                                        R. Ansari  04/95           */ | 
|---|
|  | 918 |  | 
|---|
|  | 919 | /*    Cette fonction calcule les valeurs suivantes pour une image    */ | 
|---|
|  | 920 | /*    Arguments d'entree :                                           */ | 
|---|
|  | 921 | /*     - pim : Pointeur RzImage                                      */ | 
|---|
|  | 922 | /*     - vitesse : 1 pixel / vitesse pris en compte                  */ | 
|---|
|  | 923 | /*     - FracMax : Parametre de calcul (voir ci-dessous)             */ | 
|---|
|  | 924 | /*     - NbSigmas: MinAff =  Fond + NbSigmas*SigFond                 */ | 
|---|
|  | 925 | /*       **Note : Si NbSigmas < 0 , on prend :                       */ | 
|---|
|  | 926 | /*                 MinAff =  Fond + NbSigmas*SigFond                 */ | 
|---|
|  | 927 | /*              et MaxAff =  Fond - NbSigmas*SigFond                 */ | 
|---|
|  | 928 | /*         Dans ce cas la gamme d'affichage fait ressortir le fond.  */ | 
|---|
|  | 929 | /*     - NBin : Nb de bins de l'histo                                */ | 
|---|
|  | 930 | /*     - La valeur mini des pixels et la valeur de saturation        */ | 
|---|
|  | 931 | /*       (ValNul et ValSat) doivent etre fournies ds Min/MaxPix      */ | 
|---|
|  | 932 | /*              sorties :                                            */ | 
|---|
|  | 933 | /*  - MinPix,MaxPix = Valeurs Min et Max des pixels  (<ValSat)       */ | 
|---|
|  | 934 | /*  - Fond =  Pic de l'histo des pixels                              */ | 
|---|
|  | 935 | /*  - SigFond = sigma du fond calcule par la relation                */ | 
|---|
|  | 936 | /*     NbPixel(Fond+SigFond) =  Pic histo * 0.606 (=Exp(-0.5))       */ | 
|---|
|  | 937 | /*  - MinAff = Bruit + NbSigmas * Sigma bruit                        */ | 
|---|
|  | 938 | /*  - MaxAff est calcule tel que la relation suivante soit verifie   */ | 
|---|
|  | 939 | /*     NbPixel(MinAff<Pixel<MaxAff) =  Nb de pixel entre Min-MaxAff  */ | 
|---|
|  | 940 | /*      = FracMax * NbPixel(MinAff<Pixel<ValSat                      */ | 
|---|
|  | 941 | /*  - FracNul = Nb pixel < ValNul / Nb total pixel                   */ | 
|---|
|  | 942 | /*  - FracSat = Nb pixel satures (>ValSat) / Nb total pixel          */ | 
|---|
|  | 943 |  | 
|---|
|  | 944 | /*     return code : 0 OK , 1 Erreur calloc                          */ | 
|---|
|  | 945 | /*                   2 : Pic histos non trouve                       */ | 
|---|
|  | 946 | /*                   3,4 : Probleme de Min/MaxAff avec ValSat        */ | 
|---|
|  | 947 | /*                   5 : Gamme dynamique minimale non respectee      */ | 
|---|
|  | 948 | /*                         (MaxAff - MinAff > MINDYNAMIC             */ | 
|---|
|  | 949 |  | 
|---|
|  | 950 | { | 
|---|
|  | 951 |  | 
|---|
|  | 952 | #define MINNBIN 100 | 
|---|
|  | 953 | #define MAXNBIN 32765 | 
|---|
|  | 954 |  | 
|---|
|  | 955 | int nbin; | 
|---|
|  | 956 |  | 
|---|
|  | 957 | float minhis = 0., maxhis = 65530. , binwidth; | 
|---|
|  | 958 | int xdim, ydim; | 
|---|
|  | 959 | int_4 under,over,tothis; | 
|---|
|  | 960 | int_4 *phis; | 
|---|
|  | 961 | float ValNul,ValSat; | 
|---|
|  | 962 | int_4 *q; | 
|---|
|  | 963 |  | 
|---|
|  | 964 | float val; | 
|---|
|  | 965 | int i; | 
|---|
|  | 966 | float min, max; | 
|---|
|  | 967 | int_4 nbpix,nbpix2, imin, imax; | 
|---|
|  | 968 | int bin,binmax; | 
|---|
|  | 969 | int sigl,sigr; | 
|---|
|  | 970 | float fracfond,fracsignal; | 
|---|
|  | 971 |  | 
|---|
|  | 972 | float MINDYNAMIC = 5;          /* Gamme dynamique minimale (MaxAff-MinAff)*/ | 
|---|
|  | 973 |  | 
|---|
|  | 974 | #define FRACPIC         0.6065     /*  Valeur de exp(-0.5)   */ | 
|---|
|  | 975 |  | 
|---|
|  | 976 | xdim = pim->XSize(); | 
|---|
|  | 977 | ydim = pim->YSize(); | 
|---|
|  | 978 |  | 
|---|
|  | 979 | ValNul = *MinPix; | 
|---|
|  | 980 | ValSat = *MaxPix; | 
|---|
|  | 981 | if ((ValSat < ValNul+0.01))  ValSat = ValNul+0.01; | 
|---|
|  | 982 |  | 
|---|
|  | 983 | nbin = NBin; | 
|---|
|  | 984 | if (nbin < MINNBIN)  nbin = MINNBIN; | 
|---|
|  | 985 | if (nbin > MAXNBIN)  nbin = MAXNBIN; | 
|---|
|  | 986 |  | 
|---|
|  | 987 | if (vitesse < 1)  vitesse = 1; | 
|---|
|  | 988 | if (vitesse > xdim*ydim/100)  vitesse = xdim*ydim/100; | 
|---|
|  | 989 |  | 
|---|
|  | 990 |  | 
|---|
|  | 991 | /*  Je modifie min et max histo et nbin en fonction de valnul et valsat */ | 
|---|
|  | 992 | /*  je garde binwidth constant     */ | 
|---|
|  | 993 |  | 
|---|
|  | 994 | binwidth = (ValSat-ValNul) / nbin; | 
|---|
|  | 995 | minhis = ValNul; | 
|---|
|  | 996 | maxhis = minhis + nbin*binwidth; | 
|---|
|  | 997 |  | 
|---|
|  | 998 |  | 
|---|
|  | 999 | (*MinAff) = minhis; | 
|---|
|  | 1000 | (*MaxAff) = maxhis; | 
|---|
|  | 1001 | *MinPix = 0.; | 
|---|
|  | 1002 | *MaxPix = -1.; | 
|---|
|  | 1003 | *Fond = -1.; | 
|---|
|  | 1004 | *SigFond = -1.; | 
|---|
|  | 1005 | *FracNul = 0.0; | 
|---|
|  | 1006 | *FracSature = 0.0; | 
|---|
|  | 1007 |  | 
|---|
|  | 1008 | /* allocation dynamique de memoire */ | 
|---|
|  | 1009 | phis = new int_4[nbin]; | 
|---|
|  | 1010 |  | 
|---|
|  | 1011 | /* mise a zero de l'histogramme */ | 
|---|
|  | 1012 | for ( q=phis ; q<phis+nbin ; *q++ = 0 )  ; | 
|---|
|  | 1013 | under = over = tothis = 0; | 
|---|
|  | 1014 |  | 
|---|
|  | 1015 |  | 
|---|
|  | 1016 |  | 
|---|
|  | 1017 | nbpix2 = nbpix = 0; | 
|---|
|  | 1018 | min = ValSat+1.; | 
|---|
|  | 1019 | max = ValNul-1; | 
|---|
|  | 1020 |  | 
|---|
|  | 1021 | /*   Boucle sur les pixels   */ | 
|---|
|  | 1022 | /*     Pour aller plus vite, on prend un pixel sur 7   */ | 
|---|
|  | 1023 | for ( i = 0 ; i < xdim*ydim ; i += vitesse ) | 
|---|
|  | 1024 | { | 
|---|
|  | 1025 | val = pim->IValue(i); | 
|---|
|  | 1026 |  | 
|---|
|  | 1027 | /*   Calcul PixMax/Min et NbPix sature  */ | 
|---|
|  | 1028 | if (val > ValSat)  nbpix++; | 
|---|
|  | 1029 | else { | 
|---|
|  | 1030 | if(val < ValNul) nbpix2++; | 
|---|
|  | 1031 | else { | 
|---|
|  | 1032 | if (val > max) max = val; | 
|---|
|  | 1033 | else if (val < min) min = val; | 
|---|
|  | 1034 | } | 
|---|
|  | 1035 | } | 
|---|
|  | 1036 |  | 
|---|
|  | 1037 | /* remplissage de l'histogramme */ | 
|---|
|  | 1038 | bin = (int)((val - minhis) / binwidth) ; | 
|---|
|  | 1039 | if ( bin < 0 )  under++; | 
|---|
|  | 1040 | else | 
|---|
|  | 1041 | { | 
|---|
|  | 1042 | if (bin < nbin) { (*(phis+bin))++; tothis++; } | 
|---|
|  | 1043 | else   over++; | 
|---|
|  | 1044 | } | 
|---|
|  | 1045 | }     /*  Fin de for : Boucle sur les pixels   */ | 
|---|
|  | 1046 |  | 
|---|
|  | 1047 | (*MinPix) = min; | 
|---|
|  | 1048 | (*MaxPix) = max; | 
|---|
|  | 1049 | /*       Ne pas oublier le facteur vitesse pour le calcul FracSatue  */ | 
|---|
|  | 1050 | (*FracNul) = (float)(nbpix2 * vitesse) / (float)(xdim*ydim); | 
|---|
|  | 1051 | (*FracSature) = (float)(nbpix * vitesse) / (float)(xdim*ydim); | 
|---|
|  | 1052 |  | 
|---|
|  | 1053 | /*  On verifie qu'il y a eu une entree ds l'histo */ | 
|---|
|  | 1054 | if (tothis <= 0) | 
|---|
|  | 1055 | { delete[] phis;  return(2); } | 
|---|
|  | 1056 |  | 
|---|
|  | 1057 | /* recherche du maximum */ | 
|---|
|  | 1058 | binmax = -1 ; imax = -1 ; q = phis; | 
|---|
|  | 1059 | for ( i = 0 ; i < nbin ;  i++, q++ ) | 
|---|
|  | 1060 | if ( *(q) > imax ) {imax = *(q) ; binmax = i ;} | 
|---|
|  | 1061 |  | 
|---|
|  | 1062 | if ( (binmax < 0) || (binmax >= nbin) ) | 
|---|
|  | 1063 | { delete[] phis;  return(2); } | 
|---|
|  | 1064 |  | 
|---|
|  | 1065 | *Fond = minhis + binmax * binwidth; | 
|---|
|  | 1066 |  | 
|---|
|  | 1067 | /*   On va chercher le sigma du fond  */ | 
|---|
|  | 1068 | imin = (int_4) ( (float)(imax) * FRACPIC ) ; | 
|---|
|  | 1069 |  | 
|---|
|  | 1070 | /* | 
|---|
|  | 1071 | printf("StatsDebug : min/maxhis  %g  %g  (nbin= %d) w=%g\n", | 
|---|
|  | 1072 | minhis,maxhis,nbin,binwidth); | 
|---|
|  | 1073 | printf(".binmax= %d  imax(=pic) = %d  , imin= %d  \n ",binmax,imax,imin); | 
|---|
|  | 1074 | */ | 
|---|
|  | 1075 |  | 
|---|
|  | 1076 | nbpix = 0; | 
|---|
|  | 1077 |  | 
|---|
|  | 1078 | sigr = 0; | 
|---|
|  | 1079 | q = phis+binmax; | 
|---|
|  | 1080 | while ( ((*q) > imin) && (sigr < (nbin-binmax)) ) | 
|---|
|  | 1081 | { sigr++; nbpix += (*q) ; q++; } | 
|---|
|  | 1082 |  | 
|---|
|  | 1083 | sigl = 0; | 
|---|
|  | 1084 | q = phis+binmax; | 
|---|
|  | 1085 | while ( ((*q) > imin) && (sigl < binmax) ) | 
|---|
|  | 1086 | { sigl++; nbpix += (*q) ; q--; } | 
|---|
|  | 1087 | nbpix -= (*(phis+binmax)); | 
|---|
|  | 1088 |  | 
|---|
|  | 1089 | (*SigFond) = (float)(sigl+sigr)*binwidth/2.; | 
|---|
|  | 1090 | fracfond = (float)(nbpix) / (float)(tothis); | 
|---|
|  | 1091 |  | 
|---|
|  | 1092 | (*MinAff) = minhis+(binmax-sigl)*binwidth; | 
|---|
|  | 1093 | (*MaxAff) = minhis+(binmax+sigr)*binwidth; | 
|---|
|  | 1094 |  | 
|---|
|  | 1095 |  | 
|---|
|  | 1096 | /*   Check sur NbSigmas  :  */ | 
|---|
|  | 1097 | if (NbSigmas == 0) NbSigmas = 1; | 
|---|
|  | 1098 | if (NbSigmas < 0)    /*   On considere le cas ou NbSigmas est negatif   */ | 
|---|
|  | 1099 | { | 
|---|
|  | 1100 | if ( (bin = binmax+NbSigmas*sigr) < 0 )  bin = 0; | 
|---|
|  | 1101 | (*MinAff) = minhis + (float)bin*binwidth; | 
|---|
|  | 1102 | if ( (bin = binmax-NbSigmas*sigr) >= nbin ) bin = nbin-1; | 
|---|
|  | 1103 | (*MaxAff) = minhis + (float)bin*binwidth; | 
|---|
|  | 1104 | goto sortie; | 
|---|
|  | 1105 | } | 
|---|
|  | 1106 |  | 
|---|
|  | 1107 | if ( (bin = binmax+NbSigmas*sigl) >= nbin ) | 
|---|
|  | 1108 | { delete[] phis;  return(4); } | 
|---|
|  | 1109 |  | 
|---|
|  | 1110 | (*MinAff) = minhis + bin*binwidth; | 
|---|
|  | 1111 |  | 
|---|
|  | 1112 | /*   Calcul du nb de pixel entre MinAff et ValSat  */ | 
|---|
|  | 1113 | nbpix = 0; | 
|---|
|  | 1114 | for( q = phis+bin ; q < phis+nbin ; q++)  nbpix += (*q); | 
|---|
|  | 1115 |  | 
|---|
|  | 1116 | fracsignal = (float)(nbpix) / (float)(tothis+over+under); | 
|---|
|  | 1117 | nbpix = (int_4)((float)nbpix * FracMax); | 
|---|
|  | 1118 | nbpix2 = 0; q = phis+bin; i = bin; | 
|---|
|  | 1119 | while( (nbpix2 < nbpix) && (i < nbin) ) | 
|---|
|  | 1120 | { nbpix2 += (*q); i++; q++; } | 
|---|
|  | 1121 |  | 
|---|
|  | 1122 | (*MaxAff) = minhis + (float)i*binwidth; | 
|---|
|  | 1123 |  | 
|---|
|  | 1124 |  | 
|---|
|  | 1125 | sortie:        /*   On sort en liberant l'espace   */ | 
|---|
|  | 1126 |  | 
|---|
|  | 1127 | /* | 
|---|
|  | 1128 | printf("  Stats..Debug : Fond,sigl,sigr,SigFond = %g,%d,%d,%g \n", | 
|---|
|  | 1129 | (*Fond),sigl,sigr,(*SigFond)); | 
|---|
|  | 1130 | printf("    Min/MaxPix= %g  %g , Min/MaxAff= %g  %g \n",(*MinPix), | 
|---|
|  | 1131 | (*MaxPix),(*MinAff),(*MaxAff)); | 
|---|
|  | 1132 | printf("    FracFond,FracSignal %g  %g  FracNul,FracSat  %g  %g\n", | 
|---|
|  | 1133 | fracfond,fracsignal,(*FracNul),(*FracSature)); | 
|---|
|  | 1134 | */ | 
|---|
|  | 1135 |  | 
|---|
|  | 1136 | delete[] phis; | 
|---|
|  | 1137 | if ( (*MaxAff) < ((*MinAff)+MINDYNAMIC) )  return(5); | 
|---|
|  | 1138 | return(0); | 
|---|
|  | 1139 |  | 
|---|
|  | 1140 | } | 
|---|
|  | 1141 |  | 
|---|
|  | 1142 |  | 
|---|
|  | 1143 | // ********** INSTANCES | 
|---|
|  | 1144 |  | 
|---|
|  | 1145 | #if defined(__xlC) || defined(__aCC__) | 
|---|
|  | 1146 | void instancetempimageop(int n) | 
|---|
|  | 1147 | { | 
|---|
|  | 1148 | /* Cette fonction sert uniquement a forcer le compilo a instancier les | 
|---|
|  | 1149 | classes/fonctions template   */ | 
|---|
|  | 1150 |  | 
|---|
|  | 1151 | int m = n-50; | 
|---|
|  | 1152 | ImageU2  iu2(n,n), *tiu2[5], xiu2(n,n); | 
|---|
|  | 1153 | ImageI2  ii2(n,n), *tii2[5], xii2(n,n); | 
|---|
|  | 1154 | ImageI4  ii4(n,n), *tii4[5], xii4(n,n); | 
|---|
|  | 1155 | ImageR4  ir4(n,n), *tir4[5], xir4(n,n); | 
|---|
|  | 1156 |  | 
|---|
|  | 1157 | ImageR4  matx(5,5); | 
|---|
|  | 1158 |  | 
|---|
|  | 1159 | RzCmvSigmaCiel(iu2.ImagePtr(), n, n, 100, 50, 50, (float)0.5, 2, (float)0., (float)65000., 0); | 
|---|
|  | 1160 | RzCmvSigmaCiel(ii2.ImagePtr(), n, n, 100, 50, 50, (float)0.5, 2, (float)0., (float)65000., 0); | 
|---|
|  | 1161 | RzCmvSigmaCiel(ii4.ImagePtr(), n, n, 100, 50, 50, (float)0.5, 2, (float)0., (float)65000., 0); | 
|---|
|  | 1162 | RzCmvSigmaCiel(ir4.ImagePtr(), n, n, 100, 50, 50, (float)0.5, 2, (float)0., (float)65000., 0); | 
|---|
|  | 1163 |  | 
|---|
|  | 1164 | ImgFondCielHisto(iu2); | 
|---|
|  | 1165 | ImgFondCielHisto(ii2); | 
|---|
|  | 1166 | ImgFondCielHisto(ii4); | 
|---|
|  | 1167 | ImgFondCielHisto(ir4); | 
|---|
|  | 1168 |  | 
|---|
|  | 1169 | ImgSigmaCiel(iu2, 100, 1, 100); | 
|---|
|  | 1170 | ImgSigmaCiel(ii2, 100, 1, 100); | 
|---|
|  | 1171 | ImgSigmaCiel(ii4, 100, 1, 100); | 
|---|
|  | 1172 | ImgSigmaCiel(ir4, 100, 1, 100); | 
|---|
|  | 1173 |  | 
|---|
|  | 1174 | CleanImages(5, tiu2); | 
|---|
|  | 1175 | CleanImages(5, tii2); | 
|---|
|  | 1176 | CleanImages(5, tii4); | 
|---|
|  | 1177 | CleanImages(5, tir4); | 
|---|
|  | 1178 |  | 
|---|
|  | 1179 |  | 
|---|
|  | 1180 | FilterImage(&iu2, &xiu2, &matx); | 
|---|
|  | 1181 | FilterImage(&ii2, &xii2, &matx); | 
|---|
|  | 1182 | FilterImage(&ii4, &xii4, &matx); | 
|---|
|  | 1183 | FilterImage(&ir4, &xir4, &matx); | 
|---|
|  | 1184 |  | 
|---|
|  | 1185 | FilterStat(&iu2, &xiu2,(int)3,(int)3,(float)1.,(float)-1.); | 
|---|
|  | 1186 | FilterStat(&ii2, &xii2,(int)3,(int)3,(float)1.,(float)-1.); | 
|---|
|  | 1187 | FilterStat(&ii4, &xii4,(int)3,(int)3,(float)1.,(float)-1.); | 
|---|
|  | 1188 | FilterStat(&ir4, &xir4,(int)3,(int)3,(float)1.,(float)-1.); | 
|---|
|  | 1189 |  | 
|---|
|  | 1190 | CSplineImage(iu2,(double)1,(double)0,(double)0,(int)0); | 
|---|
|  | 1191 | CSplineImage(ii2,(double)1,(double)0,(double)0,(int)0); | 
|---|
|  | 1192 | CSplineImage(ii4,(double)1,(double)0,(double)0,(int)0); | 
|---|
|  | 1193 | CSplineImage(ir4,(double)1,(double)0,(double)0,(int)0); | 
|---|
|  | 1194 |  | 
|---|
|  | 1195 | return; | 
|---|
|  | 1196 | } | 
|---|
|  | 1197 |  | 
|---|
|  | 1198 | #endif | 
|---|
|  | 1199 |  | 
|---|
|  | 1200 |  | 
|---|
|  | 1201 | #ifdef __CXX_PRAGMA_TEMPLATES__ | 
|---|
|  | 1202 |  | 
|---|
|  | 1203 | #pragma define_template RzCmvSigmaCiel<uint_2> | 
|---|
|  | 1204 | #pragma define_template RzCmvSigmaCiel<int_2> | 
|---|
|  | 1205 | #pragma define_template RzCmvSigmaCiel<int_4> | 
|---|
|  | 1206 | #pragma define_template RzCmvSigmaCiel<r_4> | 
|---|
|  | 1207 |  | 
|---|
|  | 1208 | #pragma define_template ImgFondCielHisto<uint_2> | 
|---|
|  | 1209 | #pragma define_template ImgFondCielHisto<int_2> | 
|---|
|  | 1210 | #pragma define_template ImgFondCielHisto<int_4> | 
|---|
|  | 1211 | #pragma define_template ImgFondCielHisto<r_4> | 
|---|
|  | 1212 |  | 
|---|
|  | 1213 | #pragma define_template ImgSigmaCiel<uint_2> | 
|---|
|  | 1214 | #pragma define_template ImgSigmaCiel<int_2> | 
|---|
|  | 1215 | #pragma define_template ImgSigmaCiel<int_4> | 
|---|
|  | 1216 | #pragma define_template ImgSigmaCiel<r_4> | 
|---|
|  | 1217 |  | 
|---|
|  | 1218 | #pragma define_template CleanImages<uint_2> | 
|---|
|  | 1219 | #pragma define_template CleanImages<int_2> | 
|---|
|  | 1220 | #pragma define_template CleanImages<int_4> | 
|---|
|  | 1221 | #pragma define_template CleanImages<r_4> | 
|---|
|  | 1222 |  | 
|---|
|  | 1223 |  | 
|---|
|  | 1224 | #pragma define_template FilterImage<uint_2> | 
|---|
|  | 1225 | #pragma define_template FilterImage<int_2> | 
|---|
|  | 1226 | #pragma define_template FilterImage<int_4> | 
|---|
|  | 1227 | #pragma define_template FilterImage<r_4> | 
|---|
|  | 1228 |  | 
|---|
|  | 1229 | #pragma define_template FilterStat<uint_2> | 
|---|
|  | 1230 | #pragma define_template FilterStat<int_2> | 
|---|
|  | 1231 | #pragma define_template FilterStat<int_4> | 
|---|
|  | 1232 | #pragma define_template FilterStat<r_4> | 
|---|
|  | 1233 |  | 
|---|
|  | 1234 | #pragma define_template CSplineImage<uint_2> | 
|---|
|  | 1235 | #pragma define_template CSplineImage<int_2> | 
|---|
|  | 1236 | #pragma define_template CSplineImage<int_4> | 
|---|
|  | 1237 | #pragma define_template CSplineImage<r_4> | 
|---|
|  | 1238 |  | 
|---|
|  | 1239 | #endif | 
|---|
|  | 1240 |  | 
|---|
|  | 1241 |  | 
|---|
|  | 1242 | #if ( defined(ANSI_TEMPLATES) && !defined(__aCC__) ) | 
|---|
|  | 1243 | template float RzCmvSigmaCiel<uint_2 const>(uint_2 const*, int, int, int, int, int, float, int, float, float, int); | 
|---|
|  | 1244 | template float RzCmvSigmaCiel< int_2 const>( int_2 const*, int, int, int, int, int, float, int, float, float, int); | 
|---|
|  | 1245 | template float RzCmvSigmaCiel< int_4 const>( int_4 const*, int, int, int, int, int, float, int, float, float, int); | 
|---|
|  | 1246 | template float RzCmvSigmaCiel<   r_4 const>(   r_4 const*, int, int, int, int, int, float, int, float, float, int); | 
|---|
|  | 1247 | template float ImgFondCielHisto<uint_2>(Image<uint_2> const&, float, int, float, float , float , int); | 
|---|
|  | 1248 | template float ImgFondCielHisto< int_2>(Image< int_2> const&, float, int, float, float , float , int); | 
|---|
|  | 1249 | template float ImgFondCielHisto< int_4>(Image< int_4> const&, float, int, float, float , float , int); | 
|---|
|  | 1250 | template float ImgFondCielHisto<   r_4>(Image<   r_4> const&, float, int, float, float , float , int); | 
|---|
|  | 1251 | template float ImgSigmaCiel<uint_2>(Image<uint_2> const& img, int, int, int, float, int, float , float, int); | 
|---|
|  | 1252 | template float ImgSigmaCiel< int_2>(Image< int_2> const& img, int, int, int, float, int, float , float, int); | 
|---|
|  | 1253 | template float ImgSigmaCiel< int_4>(Image< int_4> const& img, int, int, int, float, int, float , float, int); | 
|---|
|  | 1254 | template float ImgSigmaCiel<   r_4>(Image<   r_4> const& img, int, int, int, float, int, float , float, int); | 
|---|
|  | 1255 | template void CleanImages<uint_2>(int, Image<uint_2>**, double, double, double); | 
|---|
|  | 1256 | template void CleanImages< int_2>(int, Image< int_2>**, double, double, double); | 
|---|
|  | 1257 | template void CleanImages< int_4>(int, Image< int_4>**, double, double, double); | 
|---|
|  | 1258 | template void CleanImages<   r_4>(int, Image<   r_4>**, double, double, double); | 
|---|
|  | 1259 |  | 
|---|
|  | 1260 |  | 
|---|
|  | 1261 | template Image<uint_2>* FilterImage<uint_2>(Image<uint_2> const *, Image<uint_2>*, ImageR4 *); | 
|---|
|  | 1262 | template Image< int_2>* FilterImage< int_2>(Image< int_2> const *, Image< int_2>*, ImageR4 *); | 
|---|
|  | 1263 | template Image< int_4>* FilterImage< int_4>(Image< int_4> const *, Image< int_4>*, ImageR4 *); | 
|---|
|  | 1264 | template Image<   r_4>* FilterImage<   r_4>(Image<   r_4> const *, Image<   r_4>*, ImageR4 *); | 
|---|
|  | 1265 |  | 
|---|
|  | 1266 | template Image<uint_2>* FilterStat<uint_2>(Image<uint_2> const *, Image<uint_2>*,int,int,float,float); | 
|---|
|  | 1267 | template Image< int_2>* FilterStat< int_2>(Image< int_2> const *, Image< int_2>*,int,int,float,float); | 
|---|
|  | 1268 | template Image< int_4>* FilterStat< int_4>(Image< int_4> const *, Image< int_4>*,int,int,float,float); | 
|---|
|  | 1269 | template Image<   r_4>* FilterStat<   r_4>(Image<   r_4> const *, Image<   r_4>*,int,int,float,float); | 
|---|
|  | 1270 |  | 
|---|
|  | 1271 | template Image<uint_2>* CSplineImage<uint_2>(Image<uint_2> const &,double,double,double,int); | 
|---|
|  | 1272 | template Image< int_2>* CSplineImage< int_2>(Image< int_2> const &,double,double,double,int); | 
|---|
|  | 1273 | template Image< int_4>* CSplineImage< int_4>(Image< int_4> const &,double,double,double,int); | 
|---|
|  | 1274 | template Image<   r_4>* CSplineImage<   r_4>(Image<   r_4> const &,double,double,double,int); | 
|---|
|  | 1275 |  | 
|---|
|  | 1276 | #endif | 
|---|
|  | 1277 |  | 
|---|
|  | 1278 |  | 
|---|
|  | 1279 | #ifdef GNU_TEMPLATES | 
|---|
|  | 1280 | template float RzCmvSigmaCiel(uint_2 const*, int, int, int, int, int, float, int, float, float, int); | 
|---|
|  | 1281 | template float RzCmvSigmaCiel( int_2 const*, int, int, int, int, int, float, int, float, float, int); | 
|---|
|  | 1282 | template float RzCmvSigmaCiel( int_4 const*, int, int, int, int, int, float, int, float, float, int); | 
|---|
|  | 1283 | template float RzCmvSigmaCiel(   r_4 const*, int, int, int, int, int, float, int, float, float, int); | 
|---|
|  | 1284 | template float ImgFondCielHisto(Image<uint_2> const&, float, int, float, float , float , int); | 
|---|
|  | 1285 | template float ImgFondCielHisto(Image< int_2> const&, float, int, float, float , float , int); | 
|---|
|  | 1286 | template float ImgFondCielHisto(Image< int_4> const&, float, int, float, float , float , int); | 
|---|
|  | 1287 | template float ImgFondCielHisto(Image<   r_4> const&, float, int, float, float , float , int); | 
|---|
|  | 1288 | template float ImgSigmaCiel(Image<uint_2> const& img, int, int, int, float, int, float , float, int); | 
|---|
|  | 1289 | template float ImgSigmaCiel(Image< int_2> const& img, int, int, int, float, int, float , float, int); | 
|---|
|  | 1290 | template float ImgSigmaCiel(Image< int_4> const& img, int, int, int, float, int, float , float, int); | 
|---|
|  | 1291 | template float ImgSigmaCiel(Image<   r_4> const& img, int, int, int, float, int, float , float, int); | 
|---|
|  | 1292 | template void CleanImages(int, Image<uint_2>**, double, double, double); | 
|---|
|  | 1293 | template void CleanImages(int, Image< int_2>**, double, double, double); | 
|---|
|  | 1294 | template void CleanImages(int, Image< int_4>**, double, double, double); | 
|---|
|  | 1295 | template void CleanImages(int, Image<   r_4>**, double, double, double); | 
|---|
|  | 1296 |  | 
|---|
|  | 1297 |  | 
|---|
|  | 1298 | template Image<uint_2>* FilterImage(Image<uint_2> const *, Image<uint_2>*, ImageR4 *); | 
|---|
|  | 1299 | template Image< int_2>* FilterImage(Image< int_2> const *, Image< int_2>*, ImageR4 *); | 
|---|
|  | 1300 | template Image< int_4>* FilterImage(Image< int_4> const *, Image< int_4>*, ImageR4 *); | 
|---|
|  | 1301 | template Image<   r_4>* FilterImage(Image<   r_4> const *, Image<   r_4>*, ImageR4 *); | 
|---|
|  | 1302 |  | 
|---|
|  | 1303 | template Image<uint_2>* FilterStat(Image<uint_2> const *, Image<uint_2>*,int,int,float,float); | 
|---|
|  | 1304 | template Image< int_2>* FilterStat(Image< int_2> const *, Image< int_2>*,int,int,float,float); | 
|---|
|  | 1305 | template Image< int_4>* FilterStat(Image< int_4> const *, Image< int_4>*,int,int,float,float); | 
|---|
|  | 1306 | template Image<   r_4>* FilterStat(Image<   r_4> const *, Image<   r_4>*,int,int,float,float); | 
|---|
|  | 1307 |  | 
|---|
|  | 1308 | template Image<uint_2>* CSplineImage(Image<uint_2> const &,double,double,double,int); | 
|---|
|  | 1309 | template Image< int_2>* CSplineImage(Image< int_2> const &,double,double,double,int); | 
|---|
|  | 1310 | template Image< int_4>* CSplineImage(Image< int_4> const &,double,double,double,int); | 
|---|
|  | 1311 | template Image<   r_4>* CSplineImage(Image<   r_4> const &,double,double,double,int); | 
|---|
|  | 1312 |  | 
|---|
|  | 1313 | #endif | 
|---|