Exemplo n.º 1
0
const float Noise::noise2(
    float s
,   float t
,   const unsigned int r) const
{
    s += m_xoff;
    t += m_yoff;

    s *= 1 << r;
    t *= 1 << r;

    const float is = floor(s);
    const float it = floor(t);

    const osg::Vec2f f(_frac(s), _frac(t));

    // range [-1;+1]

    const float aa = grad2(is + 0, it + 0, r) * (f                       );
    const float ba = grad2(is + 1, it + 0, r) * (f - osg::Vec2f(1.f, 0.f));
    const float ab = grad2(is + 0, it + 1, r) * (f - osg::Vec2f(0.f, 1.f));
    const float bb = grad2(is + 1, it + 1, r) * (f - osg::Vec2f(1.f, 1.f));

    const osg::Vec2f i = mix(osg::Vec2f(aa, ab), osg::Vec2f(ba, bb), fade(f[0]));

    return mix(i[0], i[1], fade(f[1]));
}
Exemplo n.º 2
0
float
noise2(float x, float y, const float repeatx, const float repeaty, const int base)
{
	float fx, fy;
	int A, AA, AB, B, BA, BB;
	int i = (int)floorf(fmodf(x, repeatx));
	int j = (int)floorf(fmodf(y, repeaty));
	int ii = (int)fmodf(i + 1, repeatx);
	int jj = (int)fmodf(j + 1, repeaty);
	i = (i & 255) + base;
	j = (j & 255) + base;
	ii = (ii & 255) + base;
	jj = (jj & 255) + base;

	x -= floorf(x); y -= floorf(y);
	fx = x*x*x * (x * (x * 6 - 15) + 10);
	fy = y*y*y * (y * (y * 6 - 15) + 10);

	A = PERM[i];
	AA = PERM[A + j];
	AB = PERM[A + jj];
	B = PERM[ii];
	BA = PERM[B + j];
	BB = PERM[B + jj];
		
	return lerp(fy, lerp(fx, grad2(PERM[AA], x, y),
							 grad2(PERM[BA], x - 1, y)),
					lerp(fx, grad2(PERM[AB], x, y - 1),
							 grad2(PERM[BB], x - 1, y - 1)));
}
Exemplo n.º 3
0
/** 2D float Perlin periodic noise.
 */
