void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
   DualSROMVector<Real> &eg = Teuchos::dyn_cast<DualSROMVector<Real> >(g);
   const PrimalSROMVector<Real> &ex = Teuchos::dyn_cast<const PrimalSROMVector<Real> >(x);
   size_t dimension  = ex.getDimension();
   size_t numSamples = ex.getNumSamples();
   std::vector<Real> gradx(numSamples,0.), gradp(numSamples,0.);
   Real scale = 0.;
   std::vector<std::pair<size_t, Real> > data;
   std::vector<Real> val_wt(numSamples,0.), tmp(dimension,0.);
   std::vector<std::vector<Real> > val_pt(numSamples,tmp);
   for (size_t d = 0; d < dimension; d++) {
     data = moments_[d];
     for (size_t m = 0; m < data.size(); m++) {
       momentGradient(gradx,gradp,scale,d,(Real)data[m].first,data[m].second,ex);
       for (size_t k = 0; k < numSamples; k++) {
         (val_pt[k])[d] += scale*gradx[k];
         val_wt[k]      += scale*gradp[k];
       }
     }
   }
   for (size_t k = 0; k < numSamples; k++) {
     eg.setPoint(k,val_pt[k]);
     eg.setWeight(k,val_wt[k]);
   }
 }
 void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
   SROMVector<Real> &eg = Teuchos::dyn_cast<SROMVector<Real> >(g);
   const SROMVector<Real> &ex = Teuchos::dyn_cast<const SROMVector<Real> >(x);
   const int dimension  = ex.getDimension();
   const int numSamples = ex.getNumSamples();
   std::vector<Real> gradx(numSamples,0.), gradp(numSamples,0.);
   Real diff = 0., xpt = 0., val = 0., sum = 0.;
   std::vector<Real> val_wt(numSamples,0.), tmp(dimension,0.);
   std::vector<std::vector<Real> > val_pt(numSamples,tmp);
   for (int d = 0; d < dimension; d++) {
     for (int k = 0; k < numSamples; k++) {
       xpt = (*ex.getPoint(k))[d];
       val = gradientCDF(gradx,gradp,d,xpt,ex);
       diff = (val-dist_[d]->evaluateCDF(xpt));
       sum = 0.;
       for (int j = 0; j < numSamples; j++) {
         (val_pt[j])[d] += diff * gradx[j];
         val_wt[j]      += diff * gradp[j];
         sum            -= gradx[j]; 
       }
       (val_pt[k])[d] += diff * (sum - dist_[d]->evaluatePDF(xpt));
     }
   }
   for (int k = 0; k < numSamples; k++) {
     eg.setPoint(k,val_pt[k]);
     eg.setWeight(k,val_wt[k]);
   }
 }
