vector<vector<float> > Encoder::extractMultiLBP(Mat img, Mat landmarks, int level){ //Mat img = imread(img_path, CV_LOAD_IMAGE_COLOR); VlLbp * lbp = vl_lbp_new (VlLbpUniform, VL_TRUE) ; int dimensionx = patchSize / cellSize; int dimensiony = patchSize / cellSize; int dimensionc = vl_lbp_get_dimension(lbp) ; vector<vector<float> > ret; float* code = new float[dimensionx*dimensiony*dimensionc]; //cout<<"dim: "<<dimensionx<<" "<<dimensiony<<" "<<dimensionc <<endl; for (int l = 0; l < level; l++){ int tmpcellSize = cellSize - l; int tmppatchSize = tmpcellSize*dimensionx; for (unsigned int i = 0; i < landmarks.cols; i++){ if (landmarks.at<float>(0, i) > patchSize/2 && landmarks.at<float>(1, i) > patchSize/2 && landmarks.at<float>(0, i) + patchSize/2 < img.cols && landmarks.at<float>(1, i) + patchSize/2 < img.rows){ Mat roi(img, Rect(landmarks.at<float>(0, i) - tmppatchSize/2 , landmarks.at<float>(1, i) - tmppatchSize/2, tmppatchSize, tmppatchSize)); vector<float> data; if (lbp == NULL) { cout<<"fail to init LBP detector"<<endl; return ret; } for (int j = 0; j < roi.cols; j++){ for (int k = 0; k < roi.rows; k++){ data.push_back((float)roi.at<unsigned char>(k, j)/255); } } //float* features = new float[dimensionx * dimensiony * dimensionc]; //cout<<"code size: x: "<<dimensionx<<" y: "<<dimensiony<<" c: "<<dimensionc<<endl; for (int j = 0; j < dimensionx*dimensiony*dimensionc; j++){ code[j] = 0; } vl_lbp_process(lbp, code, &data[0], tmppatchSize, tmppatchSize, tmpcellSize); vector<float> lbpCode; for (int j = 0; j < dimensionx*dimensiony*dimensionc; j++){ //cout<<code[j]<<" "; lbpCode.push_back(code[j]); } ret.push_back(lbpCode); //cout<<"feature "<<i/2<<" size: "<<ret.size()<<endl; } else{ cout<<"Patch out of bound: "<<landmarks.at<float>(0, i)<<" "<<landmarks.at<float>(1, i)<<endl; exit(1); } } } delete[] code; vl_lbp_delete(lbp); return ret; }
int LBP_ADAPTER::getLbpCellDim() const { if (!m_lbp_model) { cerr << "Please call extractLbpFeature() first" << endl; exit(-1); } return vl_lbp_get_dimension(m_lbp_model); }
void mexFunction(int nout, mxArray *out[], int nin, const mxArray *in[]) { float * image ; vl_size width, height ; vl_size cellSize = 16 ; enum {IN_I = 0, IN_CELLSIZE} ; enum {OUT_FEATURES = 0} ; /* ----------------------------------------------------------------- * Check the arguments * -------------------------------------------------------------- */ if (nin > 2) { vlmxError(vlmxErrTooManyInputArguments, NULL) ; } if (nin < 2) { vlmxError(vlmxErrNotEnoughInputArguments, NULL) ; } if (nout > 1) { vlmxError(vlmxErrTooManyOutputArguments, NULL) ; } if (! mxIsNumeric(IN(I)) || ! vlmxIsReal(IN(I)) || ! vlmxIsMatrix(IN(I), -1, -1)) { vlmxError(vlmxErrInvalidArgument, "I is not a numeric matrix.") ; } if (mxGetClassID(IN(I)) != mxSINGLE_CLASS) { vlmxError(vlmxErrInvalidArgument, "I is not of class SINGLE.") ; } if (! vlmxIsPlainScalar(IN(CELLSIZE))) { vlmxError(vlmxErrInvalidArgument, "CELLSIZE is not a plain scalar.") ; } if (mxGetScalar(IN(CELLSIZE)) < 1.0) { vlmxError(vlmxErrInvalidArgument, "CELLSIZE is less than 1.") ; } cellSize = (vl_size) mxGetScalar(IN(CELLSIZE)) ; image = mxGetData(IN(I)) ; width = mxGetN(IN(I)) ; height = mxGetM(IN(I)) ; /* do job */ { /* recall that MATLAB images are transposed */ mwSize dimensions [3] ; /* get LBP object */ VlLbp * lbp = vl_lbp_new (VlLbpUniform, VL_TRUE) ; if (lbp == NULL) { vlmxError(vlmxErrAlloc, NULL) ; } /* get output buffer */ dimensions[0] = height / cellSize ; dimensions[1] = width / cellSize ; dimensions[2] = vl_lbp_get_dimension(lbp) ; OUT(FEATURES) = mxCreateNumericArray(3, dimensions, mxSINGLE_CLASS, mxREAL) ; vl_lbp_process(lbp, mxGetData(OUT(FEATURES)), image, height, width, cellSize) ; vl_lbp_delete(lbp) ; } }
vector<vector<float> > Encoder::extractTunedLBP(Mat normMat, Mat landmarks){ //40x20x2, 100x40, 40x50, 60x40 VlLbp * lbp = vl_lbp_new (VlLbpUniform, VL_TRUE) ; int binSize = vl_lbp_get_dimension(lbp) ; vector<vector<float> > ret; //hard code max LBP size float* code = new float[50 * 50 * binSize]; int tunedCellSize = 5; int tunedCols, tunedRows; vector<float> lbpCode; for (int l = 0; l <= 2; l++){ for (int i = 0; i < landmarks.cols; i++){ switch (i){ case 0: case 1: case 2: case 3: tunedCols = 30 + 10*l; tunedRows = 10 + 10*l; break; case 4: tunedCols = 70 + 20*l; tunedRows = 30 + 10*l; break; case 5: tunedCols = 30 + 10*l; tunedRows = 40 + 20*l; break; case 6: tunedCols = 40 + 20*l; tunedRows = 30 + 10*l; break; case 7: case 8: tunedCols = 30 + 10*l; tunedRows = 40 + 20*l; break; case 9: tunedCols = 40 + 10*l; tunedRows = 20; break; case 10: tunedCols = 60 + 20*l; tunedRows = 30; break; case 11: tunedCols = 40 + 10*l; tunedRows = 20 + 10*l; break; /* case 0: case 1: case 2: case 3: tunedCols = 40 - 10*l; tunedRows = 20 - 10*l; break; case 4: tunedCols = 100 - 20*l; tunedRows = 40 - 10*l; break; case 5: tunedCols = 40 - 10*l; tunedRows = 60 - 20*l; break; case 6: tunedCols = 60 - 20*l; tunedRows = 40 - 10*l; break; case 7: case 8: tunedCols = 40 - 10*l; tunedRows = 60 - 20*l; break; case 9: tunedCols = 60 - 20*l; tunedRows = 20; break; */ default: cout<<"Wrong landmark size: "<<i<<endl; exit(1); } if (landmarks.at<float>(0, i) > tunedCols/2 && landmarks.at<float>(1, i) > tunedRows/2 && landmarks.at<float>(0, i) + tunedCols/2 < normMat.cols && landmarks.at<float>(1, i) + tunedRows/2 < normMat.rows){ Mat roi(normMat, Rect(landmarks.at<float>(0, i) - tunedCols/2 , landmarks.at<float>(1, i) - tunedRows/2, tunedCols, tunedRows)); vector<float> data; if (lbp == NULL) { cout<<"fail to init LBP detector"<<endl; return ret; } for (int j = 0; j < roi.cols; j++){ for (int k = 0; k < roi.rows; k++){ data.push_back((float)roi.at<unsigned char>(k, j)/255); } } for (int j = 0; j < (tunedCols/tunedCellSize) * (tunedRows/tunedCellSize) * binSize; j++){ code[j] = 0; } vl_lbp_process(lbp, code, &data[0], tunedCols, tunedRows, tunedCellSize); for (int j = 0; j < (tunedCols/tunedCellSize) * (tunedRows/tunedCellSize) * binSize; j++){ //cout<<code[j]<<" "; lbpCode.push_back(code[j]); } //if (i != 0 && i != 2){ ret.push_back(lbpCode); lbpCode.clear(); //} //cout<<"feature "<<i/2<<" size: "<<ret.size()<<endl; } else{ cout<<"Patch out of bound: "<<landmarks.at<float>(0, i)<<" "<<landmarks.at<float>(1, i)<<endl; cout<<"Cols: "<<tunedCols<<" Rows: "<<tunedRows<<endl; exit(1); } } } delete[] code; vl_lbp_delete(lbp); return ret; }