float pnoise2( float x, float y, int px, int py )
{
    int ix0, iy0, ix1, iy1;
    float fx0, fy0, fx1, fy1;
    float s, t, nx0, nx1, n0, n1;

    ix0 = FASTFLOOR( x ); // Integer part of x
    iy0 = FASTFLOOR( y ); // Integer part of y
    fx0 = x - ix0;        // Fractional part of x
    fy0 = y - iy0;        // Fractional part of y
    fx1 = fx0 - 1.0f;
    fy1 = fy0 - 1.0f;
    ix1 = (( ix0 + 1 ) % px) & 0xff;  // Wrap to 0..px-1 and wrap to 0..255
    iy1 = (( iy0 + 1 ) % py) & 0xff;  // Wrap to 0..py-1 and wrap to 0..255
    ix0 = ( ix0 % px ) & 0xff;
    iy0 = ( iy0 % py ) & 0xff;
    
    t = FADE( fy0 );
    s = FADE( fx0 );

    nx0 = grad2(perm[ix0 + perm[iy0]], fx0, fy0);
    nx1 = grad2(perm[ix0 + perm[iy1]], fx0, fy1);
    n0 = LERP( t, nx0, nx1 );

    nx0 = grad2(perm[ix1 + perm[iy0]], fx1, fy0);
    nx1 = grad2(perm[ix1 + perm[iy1]], fx1, fy1);
    n1 = LERP(t, nx0, nx1);

    return 0.507f * ( LERP( s, n0, n1 ) );
}
Exemplo n.º 4
0
const float Noise::noise2(
    float s
,   float t
,   const unsigned int r) const
{
    s += m_xoff;
    t += m_yoff;

    s *= 1 << r;
    t *= 1 << r;

    const float is = floor(s);
    const float it = floor(t);

    const regen::Vec2f f(_frac(s), _frac(t));

    // range [-1;+1]

    const float aa = dot(grad2(is + 0, it + 0, r), (f                       ));
    const float ba = dot(grad2(is + 1, it + 0, r), (f - regen::Vec2f(1.f, 0.f)));
    const float ab = dot(grad2(is + 0, it + 1, r), (f - regen::Vec2f(0.f, 1.f)));
    const float bb = dot(grad2(is + 1, it + 1, r), (f - regen::Vec2f(1.f, 1.f)));

    const regen::Vec2f i = mix(regen::Vec2f(aa, ab), regen::Vec2f(ba, bb), fade(f.x));

    return mix(i.x, i.y, fade(f.y));
}
Exemplo n.º 5
0
// [Gx,Gy] = grad2(I) - see gradient2.m
void mGrad2( int nl, mxArray *pl[], int nr, const mxArray *pr[] ) {
  int h, w, d; float *I, *Gx, *Gy;
  checkArgs(nl,pl,nr,pr,1,2,1,1,&h,&w,&d,mxSINGLE_CLASS,(void**)&I);
  if(h<2 || w<2) mexErrMsgTxt("I must be at least 2x2.");
  pl[0]= mxCreateMatrix3( h, w, d, mxSINGLE_CLASS, 0, (void**) &Gx );
  pl[1]= mxCreateMatrix3( h, w, d, mxSINGLE_CLASS, 0, (void**) &Gy );
  grad2( I, Gx, Gy, h, w, d );
}
Exemplo n.º 6
0
void GameWidget::paintEvent(QPaintEvent *e)
{
    QPainter p(this);

    if (m_bShowSoser)
    {
        p.drawPixmap(m_soserX, 0, m_soser);
    }

    for(TShips::const_iterator sit = m_ships.constBegin(); sit != m_ships.constEnd(); ++sit)
    {
        p.drawPixmap(sit->x, sit->y, *sit->pix);
    }

    QRadialGradient grad1(0, 0, 7);
    grad1.setColorAt(1, QColor(0,0,0));
    grad1.setColorAt(0, QColor(255,255,255));
    QPen pen1(QColor(0,0,0));
    QRadialGradient grad2(0, 0, 7);
    grad2.setColorAt(1, QColor(255,0,0));
    grad2.setColorAt(0, QColor(255,200,200));
    QPen pen2(QColor(255,0,0));
    
    p.setPen(pen1);
    p.setBrush(QBrush(grad1));
    int lastL = 1;

    for(TShots::const_iterator it = m_shots.constBegin(); it != m_shots.constEnd(); ++it)
    {
        int x = it->x, y = it->y, l = it->level;
        if (l != lastL)
        {
            p.setPen((l==1)?pen1:pen2);
            p.setBrush((l==1)?grad1:grad2);
            lastL = l;
        }
        p.setBrushOrigin(x, y);
        p.drawEllipse(x - 7, y - 7, 14, 14);
    }

    p.drawPixmap(m_gunX-32, m_gunY, PicBucket::instance().getPic(1, 2).pixmap);
    m_shotspreg = QRegion();

    if (m_lives == 0)
    {	
        p.setPen(QPen(Qt::NoPen));
        p.setBrush(QBrush(QColor(255,255,255,188)));
        p.drawRect(0, 0, sizeX, sizeY);
        QLabel::paintEvent(e);
        return;
    }
}
Exemplo n.º 7
0
const float Noise::noise2(
    const float s
,   const float t) const
{
    //const float o10 = 1.0 / static_cast<float>(m_size);
    //const float o05 = 0.5 / static_cast<float>(m_size);

    const float is = floor(s);
    const float it = floor(t);

    const regen::Vec2f f(_frac(s), _frac(t));

    // range [-1;+1]

    const float aa = dot(grad2(is + 0, it + 0), (f                       ));
    const float ba = dot(grad2(is + 1, it + 0), (f - regen::Vec2f(1.f, 0.f)));
    const float ab = dot(grad2(is + 0, it + 1), (f - regen::Vec2f(0.f, 1.f)));
    const float bb = dot(grad2(is + 1, it + 1), (f - regen::Vec2f(1.f, 1.f)));

    const regen::Vec2f i = mix(regen::Vec2f(aa, ab), regen::Vec2f(ba, bb), fade(f.x));

    return mix(i.x, i.y, fade(f.y));
}
Exemplo n.º 8
0
const float Noise::noise2(
    const float s
,   const float t) const
{
    //const float o10 = 1.0 / static_cast<float>(m_size);
    //const float o05 = 0.5 / static_cast<float>(m_size);

    const float is = floor(s);
    const float it = floor(t);

    const osg::Vec2f f(_frac(s), _frac(t));

    // range [-1;+1]

    const float aa = grad2(is + 0, it + 0) * (f                       );
    const float ba = grad2(is + 1, it + 0) * (f - osg::Vec2f(1.f, 0.f));
    const float ab = grad2(is + 0, it + 1) * (f - osg::Vec2f(0.f, 1.f));
    const float bb = grad2(is + 1, it + 1) * (f - osg::Vec2f(1.f, 1.f));

    const osg::Vec2f i = mix(osg::Vec2f(aa, ab), osg::Vec2f(ba, bb), fade(f[0]));

    return mix(i[0], i[1], fade(f[1]));
}
Exemplo n.º 9
0
void MusicControl::paintEvent(QPaintEvent *) // Draws the beautifull gradient !
{
    QPainter painter(this);
    QColor mainColor = m_c->mainColor();

    QLinearGradient grad1(0, 0, 0, height()/2);
    grad1.setColorAt(0, mainColor.lighter(110));
    grad1.setColorAt(1, mainColor.darker(130));
    painter.fillRect(0, 0, width(), height(), grad1);

    QLinearGradient grad2(0, height()/2, 0, height());
    grad2.setColorAt(0, mainColor.lighter(60));
    grad2.setColorAt(1, mainColor);
    painter.fillRect(0, height()/2, width(), height()/2, grad2);
}
Exemplo n.º 10
0
real grad3(pot_table_t *pt, int col, int inc, real r2)
{
  real  r2a, istep, chi, p0, p1, p2, p3, *ptr;
  real  dfac0, dfac1, dfac2, dfac3;
  int   k;

  /* check for distances shorter than minimal distance in table */
  /* we need one extra value at the lower end for interpolation */
  r2a = MIN(r2, pt->end[col]);
  r2a = r2a - pt->begin[col];
  if (r2a < 0) {
    r2a = 0;
  }

  /* indices into potential table */
  istep = pt->invstep[col];
  k = (int)(r2a * istep);
  if (k == 0)
    return grad2(pt, col, inc, r2);	/* parabolic fit if on left border */
  chi = (r2a - k * pt->step[col]) * istep;
  k--;


  /* intermediate values */
  ptr = PTR_2D(pt->table, k, col, pt->maxsteps, inc);
  p0 = *ptr;
  ptr += inc;			/* leftmost value */
  p1 = *ptr;
  ptr += inc;			/* next left */
  p2 = *ptr;
  ptr += inc;			/* first right */
  p3 = *ptr;			/* rightmost value */

  /* factors for the interpolation */
  dfac0 = -(1.0 / 6.0) * ((3.0 * chi - 6.0) * chi + 2.0);
  dfac1 = 0.5 * ((3.0 * chi - 4.0) * chi - 1.0);
  dfac2 = -0.5 * ((3.0 * chi - 2.0) * chi - 2.0);
  dfac3 = 1.0 / 6.0 * (3.0 * chi * chi - 1.0);

/* return the gradient value */
//  return 2. * istep * (dfac0 * p0 + dfac1 * p1 + dfac2 * p2 + dfac3 * p3);
  chi = 2. * istep * (dfac0 * p0 + dfac1 * p1 + dfac2 * p2 + dfac3 * p3);
  return chi;
}
Exemplo n.º 11
0
void DeviceWidget::paintEvent(QPaintEvent *)
	{
		QPainter painter(this);

		QColor hilight(style()->standardPalette().window().color().lighter(104));
		QColor dark(style()->standardPalette().window().color().darker(100));

		if (isDefaultDevice())
		{
			QPen pen(dark.darker(170));
			painter.setPen(pen);

			QLinearGradient grad(width(), 0, width(), height());
			grad.setColorAt(0.48, hilight);
			grad.setColorAt(0.5, dark);

			hilight.setAlpha(100);
			dark.setAlpha(100);

			QLinearGradient grad2(width(), 0, width(), height());
			grad2.setColorAt(0.3, hilight);
			grad2.setColorAt(0.5, dark.darker(105));
			grad2.setColorAt(1, dark.darker(115));

			QBrush brush(grad);
			painter.setBrush(brush);

			painter.drawRoundedRect(0, 0, width()-1, height()-1, 2, 2);

			QBrush brush2(grad2);
			painter.setBrush(brush2);

			painter.drawRoundedRect(0, 0, width()-1, height()-1, 2, 2);
		}

		else
		{
			QPen pen(dark.darker(110));
			painter.setPen(pen);

			painter.drawRoundedRect(0, 0, width()-1, height()-1, 2, 2);
		}
	}
Exemplo n.º 12
0
 void CStartButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
 {
     bool down = option->state & QStyle::State_Sunken;
     QRectF r = boundingRect();
     QLinearGradient grad(r.topLeft(), r.bottomRight());
     grad.setColorAt(down ? 1 : 0, option->state & QStyle::State_MouseOver ? Qt::white : Qt::lightGray);
     grad.setColorAt(down ? 0 : 1, Qt::darkGray);
     painter->setPen(Qt::darkGray);
     painter->setBrush(grad);
     painter->drawEllipse(r);
     QLinearGradient grad2(r.topLeft(), r.bottomRight());
     grad.setColorAt(down ? 1 : 0, Qt::darkGray);
     grad.setColorAt(down ? 0 : 1, Qt::lightGray);
     painter->setPen(Qt::NoPen);
     painter->setBrush(grad);
     if (down)
         painter->translate(2, 2);
     painter->drawEllipse(r.adjusted(5, 5, -5, -5));
     painter->drawPixmap(-_pix.width()/2, -_pix.height()/2, _pix);
 }
