int LBPFeatures::countSetBits(int code) { int count=0; int v=code; for(count=0;v;count++) { v&=v-1; //clears the LSB } return count; } int LBPFeatures::rightshift(int num, int shift) { return (num >> shift) | ((num << (8 - shift)&0xFF)); } bool LBPFeatures::checkUniform(int code) { int b = rightshift(code,1); //int d = code << 1; int c = code ^ b; //d= code ^d; int count=countSetBits(c); //int count1=countSetBits(d); if (count <=2 ) return true; else return false; } void LBPFeatures::initUniform( int check) { lookup.resize(256); int index=0; for(int i=0;i<=255;i++) { bool status=checkUniform(check); if(status==true) { lookup[i]=index; index++; } else { lookup[i]=59; } } spatialhist Hist; Hist.initHistogram(); } void LBPFeatures::computeuniformlbp(Mat image,Mat &dst) { uchar *ptr=image.data; image.copyTo(dst); uchar *optr=dst.data; int width=image.cols; int height=image.rows; for(int i=1;i<height-1;i++) { for(int j=1;j<width-1;j++) { int center=(uchar)ptr[j+i*width]; unsigned char code=0; //for(int k=7;k>=0;k++) code|=((uchar)ptr[(j-1)+(i-1)*width] >=center)<<7; code|=((uchar)ptr[j+(i-1)*width] >=center)<<6 ; code|=((uchar)ptr[(j+1)+(i-1)*width] >=center)<<5 ; code|=((uchar)ptr[(j+1)+(i)*width] >=center)<<4 ; code|=((uchar)ptr[(j+1)+(i+1)*width] >=center)<<3 ; code|=((uchar)ptr[j+(i+1)*width] >=center)<<2 ; code|=((uchar)ptr[j-1+(i+1)*width] >=center)<<1 ; code|=((uchar)ptr[j-1+(i)*width] >=center)<<0 ; //heck if the code is uniform code //encode only if it is a uniform code else //assign it a number 255 initUniform(code); //lookup.push_back(code); optr[j+i*width]=lookup[code]; } } }
spatialhist::spatialhist(){ sizeb=59; } Mat spatialhist::computeHistogram(Mat& cell) { Mat histogram; histogram= hist.BuildHistogram(cell); return histogram; } void spatialhist::initHistogram() { vector<int> channel; channel.push_back(0); hist.setChannel(channel); vector<int> size; size.push_back(sizeb); hist.setHistSize(size); vector<float> range; range.push_back(0); range.push_back(59); hist.setRange(range); } vector<float> spatialhist::spatialHistogram( const Mat& lbpImage, const Size& grid) { vector<float> histograms; histograms.resize(grid.width*grid.height*sizeb); int width=lbpImage.cols/grid.width; int height=lbpImage.rows/grid.height; int cnt=0; //#pragma omp parallel for for(int i=0;i<grid.height;i++) { for(int j=0;j<grid.width;j++) { Mat cell=lbpImage(Rect(j*width,i*height,width,height)); Mat cell_hist=computeHistogram(cell); histogramsimage.push_back(cell_hist); //imshow("FFF",cell_hist); Mat tmp_feature; Mat feature =cell_hist.reshape(1,1); feature.convertTo(tmp_feature,CV_32FC1); float * ptr=tmp_feature.ptr<float>(0); for(int k=0;k<tmp_feature.cols-1;k++) { if(ptr[k]==0) ptr[k]=1.0/sizeb; histograms[(cnt*sizeb)+k]=ptr[k]; // cerr << ptr[k] << endl; } cnt++; } } histogramsimage.reshape(1,1); return histograms; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)