static Mat spatial_histogram(InputArray _src, int numPatterns, int grid_x, int grid_y, bool /*normed*/) { Mat src = _src.getMat(); // calculate LBP patch size int width = src.cols/grid_x; int height = src.rows/grid_y; // allocate memory for the spatial histogram Mat result = Mat::zeros(grid_x * grid_y, numPatterns, CV_32FC1); // return matrix with zeros if no data was given if(src.empty()) return result.reshape(1,1); // initial result_row int resultRowIdx = 0; // iterate through grid for(int i = 0; i < grid_y; i++) { for(int j = 0; j < grid_x; j++) { Mat src_cell = Mat(src, Range(i*height,(i+1)*height), Range(j*width,(j+1)*width)); Mat cell_hist = histc(src_cell, 0, (numPatterns-1), true); // copy to the result matrix Mat result_row = result.row(resultRowIdx); cell_hist.reshape(1,1).convertTo(result_row, CV_32FC1); // increase row count in result matrix resultRowIdx++; } } // return result as reshaped feature vector return result.reshape(1,1); }
vector<string> anagrams (vector<string> &strs) { vector<string> result; map<vector<int>, string> ec; vector<int> histc(26, 0); set<string> init; int i = 0; for (string &s : strs){ fill (histc.begin(), histc.end(), 0); for (char &c : s){ histc[c - 'a']++; } if (ec.count(histc) > 0) { init.insert(ec[histc]); result.push_back(s); } else{ ec[histc] = s; } i++; } for (string s : init){ result.push_back(s); } return result; }
void expectedArmaHistc() { cout << "- Compute expectedArmaHistc() ... "; save<uword>("Arma.histc", histc(_genRowVec, _monRowVec)); cout << "done." << endl; }
void kde(double *data, int length, int n ,double dataMIN, double dataMAX, double **out_density, double **out_x, double *bw) { XML_IN; /* * function [bandwidth,density,xmesh,cdf]=kde(data,n,dataMIN,dataMAX) * Reference: * Kernel density estimation via diffusion * Z. I. Botev, J. F. Grotowski, and D. P. Kroese (2010) * Annals of Statistics, Volume 38, Number 5, pages 2916-2957. */ /* if n is not supplied switch to the default n = pow( 2 , 14 ); */ n = pow( 2 , ceil(log2(n)) ); // round up n to the next power of 2; double tol=1e-6; if ( (fabs(dataMIN+1)<tol) && (fabs(dataMAX+1)<tol) ) { printf("using automatic extrema determination\n"); //define the default interval [MIN,MAX] double maximum,minimum; find_max_min_array_doubles(data,length,&maximum,&minimum); double Range=maximum-minimum; dataMIN=minimum-Range/10.0; dataMAX=maximum+Range/10.0; printf("min: %g max: %g\n",dataMIN,dataMAX); } /*set up the grid over which the density estimate is computed;*/ double R=dataMAX-dataMIN; double dx=R/(n-1); double* xmesh=(double*)malloc(n*sizeof(*xmesh)); for (int i=0; i<n; i++ ) xmesh[i] = dataMIN+i*dx; double N = length; //double N=length(unique(data)); //qsort(data, length, sizeof(double), compare_doubles); N=256; //FIXME: we have emulate the unique matlab function. /*bin the data uniformly using the grid defined above;*/ double initial_data[n]; histc(data, length, xmesh, n , initial_data); double sum_initial_data = 0; for(int i=0;i<n;i++){ initial_data[i]=initial_data[i]/N; sum_initial_data+=initial_data[i]; } for(int i=0;i<n;i++) initial_data[i]=initial_data[i]/sum_initial_data; double a[n]; kde_dct_fftw(initial_data,n,a); // discrete cosine transform of initial data /*now compute the optimal bandwidth^2 using the referenced method*/ double It[n-1]; for (int i=0;i<n-1;i++) It[i]=pow(i+1,2.0); double a2[n-1]; for(int i=0;i<n-1;i++) a2[i] = pow(a[i+1]/2.0,2.0); double t_star=0; /*use fzero to solve the equation t=zeta*gamma^[5](t)*/ /* try t_star=fzero(@(t)fixed_point(t,N,I,a2),[0,.1]) catch t_star=.28*N^(-2/5); end */ //test fixed point values double t=0; double tt; tt=fixed_point(0.01,N,It,a2,n); printf("tt: %g\n",tt); tt=fixed_point(0.0,N,It,a2,n); printf("tt: %g\n",tt); tt=fixed_point(0.1,N,It,a2,n); printf("tt: %g\n",tt); int status=fzero(&t_star,N,It,a2,n); printf("t_star: %g\n",t_star); //t_star=.28*pow(N,-2.0/5.0); //printf("t_star: %g\n",t_star); /*smooth the discrete cosine transform of initial data using t_star*/ double a_t[n]; for(int i=0;i<n;i++) a_t[i]=a[i]*exp(-pow(i*M_PI,2.0)*t_star/2.0); double *density=(double* )malloc(n*sizeof(*density)); kde_idct_fftw(a_t,n,density); for(int i=0;i<n;i++) density[i]/=R*n*2; double bandwidth=sqrt(t_star)*R; printf("bandwidth: %g\n",bandwidth); if (verbose>=2 || verbose<=3) { int range[2]={0,128}; print_vec(xmesh,"xmesh",0,n); print_vec(data,"data",0,length); print_vec(initial_data,"initial_data",0,n); print_vec(a,"a",0,n); print_vec(a_t,"a_t",0,n); print_vec(a2,"a_2",0,n-1); print_vec(density,"density",0,n); } if (verbose>=3) { array_write_ascii(xmesh,n,"xmesh.txt"); array_write_ascii(initial_data, n, "initial_data.txt"); array_write_ascii(data, length, "data.txt"); array_write_ascii(density, n, "density.txt"); array_write_ascii(a_t, n, "a_t.txt"); array_write_ascii(a2, n-1, "a_2.txt"); } //prepare output *bw=bandwidth; if(!(*out_density)) *out_density=density; if(!(*out_x)) *out_x=xmesh; XML_OUT; }
// // Arguments : const emxArray_real_T *Y // double no[32] // double xo[32] // Return Type : void // void hist(const emxArray_real_T *Y, double no[32], double xo[32]) { int k; int b_Y[1]; emxArray_real_T c_Y; double maxy; double miny; double edges[33]; int exponent; int d_Y[1]; double nn[33]; k = Y->size[0]; if (k == 0) { for (k = 0; k < 32; k++) { xo[k] = 1.0 + (double)k; no[k] = 0.0; } } else { b_Y[0] = Y->size[0]; c_Y = *Y; c_Y.size = (int *)&b_Y; c_Y.numDimensions = 1; MinAndMaxNoNonFinites(&c_Y, &miny, &maxy); if (miny == maxy) { miny = (miny - 16.0) - 0.5; maxy = (maxy + 16.0) - 0.5; } linspace(miny, maxy, edges); miny = (edges[1] - edges[0]) / 2.0; for (k = 0; k < 32; k++) { xo[k] = edges[k] + miny; } edges[0] = rtMinusInf; edges[32] = rtInf; for (k = 0; k < 31; k++) { miny = std::abs(edges[k + 1]); if ((!rtIsInf(miny)) && (!rtIsNaN(miny))) { if (miny <= 2.2250738585072014E-308) { miny = 4.94065645841247E-324; } else { frexp(miny, &exponent); miny = std::ldexp(1.0, exponent - 53); } } else { miny = rtNaN; } edges[k + 1] += miny; } d_Y[0] = Y->size[0]; c_Y = *Y; c_Y.size = (int *)&d_Y; c_Y.numDimensions = 1; histc(&c_Y, edges, nn); memcpy(&no[0], &nn[0], sizeof(double) << 5); no[31] += nn[32]; } }