Exemplo n.º 13
0
void caCircularGauge::drawNeedle(QPainter *p)
{
    double angle = (m_startAngle-(m_value-m_minValue)/(m_maxValue-m_minValue)*m_arcLength)*3.1415927/180.0;

    QPolygonF tr1, tr2;
    QPointF longArm, shortArm, side1, side2;

    longArm = QPointF(m_outerRadius*cos(angle),-m_outerRadius*sin(angle));
    shortArm = QPointF(-2*cos(angle),2*sin(angle));
    side1 = QPointF(-2*sin(angle),-2*cos(angle));
    side2 = -side1;

    tr1 << longArm << side1 << shortArm;
    tr2 << longArm << side2 << shortArm;

    p->setPen(Qt::NoPen);
    QRadialGradient grad1(QPointF(0,0),m_outerRadius,side1*.5);
    grad1.setColorAt(0.0, palette().color(QPalette::Mid));
    grad1.setColorAt(1.0, palette().color(QPalette::Dark));
    QRadialGradient grad2(QPointF(0,0),m_outerRadius,side2*.5);
    grad2.setColorAt(0.0, palette().color(QPalette::Midlight));
    grad2.setColorAt(1.0, palette().color(QPalette::Dark));
    p->setBrush(grad1);
    p->drawPolygon(tr1);
    p->setBrush(grad2);
    p->drawPolygon(tr2);

    QPen pen(Qt::black);
    pen.setJoinStyle(Qt::RoundJoin);
    p->setPen(pen);
    p->drawLine(longArm,side1);
    p->drawLine(side1,shortArm);
    p->drawLine(shortArm,side2);
    p->drawLine(side2,longArm);

    p->setBrush(palette().color(QPalette::Dark));
    p->drawEllipse(QRectF(-.5,-.5,1,1));
}
Exemplo n.º 14
0
// GUI calls this function when button "Clear" is pressed, or when new image is loaded
// THIS FUNCTION IS FULLY IMPLEMENTED, YOU DO NOT NEED TO CHANGE THE CODE IN IT
void reset_segm()
{
	cout << "resetting 'contour' and 'region'" << endl;

	// removing all region markings
	region.reset(image.getWidth(),image.getHeight(),0);

	// remove all points from the "contour"
	while (!contour.isEmpty()) contour.popBack();
	closedContour=false;

	// resetting 2D tables "dist" and "toParent" (erazing paths) 
	dist.reset(image.getWidth(),image.getHeight(),INFTY);
	toParent.reset(image.getWidth(),image.getHeight(),NONE);

	// recomputing "penalties" from an estimate of image contrast at each pixel p=(x,y)
	if (image.isEmpty()) {penalty.resize(0,0); return;} 
	Table2D<double> contrast = grad2(image);  //(implicit conversion of RGB "image" to "Table2D<double>") 
	// NOTE: function grad2() (see Math2D.h) computes (at each pixel) expression  Ix*Ix+Iy*Iy  where Ix and Iy
	// are "horizontal" and "vertical" derivatives of image intensity I(x,y) - the average of RGB values.
	// This expression describes the "rate of change" of intensity, or local image "contrast". 
	penalty = convert(contrast,&fn); // "&fn" - address of function "fn" (defined at the top of this file) 
	// "convert" (see Math2D.h) sets penalty at each pixel according to formula "penalty[x][y] = fn (contrast[x][y])" 
}
void AbstractWheelWidget::paintEvent(QPaintEvent* event)
{
    Q_UNUSED( event );

    // -- first calculate size and position.
    int w = width();
    int h = height();

    QPainter painter(this);
    QPalette palette = QApplication::palette();
    QPalette::ColorGroup colorGroup = isEnabled() ? QPalette::Active : QPalette::Disabled;

    // linear gradient brush
    QLinearGradient grad(0.5, 0, 0.5, 1.0);
    grad.setColorAt(0, palette.color(colorGroup, QPalette::ButtonText));
    grad.setColorAt(0.2, palette.color(colorGroup, QPalette::Button));
    grad.setColorAt(0.8, palette.color(colorGroup, QPalette::Button));
    grad.setColorAt(1.0, palette.color(colorGroup, QPalette::ButtonText));
    grad.setCoordinateMode( QGradient::ObjectBoundingMode );
    QBrush gBrush( grad );

    // paint a border and background
    painter.setPen(palette.color(colorGroup, QPalette::ButtonText));
    painter.setBrush(gBrush);
    // painter.setBrushOrigin( QPointF( 0.0, 0.0 ) );
    painter.drawRect( 0, 0, w-1, h-1 );

    // paint inner border
    painter.setPen(palette.color(colorGroup, QPalette::Button));
    painter.setBrush(Qt::NoBrush);
    painter.drawRect( 1, 1, w-3, h-3 );

    // paint the items
    painter.setClipRect( QRect( 3, 3, w-6, h-6 ) );
    painter.setPen(palette.color(colorGroup, QPalette::ButtonText));

    int iH = itemHeight();
    int iC = itemCount();
    if (iC > 0) {

        m_itemOffset = m_itemOffset % iH;

        for (int i=-h/2/iH; i<=h/2/iH+1; i++) {

            int itemNum = m_currentItem + i;
            while (itemNum < 0)
                itemNum += iC;
            while (itemNum >= iC)
                itemNum -= iC;

            paintItem(&painter, itemNum, QRect(6, h/2 +i*iH - m_itemOffset - iH/2, w-6, iH ));
        }
    }

    // draw a transparent bar over the center
    QColor highlight = palette.color(colorGroup, QPalette::Highlight);
    highlight.setAlpha(150);

    QLinearGradient grad2(0.5, 0, 0.5, 1.0);
    grad2.setColorAt(0, highlight);
    grad2.setColorAt(1.0, highlight.lighter());
    grad2.setCoordinateMode( QGradient::ObjectBoundingMode );
    QBrush gBrush2( grad2 );

    QLinearGradient grad3(0.5, 0, 0.5, 1.0);
    grad3.setColorAt(0, highlight);
    grad3.setColorAt(1.0, highlight.darker());
    grad3.setCoordinateMode( QGradient::ObjectBoundingMode );
    QBrush gBrush3( grad3 );

    painter.fillRect( QRect( 0, h/2 - iH/2, w, iH/2 ), gBrush2 );
    painter.fillRect( QRect( 0, h/2,        w, iH/2 ), gBrush3 );
}
// 2D simplex noise
float snoise2(float x, float y) {

#define F2 0.366025403 // F2 = 0.5*(sqrt(3.0)-1.0)
#define G2 0.211324865 // G2 = (3.0-Math.sqrt(3.0))/6.0

    float n0, n1, n2; // Noise contributions from the three corners

    // Skew the input space to determine which simplex cell we're in
    float s = (x+y)*F2; // Hairy factor for 2D
    float xs = x + s;
    float ys = y + s;
    int i = FASTFLOOR(xs);
    int j = FASTFLOOR(ys);

    float t = (float)(i+j)*G2;
    float X0 = i-t; // Unskew the cell origin back to (x,y) space
    float Y0 = j-t;
    float x0 = x-X0; // The x,y distances from the cell origin
    float y0 = y-Y0;

    // For the 2D case, the simplex shape is an equilateral triangle.
    // Determine which simplex we are in.
    int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
    if(x0>y0) {i1=1; j1=0;} // lower triangle, XY order: (0,0)->(1,0)->(1,1)
    else {i1=0; j1=1;}      // upper triangle, YX order: (0,0)->(0,1)->(1,1)

    // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
    // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
    // c = (3-sqrt(3))/6

    float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
    float y1 = y0 - j1 + G2;
    float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords
    float y2 = y0 - 1.0f + 2.0f * G2;

    // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
    int ii = i % 256;
    int jj = j % 256;

    // Calculate the contribution from the three corners
    float t0 = 0.5f - x0*x0-y0*y0;
    if(t0 < 0.0f) n0 = 0.0f;
    else {
      t0 *= t0;
      n0 = t0 * t0 * grad2(perm[ii+perm[jj]], x0, y0); 
    }

    float t1 = 0.5f - x1*x1-y1*y1;
    if(t1 < 0.0f) n1 = 0.0f;
    else {
      t1 *= t1;
      n1 = t1 * t1 * grad2(perm[ii+i1+perm[jj+j1]], x1, y1);
    }

    float t2 = 0.5f - x2*x2-y2*y2;
    if(t2 < 0.0f) n2 = 0.0f;
    else {
      t2 *= t2;
      n2 = t2 * t2 * grad2(perm[ii+1+perm[jj+1]], x2, y2);
    }

    // Add contributions from each corner to get the final noise value.
    // The result is scaled to return values in the interval [-1,1].
    return 40.0f * (n0 + n1 + n2); // TODO: The scale factor is preliminary!
  }
