Android 位图到缓冲图像
有没有一种简单的方法将位图图像数据类型转换为 BufferedImage?
我需要将一个缩放到 224*224 的位图图像转换为 BufferedImage,以便在两个 BufferedImage 之间进行像素比较。
我一直在尝试开发一个简单的面部识别类,它将获取 2 个位图图像(从 Android 相机获取)并使用本地二进制模式识别算法对它们进行比较。图像比较源码:
import java.awt.image.*;
import java.awt.color.ColorSpace;
public class ImageEncode {
public static boolean facialRecognition(BufferedImage i, BufferedImage i2) {
int currentPixelValue, newPixelValue;
int[][] imageArray = new int[224][224], imageArray2 = new int[224][224], lbpArray = new int[224][224], lbpArray2 = new int[224][224], histogram = new int[9][256], histogram2 = new int[9][256];
//input pictures, resize to 224x224
BufferedImage image = i;
BufferedImage image2 = i2;
//convert to gray scale
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
image = op.filter(image, null);
image2=op.filter(image2, null);
//gaussian filter
Kernel kernel = new Kernel(3,3,
new float[] {
1f/9f, 1f/9f, 1f/9f,
1f/9f, 1f/9f, 1f/9f,
1f/9f, 1f/9f, 1f/9f});
BufferedImageOp op2 = new ConvolveOp(kernel);
image = op2.filter(image, null);
image2= op2.filter(image2, null);
//convert images to pixel value array
for(int row=0; row<=223; row++){
for(int col=0; col<=223; col++){
imageArray[row][col]=image.getRGB(row, col);
imageArray2[row][col]=image2.getRGB(row, col);
}
}
//perform lbp calculations
for(int row=1; row<223; row++){
for(int col=1; col<223; col++){
currentPixelValue=imageArray[row][col];
newPixelValue=0;
if(imageArray[row-1][col-1]>currentPixelValue) newPixelValue=newPixelValue+1;
if(imageArray[row-1][col]>currentPixelValue) newPixelValue=newPixelValue+2;
if(imageArray[row-1][col+1]>currentPixelValue) newPixelValue=newPixelValue+4;
if(imageArray[row][col+1]>currentPixelValue) newPixelValue=newPixelValue+8;
if(imageArray[row+1][col+1]>currentPixelValue) newPixelValue=newPixelValue+16;
if(imageArray[row+1][col]>currentPixelValue) newPixelValue=newPixelValue+32;
if(imageArray[row+1][col-1]>currentPixelValue) newPixelValue=newPixelValue+64;
if(imageArray[row][col-1]>currentPixelValue) newPixelValue=newPixelValue+128;
lbpArray[row][col]=newPixelValue;
}
}
for(int row=1; row<223; row++){
for(int col=1; col<223; col++){
currentPixelValue=imageArray2[row][col];
newPixelValue=0;
if(imageArray2[row-1][col-1]>currentPixelValue) newPixelValue=newPixelValue+1;
if(imageArray2[row-1][col]>currentPixelValue) newPixelValue=newPixelValue+2;
if(imageArray2[row-1][col+1]>currentPixelValue) newPixelValue=newPixelValue+4;
if(imageArray2[row][col+1]>currentPixelValue) newPixelValue=newPixelValue+8;
if(imageArray2[row+1][col+1]>currentPixelValue) newPixelValue=newPixelValue+16;
if(imageArray2[row+1][col]>currentPixelValue) newPixelValue=newPixelValue+32;
if(imageArray2[row+1][col-1]>currentPixelValue) newPixelValue=newPixelValue+64;
if(imageArray2[row][col-1]>currentPixelValue) newPixelValue=newPixelValue+128;
lbpArray2[row][col]=newPixelValue;
}
}
//create histograms
for(int row=1; row<=222; row++){
for(int col=1; col<=222; col++){
if(row<75 && col<75) histogram[0][imageArray[row][col]]++;
if(row<75 && col>74 && col<149) histogram[1][imageArray[row][col]]++;
if(row<75 && col>148 && col<223) histogram[2][imageArray[row][col]]++;
if(row>74 && row<149 && col<75) histogram[3][imageArray[row][col]]++;
if(row>74 && row<149 && col>75 && col<149) histogram[4][imageArray[row][col]]++;
if(row>74 && row<149 && col>148 && col<223) histogram[5][imageArray[row][col]]++;
if(row>148 && row<223 && col<75) histogram[6][imageArray[row][col]]++;
if(row>148 && row<223 && col>74 && col<149) histogram[7][imageArray[row][col]]++;
if(row>148 && row<223 && col>148 && col<223) histogram[8][imageArray[row][col]]++;
}
}
for(int row=1; row<=222; row++){
for(int col=1; col<=222; col++){
if(row<75 && col<75) histogram2[0][imageArray2[row][col]]++;
if(row<75 && col>74 && col<149) histogram2[1][imageArray2[row][col]]++;
if(row<75 && col>148 && col<223) histogram2[2][imageArray2[row][col]]++;
if(row>74 && row<149 && col<75) histogram2[3][imageArray2[row][col]]++;
if(row>74 && row<149 && col>75 && col<149) histogram2[4][imageArray2[row][col]]++;
if(row>74 && row<149 && col>148 && col<223) histogram2[5][imageArray2[row][col]]++;
if(row>148 && row<223 && col<75) histogram2[6][imageArray2[row][col]]++;
if(row>148 && row<223 && col>74 && col<149) histogram2[7][imageArray2[row][col]]++;
if(row>148 && row<223 && col>148 && col<223) histogram2[8][imageArray2[row][col]]++;
}
}
//Compare histograms
for(int k=0; k<=8; k++){
for(int j=0; j<=255; j++){
if((((histogram[k][j])*0.1)+histogram[k][j]) < histogram2[k][j] || (histogram[k][j]-((histogram[k][j])*0.1)) > histogram2[k][j]){
return false;
}
}
}
return true;
}
}
Is there a simple way to convert a Bitmap image datatype to a BufferedImage?
I need a bitmap image scaled to 224*224 to be converted into a BufferedImage in order to do pixel comparisons between two BufferedImages.
I've been trying to develop a simple facial recognition class that would take 2 bitmap images (taken from the android camera) and compare them using a local binary pattern recognition algorithm. source code for image comparison:
import java.awt.image.*;
import java.awt.color.ColorSpace;
public class ImageEncode {
public static boolean facialRecognition(BufferedImage i, BufferedImage i2) {
int currentPixelValue, newPixelValue;
int[][] imageArray = new int[224][224], imageArray2 = new int[224][224], lbpArray = new int[224][224], lbpArray2 = new int[224][224], histogram = new int[9][256], histogram2 = new int[9][256];
//input pictures, resize to 224x224
BufferedImage image = i;
BufferedImage image2 = i2;
//convert to gray scale
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
image = op.filter(image, null);
image2=op.filter(image2, null);
//gaussian filter
Kernel kernel = new Kernel(3,3,
new float[] {
1f/9f, 1f/9f, 1f/9f,
1f/9f, 1f/9f, 1f/9f,
1f/9f, 1f/9f, 1f/9f});
BufferedImageOp op2 = new ConvolveOp(kernel);
image = op2.filter(image, null);
image2= op2.filter(image2, null);
//convert images to pixel value array
for(int row=0; row<=223; row++){
for(int col=0; col<=223; col++){
imageArray[row][col]=image.getRGB(row, col);
imageArray2[row][col]=image2.getRGB(row, col);
}
}
//perform lbp calculations
for(int row=1; row<223; row++){
for(int col=1; col<223; col++){
currentPixelValue=imageArray[row][col];
newPixelValue=0;
if(imageArray[row-1][col-1]>currentPixelValue) newPixelValue=newPixelValue+1;
if(imageArray[row-1][col]>currentPixelValue) newPixelValue=newPixelValue+2;
if(imageArray[row-1][col+1]>currentPixelValue) newPixelValue=newPixelValue+4;
if(imageArray[row][col+1]>currentPixelValue) newPixelValue=newPixelValue+8;
if(imageArray[row+1][col+1]>currentPixelValue) newPixelValue=newPixelValue+16;
if(imageArray[row+1][col]>currentPixelValue) newPixelValue=newPixelValue+32;
if(imageArray[row+1][col-1]>currentPixelValue) newPixelValue=newPixelValue+64;
if(imageArray[row][col-1]>currentPixelValue) newPixelValue=newPixelValue+128;
lbpArray[row][col]=newPixelValue;
}
}
for(int row=1; row<223; row++){
for(int col=1; col<223; col++){
currentPixelValue=imageArray2[row][col];
newPixelValue=0;
if(imageArray2[row-1][col-1]>currentPixelValue) newPixelValue=newPixelValue+1;
if(imageArray2[row-1][col]>currentPixelValue) newPixelValue=newPixelValue+2;
if(imageArray2[row-1][col+1]>currentPixelValue) newPixelValue=newPixelValue+4;
if(imageArray2[row][col+1]>currentPixelValue) newPixelValue=newPixelValue+8;
if(imageArray2[row+1][col+1]>currentPixelValue) newPixelValue=newPixelValue+16;
if(imageArray2[row+1][col]>currentPixelValue) newPixelValue=newPixelValue+32;
if(imageArray2[row+1][col-1]>currentPixelValue) newPixelValue=newPixelValue+64;
if(imageArray2[row][col-1]>currentPixelValue) newPixelValue=newPixelValue+128;
lbpArray2[row][col]=newPixelValue;
}
}
//create histograms
for(int row=1; row<=222; row++){
for(int col=1; col<=222; col++){
if(row<75 && col<75) histogram[0][imageArray[row][col]]++;
if(row<75 && col>74 && col<149) histogram[1][imageArray[row][col]]++;
if(row<75 && col>148 && col<223) histogram[2][imageArray[row][col]]++;
if(row>74 && row<149 && col<75) histogram[3][imageArray[row][col]]++;
if(row>74 && row<149 && col>75 && col<149) histogram[4][imageArray[row][col]]++;
if(row>74 && row<149 && col>148 && col<223) histogram[5][imageArray[row][col]]++;
if(row>148 && row<223 && col<75) histogram[6][imageArray[row][col]]++;
if(row>148 && row<223 && col>74 && col<149) histogram[7][imageArray[row][col]]++;
if(row>148 && row<223 && col>148 && col<223) histogram[8][imageArray[row][col]]++;
}
}
for(int row=1; row<=222; row++){
for(int col=1; col<=222; col++){
if(row<75 && col<75) histogram2[0][imageArray2[row][col]]++;
if(row<75 && col>74 && col<149) histogram2[1][imageArray2[row][col]]++;
if(row<75 && col>148 && col<223) histogram2[2][imageArray2[row][col]]++;
if(row>74 && row<149 && col<75) histogram2[3][imageArray2[row][col]]++;
if(row>74 && row<149 && col>75 && col<149) histogram2[4][imageArray2[row][col]]++;
if(row>74 && row<149 && col>148 && col<223) histogram2[5][imageArray2[row][col]]++;
if(row>148 && row<223 && col<75) histogram2[6][imageArray2[row][col]]++;
if(row>148 && row<223 && col>74 && col<149) histogram2[7][imageArray2[row][col]]++;
if(row>148 && row<223 && col>148 && col<223) histogram2[8][imageArray2[row][col]]++;
}
}
//Compare histograms
for(int k=0; k<=8; k++){
for(int j=0; j<=255; j++){
if((((histogram[k][j])*0.1)+histogram[k][j]) < histogram2[k][j] || (histogram[k][j]-((histogram[k][j])*0.1)) > histogram2[k][j]){
return false;
}
}
}
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经多次使用这个类将不同的位图转换为缓冲图像
I've used this class many times to convert diferent bitmaps to bufferedimages