Ejemplo n.º 1
0
  inline void operator+=(const histogram& hist)
  {
    const_iterator it = hist.begin(), e = hist.end();

    for (; it != e; ++it) 
      update(it->first, it->second);
  }
Ejemplo n.º 2
0
//!  apply k-means classify to histogram
void apply_k_means_classify(const histogram<double>& input,
                    const std::vector<double>&  mu,
                    std::vector<int>& cls )
{
  int number_of_classes=mu.size();
  int i,j,k;
  
  //calculate all the classes
  for(j=0;j<input.size();j++)
  {
    int best_k=0;
    double best_dist=0;
    
    for(k=0;k<number_of_classes;k++)
    {
      double dist=fabs(input.value(j)-mu[k]);
      if(dist<best_dist|| best_k==0)
      {
        best_dist=dist;
        best_k=k+1;
      }
    }
    cls[j]=best_k;
  }
}
Ejemplo n.º 3
0
double ks_distance(const histogram<double>& sample1,const histogram<double>& sample2)
{
  //let's include the whole range 
  double _min=std::min(sample1.min(),sample2.min());
  double _max=std::max(sample1.max(),sample2.max());
  double _range=_max-_min;
  
  histogram<double> _s1(sample1);
  histogram<double> _s2(sample2);
  
  _s1.convert_to_commulative();
  _s2.convert_to_commulative();
  
  int _size=std::max(_s1.size(),_s2.size())*2;
  double _bin=_range/_size;
  
  double dist=0.0;
  double v=0.0;
  for(int i=0;i<_size;i++,v+=_bin)
  {
    double d=fabs(_s1[v]-_s2[v]);
    if(d>dist) dist=d;
  }
  return dist;
}
Ejemplo n.º 4
0
double kl_distance(const histogram<double>& sample1,const histogram<double>& sample2)
{
  //let's include the whole range 
  double _min=std::min(sample1.min(),sample2.min());
  double _max=std::max(sample1.max(),sample2.max());
  double _range=_max-_min;
  
  //now we are going to iterate through samples
  double distance=0.0;
  int _size=std::max(sample1.size(),sample2.size());
  double _bin=_range/_size;
  double v=_min+_bin/2.0;
  
  for(int i=0;i<_size;i++,v+=_bin)
  {
    double _P=0.0;
    if(v>=sample1.min() && v<=sample1.max()) _P=sample1[v];
    double _Q=0.0;
    if(v>=sample2.min() && v<=sample2.max()) _Q=sample2[v];
    
    if(_P>0.0 && _Q>0.0)//TODO: epsilon?
      distance+=_P*log(_P/_Q);
  }
  return distance;
}
Ejemplo n.º 5
0
  /* Add a other histogram to this one.
   * Does nothing if histogram parameters ( min, max, num_buckets ) don't match.
   */
  void accumulate( const histogram& other )
  {
    if ( min() != other.min() || max() != other.max() || data().size() != other.data().size() )
      return;

    for ( size_t j = 0, num_buckets = _data.size(); j < num_buckets; ++j )
      _data[ j ] += other.data()[ j ];

    _num_entries += other.num_entries();
  }
void compute_histogram(unsigned short *depthimage,
                       int npts,
                       histogram &hist,
                       bool normalize)
{
    hist.clear();

    for (int i = 0; i < npts; i++)
        hist.insert_point(depthimage[i]);

    if (normalize)
        hist.normalize();
}
Ejemplo n.º 7
0
void checkForUniq(const vector<string>& matrix, histogram& hist, vector<Block>& uniqs,
                  int rowSize, int colSize, int numRows, int numCols) {
    int x = numRows - rowSize + 1;
    int y = numCols - colSize + 1;
    for(int i=0;i<x;++i) {
        for(int j=0;j<y;++j) {
            hist.init();
            hist.evaluate(matrix, i, j, rowSize, colSize);
            if(hist.isUnique()) {
                Block blk(i, j, rowSize, colSize);
                uniqs.push_back(blk);
            }
        }
    }
}
Ejemplo n.º 8
0
 static
 python::dict
 histogram_array_interface(histogram& self) {
     python::dict d;
     python::list shape;
     for (unsigned i = 0; i < self.dim(); ++i)
         shape.append(self.shape(i));
     if (self.depth() == sizeof(detail::wtype)) {
         shape.append(2);
         d["typestr"] = python::str("<f") + python::str(sizeof(double));
     } else {
         d["typestr"] = python::str("<u") + python::str(self.depth());
     }
     d["shape"] = python::tuple(shape);
     d["data"] = python::make_tuple(reinterpret_cast<boost::uintptr_t>(self.buffer()), false);
     return d;
 }