//computes the energy of the pixel at x,y and updates the max energy if needed
double energy(int x, int y) {
    double result = sqrt(pow(gradx(x, y), 2) + pow(grady(x, y), 2));
    if (result > max_energy) {
        max_energy = result;
    }
    return result;
}
Example #4
0
//computes the energy of the pixel at x,y and updates the max energy if needed
double energy(int x, int y) {
    double result = sqrt(pow(gradx(x, y), 2) + pow(grady(x, y), 2));
    /*
    maximum energy communication results in a serious slowdown and does not
    have a major effect on the resulting image quality
    */
    if (result > my_max_energy) {
        my_max_energy = result;
    }
    return result;
}
Example #5
0
static void
compute_lic (GimpDrawable *drawable,
             const guchar *scalarfield,
             gboolean      rotate)
{
  gint xcount, ycount;
  GimpRGB color;
  gdouble vx, vy, tmp;
  GimpPixelRgn src_rgn, dest_rgn;

  gimp_pixel_rgn_init (&src_rgn, drawable,
                       border_x, border_y,
                       border_w, border_h, FALSE, FALSE);

  gimp_pixel_rgn_init (&dest_rgn, drawable,
                       border_x, border_y,
                       border_w, border_h, TRUE, TRUE);

  for (ycount = 0; ycount < src_rgn.h; ycount++)
    {
      for (xcount = 0; xcount < src_rgn.w; xcount++)
        {
          /* Get derivative at (x,y) and normalize it */
          /* ============================================================== */

          vx = gradx (scalarfield, xcount, ycount);
          vy = grady (scalarfield, xcount, ycount);

          /* Rotate if needed */
          if (rotate)
            {
              tmp = vy;
              vy = -vx;
              vx = tmp;
            }

          tmp = sqrt (vx * vx + vy * vy);
          if (tmp >= 0.000001)
            {
              tmp = 1.0 / tmp;
              vx *= tmp;
              vy *= tmp;
            }

          /* Convolve with the LIC at (x,y) */
          /* ============================== */

          if (licvals.effect_convolve == 0)
            {
              peek (&src_rgn, xcount, ycount, &color);
              tmp = lic_noise (xcount, ycount, vx, vy);
              if (source_drw_has_alpha)
                gimp_rgba_multiply (&color, tmp);
              else
                gimp_rgb_multiply (&color, tmp);
            }
          else
            {
              lic_image (&src_rgn, xcount, ycount, vx, vy, &color);
            }
          poke (&dest_rgn, xcount, ycount, &color);
        }

      gimp_progress_update ((gfloat) ycount / (gfloat) src_rgn.h);
    }
  gimp_progress_update (1.0);
}
Example #6
0
/**
 * @brief Detection of a X corner in the imput image
 * @param in - input image float
 * @param ptx - approximate x coord. of the X corner; (output) refined position
 * @param pty - approximate y coord. of the X corner; (output) refined position
 * @return int - 0 if no error
 */
static int detect_xcorner(ImageFloat in, float *ptx, float *pty)
{

    ImageFloat mask, src_buff, gx_buff, gy_buff;
    float coeff;
    int i, j, k, y, x;
    float cx = *ptx;
    float cy = *pty;
    float c2x, c2y;

    int max_iters = 400;

    float eps = 0.00001;
    int wsize = 3;
    float a11, a12, a22, p, q, d;
    int iter = 0;
    float err;
    float py, px;
    float tgx, tgy, gxx, gxy, gyy, m;

    float *mask1D;


    /*mask1D = new_vector(2*wsize+1); */

    mask1D = (float *) malloc((2 * wsize + 1) * sizeof(float));

    mask = new_imageFloat(2 * wsize + 1, 2 * wsize + 1);

    coeff = 1. / (mask->ncol * mask->nrow);

    /* calculate mask */
    for (i = -wsize, k = 0; i <= wsize; i++, k++)
    {
        mask1D[k] = exp(-i * i * coeff);
    }

    for (i = 0; i < (int) mask->nrow; i++)
    {
        for (j = 0; j < (int) mask->ncol; j++)
        {
            mask->val[i * mask->nrow + j] = mask1D[j] * mask1D[i];
        }
    }


    do {
        src_buff = extract_subpx_window(in, wsize, cx, cy);
        gx_buff = gradx(src_buff);
        gy_buff = grady(src_buff);
        a11 = a12 = a22 = p = q = 0;

        /* process gradient */
        for (y = -wsize, k = 0; y <= wsize; y++)
        {
            py = cy + (float) y;

            for (x = -wsize; x <= wsize; x++, k++)
            {
                m = mask->val[k];
                tgx = gx_buff->val[k];
                tgy = gy_buff->val[k];
                gxx = tgx * tgx * m;
                gxy = tgx * tgy * m;
                gyy = tgy * tgy * m;
                px = cx + (float) x;

                a11 += gxx;
                a12 += gxy;
                a22 += gyy;

                p += gxx * px + gxy * py;
                q += gxy * px + gyy * py;
            }
        }

        d = a11 * a22 - a12 * a12;
        c2x = 1 / d * (a22 * p - a12 * q);
        c2y = 1 / d * (-a12 * p + a11 * q);

        err = dist_l2(cx, cy, c2x, c2y);
        cx = c2x;
        cy = c2y;

        free_imageFloat(src_buff);
        free_imageFloat(gx_buff);
        free_imageFloat(gy_buff);
    } while (++iter < max_iters && err > eps);


    *ptx = cx;
    *pty = cy;

    free_imageFloat(mask);
    free((void *) mask1D);

    return 0;
}