Exemplo n.º 17
0
Feature 
HOGFeatureExtractor::operator()(const CByteImage& img_) const
{
	/******** BEGIN TODO ********/
	// Compute the Histogram of Oriented Gradients feature
	// Steps are:
	// 1) Compute gradients in x and y directions. We provide the 
	//    derivative kernel proposed in the paper in _kernelDx and
	//    _kernelDy.
	// 2) Compute gradient magnitude and orientation
	// 3) Add contribution each pixel to HOG cells whose
	//    support overlaps with pixel. Each cell has a support of size
	//    _cellSize and each histogram has _nAngularBins.
	// 4) Normalize HOG for each cell. One simple strategy that is
	//    is also used in the SIFT descriptor is to first threshold
	//    the bin values so that no bin value is larger than some
	//    threshold (we leave it up to you do find this value) and
	//    then re-normalize the histogram so that it has norm 1. A more 
	//    elaborate normalization scheme is proposed in Dalal & Triggs
	//    paper but we leave that as extra credit.
	// 
	// Useful functions:
	// convertRGB2GrayImage, TypeConvert, WarpGlobal, Convolve

	
	int xCells = ceil(1.*img_.Shape().width  / _cellSize);
	int yCells = ceil(1.*img_.Shape().height / _cellSize);

	CFloatImage HOGHist(xCells, yCells, _nAngularBins);
	HOGHist.ClearPixels();

	CByteImage gray(img_.Shape());
	CFloatImage grayF(img_.Shape().width, img_.Shape().height, 1);

	convertRGB2GrayImage(img_, gray);
	TypeConvert(gray, grayF);

	CFloatImage diffX( img_.Shape()), diffY( img_.Shape());

	Convolve(grayF, diffX, _kernelDx);
	Convolve(grayF, diffY, _kernelDy);
	
	CFloatImage grad(grayF.Shape()), grad2(grayF.Shape());
	CFloatImage angl(grayF.Shape()), angl2(grayF.Shape());
	
	for (int y = 0; y <grayF.Shape().height; y++){
		for (int x = 0; x<grayF.Shape().width; x++) {
			grad2.Pixel(x,y,0) = (diffX.Pixel(x,y,0) * diffX.Pixel(x,y,0) +
							     diffY.Pixel(x,y,0) * diffY.Pixel(x,y,0));
			angl2.Pixel(x,y,0) = atan(diffY.Pixel(x,y,0) / abs(diffY.Pixel(x,y,0)));
		}
	}

	// Bilinear Filter
	ConvolveSeparable(grad2, grad, ConvolveKernel_121,ConvolveKernel_121,1);
	ConvolveSeparable(angl2, angl, ConvolveKernel_121,ConvolveKernel_121,1);
	//WriteFile(diffX, "angle.tga");
	//WriteFile(diffY, "angleG.tga");

	for (int y = 0; y <grayF.Shape().height; y++){
		for (int x = 0; x<grayF.Shape().width; x++) {
			// Fit in the bins
			int a = angl.Pixel(x,y,0) / 3.14 * (_nAngularBins) + _nAngularBins/2;		
			// Histogram
			HOGHist.Pixel(floor(1.*x / _cellSize),
						  floor(1.*y / _cellSize),
						  a) += grad.Pixel(x,y,0);
		}
	}
	
	// Normalization 
	float threshold = 0.7;
	for (int y = 0; y < yCells; y++){
		for (int x = 0; x < xCells; x++){
			float total = 0;
			for (int a = 0; a < _nAngularBins; a++) {
				if (HOGHist.Pixel(x,y,a) > threshold)
					HOGHist.Pixel(x,y,a) = threshold;
				// Sum for normalization
				total += HOGHist.Pixel(x,y,a);
			}
			for (int a = 0;a< _nAngularBins; a++) {
				HOGHist.Pixel(x,y,a) /= total;
				
			}
		}
	}
	
	return HOGHist;
	/******** END TODO ********/

}
Exemplo n.º 18
0
/** 2D simplex noise with derivatives.
 * If the last two arguments are not null, the analytic derivative
 * (the 2D gradient of the scalar noise field) is also calculated.
 */
