| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 
 | 
//! Constructor that computes statistics of an input image \p img.
    /**
        \param img The input image.
        \param compute_variance If true, the \c variance field is computed, else it is set to 0.
    **/
    template<typename T> CImgStats(const CImg<T>& img, const bool compute_variance=true) {
      assign(img,compute_variance);
    }
    //! In-place version of the previous constructor.
    template<typename T> CImgStats& assign(const CImg<T>& img, const bool compute_variance=true) {
      if (!img)
        throw CImgArgumentException("CImgStats::CImgStats() : Specified input image (%u,%u,%u,%u,%p) is empty.",
                                    img.width,img.height,img.depth,img.dim,img.data);
      mean = variance = 0;
      lmin = lmax = -1;
      T pmin=img[0], pmax=pmin, *ptrmin=img.data, *ptrmax=ptrmin;
      cimg_for(img,ptr,T) {
        const T& a=*ptr;
        mean+=(double)a;
        if (a<pmin) { pmin=a; ptrmin = ptr; }
        if (a>pmax) { pmax=a; ptrmax = ptr; }
        if (a>230) {je voudrais stocker les a dans un vecteur ou une liste ainsi que les pointeurs ptr}
      }
      mean/=img.size();
      min=(double)pmin;
      max=(double)pmax;
      unsigned long offmin = (unsigned long)(ptrmin-img.data), offmax = (unsigned long)(ptrmax-img.data);
      const unsigned long whz = img.width*img.height*img.depth, wh = img.width*img.height;
      vmin = offmin/whz; offmin%=whz; zmin = offmin/wh; offmin%=wh; ymin = offmin/img.width; xmin = offmin%img.width;
      vmax = offmax/whz; offmax%=whz; zmax = offmax/wh; offmax%=wh; ymax = offmax/img.width; xmax = offmax%img.width;
      if (compute_variance) {
        cimg_for(img,ptr,T) { const double tmpf=(*ptr)-mean; variance+=tmpf*tmpf; }
        const unsigned int siz = img.size();
        if (siz>1) variance/=(siz-1); else variance=0;
      }
      return *this;
    } | 
Partager