void EdgeBoxesImpl::getBoundingBoxes(InputArray edge_map, InputArray orientation_map, std::vector<Rect> &boxes) { CV_Assert(edge_map.depth() == CV_32F); CV_Assert(orientation_map.depth() == CV_32F); Mat E = edge_map.getMat().t(); Mat O = orientation_map.getMat().t(); h = E.cols; w = E.rows; clusterEdges(E, O); prepDataStructs(E); Boxes b; scoreAllBoxes(b); boxesNms(b, _beta, _eta, _maxBoxes); // create output boxes int n = (int) b.size(); boxes.resize(n); for(int i=0; i < n; i++) { boxes[i] = Rect((int)b[i].x + 1, (int)b[i].y + 1, (int)b[i].w, (int)b[i].h); } }
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 Problem::make_boxes_mx(Boxes boxes, mxArray *mx) { for (int i = 0; i < boxes.size(); ++i) { // Creates a matrix(boxes.size(), 2) and stores the interval inside the box mxArray *box = mxCreateDoubleMatrix(boxes[i].size(), 2, mxREAL); make_interval_mx(boxes[i], mxGetPr(box)); mxSetCell(mx, i, mxDuplicateArray(box)); }; }
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); }
void get_edge_boxes(Mat &im, vector<vector<float> > &bbs) { Mat ime, grad_ori, ime_t, grad_ori_t; // setup and run EdgeBoxGenerator EdgeBoxGenerator edgeBoxGen; Boxes boxes; edgeBoxGen._alpha = 0.65; edgeBoxGen._beta = 0.75; edgeBoxGen._eta = 1; edgeBoxGen._minScore = 0.01; edgeBoxGen._maxBoxes = 10000; edgeBoxGen._edgeMinMag = 0.1; edgeBoxGen._edgeMergeThr = 0.5; edgeBoxGen._clusterMinMag = 0.5; edgeBoxGen._maxAspectRatio = 3; edgeBoxGen._minBoxArea = 1000; edgeBoxGen._gamma = 2; edgeBoxGen._kappa = 1.5; double t = (double)getTickCount(); edge_detect(im, ime, grad_ori, string("/home/samarth/Documents/MATLAB/edges/cpp/external/gop_1.3/data/sf.dat")); //vis_matrix(ime, "E"); transpose(ime, ime_t); transpose(grad_ori, grad_ori_t); if(!(ime_t.isContinuous() && grad_ori_t.isContinuous())) { cout << "Matrices are not continuous, hence the Array struct will not work" << endl; } arrayf E; E._x = ime_t.ptr<float>(); arrayf O; O._x = grad_ori_t.ptr<float>(); Size sz = ime.size(); int h = sz.height; O._h=E._h=h; int w = sz.width; O._w=E._w=w; arrayf V; edgeBoxGen.generate( boxes, E, O, V ); t = ((double)getTickCount() - t)/getTickFrequency(); cout << "Generated boxes, t = " << t*1000 << " ms" << endl; // create output bbs int n = (int) boxes.size(); //cout << "Found " << n << " boxes" << endl; bbs.resize(n, vector<float>(5, 0)); for(int i=0; i<n; i++) { bbs[i][0] = (float) boxes[i].c+1; bbs[i][1] = (float) boxes[i].r+1; bbs[i][2] = (float) boxes[i].w; bbs[i][3] = (float) boxes[i].h; bbs[i][4] = boxes[i].s; } }
void edgeBoxes(float*EE, float* OO, int h, int w, float _alpha, float _beta, float _minScore, float _maxBoxes, float _edgeMinMag, float _edgeMergeThr, float _clusterMinMag, float _maxAspectRatio, float _minBoxArea, float _gamma, float _kappa, vector<bb>& bbs) { arrayf E; E._x = EE; arrayf O; O._x = OO; O._h = E._h = h; O._w = E._w = w; // optionally create memory for visualization // setup and run EdgeBoxGenerator EdgeBoxGenerator edgeBoxGen; Boxes boxes; edgeBoxGen._alpha = float(_alpha); edgeBoxGen._beta = float(_beta); edgeBoxGen._minScore = float(_minScore); edgeBoxGen._maxBoxes = int(_maxBoxes); edgeBoxGen._edgeMinMag = float(_edgeMinMag); edgeBoxGen._edgeMergeThr = float(_edgeMergeThr); edgeBoxGen._clusterMinMag = float(_clusterMinMag); edgeBoxGen._maxAspectRatio = float(_maxAspectRatio); edgeBoxGen._minBoxArea = float(_minBoxArea); edgeBoxGen._gamma = float(_gamma); edgeBoxGen._kappa = float(_kappa); arrayf V; V._h = h; V._w = w; edgeBoxGen.generate(boxes, E, O, V); // create output bbs and output to Matlab int n = (int)boxes.size(); bb tmp_bb; for (int i = 0; i < n; i++) { //tmp_bb.coord[0] = (float)boxes[i].c + 1; //tmp_bb.coord[1] = (float)boxes[i].r + 1; //tmp_bb.coord[2] = (float)boxes[i].w + 1; //tmp_bb.coord[3] = (float)boxes[i].h + 1; tmp_bb.coord[0] = (float)boxes[i].c; tmp_bb.coord[1] = (float)boxes[i].r; tmp_bb.coord[2] = (float)boxes[i].w; tmp_bb.coord[3] = (float)boxes[i].h; tmp_bb.score = boxes[i].s; bbs.push_back(tmp_bb); } }
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); }
// Matlab entry point: bbs = mex( E, O, prm1, prm2, ... ) void mexFunction( int nl, mxArray *pl[], int nr, const mxArray *pr[] ) { // check and get inputs if(nr != 13) mexErrMsgTxt("Thirteen inputs required."); if(nl > 2) mexErrMsgTxt("At most two outputs expected."); if(mxGetClassID(pr[0])!=mxSINGLE_CLASS) mexErrMsgTxt("E must be a float*"); if(mxGetClassID(pr[1])!=mxSINGLE_CLASS) mexErrMsgTxt("O must be a float*"); arrayf E; E._x = (float*) mxGetData(pr[0]); arrayf O; O._x = (float*) mxGetData(pr[1]); int h = (int) mxGetM(pr[0]); O._h=E._h=h; int w = (int) mxGetN(pr[0]); O._w=E._w=w; // optionally create memory for visualization arrayf V; if( nl>1 ) { const int ds[3] = {h,w,3}; pl[1] = mxCreateNumericArray(3,ds,mxSINGLE_CLASS,mxREAL); V._x = (float*) mxGetData(pl[1]); V._h=h; V._w=w; } // setup and run EdgeBoxGenerator EdgeBoxGenerator edgeBoxGen; Boxes boxes; edgeBoxGen._alpha = float(mxGetScalar(pr[2])); edgeBoxGen._beta = float(mxGetScalar(pr[3])); edgeBoxGen._minScore = float(mxGetScalar(pr[4])); edgeBoxGen._maxBoxes = int(mxGetScalar(pr[5])); edgeBoxGen._edgeMinMag = float(mxGetScalar(pr[6])); edgeBoxGen._edgeMergeThr = float(mxGetScalar(pr[7])); edgeBoxGen._clusterMinMag = float(mxGetScalar(pr[8])); edgeBoxGen._maxAspectRatio = float(mxGetScalar(pr[9])); edgeBoxGen._minBoxArea = float(mxGetScalar(pr[10])); edgeBoxGen._gamma = float(mxGetScalar(pr[11])); edgeBoxGen._kappa = float(mxGetScalar(pr[12])); edgeBoxGen.generate( boxes, E, O, V ); // create output bbs and output to Matlab int n = (int) boxes.size(); pl[0] = mxCreateNumericMatrix(n,5,mxSINGLE_CLASS,mxREAL); float *bbs = (float*) mxGetData(pl[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].w; bbs[ i + 3*n ] = (float) boxes[i].h; bbs[ i + 4*n ] = boxes[i].s; } }
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); }