float sdnoise2( float x, float y, float *dnoise_dx, float *dnoise_dy )
  {
    float n0, n1, n2; /* Noise contributions from the three simplex corners */
    float gx0, gy0, gx1, gy1, gx2, gy2; /* Gradients at simplex corners */

    /* Skew the input space to determine which simplex cell we're in */
    float s = ( x + y ) * F2; /* Hairy factor for 2D */
    float xs = x + s;
    float ys = y + s;
    int i = FASTFLOOR( xs );
    int j = FASTFLOOR( ys );

    float t = ( float ) ( i + j ) * G2;
    float X0 = i - t; /* Unskew the cell origin back to (x,y) space */
    float Y0 = j - t;
    float x0 = x - X0; /* The x,y distances from the cell origin */
    float y0 = y - Y0;

    /* For the 2D case, the simplex shape is an equilateral triangle.
     * Determine which simplex we are in. */
    int i1, j1; /* Offsets for second (middle) corner of simplex in (i,j) coords */
    if( x0 > y0 ) { i1 = 1; j1 = 0; } /* lower triangle, XY order: (0,0)->(1,0)->(1,1) */
    else { i1 = 0; j1 = 1; }      /* upper triangle, YX order: (0,0)->(0,1)->(1,1) */

    /* A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
     * a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
     * c = (3-sqrt(3))/6   */
    float x1 = x0 - i1 + G2; /* Offsets for middle corner in (x,y) unskewed coords */
    float y1 = y0 - j1 + G2;
    float x2 = x0 - 1.0f + 2.0f * G2; /* Offsets for last corner in (x,y) unskewed coords */
    float y2 = y0 - 1.0f + 2.0f * G2;

    /* Wrap the integer indices at 256, to avoid indexing perm[] out of bounds */
    int ii = i % 256;
    int jj = j % 256;

    /* Calculate the contribution from the three corners */
    float t0 = 0.5f - x0 * x0 - y0 * y0;
    float t20, t40;
    if( t0 < 0.0f ) t40 = t20 = t0 = n0 = gx0 = gy0 = 0.0f; /* No influence */
    else {
      grad2( perm[ii + perm[jj]], &gx0, &gy0 );
      t20 = t0 * t0;
      t40 = t20 * t20;
      n0 = t40 * ( gx0 * x0 + gy0 * y0 );
    }

    float t1 = 0.5f - x1 * x1 - y1 * y1;
    float t21, t41;
    if( t1 < 0.0f ) t21 = t41 = t1 = n1 = gx1 = gy1 = 0.0f; /* No influence */
    else {
      grad2( perm[ii + i1 + perm[jj + j1]], &gx1, &gy1 );
      t21 = t1 * t1;
      t41 = t21 * t21;
      n1 = t41 * ( gx1 * x1 + gy1 * y1 );
    }

    float t2 = 0.5f - x2 * x2 - y2 * y2;
    float t22, t42;
    if( t2 < 0.0f ) t42 = t22 = t2 = n2 = gx2 = gy2 = 0.0f; /* No influence */
    else {
      grad2( perm[ii + 1 + perm[jj + 1]], &gx2, &gy2 );
      t22 = t2 * t2;
      t42 = t22 * t22;
      n2 = t42 * ( gx2 * x2 + gy2 * y2 );
    }

    /* Add contributions from each corner to get the final noise value.
     * The result is scaled to return values in the interval [-1,1]. */
    float noise = 40.0f * ( n0 + n1 + n2 );

    /* Compute derivative, if requested by supplying non-null pointers
     * for the last two arguments */
    if( ( dnoise_dx != 0 ) && ( dnoise_dy != 0 ) )
      {
	/*  A straight, unoptimised calculation would be like:
     *    *dnoise_dx = -8.0f * t20 * t0 * x0 * ( gx0 * x0 + gy0 * y0 ) + t40 * gx0;
     *    *dnoise_dy = -8.0f * t20 * t0 * y0 * ( gx0 * x0 + gy0 * y0 ) + t40 * gy0;
     *    *dnoise_dx += -8.0f * t21 * t1 * x1 * ( gx1 * x1 + gy1 * y1 ) + t41 * gx1;
     *    *dnoise_dy += -8.0f * t21 * t1 * y1 * ( gx1 * x1 + gy1 * y1 ) + t41 * gy1;
     *    *dnoise_dx += -8.0f * t22 * t2 * x2 * ( gx2 * x2 + gy2 * y2 ) + t42 * gx2;
     *    *dnoise_dy += -8.0f * t22 * t2 * y2 * ( gx2 * x2 + gy2 * y2 ) + t42 * gy2;
	 */
        float temp0 = t20 * t0 * ( gx0* x0 + gy0 * y0 );
        *dnoise_dx = temp0 * x0;
        *dnoise_dy = temp0 * y0;
        float temp1 = t21 * t1 * ( gx1 * x1 + gy1 * y1 );
        *dnoise_dx += temp1 * x1;
        *dnoise_dy += temp1 * y1;
        float temp2 = t22 * t2 * ( gx2* x2 + gy2 * y2 );
        *dnoise_dx += temp2 * x2;
        *dnoise_dy += temp2 * y2;
        *dnoise_dx *= -8.0f;
        *dnoise_dy *= -8.0f;
        *dnoise_dx += t40 * gx0 + t41 * gx1 + t42 * gx2;
        *dnoise_dy += t40 * gy0 + t41 * gy1 + t42 * gy2;
        *dnoise_dx *= 40.0f; /* Scale derivative to match the noise scaling */
        *dnoise_dy *= 40.0f;
      }
    return noise;
  }
Exemplo n.º 19
0
        void operator()( const Range& range ) const
        {
            double inv_count = 1./inputs.rows;
            int ivcount = ann->layer_sizes.front();
            int ovcount = ann->layer_sizes.back();
            int itype = inputs.type(), otype = outputs.type();
            int count = inputs.rows;
            int i, j, k, l_count = ann->layer_count();
            vector<vector<double> > x(l_count);
            vector<vector<double> > df(l_count);
            vector<double> _buf(ann->max_lsize*dcount0*2);
            double* buf[] = { &_buf[0], &_buf[ann->max_lsize*dcount0] };
            double E = 0;

            for( i = 0; i < l_count; i++ )
            {
                x[i].resize(ann->layer_sizes[i]*dcount0);
                df[i].resize(ann->layer_sizes[i]*dcount0);
            }

            for( int si = range.start; si < range.end; si++ )
            {
                int i0 = si*dcount0, i1 = std::min((si + 1)*dcount0, count);
                int dcount = i1 - i0;
                const double* w = ann->weights[0].ptr<double>();

                // grab and preprocess input data
                for( i = 0; i < dcount; i++ )
                {
                    const uchar* x0data_p = inputs.ptr(i0 + i);
                    const float* x0data_f = (const float*)x0data_p;
                    const double* x0data_d = (const double*)x0data_p;

                    double* xdata = &x[0][i*ivcount];
                    for( j = 0; j < ivcount; j++ )
                        xdata[j] = (itype == CV_32F ? (double)x0data_f[j] : x0data_d[j])*w[j*2] + w[j*2+1];
                }
                Mat x1(dcount, ivcount, CV_64F, &x[0][0]);

                // forward pass, compute y[i]=w*x[i-1], x[i]=f(y[i]), df[i]=f'(y[i])
                for( i = 1; i < l_count; i++ )
                {
                    Mat x2( dcount, ann->layer_sizes[i], CV_64F, &x[i][0] );
                    Mat _w = ann->weights[i].rowRange(0, x1.cols);
                    gemm( x1, _w, 1, noArray(), 0, x2 );
                    Mat _df( x2.size(), CV_64F, &df[i][0] );
                    ann->calc_activ_func_deriv( x2, _df, ann->weights[i] );
                    x1 = x2;
                }

                Mat grad1(dcount, ovcount, CV_64F, buf[l_count & 1]);

                w = ann->weights[l_count+1].ptr<double>();

                // calculate error
                for( i = 0; i < dcount; i++ )
                {
                    const uchar* udata_p = outputs.ptr(i0+i);
                    const float* udata_f = (const float*)udata_p;
                    const double* udata_d = (const double*)udata_p;

                    const double* xdata = &x[l_count-1][i*ovcount];
                    double* gdata = grad1.ptr<double>(i);
                    double sweight = sw ? sw[si+i] : inv_count, E1 = 0;

                    for( j = 0; j < ovcount; j++ )
                    {
                        double t = (otype == CV_32F ? (double)udata_f[j] : udata_d[j])*w[j*2] + w[j*2+1] - xdata[j];
                        gdata[j] = t*sweight;
                        E1 += t*t;
                    }
                    E += sweight*E1;
                }

                for( i = l_count-1; i > 0; i-- )
                {
                    int n1 = ann->layer_sizes[i-1], n2 = ann->layer_sizes[i];
                    Mat _df(dcount, n2, CV_64F, &df[i][0]);
                    multiply(grad1, _df, grad1);

                    {
                        AutoLock lock(ann->mtx);
                        Mat _dEdw = dEdw->at(i).rowRange(0, n1);
                        x1 = Mat(dcount, n1, CV_64F, &x[i-1][0]);
                        gemm(x1, grad1, 1, _dEdw, 1, _dEdw, GEMM_1_T);

                        // update bias part of dEdw
                        double* dst = dEdw->at(i).ptr<double>(n1);
                        for( k = 0; k < dcount; k++ )
                        {
                            const double* src = grad1.ptr<double>(k);
                            for( j = 0; j < n2; j++ )
                                dst[j] += src[j];
                        }
                    }

                    Mat grad2( dcount, n1, CV_64F, buf[i&1] );
                    if( i > 1 )
                    {
                        Mat _w = ann->weights[i].rowRange(0, n1);
                        gemm(grad1, _w, 1, noArray(), 0, grad2, GEMM_2_T);
                    }
                    grad1 = grad2;
                }
            }
            {
                AutoLock lock(ann->mtx);
                *pE += E;
            }
        }
