void EdgeBoxGenerator::scoreAllBoxes( Boxes &boxes )
{
  // get list of all boxes roughly distributed in grid
  boxes.resize(0); int arRad, scNum; float minSize=sqrt(_minBoxArea);
  arRad = int(log(_maxAspectRatio)/log(_arStep*_arStep));
  scNum = int(ceil(log(std::max(w,h)/minSize)/log(_scStep)));
  for( int s=0; s<scNum; s++ ) {
    int a, r, c, bh, bw, kr, kc, bId=-1; float ar, sc;
    for( a=0; a<2*arRad+1; a++ ) {
      ar=pow(_arStep,float(a-arRad)); sc=minSize*pow(_scStep,float(s));
      bh=int(sc/ar); kr=std::max(2,int(bh*_rcStepRatio));
      bw=int(sc*ar); kc=std::max(2,int(bw*_rcStepRatio));
      for( c=0; c<w-bw+kc; c+=kc ) for( r=0; r<h-bh+kr; r+=kr ) {
        Box b; b.r=r; b.c=c; b.h=bh; b.w=bw; boxes.push_back(b);
      }
    }
  }

  // score all boxes, refine top candidates, perform nms
  int i, k=0, m = int(boxes.size());
  for( i=0; i<m; i++ ) {
    scoreBox(boxes[i]);
    if( !boxes[i].s ) continue; k++;
    refineBox(boxes[i]);
  }
  sort(boxes.rbegin(),boxes.rend(),boxesCompare);
  boxes.resize(k); boxesNms(boxes,_beta,_eta,_maxBoxes);
}
void EdgeBoxesImpl::scoreAllBoxes(Boxes &boxes)
{
    // get list of all boxes roughly distributed in grid
    boxes.resize(0);
    int ayRad, sxNum;
    float minSize = sqrt(_minBoxArea);
    ayRad = (int)(log(_maxAspectRatio) / log(_ayStep * _ayStep));
    sxNum = (int)(ceil(log(max(w, h) / minSize) / log(_sxStep)));

    for (int s = 0; s < sxNum; s++)
    {
        int a, y, x, bh, bw, ky, kx = -1;
        float ay, sx;
        for (a = 0; a < 2 * ayRad + 1; a++)
        {
            ay = pow(_ayStep, float(a - ayRad));
            sx = minSize * pow(_sxStep, float(s));
            bh = (int)(sx / ay);
            ky = max(2, (int)(bh * _xyStepRatio));
            bw = (int)(sx * ay);
            kx = max(2, (int)(bw * _xyStepRatio));
            for (x = 0; x < w - bw + kx; x += kx)
            {
                for (y = 0; y < h - bh + ky; y += ky)
                {
                    Box b;
                    b.y = y;
                    b.x = x;
                    b.h = bh;
                    b.w = bw;
                    boxes.push_back(b);
                }
            }
        }
    }

    // score all boxes, refine top candidates
    int i, k = 0, m = (int)boxes.size();
    for (i = 0; i < m; i++)
    {
        scoreBox(boxes[i]);
        if (!boxes[i].score) continue;
        k++;
        refineBox(boxes[i]);
    }
    sort(boxes.rbegin(), boxes.rend(), boxesCompare);
    boxes.resize(k);
}
void EdgeBoxesImpl::boxesNms(Boxes &boxes, float thr, float eta, int maxBoxes)
{
    sort(boxes.rbegin(), boxes.rend(), boxesCompare);
    if (thr > .99f) return;

    const int nBin = 10000;
    const float step = 1 / thr;
    const float lstep = log(step);

    vector<Boxes> kept;
    kept.resize(nBin + 1);
    int n = (int) boxes.size();
    int i = 0;
    int j, k, b;
    int m = 0;
    int d = 1;

    while (i < n && m < maxBoxes)
    {
        b = boxes[i].w * boxes[i].h;

        bool keep = 1;
        b = clamp((int)(ceil(log(float(b)) / lstep)), d, nBin - d);
        for (j = b - d; j <= b + d; j++)
        {
            for (k = 0; k < (int)kept[j].size(); k++)
            {
                if (keep)
                    keep = boxesOverlap(boxes[i], kept[j][k]) <= thr;
            }
        }

        if (keep)
        {
            kept[b].push_back(boxes[i]);
            m++;
        }

        i++;
        if (keep && eta < 1.0f && thr > .5f)
        {
            thr *= eta;
            d = (int)ceil(log(1.0f / thr) / lstep);
        }
    }

    boxes.resize(m);
    i = 0;
    for (j = 0; j < nBin; j++)
    {
        for (k = 0; k < (int)kept[j].size(); k++)
        {
            boxes[i++] = kept[j][k];
        }
    }
    sort(boxes.rbegin(), boxes.rend(), boxesCompare);
}
Exemple #4
0
void  mexFunction(int nlhs, mxArray *out[], int nrhs, const mxArray *input[]) {
    
    float thr=0.5, eta=1;
    int maxBoxes=100000;
    if(mxGetClassID(input[0])!=mxSINGLE_CLASS) mexErrMsgTxt("first input must be single");
    if (nrhs==4) eta = (float) mxGetScalar(input[3]);
    if (nrhs>=3) maxBoxes = (int) mxGetScalar(input[2]);
    if (nrhs<2) mexErrMsgTxt("Usage: nms_c(boxes, thre, max_nbox=Inf, eta=1)");
    
    thr = (float) mxGetScalar(input[1]);    
    float* boxes_array = (float*)mxGetPr( input[0] );
    int nbox = (int) mxGetM(input[0]);  //number of input boxes
    //mexPrintf("nbox: %d, thr: %f \n", nbox, thr);
    
    int x2,y2;
    Boxes boxes;
    boxes.resize(0);
    for(int i=0; i<nbox; i++) {
      Box b;
      b.c = (int)boxes_array[ i + 0*nbox ]-1;
      b.r = (int)boxes_array[ i + 1*nbox ]-1;
      x2  = (int) boxes_array[ i + 2*nbox ]-1;
      y2  = (int) boxes_array[ i + 3*nbox ]-1;
      b.w = (int) x2 - b.c + 1;
      b.h = (int) y2 - b.r + 1;
      b.s = (float) boxes_array[ i + 4*nbox ];
      boxes.push_back(b);
    }
        
    boxesNms(boxes, thr, maxBoxes, eta);
        
    //output
    int n = (int) boxes.size();
    out[0] = mxCreateNumericMatrix(n,5,mxSINGLE_CLASS,mxREAL);
    float *bbs = (float*) mxGetData(out[0]);
    for(int i=0; i<n; i++) {
      bbs[ i + 0*n ] = (float) boxes[i].c+1;
      bbs[ i + 1*n ] = (float) boxes[i].r+1;
      bbs[ i + 2*n ] = (float) (boxes[i].c+boxes[i].w);
      bbs[ i + 3*n ] = (float) (boxes[i].r+boxes[i].h);
      bbs[ i + 4*n ] = boxes[i].s;
    }
    
    
}
void boxesNms( Boxes &boxes, float thr, int maxBoxes )
{
	sort(boxes.rbegin(),boxes.rend(),boxesCompare);
	if( thr>.99 ) return; const int nBin=10000;
	const float step=1/thr, lstep=log(step);
	vector<Boxes> kept; kept.resize(nBin+1);
	int i=0, j, k, n=(int) boxes.size(), m=0, b;
	while( i<n && m<maxBoxes ) {
		b = boxes[i].w*boxes[i].h; bool keep=1;
		b = clamp(int(ceil(log(float(b))/lstep)),1,nBin-1);
		for( j=b-1; j<=b+1; j++ )
			for( k=0; k<(int)kept[j].size(); k++ ) if( keep )
				keep = boxesOverlap( boxes[i], kept[j][k] ) <= thr;
		if(keep) { kept[b].push_back(boxes[i]); m++; } i++;
	}
	boxes.resize(m); i=0;
	for( j=0; j<nBin; j++ )
		for( k=0; k<(int)kept[j].size(); k++ )
			boxes[i++]=kept[j][k];
	sort(boxes.rbegin(),boxes.rend(),boxesCompare);
}