Beispiel #1
0
static void computeOffset(cv::Mat img1, cv::Mat img2, int d, cv::Point& _off)
{
  cv::Point offset;
  cv::Size size1 = img1.size(), size2 = img2.size();
  
  if ((size1.width > 2 && size1.height > 2) &&
      (size2.width > 2 && size2.height > 2) &&
      (d < 16)) {
    cv::Mat tmp1, tmp2;
    cv::resize(img1, tmp1, cv::Size(), 0.5f, 0.5f);
    cv::resize(img2, tmp2, cv::Size(), 0.5f, 0.5f);
    computeOffset(tmp1, tmp2, d+1, offset);
    offset.x *= 2, offset.y *= 2;
  } else {
    offset.x = 0, offset.y = 0;
  }
  
  cv::Point result;
  cv::Mat tb1, tb2, eb1, eb2;
  computeBitmaps(img1, tb1, eb1);
  computeBitmaps(img2, tb2, eb2);
  
  cv::Rect bound1(cv::Point(0,0), size1);
  cv::Rect bound2(cv::Point(0,0), size2);
  
  int min_err = (bound1 & bound2).area();
  
  for (int dx = -1; dx <= 1; ++dx) {
    for (int dy = -1; dy <= 1; ++dy) {
      cv::Point displace = offset + cv::Point(dx,dy);
      cv::Rect frame = bound1 & (bound2 + displace);
      cv::Rect shift = bound2 & (bound1 - displace);
      
      // ROIs
      cv::Mat framed_tb1(tb1, frame);
      cv::Mat framed_eb1(eb1, frame);
      cv::Mat shifted_tb2(tb2, shift);
      cv::Mat shifted_eb2(eb2, shift);
      
      //
      cv::Mat tmp1, tmp2;
      cv::bitwise_xor(framed_tb1, shifted_tb2, tmp1);
      cv::bitwise_and(tmp1, framed_eb1, tmp2);
      cv::bitwise_and(shifted_eb2, tmp2, tmp1);
      
      //
      int err = (int)cv::sum(tmp1)[0];
      if (dx == 0 && dy == 0)
        err -= 1;
      if (err < min_err) {
        result = displace;
        min_err = err;
      }
    }
  }
  
  _off = result;
}
typename TreeType::ElemType MinimalCoverageSweep<SplitPolicy>::
SweepLeafNode(const size_t axis,
              const TreeType* node,
              typename TreeType::ElemType& axisCut)
{
  typedef typename TreeType::ElemType ElemType;
  typedef bound::HRectBound<metric::EuclideanDistance, ElemType> BoundType;

  std::vector<std::pair<ElemType, size_t>> sorted(node->Count());

  sorted.resize(node->Count());

  for (size_t i = 0; i < node->NumPoints(); i++)
  {
    sorted[i].first = node->Dataset().col(node->Point(i))[axis];
    sorted[i].second = i;
  }

  // Sort high bounds of children.
  std::sort(sorted.begin(), sorted.end(),
      [] (const std::pair<ElemType, size_t>& s1,
          const std::pair<ElemType, size_t>& s2)
      {
        return s1.first < s2.first;
      });

  size_t splitPointer = node->Count() / 2;

  axisCut = sorted[splitPointer - 1].first;

  // Check if the partition is suitable.
  if (!CheckLeafSweep(node, axis, axisCut))
    return std::numeric_limits<ElemType>::max();

  BoundType bound1(node->Bound().Dim());
  BoundType bound2(node->Bound().Dim());

  // Find bounds of two resulting nodes.
  for (size_t i = 0; i < splitPointer; i++)
    bound1 |= node->Dataset().col(node->Point(sorted[i].second));

  for (size_t i = splitPointer; i < node->NumChildren(); i++)
    bound2 |= node->Dataset().col(node->Point(sorted[i].second));

  // Evaluate the cost of the split i.e. calculate the total coverage
  // of two resulting nodes.

  return bound1.Volume() + bound2.Volume();
}
Beispiel #3
0
  Interval MeanValueForm::operator ()(const MeanValueForm::ipolynomial_type    *poly_ptr,
                                      const MeanValueForm::additional_var_data *data_ptr  ) const
  {
    Interval bound1(0.0), bound2(0.0);
    std::vector<Interval> bound_of_derivatives( data_ptr->size(), Interval(0.0) );

    std::vector<Interval> arguments( data_ptr->size() ), arguments_mid( data_ptr->size() );
    for(unsigned int i = 0; i < data_ptr->size(); i++)
      if( (*data_ptr)[ i ].operator ->() != 0 )
      {
        arguments    [ i ] = ((*data_ptr)[ i ])->domain_ - ((*data_ptr)[ i ])->devel_point_;
        arguments_mid[ i ] = Interval( arguments[ i ].mid() );
      }

    MeanValueForm::const_ipolynomial_iterator
      curr = poly_ptr->begin(),
      last = poly_ptr->end();

    while( curr != last ) //Walk trough the polynomial.
    {
      Interval prod1(1.0), prod2(1.0);

      //Evaluate the current monomial at the midpoint of the domain.
      for(unsigned int i = (*curr).key().min_var_code(); i <= (*curr).key().max_var_code(); i++)
      {
        unsigned int e = (*curr).key().expnt_of_var(i);
        if( e != 0 )
        {
          prod1 *= power( arguments    [i-1], e );
          prod2 *= power( arguments_mid[i-1], e );
        }
      }

      //Multiply with coefficient.
      prod1 *= (*curr).value();   // <---- Multiply with double value.
      prod2 *= (*curr).value();   // <---- Multiply with double value.

      //Add enclosure to bound interval.
      bound1 += prod1;
      bound2 += prod2;

      //Derivate the current monomial and evaluate it.
      for(unsigned int i = (*curr).key().min_var_code(); i <= (*curr).key().max_var_code(); i++)
      {
        unsigned int e_i = (*curr).key().expnt_of_var(i);
        if( e_i != 0 )
        {
          prod2 = e_i * power( arguments[i-1], e_i-1 ); //Value of derivative.

          unsigned int j;
          for(j = (*curr).key().min_var_code(); j < i; j++)
          {
            unsigned int e_j = (*curr).key().expnt_of_var(j);
            if( e_j != 0 )
            {
              prod2 *= power( arguments[j-1], e_j );
            }
          }
          for(j = i+1; j <= (*curr).key().max_var_code(); j++)
          {
            unsigned int e_j = (*curr).key().expnt_of_var(j);
            if( e_j != 0 )
            {
              prod2 *= power( arguments[j-1], e_j );
            }
          }

          //Multiply with coefficient.
          prod2 *= (*curr).value();   // <---- Multiply with double value.

          //Save enclosure of derivative.
          bound_of_derivatives[ i - 1 ] += prod2;
        }
      }

      ++curr;
    }

    for(unsigned int i = 0; i < bound_of_derivatives.size(); i++)
      if( bound_of_derivatives[i] != Interval(0.0) )
        bound2 += bound_of_derivatives[i] * (((*data_ptr)[i])->domain_ - (((*data_ptr)[i])->domain_).mid() );

    return bound1 & bound2; //Intersection of naive and mean value form evaluation.
  }
