結果大概是這樣:
原本的圖片如果是這樣的:
那麼提取人臉之後是這樣的:
然後計算LBP圖像是這樣的:
因爲項目中設計到服務器信息, 沒有託管代碼, 核心的LBP處理代碼如下:
public int[] getLbpHistogram(Mat face){
Mat lbpface = new Mat(new Size(198,198), CvType.CV_32SC3);
int width = face.width();
int height = face.height();
//initialize the lbp histogram
int[] lbpHistogram = new int[256];
Arrays.fill(lbpHistogram,0);
for(int i = 0; i<width; i++){
for(int j = 0;j<height; j++){
//boundary solution
if(i==0||j==0||i==width-1||j==height-1){
lbpface.put(i,j,new int[]{0,0,0});
continue;
}
int l = 0;
if(face.get(i-1,j-1)[0]>face.get(i, j)[0]) l+=1<<7;
if(face.get(i-1,j)[0]>face.get(i, j)[0]) l+=1<<6;
if(face.get(i-1,j+1)[0]>face.get(i, j)[0]) l+=1<<5;
if(face.get(i,j+1)[0]>face.get(i, j)[0]) l+=1<<4;
if(face.get(i+1,j+1)[0]>face.get(i, j)[0]) l+=1<<3;
if(face.get(i+1,j)[0]>face.get(i, j)[0]) l+=1<<2;
if(face.get(i+1,j-1)[0]>face.get(i, j)[0]) l+=1<<1;
if(face.get(i,j-1)[0]>face.get(i, j)[0]) l+=1;
//fill the lbp image
lbpface.put(i, j, new int[]{l,l,l});
//calc the lbp histogram
lbpHistogram[l]++;
}
}
Imgcodecs.imwrite("out.jpg",lbpface);
return lbpHistogram;
}