Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid bitmap to bufferedimage
    text
    copied!<p>Is there a simple way to convert a Bitmap image datatype to a BufferedImage?</p> <p>I need a bitmap image scaled to 224*224 to be converted into a BufferedImage in order to do pixel comparisons between two BufferedImages.</p> <p>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:</p> <pre><code>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&lt;=223; row++){ for(int col=0; col&lt;=223; col++){ imageArray[row][col]=image.getRGB(row, col); imageArray2[row][col]=image2.getRGB(row, col); } } //perform lbp calculations for(int row=1; row&lt;223; row++){ for(int col=1; col&lt;223; col++){ currentPixelValue=imageArray[row][col]; newPixelValue=0; if(imageArray[row-1][col-1]&gt;currentPixelValue) newPixelValue=newPixelValue+1; if(imageArray[row-1][col]&gt;currentPixelValue) newPixelValue=newPixelValue+2; if(imageArray[row-1][col+1]&gt;currentPixelValue) newPixelValue=newPixelValue+4; if(imageArray[row][col+1]&gt;currentPixelValue) newPixelValue=newPixelValue+8; if(imageArray[row+1][col+1]&gt;currentPixelValue) newPixelValue=newPixelValue+16; if(imageArray[row+1][col]&gt;currentPixelValue) newPixelValue=newPixelValue+32; if(imageArray[row+1][col-1]&gt;currentPixelValue) newPixelValue=newPixelValue+64; if(imageArray[row][col-1]&gt;currentPixelValue) newPixelValue=newPixelValue+128; lbpArray[row][col]=newPixelValue; } } for(int row=1; row&lt;223; row++){ for(int col=1; col&lt;223; col++){ currentPixelValue=imageArray2[row][col]; newPixelValue=0; if(imageArray2[row-1][col-1]&gt;currentPixelValue) newPixelValue=newPixelValue+1; if(imageArray2[row-1][col]&gt;currentPixelValue) newPixelValue=newPixelValue+2; if(imageArray2[row-1][col+1]&gt;currentPixelValue) newPixelValue=newPixelValue+4; if(imageArray2[row][col+1]&gt;currentPixelValue) newPixelValue=newPixelValue+8; if(imageArray2[row+1][col+1]&gt;currentPixelValue) newPixelValue=newPixelValue+16; if(imageArray2[row+1][col]&gt;currentPixelValue) newPixelValue=newPixelValue+32; if(imageArray2[row+1][col-1]&gt;currentPixelValue) newPixelValue=newPixelValue+64; if(imageArray2[row][col-1]&gt;currentPixelValue) newPixelValue=newPixelValue+128; lbpArray2[row][col]=newPixelValue; } } //create histograms for(int row=1; row&lt;=222; row++){ for(int col=1; col&lt;=222; col++){ if(row&lt;75 &amp;&amp; col&lt;75) histogram[0][imageArray[row][col]]++; if(row&lt;75 &amp;&amp; col&gt;74 &amp;&amp; col&lt;149) histogram[1][imageArray[row][col]]++; if(row&lt;75 &amp;&amp; col&gt;148 &amp;&amp; col&lt;223) histogram[2][imageArray[row][col]]++; if(row&gt;74 &amp;&amp; row&lt;149 &amp;&amp; col&lt;75) histogram[3][imageArray[row][col]]++; if(row&gt;74 &amp;&amp; row&lt;149 &amp;&amp; col&gt;75 &amp;&amp; col&lt;149) histogram[4][imageArray[row][col]]++; if(row&gt;74 &amp;&amp; row&lt;149 &amp;&amp; col&gt;148 &amp;&amp; col&lt;223) histogram[5][imageArray[row][col]]++; if(row&gt;148 &amp;&amp; row&lt;223 &amp;&amp; col&lt;75) histogram[6][imageArray[row][col]]++; if(row&gt;148 &amp;&amp; row&lt;223 &amp;&amp; col&gt;74 &amp;&amp; col&lt;149) histogram[7][imageArray[row][col]]++; if(row&gt;148 &amp;&amp; row&lt;223 &amp;&amp; col&gt;148 &amp;&amp; col&lt;223) histogram[8][imageArray[row][col]]++; } } for(int row=1; row&lt;=222; row++){ for(int col=1; col&lt;=222; col++){ if(row&lt;75 &amp;&amp; col&lt;75) histogram2[0][imageArray2[row][col]]++; if(row&lt;75 &amp;&amp; col&gt;74 &amp;&amp; col&lt;149) histogram2[1][imageArray2[row][col]]++; if(row&lt;75 &amp;&amp; col&gt;148 &amp;&amp; col&lt;223) histogram2[2][imageArray2[row][col]]++; if(row&gt;74 &amp;&amp; row&lt;149 &amp;&amp; col&lt;75) histogram2[3][imageArray2[row][col]]++; if(row&gt;74 &amp;&amp; row&lt;149 &amp;&amp; col&gt;75 &amp;&amp; col&lt;149) histogram2[4][imageArray2[row][col]]++; if(row&gt;74 &amp;&amp; row&lt;149 &amp;&amp; col&gt;148 &amp;&amp; col&lt;223) histogram2[5][imageArray2[row][col]]++; if(row&gt;148 &amp;&amp; row&lt;223 &amp;&amp; col&lt;75) histogram2[6][imageArray2[row][col]]++; if(row&gt;148 &amp;&amp; row&lt;223 &amp;&amp; col&gt;74 &amp;&amp; col&lt;149) histogram2[7][imageArray2[row][col]]++; if(row&gt;148 &amp;&amp; row&lt;223 &amp;&amp; col&gt;148 &amp;&amp; col&lt;223) histogram2[8][imageArray2[row][col]]++; } } //Compare histograms for(int k=0; k&lt;=8; k++){ for(int j=0; j&lt;=255; j++){ if((((histogram[k][j])*0.1)+histogram[k][j]) &lt; histogram2[k][j] || (histogram[k][j]-((histogram[k][j])*0.1)) &gt; histogram2[k][j]){ return false; } } } return true; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload