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 Messenger::read(std::vector<Boxes> &answer, const char type) { // If kodiak has not produced an answer yet, this thread goes to sleep for 1000miliseconds. // Each iteration the file is read again and the condition checked. while (!kodiak_messages.k_done()) { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); std::fstream input(f_name.c_str(), std::ios::in | std::ios::binary); if (!input) { std::cout << f_name << ": File not found. Creating a new file." << std::endl; } else if (!kodiak_messages.ParseFromIstream(&input)) { std::cerr << "Failed to parse messages." << std::endl; } } // b stands for bifurcation. // Read in the boxes from respective variable in the shared file. if (type == 'b') { for (int i = 0; i < kodiak_messages.bifans_size(); ++i) { const kodiak::Bif_Ans &bifans = kodiak_messages.bifans(i); for (int o = 0; o < bifans.boxtype_size(); ++o) { Boxes type; const kodiak::Box_Type boxtype = bifans.boxtype(o); for (int p = 0; p < boxtype.box_size(); ++p) { Box b; const kodiak::Box box = boxtype.box(p); for (int y = 0; y < box.interval_size(); ++y) { const kodiak::Interval interval = box.interval(y); b.push_back(std::make_pair(interval.lb(), interval.ub())); } type.push_back(b); } answer.push_back(type); } } } };
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 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; } }
// 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 != 15) mexErrMsgTxt("Fourteen 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._eta = float(mxGetScalar(pr[4])); edgeBoxGen._minScore = float(mxGetScalar(pr[5])); edgeBoxGen._maxBoxes = int(mxGetScalar(pr[6])); edgeBoxGen._edgeMinMag = float(mxGetScalar(pr[7])); edgeBoxGen._edgeMergeThr = float(mxGetScalar(pr[8])); edgeBoxGen._clusterMinMag = float(mxGetScalar(pr[9])); edgeBoxGen._maxAspectRatio = float(mxGetScalar(pr[10])); edgeBoxGen._minBoxArea = float(mxGetScalar(pr[11])); edgeBoxGen._gamma = float(mxGetScalar(pr[12])); edgeBoxGen._kappa = float(mxGetScalar(pr[13])); ConstMatlabMultiArray<double> bbox(pr[14]); std::size_t n_bbox=bbox.shape()[0]; for(int ii=0;ii<n_bbox;++ii) { Box b; b.c=bbox[ii][0]-1; b.r=bbox[ii][1]-1; b.w=bbox[ii][2]-bbox[ii][0]; b.h=bbox[ii][3]-bbox[ii][1]; if(b.w<=1 | b.h<=1) continue; boxes.push_back(b); } 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; } }