Beispiel #4
0
int main (int argc, char* argv[]) 
{
    int n1,n2,n3; /*n1 is trace length, n2 is the number of traces, n3 is the number of 3th axis*/
    int i,j,k,kk,ii;
    int nfw;    /*nfw is the reference filter-window length*/
    int tempnfw;  /*temporary variable*/
    int m;
    float medianv; /*temporary median variable*/
    bool boundary;
    int alpha,beta,gamma,delta; /*time-varying window coefficients*/
    
    float *trace;
    float *tempt; /*temporary array*/
    float *result; /*output array*/
    float *extendt;
    float *medianarray;   /*1D median filtered array*/
    float *temp1,*temp2,*temp3; /*temporary array*/
    sf_file in, out;
    
    sf_init (argc, argv); 
    in = sf_input("in");
    out = sf_output("out");
    
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input");
    n3 = sf_leftsize(in,2);
    /* get the trace length (n1) and the number of traces (n2) and n3*/
    
    if (!sf_getbool("boundary",&boundary)) boundary=false;
    /* if y, boundary is data, whereas zero*/
    
    if (!sf_getint("nfw",&nfw)) sf_error("Need integer input");
    /* reference filter-window length (>delta, positive and odd integer)*/
    
    if (!sf_getint("alpha",&alpha)) alpha=2;
    /* time-varying window parameter "alpha" (default=2)*/
    
    if (!sf_getint("beta",&beta)) beta=0;
    /* time-varying window parameter "beta" (default=0)*/
    
    if (!sf_getint("gamma",&gamma)) gamma=2;
    /* time-varying window parameter "gamma" (default=2)*/
    
    if (!sf_getint("delta",&delta)) delta=4;
    /* time-varying window parameter "delta" (default=4)*/
    
    if (alpha<beta || delta<gamma) sf_error("Need alpha>=beta && delta>=gamma"); 
    if ((alpha%2)!=0) alpha = alpha+1;
    if ((beta%2)!=0) beta = beta+1;
    if ((gamma%2)!=0) gamma = gamma+1;
    if ((delta%2)!=0) delta = delta+1;
    
    if (nfw <=delta)  sf_error("Need nfw > delta"); 
    if (nfw%2 == 0)  nfw++;
    m=(nfw-1)/2;
    tempnfw=nfw;
    
    trace = sf_floatalloc(n1*n2);
    tempt = sf_floatalloc(n1*n2);
    result = sf_floatalloc(n1*n2);
    extendt = sf_floatalloc((n1+2*m)*n2);
    medianarray =sf_floatalloc(n1*n2);
    temp1 = sf_floatalloc(nfw);
    temp2 = sf_floatalloc(n1);
    /*set the data space*/
    
    for(ii=0;ii<n3;ii++){
	sf_floatread(trace,n1*n2,in);
	for(i=0;i<n1*n2;i++){
	    tempt[i]=trace[i];
	}
	
	bound1(tempt,extendt,nfw,n1,n2,boundary);
	
	/************1D reference median filtering****************/
	
	for(i=0;i<n2;i++){
	    for(j=0;j<n1;j++){
		for(k=0;k<nfw;k++){
		    temp1[k]=extendt[(n1+2*m)*i+j+k];
		}
		medianarray[n1*i+j]=sf_quantile(m,nfw,temp1); 
	    }
	}
	medianv=0.0;
	for(i=0;i<n1*n2;i++){
	    medianv=medianv+fabs(medianarray[i]);
	}
	medianv=medianv/(1.0*n1*n2);
	
	/************1D time-varying median filter****************/
	for(i=0;i<n2;i++){
	    for(kk=0;kk<n1;kk++){
		temp2[kk]=trace[n1*i+kk];
	    }
	    for(j=0;j<n1;j++){
		if(fabs(medianarray[n1*i+j])<medianv){
		    if(fabs(medianarray[n1*i+j])<medianv/2.0){
			tempnfw=nfw+alpha;
		    }
		    else{
			tempnfw=nfw+beta;
		    }
		}
		else{
		    if(fabs(medianarray[n1*i+j])>=(medianv*2.0)){
			tempnfw=nfw-delta;
		    }
		    else{
			tempnfw=nfw-gamma;
		    }
		}
		temp3 = sf_floatalloc(tempnfw);
		bound2(temp2,temp3,n1,tempnfw,j,boundary);
		result[n1*i+j]=sf_quantile((tempnfw-1)/2,tempnfw,temp3); 
		tempnfw=nfw;
	    }
	}
	sf_floatwrite(result,n1*n2,out);
    }

    exit (0);
}
typename TreeType::ElemType MinimalCoverageSweep<SplitPolicy>::
SweepNonLeafNode(const size_t axis,
                 const TreeType* node,
                 typename TreeType::ElemType& axisCut)
{
  typedef typename TreeType::ElemType ElemType;
  typedef bound::HRectBound<metric::EuclideanDistance, ElemType> BoundType;

  std::vector<std::pair<ElemType, size_t>> sorted(node->NumChildren());

  for (size_t i = 0; i < node->NumChildren(); i++)
  {
    sorted[i].first = SplitPolicy::Bound(node->Child(i))[axis].Hi();
    sorted[i].second = i;
  }
  // Sort high bounds of children.
  std::sort(sorted.begin(), sorted.end(),
      [] (const std::pair<ElemType, size_t>& s1,
          const std::pair<ElemType, size_t>& s2)
      {
        return s1.first < s2.first;
      });

  size_t splitPointer = node->NumChildren() / 2;

  axisCut = sorted[splitPointer - 1].first;

  // Check if the midpoint split is suitable.
  if (!CheckNonLeafSweep(node, axis, axisCut))
  {
    // Find any suitable partition if the default partition is not acceptable.
    for (splitPointer = 1; splitPointer < sorted.size(); splitPointer++)
    {
      axisCut = sorted[splitPointer - 1].first;
      if (CheckNonLeafSweep(node, axis, axisCut))
        break;
    }

    if (splitPointer == node->NumChildren())
      return std::numeric_limits<ElemType>::max();
  }

  BoundType bound1(node->Bound().Dim());
  BoundType bound2(node->Bound().Dim());

  // Find bounds of two resulting nodes.
  for (size_t i = 0; i < splitPointer; i++)
    bound1 |= node->Child(sorted[i].second).Bound();

  for (size_t i = splitPointer; i < node->NumChildren(); i++)
    bound2 |= node->Child(sorted[i].second).Bound();


  // Evaluate the cost of the split i.e. calculate the total coverage
  // of two resulting nodes.

  ElemType area1 = bound1.Volume();
  ElemType area2 = bound2.Volume();

  return area1 + area2;
}