Exemplo n.º 20
0
    /**
     * @param   inarray         Input array.
     * @param   outarray        Output array.
     * @param   time            Current simulation time.
     * @param   lambda          Timestep.
     */
    void Bidomain::DoImplicitSolve(
            const Array<OneD, const Array<OneD, NekDouble> >&inarray,
            Array<OneD, Array<OneD, NekDouble> >&outarray,
            const NekDouble time,
            const NekDouble lambda)
    {
        int nvariables  = inarray.num_elements();
        int nq          = m_fields[0]->GetNpoints();

        Array<OneD, NekDouble> grad0(nq), grad1(nq), grad2(nq), grad(nq);
        Array<OneD, NekDouble> ggrad0(nq), ggrad1(nq), ggrad2(nq), ggrad(nq), temp(nq);

        // We solve ( \sigma\nabla^2 - HHlambda ) Y[i] = rhs [i]
        // inarray = input: \hat{rhs} -> output: \hat{Y}
        // outarray = output: nabla^2 \hat{Y}
        // where \hat = modal coeffs
        for (int i = 0; i < nvariables; ++i)
        {
            // Only apply diffusion to first variable.
            if (i > 1) {
                Vmath::Vcopy(nq, &inarray[i][0], 1, &outarray[i][0], 1);
                continue;
            }
            if (i == 0) {
                StdRegions::ConstFactorMap factors;
                factors[StdRegions::eFactorLambda] = (1.0/lambda)*(m_capMembrane*m_chi);
                if (m_spacedim==1) {
                // Take first partial derivative
                m_fields[i]->PhysDeriv(inarray[1],ggrad0);
                // Take second partial derivative
                m_fields[i]->PhysDeriv(0,ggrad0,ggrad0);
                // Multiply by Intracellular-Conductivity
		if (m_session->DefinesFunction("IntracellularConductivity") && m_session->DefinesFunction("ExtracellularConductivity"))
		{
                Vmath::Smul(nq, m_session->GetParameter("sigmaix"), ggrad0, 1, ggrad0, 1);
                }
                // Add partial derivatives together
                Vmath::Vcopy(nq, ggrad0, 1, ggrad, 1);
                Vmath::Smul(nq, -1.0, ggrad, 1, ggrad, 1);
                // Multiply 1.0/timestep/lambda
                Vmath::Smul(nq, -factors[StdRegions::eFactorLambda], inarray[i], 1, temp, 1);
                Vmath::Vadd(nq, ggrad, 1, temp, 1, m_fields[i]->UpdatePhys(), 1);
                // Solve a system of equations with Helmholtz solver and transform
                // back into physical space.
                m_fields[i]->HelmSolve(m_fields[i]->GetPhys(), m_fields[i]->UpdateCoeffs(),NullFlagList,factors);
                m_fields[i]->BwdTrans( m_fields[i]->GetCoeffs(), m_fields[i]->UpdatePhys());
                m_fields[i]->SetPhysState(true);
                // Copy the solution vector (required as m_fields must be set).
                outarray[i] = m_fields[i]->GetPhys();
                }
                
                if (m_spacedim==2) {
                // Take first partial derivative
                m_fields[i]->PhysDeriv(inarray[1],ggrad0,ggrad1);
                // Take second partial derivative
                m_fields[i]->PhysDeriv(0,ggrad0,ggrad0);
                m_fields[i]->PhysDeriv(1,ggrad1,ggrad1);
                // Multiply by Intracellular-Conductivity 
		if (m_session->DefinesFunction("IntracellularConductivity") && m_session->DefinesFunction("ExtracellularConductivity"))
		{                 
                Vmath::Smul(nq, m_session->GetParameter("sigmaix"), ggrad0, 1, ggrad0, 1);   
                Vmath::Smul(nq, m_session->GetParameter("sigmaiy"), ggrad1, 1, ggrad1, 1); 
                }
                // Add partial derivatives together
                Vmath::Vadd(nq, ggrad0, 1, ggrad1, 1, ggrad, 1);
                Vmath::Smul(nq, -1.0, ggrad, 1, ggrad, 1); 
                // Multiply 1.0/timestep/lambda
                Vmath::Smul(nq, -factors[StdRegions::eFactorLambda], inarray[i], 1, temp, 1);
                Vmath::Vadd(nq, ggrad, 1, temp, 1, m_fields[i]->UpdatePhys(), 1);
                // Solve a system of equations with Helmholtz solver and transform
                // back into physical space.
                m_fields[i]->HelmSolve(m_fields[i]->GetPhys(), m_fields[i]->UpdateCoeffs(),NullFlagList,factors,m_vardiffi);
                m_fields[i]->BwdTrans( m_fields[i]->GetCoeffs(), m_fields[i]->UpdatePhys());
                m_fields[i]->SetPhysState(true);
                // Copy the solution vector (required as m_fields must be set).
                outarray[i] = m_fields[i]->GetPhys();                                                                          
                }
                
                if (m_spacedim==3) {
                // Take first partial derivative
                m_fields[i]->PhysDeriv(inarray[1],ggrad0,ggrad1,ggrad2);
                // Take second partial derivative
                m_fields[i]->PhysDeriv(0,ggrad0,ggrad0);
                m_fields[i]->PhysDeriv(1,ggrad1,ggrad1);                
                m_fields[i]->PhysDeriv(2,ggrad2,ggrad2); 
                // Multiply by Intracellular-Conductivity 
		if (m_session->DefinesFunction("IntracellularConductivity") && m_session->DefinesFunction("ExtracellularConductivity"))
		{                 
                Vmath::Smul(nq, m_session->GetParameter("sigmaix"), ggrad0, 1, ggrad0, 1);  
                Vmath::Smul(nq, m_session->GetParameter("sigmaiy"), ggrad1, 1, ggrad1, 1);
                Vmath::Smul(nq, m_session->GetParameter("sigmaiz"), ggrad2, 1, ggrad2, 1);
                }
                // Add partial derivatives together
                Vmath::Vadd(nq, ggrad0, 1, ggrad1, 1, ggrad, 1);
                Vmath::Vadd(nq, ggrad2, 1, ggrad, 1, ggrad, 1);
                Vmath::Smul(nq, -1.0, ggrad, 1, ggrad, 1);
                // Multiply 1.0/timestep/lambda
                Vmath::Smul(nq, -factors[StdRegions::eFactorLambda], inarray[i], 1, temp, 1);
                Vmath::Vadd(nq, ggrad, 1, temp, 1, m_fields[i]->UpdatePhys(), 1);
                // Solve a system of equations with Helmholtz solver and transform
                // back into physical space.
                m_fields[i]->HelmSolve(m_fields[i]->GetPhys(), m_fields[i]->UpdateCoeffs(),NullFlagList,factors,m_vardiffi);
                m_fields[i]->BwdTrans( m_fields[i]->GetCoeffs(), m_fields[i]->UpdatePhys());
                m_fields[i]->SetPhysState(true);
                // Copy the solution vector (required as m_fields must be set).
                outarray[i] = m_fields[i]->GetPhys();                                         
                }                                

            }
            if (i == 1) {
                StdRegions::ConstFactorMap factors;
                factors[StdRegions::eFactorLambda] = 0.0;
                if (m_spacedim==1) {
                // Take first partial derivative
                m_fields[i]->PhysDeriv(m_fields[0]->UpdatePhys(),grad0);
                // Take second derivative
                m_fields[i]->PhysDeriv(0,grad0,grad0);  
                // Multiply by Intracellular-Conductivity
		if (m_session->DefinesFunction("IntracellularConductivity") && m_session->DefinesFunction("ExtracellularConductivity"))
		{                
                Vmath::Smul(nq, m_session->GetParameter("sigmaix"), grad0, 1, grad0, 1);   
                }
                // and sum terms
                Vmath::Vcopy(nq, grad0, 1, grad, 1);
                Vmath::Smul(nq, (-1.0*m_session->GetParameter("sigmaix"))/(m_session->GetParameter("sigmaix")+m_session->GetParameter("sigmaix")), grad, 1, grad, 1);  
                // Now solve Poisson problem for \phi_e
                m_fields[i]->SetPhys(grad);
                m_fields[i]->HelmSolve(m_fields[i]->GetPhys(), m_fields[i]->UpdateCoeffs(), NullFlagList, factors);
                m_fields[i]->BwdTrans( m_fields[i]->GetCoeffs(), m_fields[i]->UpdatePhys());
                m_fields[i]->SetPhysState(true);
                // Copy the solution vector (required as m_fields must be set).
                outarray[i] = m_fields[i]->GetPhys();                                                         
                }
                
                if (m_spacedim==2) {
                // Take first partial derivative
                m_fields[i]->PhysDeriv(m_fields[0]->UpdatePhys(),grad0,grad1);
                // Take second derivative
                m_fields[i]->PhysDeriv(0,grad0,grad0);
                m_fields[i]->PhysDeriv(1,grad1,grad1);  
                // Multiply by Intracellular-Conductivity
		if (m_session->DefinesFunction("IntracellularConductivity") && m_session->DefinesFunction("ExtracellularConductivity"))
		{                
                Vmath::Smul(nq, m_session->GetParameter("sigmaix"), grad0, 1, grad0, 1);
                Vmath::Smul(nq, m_session->GetParameter("sigmaiy"), grad1, 1, grad1, 1); 
                }
                // and sum terms
                Vmath::Vadd(nq, grad0, 1, grad1, 1, grad, 1);
                Vmath::Smul(nq, -1.0, grad, 1, grad, 1);   
                // Now solve Poisson problem for \phi_e
                m_fields[i]->SetPhys(grad);
                m_fields[i]->HelmSolve(m_fields[i]->GetPhys(), m_fields[i]->UpdateCoeffs(), NullFlagList, factors, m_vardiffie);
                m_fields[i]->BwdTrans( m_fields[i]->GetCoeffs(), m_fields[i]->UpdatePhys());
                m_fields[i]->SetPhysState(true);
                // Copy the solution vector (required as m_fields must be set).
                outarray[i] = m_fields[i]->GetPhys();                                                        
                }
                
                if (m_spacedim==3) {
                // Take first partial derivative
                m_fields[i]->PhysDeriv(m_fields[0]->UpdatePhys(),grad0,grad1,grad2);
                // Take second derivative
                m_fields[i]->PhysDeriv(0,grad0,grad0);
                m_fields[i]->PhysDeriv(1,grad1,grad1);
                m_fields[i]->PhysDeriv(2,grad2,grad2); 
                // Multiply by Intracellular-Conductivity
		if (m_session->DefinesFunction("IntracellularConductivity") && m_session->DefinesFunction("ExtracellularConductivity"))
		{                
                Vmath::Smul(nq, m_session->GetParameter("sigmaix"), grad0, 1, grad0, 1);
                Vmath::Smul(nq, m_session->GetParameter("sigmaiy"), grad1, 1, grad1, 1);
                Vmath::Smul(nq, m_session->GetParameter("sigmaiz"), grad2, 1, grad2, 1);       
                }
                // and sum terms
                Vmath::Vadd(nq, grad0, 1, grad1, 1, grad, 1);
                Vmath::Vadd(nq, grad2, 1, grad, 1, grad, 1);
                Vmath::Smul(nq, -1.0, grad, 1, grad, 1);  
                // Now solve Poisson problem for \phi_e
                m_fields[i]->SetPhys(grad);
                m_fields[i]->HelmSolve(m_fields[i]->GetPhys(), m_fields[i]->UpdateCoeffs(), NullFlagList, factors, m_vardiffie);
                m_fields[i]->BwdTrans( m_fields[i]->GetCoeffs(), m_fields[i]->UpdatePhys());
                m_fields[i]->SetPhysState(true);
                // Copy the solution vector (required as m_fields must be set).
                outarray[i] = m_fields[i]->GetPhys();                                                      
                }
                                                
            }
        }
    }