Ejemplo n.º 9
0
void simple_k_means( histogram<double>& hist, std::vector<double>& mu,int k_means,int maxiter)
{
  std::vector<int>    cls(hist.size(),0);
  
  if(mu.size()!=k_means)
  {
  
    mu.resize(k_means);
    //initializing k-means using uniform distribution
    double vol_min=hist.find_percentile(0.001);
    double vol_max=hist.find_percentile(0.999);
        
    for(int j=0;j<k_means;j++)
        mu[j]= vol_min+(vol_max-vol_min)*j/(double)(k_means-1);
  }
        
  for(int iter=0;iter<maxiter;iter++)
  {
    apply_k_means_classify(hist,mu,cls);
    estimate_mu(hist,cls,mu);
  }
}
Ejemplo n.º 10
0
// Run skin-detection algorithm
frame detect_skin(const frame& in) {
    dtn_frame = in;

    const rgb_byte ZERO = { };
    for(int y = HEIGHT - 1; y; --y) {
        for(int x = WIDTH - 1; x; --x) {
            auto orig = dtn_frame.get_pixel(x, y);
            rgb hist_in = { orig[2], orig[1], orig[0] };
            if(hist.value(hist_in) < tld) {
                orig[0] = orig[1] = orig[2] = 0;
            }
        }
    }

    return dtn_frame;
}
Ejemplo n.º 11
0
//! estimate sample mu using discrete classes
void estimate_mu(const histogram<double>& input,
                 const std::vector<int>& cls,
                 std::vector<double>&  mu )
{
  int number_of_classes=mu.size();
  int i,j,k;
  std::vector<double>    _counts(number_of_classes,0);
  
  for(k=0;k<number_of_classes;k++)
    mu[k]=0;
    
  double _count=0;
  
  //1 calculate means
  for(j=0;j<cls.size();j++)
  {
    _count+=input[j];
    
    int _c=cls[j];
    if(!_c || _c>number_of_classes) continue; //only use classified voxel
    _c--;
    
    _counts[_c]+=input[j];
    mu[_c]+=input[j]*input.value(j);
  }
  
  if(!_count)
    REPORT_ERROR("No voxels defined in ROI!");
  
  for(k=0;k<number_of_classes;k++)
  {
    for(k=0;k<number_of_classes;k++)
    {
      if(_counts[k]>0)
        mu[k]/=_counts[k];
    }
  }
}
Ejemplo n.º 12
0
  // protected functions
  void medianCut::computeBoxInfo(const histogram& hist,
                                       boxInfo& theBox) const {

    // boxInfo.min and .max must be specified before entry and histogram
    // must be valid.  Missing information in boxInfo is computed (mean,
    // var, colorFrequency, colors) and box boundaries (min,max) are set
    // to the smallest size, that still encloses all entries in the
    // specified range of the histogram.

    int rLow=theBox.min.getRed();
    int gLow=theBox.min.getGreen();
    int bLow=theBox.min.getBlue();

    int rUp=theBox.max.getRed();
    int gUp=theBox.max.getGreen();
    int bUp=theBox.max.getBlue();

    int rMin=rUp,rMax=rLow,gMin=gUp,gMax=gLow,bMin=bUp,bMax=bLow;
    int i=rLow,j=gLow,k=bLow;
    int freq;
    ivector iVec(3);

    double meanR, meanG, meanB, meanSquareR, meanSquareG, meanSquareB;
    meanR = meanG = meanB = meanSquareR = meanSquareG = meanSquareB = 0;
    double accu = 0;

    theBox.colors = 0;
    for (i=rLow;i<=rUp;i++) {
      for (j=gLow;j<=gUp;j++) {
        for (k=bLow;k<=bUp;k++) {
          iVec[0]=i;
          iVec[1]=j;
          iVec[2]=k;
          if (hist.at(iVec)>0.0f) {
            if (k<bMin) {bMin=k;}
            if (k>bMax) {bMax=k;}
            if (j<gMin) {gMin=j;}
            if (j>gMax) {gMax=j;}
            if (i<rMin) {rMin=i;}
            if (i>rMax) {rMax=i;}

            freq = static_cast<const int>(hist.at(iVec));

            meanR += freq * i;
            meanG += freq * j;
            meanB += freq * k;

            meanSquareR += freq *i*i;
            meanSquareG += freq *j*j;
            meanSquareB += freq *k*k;

            accu  += freq;
            // Count number of distinct colors
            theBox.colors++;
          }
        }
      }
    }

    meanR /= accu;
    meanG /= accu;
    meanB /= accu;

    meanSquareR /= accu;
    meanSquareG /= accu;
    meanSquareB /= accu;

    // Set minimum and maximum enclosing bounds
    theBox.min.setRed(rMin);
    theBox.min.setGreen(gMin);
    theBox.min.setBlue(bMin);

    theBox.max.setRed(rMax);
    theBox.max.setGreen(gMax);
    theBox.max.setBlue(bMax);

    // Set number of entries inside box
    theBox.colorFrequency = static_cast<long int>(accu);

    // set mean and variance inside box
    theBox.mean[0] = meanR;
    theBox.mean[1] = meanG;
    theBox.mean[2] = meanB;

    theBox.var[0] = meanSquareR - meanR*meanR;
    theBox.var[1] = meanSquareG - meanG*meanG;
    theBox.var[2] = meanSquareB - meanB*meanB;
  }
