void eng_make_SparseLogical(int *ir, int irlen, int *jc, int jclen, short *list, int len, int m, int n) { mxArray *var = mxCreateSparseLogicalMatrix(m, n, len); std::copy(ir, ir + irlen, mxGetIr(var)); std::copy(jc, jc + jclen, mxGetJc(var)); std::copy(list, list+len, mxGetLogicals(var)); returnHandle(var); }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) { S2Grid S2G; double *xtheta, *xrho; double epsilon; mwSize nx,nytheta,nyrho; buffer ind; int ix, *indx; mxLogical *sr; mwIndex *irs,*jcs; int *iytheta; /* Check for proper number of arguments */ if (nrhs != 7) { mexErrMsgTxt("Seven input arguments required."); } else if (nlhs > 1) { mexErrMsgTxt("Too many output arguments."); } /* get input dimensions */ nx = mxGetM(xtheta_IN) * mxGetN(xtheta_IN); nytheta = mxGetM(ytheta_IN) * mxGetN(ytheta_IN); nyrho = mxGetM(yrho_IN) * mxGetN(yrho_IN); /* Assign pointers to the various parameters */ S2Grid_init(&S2G, mxGetPr(yrho_IN), mxGetPr(ytheta_IN), nytheta, (int*) mxGetData(iytheta_IN), *((double*) mxGetPr(prho_IN))); xtheta = mxGetPr(xtheta_IN); xrho = mxGetPr(xrho_IN); epsilon = *(double*) mxGetPr(e_IN); /* init buffers */ buffer_init(&ind,50*nx); indx = (int*) mxCalloc(nx+1,sizeof(int)); /* find */ for (ix=0;ix<nx;ix++){ indx[ix] = ind.used; S2Grid_find_region(&S2G, xtheta[ix], xrho[ix], epsilon, &ind); } indx[nx] = ind.used; /*printf("found: %d - ",ind.used); print_int(ind.data,ind.used);*/ /* generate sparse output matrix */ plhs[0] = mxCreateSparseLogicalMatrix(nyrho,nx,1+indx[nx]); sr = mxGetLogicals(plhs[0]); /* elements */ irs = mxGetIr(plhs[0]); /* rows - y */ jcs = mxGetJc(plhs[0]); /* columns - x */ for (ix=0;ix<indx[nx];ix++){ sr[ix] = 1; irs[ix] = ind.data[ix]; } for (ix=0;ix<=nx;ix++) jcs[ix] = indx[ix]; /* free memory */ mxFree(indx); buffer_finalize(&ind); S2Grid_finalize(&S2G); }
void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] ) { uint32_t i; double * tmp; uint32_t * lbp; mxArray * lbparray; int verb; if(nrhs < 5 || nrhs > 6) mexErrMsgTxt("Improper number of input arguments.\n\n" "LBP_FEATURES_SPARSE computes pyramid of LBP features for each defined window in input images.\n\n" "Synopsis: \n" " [ Features, sparse_lbp ]= lbppyr_features(Images, imSize, Wins, winSize, height_of_pyramid, verb) \n" "\n" " Input: \n" " Images [(im_H*im_W) x nImages (uint8)]\n" " imSize [2 x 1 (double)] imSize = [im_H im_W]\n" " Wins [4 x nExamples (uint32)] [image_idx; x1; y1;mirror] (1-based)\n" " winSize [2 x 1 (double)] [win_H win_W]\n" " height_of_pyramid [1 x 1 (double)]\n" " verb [1x1] \n" " Output: \n" " Features [nDims x nExamples (sparse logical)]\n"); if(nrhs == 6) verb = (int)mxGetScalar(prhs[5]); else verb = 1; uint8_t * Images = (uint8_t*)mxGetPr(prhs[0]); uint32_t nImages = mxGetN(prhs[0]); tmp = (double*)mxGetPr(prhs[1]); uint32_t im_H = (uint32_t)tmp[0]; uint32_t im_W = (uint32_t)tmp[1]; if(mxGetM(prhs[0]) != im_H*im_W) mexErrMsgTxt("Dimension of Images does not match to im_H*im_W."); uint32_t *Wins = (uint32_t*)mxGetPr(prhs[2]); tmp = (double*)mxGetPr(prhs[3]); uint16_t win_H = (uint16_t)tmp[0]; uint16_t win_W = (uint16_t)tmp[1]; uint16_t nPyramids = (uint32_t)mxGetScalar(prhs[4]); uint32_t nDim = liblbp_pyr_get_dim(win_H,win_W,nPyramids); uint32_t nDimLBP = liblbp_pyr_get_dim(win_H,win_W,nPyramids)/256; uint32_t nData = mxGetN(prhs[2]); if(verb) { mexPrintf("Input data:\n" " # of images : %d\n" " image height : %d\n" " image width : %d\n", nImages, im_H, im_W); mexPrintf("Feature represenation:\n" " base window height : %d\n" " base window width : %d\n" " nPyramids : %d\n" " # of virtual examples : %d\n" " # of features per example : %d\n", win_H, win_W, nPyramids, nData, nDim); } //lbp = (uint32_t*)mxGetPr(mxCreateNumericMatrix(nDimLBP, nData, mxINT32_CLASS, mxREAL)); lbparray = mxCreateNumericMatrix(nDimLBP, nData, mxINT32_CLASS, mxREAL); lbp = (uint32_t*)mxGetPr(lbparray); uint32_t cnt, cnt0, mirror,x,x1,y,y1,idx; uint32_t *win; uint8_t *img_ptr; win = (uint32_t*)mxCalloc(win_H*win_W,sizeof(uint32_t)); if(win == NULL) mexErrMsgTxt("Not enough memory for croped_window."); cnt=0; for(i=0; i < nData; i++) { idx = Wins[INDEX(0,i,4)]-1; x1 = Wins[INDEX(1,i,4)]-1; y1 = Wins[INDEX(2,i,4)]-1; mirror = Wins[INDEX(3,i,4)]; img_ptr = &Images[idx*im_H*im_W]; cnt0 = 0; if(mirror==0) { for(x=x1; x < x1+win_W; x++) for(y=y1; y < y1+win_H; y++) { win[cnt0++] = img_ptr[INDEX(y,x,im_H)]; } } else { for(x=x1+win_W-1; x >= x1; x--) for(y=y1; y < y1+win_H; y++) { win[cnt0++] = img_ptr[INDEX(y,x,im_H)]; } } liblbp_pyr_features_sparse(&lbp[nDimLBP*i], nDimLBP, win, win_H, win_W); } mxFree(win); mwSize nzmax = nDimLBP*nData; plhs[0] = mxCreateSparseLogicalMatrix(nDim, nData, nzmax); mwIndex *ir, *jc; mxLogical *pr = mxGetLogicals(plhs[0]); ir = mxGetIr(plhs[0]); jc = mxGetJc(plhs[0]); for (mwSize i = 0; i < nzmax; ++i) { ir[i] = lbp[i]; pr[i] = 1; } for (mwSize i = 0; i < nData+1; ++i) { jc[i] = i*nDimLBP; } // return also lbp_sparse_indices or destroy it if (nlhs == 2) { plhs[1] = lbparray; } else { mxDestroyArray(lbparray); } return; }
// The gateway mex routine void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* Check for proper number of input and output arguments */ if ((nlhs != 1) || (nrhs != 2)) mexErrMsgTxt("Usage: B = BIADJANCENCY_MATRIX(A,M) OR " "B = BIADJANCENCY_MATRIX(A,M,K).\n"); /* Read input */ double *A = mxGetPr(prhs[0]); // assignment image int X = mxGetM(prhs[0]); // image size X int Y = mxGetN(prhs[0]); // image size Y int M = (int)mxGetPr(prhs[1])[0]; // patch size int K; // number dict patches if (nrhs==3) K = (int)mxGetPr(prhs[2])[0]; else{ // assumes number of dict patches is max(A) K = 0; for (int a=0; a<X*Y; a++) if (A[a]>K) K = A[a]; } /* Compute some useful sizes */ int c = (M-1)/2; // width of boundary having no assignment int n = X*Y; // number of image pixels int m = M*M*K; // number of dict pixels int s = (X-M+1)*(Y-M+1)*M*M; // number image-dict links (elements in B) /* Finding elements of B as row-column indices */ std::vector<ij> bij; bij.reserve(s); int ic,i,j; for (int y=0+c; y<Y-c; y++){ // visiting patches centered around pixels for (int x=0+c; x<X-c; x++){ ic = x+y*X; // central patch pixel for (int dy=-c; dy<=c; dy++){ // visiting pixels around central for (int dx=-c; dx<=c; dx++){ i = (x+dx)+(y+dy)*X; j = (c+dx)+(c+dy)*M+(A[ic]-1)*M*M; bij.push_back(ij(i,j)); } } } } /* Sorting elements in bij columnwise */ std::sort (bij.begin(), bij.end()); /* Placeholder for output */ plhs[0] = mxCreateSparseLogicalMatrix(n,m,s); // output mxArray, sparse logical matrix B if (plhs[0]==NULL) mexErrMsgTxt("Could not allocate enough memory!\n"); /* Access fields of output mxArray via pointers */ mwIndex *ir = mxGetIr(plhs[0]); // row index (0 indexed) mwIndex *jc = mxGetJc(plhs[0]); // cumulative number of elements per column mxLogical *pr = mxGetLogicals(plhs[0]); // element values (will be all true) /* Converting row-column indices into row-cumulative column */ int k = 0; // for visiting elements of bij jc[0] = 0; // first element of cumulative sum is 0 for (int bc=0; bc<m; bc++){ // all columns of B jc[bc+1] = jc[bc]; while (k<bij.size() && bij[k].j==bc){ jc[bc+1]++; ir[k] = bij[k].i; pr[k] = true; k++; } } }