结果大概是这样:
原本的图片如果是这样的:
那么提取人脸之后是这样的:
然后计算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;
}