Ejemplo n.º 13
0
void sector_classifier::check_ngram_entropy()
{
    /* Quickly compute the histogram */ 

    /* Scan for a repeating ngram */
    for(size_t ngram_size = 1; ngram_size < 20; ngram_size++){
	bool ngram_match = true;
	for(size_t i=ngram_size;i<sbuf.pagesize && ngram_match;i++){
	    if(sbuf[i%ngram_size]!=sbuf[i]) ngram_match = false;
	}
	if(ngram_match){
            std::stringstream ss;
	    ss << CONSTANT << "(";
	    for(size_t i=0;i<ngram_size;i++){
		char buf[16];
		snprintf(buf,sizeof(buf),"0x%02x",sbuf[i]);
		if(i>0) ss << ' ';
		ss << buf;
	    }
	    ss << ")";
	    bulk_fr->write(sbuf.pos0,ss.str(),"");
	    return;			// ngram is better than entropy
	}
    }


    /* Couldn't find ngram; check entropy and FF00 counts...*/
    size_t ff00_count=0;
    h.add(sbuf.buf,sbuf.pagesize);
    h.calc_distribution();		

    for(size_t i=0;i<sbuf.pagesize-1;i++){
	if(sbuf[i]==0xff && sbuf[i+1]==0x00) ff00_count++;
    }

    if(sbuf.pagesize<=4096){		// we only tuned for 4096
	if(ff00_count>2 && h.unique_counts()>=220){
	    bulk_fr->write(sbuf.pos0,JPEG,"");
	    return;
	}
    }

    float entropy = h.entropy();

    std::stringstream ss;
    if(entropy>opt_high_entropy){
	float cosineVariance = sd_autocorrelation_cosine_variance();
	if(debug & DEBUG_INFO) ss << "high entropy ( S=" << entropy << ")" << " ACV= " << cosineVariance << " ";
	if(cosineVariance > opt_MinimumCosineVariance){
	    ss << RANDOM;
	} else {
	    ss << HUFFMAN;
	}
	bulk_fr->write(sbuf.pos0,ss.str(),"");
    } 

    // don't bother recording 'low entropy' unless debuggin
    if(entropy<=opt_high_entropy && opt_low_entropy) {
	ss << "low entropy ( S=" << entropy << ")";
	if(h.unique_counts() < 5){
	    ss << " Unique Counts: " << h.unique_counts() << " ";
	    for(std::vector<histogram::hist_element *>::const_iterator it = h.counts.begin();it!=h.counts.end();it++){
		ss << (int)((*it)->val) << ":" << (*it)->count << " ";
	    }
	}
    }
}