Exemplo n.º 21
0
/* 2D simplex noise */
GLfloat _slang_library_noise2 (GLfloat x, GLfloat y)
{
#define F2 0.366025403f /* F2 = 0.5*(sqrt(3.0)-1.0) */
#define G2 0.211324865f /* G2 = (3.0-Math.sqrt(3.0))/6.0 */

    float n0, n1, n2; /* Noise contributions from the three corners */

    /* Skew the input space to determine which simplex cell we're in */
    float s = (x+y)*F2; /* Hairy factor for 2D */
    float xs = x + s;
    float ys = y + s;
    int i = FASTFLOOR(xs);
    int j = FASTFLOOR(ys);

    float t = (float)(i+j)*G2;
    float X0 = i-t; /* Unskew the cell origin back to (x,y) space */
    float Y0 = j-t;
    float x0 = x-X0; /* The x,y distances from the cell origin */
    float y0 = y-Y0;

    float x1, y1, x2, y2;
    int ii, jj;
    float t0, t1, t2;

    /* For the 2D case, the simplex shape is an equilateral triangle. */
    /* Determine which simplex we are in. */
    int i1, j1; /* Offsets for second (middle) corner of simplex in (i,j) coords */
    if(x0>y0) {i1=1; j1=0;} /* lower triangle, XY order: (0,0)->(1,0)->(1,1) */
    else {i1=0; j1=1;}      /* upper triangle, YX order: (0,0)->(0,1)->(1,1) */

    /* A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and */
    /* a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where */
    /* c = (3-sqrt(3))/6 */

    x1 = x0 - i1 + G2; /* Offsets for middle corner in (x,y) unskewed coords */
    y1 = y0 - j1 + G2;
    x2 = x0 - 1.0f + 2.0f * G2; /* Offsets for last corner in (x,y) unskewed coords */
    y2 = y0 - 1.0f + 2.0f * G2;

    /* Wrap the integer indices at 256, to avoid indexing perm[] out of bounds */
    ii = i % 256;
    jj = j % 256;

    /* Calculate the contribution from the three corners */
    t0 = 0.5f - x0*x0-y0*y0;
    if(t0 < 0.0f) n0 = 0.0f;
    else {
      t0 *= t0;
      n0 = t0 * t0 * grad2(perm[ii+perm[jj]], x0, y0); 
    }

    t1 = 0.5f - x1*x1-y1*y1;
    if(t1 < 0.0f) n1 = 0.0f;
    else {
      t1 *= t1;
      n1 = t1 * t1 * grad2(perm[ii+i1+perm[jj+j1]], x1, y1);
    }

    t2 = 0.5f - x2*x2-y2*y2;
    if(t2 < 0.0f) n2 = 0.0f;
    else {
      t2 *= t2;
      n2 = t2 * t2 * grad2(perm[ii+1+perm[jj+1]], x2, y2);
    }

    /* Add contributions from each corner to get the final noise value. */
    /* The result is scaled to return values in the interval [-1,1]. */
    return 40.0f * (n0 + n1 + n2); /* TODO: The scale factor is preliminary! */
}
Exemplo n.º 22
0
    int train_backprop( const Mat& inputs, const Mat& outputs, const Mat& _sw, TermCriteria termCrit )
    {
        int i, j, k;
        double prev_E = DBL_MAX*0.5, E = 0;
        int itype = inputs.type(), otype = outputs.type();

        int count = inputs.rows;

        int iter = -1, max_iter = termCrit.maxCount*count;
        double epsilon = termCrit.epsilon*count;

        int l_count = layer_count();
        int ivcount = layer_sizes[0];
        int ovcount = layer_sizes.back();

        // allocate buffers
        vector<vector<double> > x(l_count);
        vector<vector<double> > df(l_count);
        vector<Mat> dw(l_count);

        for( i = 0; i < l_count; i++ )
        {
            int n = layer_sizes[i];
            x[i].resize(n+1);
            df[i].resize(n);
            dw[i] = Mat::zeros(weights[i].size(), CV_64F);
        }

        Mat _idx_m(1, count, CV_32S);
        int* _idx = _idx_m.ptr<int>();
        for( i = 0; i < count; i++ )
            _idx[i] = i;

        AutoBuffer<double> _buf(max_lsize*2);
        double* buf[] = { _buf, (double*)_buf + max_lsize };

        const double* sw = _sw.empty() ? 0 : _sw.ptr<double>();

        // run back-propagation loop
        /*
         y_i = w_i*x_{i-1}
         x_i = f(y_i)
         E = 1/2*||u - x_N||^2
         grad_N = (x_N - u)*f'(y_i)
         dw_i(t) = momentum*dw_i(t-1) + dw_scale*x_{i-1}*grad_i
         w_i(t+1) = w_i(t) + dw_i(t)
         grad_{i-1} = w_i^t*grad_i
        */
        for( iter = 0; iter < max_iter; iter++ )
        {
            int idx = iter % count;
            double sweight = sw ? count*sw[idx] : 1.;

            if( idx == 0 )
            {
                //printf("%d. E = %g\n", iter/count, E);
                if( fabs(prev_E - E) < epsilon )
                    break;
                prev_E = E;
                E = 0;

                // shuffle indices
                for( i = 0; i < count; i++ )
                {
                    j = rng.uniform(0, count);
                    k = rng.uniform(0, count);
                    std::swap(_idx[j], _idx[k]);
                }
            }

            idx = _idx[idx];

            const uchar* x0data_p = inputs.ptr(idx);
            const float* x0data_f = (const float*)x0data_p;
            const double* x0data_d = (const double*)x0data_p;

            double* w = weights[0].ptr<double>();
            for( j = 0; j < ivcount; j++ )
                x[0][j] = (itype == CV_32F ? (double)x0data_f[j] : x0data_d[j])*w[j*2] + w[j*2 + 1];

            Mat x1( 1, ivcount, CV_64F, &x[0][0] );

            // forward pass, compute y[i]=w*x[i-1], x[i]=f(y[i]), df[i]=f'(y[i])
            for( i = 1; i < l_count; i++ )
            {
                int n = layer_sizes[i];
                Mat x2(1, n, CV_64F, &x[i][0] );
                Mat _w = weights[i].rowRange(0, x1.cols);
                gemm(x1, _w, 1, noArray(), 0, x2);
                Mat _df(1, n, CV_64F, &df[i][0] );
                calc_activ_func_deriv( x2, _df, weights[i] );
                x1 = x2;
            }

            Mat grad1( 1, ovcount, CV_64F, buf[l_count&1] );
            w = weights[l_count+1].ptr<double>();

            // calculate error
            const uchar* udata_p = outputs.ptr(idx);
            const float* udata_f = (const float*)udata_p;
            const double* udata_d = (const double*)udata_p;

            double* gdata = grad1.ptr<double>();
            for( k = 0; k < ovcount; k++ )
            {
                double t = (otype == CV_32F ? (double)udata_f[k] : udata_d[k])*w[k*2] + w[k*2+1] - x[l_count-1][k];
                gdata[k] = t*sweight;
                E += t*t;
            }
            E *= sweight;

            // backward pass, update weights
            for( i = l_count-1; i > 0; i-- )
            {
                int n1 = layer_sizes[i-1], n2 = layer_sizes[i];
                Mat _df(1, n2, CV_64F, &df[i][0]);
                multiply( grad1, _df, grad1 );
                Mat _x(n1+1, 1, CV_64F, &x[i-1][0]);
                x[i-1][n1] = 1.;
                gemm( _x, grad1, params.bpDWScale, dw[i], params.bpMomentScale, dw[i] );
                add( weights[i], dw[i], weights[i] );
                if( i > 1 )
                {
                    Mat grad2(1, n1, CV_64F, buf[i&1]);
                    Mat _w = weights[i].rowRange(0, n1);
                    gemm( grad1, _w, 1, noArray(), 0, grad2, GEMM_2_T );
                    grad1 = grad2;
                }
            }
        }

        iter /= count;
        return iter;
    }