How to create an Average Image from Gray Scale (8 bits) JPEG Images
I磎 trying to create a mean image from 9 JPEG Images, but this mean image has lots of "noise", is distorced, and doesnt磖esemble at all the original ones.
I need this mean image so I磍l correlate each of the original ones to find out the best image I磎 going to choose ussing NCC (normalized cross correlation coeficient).
When I usesetRGB(x,y,rgb), it returns diferent "images" according to the number of zeros, for ex.: rgb = 028028028 is different from 28028028 and from 282828. I can磘 find a way to work it out.
My algorithm is the folowing:
File f;
double [] [] pixels =null;
Color [] [] tempPixel =null;
Integer [] [] byte0 =null;
Integer [] [] byte1 =null;
Integer [] [] byte2 =null;
Integer [] [] byte3 =null;
Integer [] [] tempPixels =null;
mean =null;
int w,h;
fileNames =new String[numFrames];
for (int i=1; i<numFrames; i++){
fileName = path +"Frame#" + i +".jpg";
fileNames[i-1] = fileName;
}
// just for getting width, height, for configuring pixels array and
// buffered images - mean & best.
fileName = path +"Frame#8.jpg";
f=new File(fileName);
try{
temp = ImageIO.read(f);
}catch(IOException io){returnfalse;}
w = temp.getWidth();
h = temp.getHeight();
mean =new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
pixels =newdouble [w][h];
byte0 =new Integer [w][h];
byte1 =new Integer [w][h];
byte2 =new Integer [w][h];
byte3 =new Integer [w][h];
tempPixel =new Color[w][h];
tempPixels =new Integer[w][h];
f=null; temp =null;
//reset pixels values.
for(int x = 0; x >< w; x++){
for(int y = 0; y < h; y++){
byte0[x][y] =0;
byte1[x][y]=0;
byte2[x][y]=0;
byte3[x][y] = 0;
pixels[x][y] = 0;
}
}
// read each file (image), and according to W & H and collects the
// pixels values.
for(int j=0; j<numFrames-1; j++){
f =new File(fileNames[j]);
try{
temp = ImageIO.read(f);
}catch(IOException io){returnfalse;}
if(temp!=null)
for(int x=0 ; x>< temp.getWidth(); x++){
for(int y=0; y<temp.getHeight(); y++){
tempPixel[x][y] =new Color(temp.getRGB(x,y));
pixels[x][y] = pixels[x][y] + temp.getRGB(x,y) / 16581375;
//BLUE
byte0[x][y] = byte0[x][y] + (tempPixel[x][y].getBlue());//tempPixel[x][y].getBlue(); //& 0xff;
//GREEN
byte1[x][y] = byte1[x][y] + (tempPixel[x][y].getGreen());// >>8 & 0xff;
//RED
byte2[x][y] = byte2[x][y] + (tempPixel[x][y].getRed());//>>16 & 0xff;
//ALPHA
byte3[x][y] = byte3[x][y] + (tempPixel[x][y].getAlpha());//>>24 & 0xff;
}
}
f=null;
}//end of loop for j<numFrames
String t0, t1, t2, t3;
for(int x=0 ; x>< temp.getWidth(); x++){
for(int y=0; y< temp.getHeight(); y++){
byte0[x][y] = byte0[x][y] / numFrames;
if((byte0[x][y])<10)
t0="0" + byte0[x][y].toString();
//else if((byte0[x][y])>=10 & ((byte0[x][y])<100))
//t0 = "0" + byte0[x][y].toString();
else
t0 = byte0[x][y].toString();
byte1[x][y] = byte1[x][y] / numFrames;
if((byte1[x][y])<10)
t1="0" + byte1[x][y].toString();
//else if((byte1[x][y])>=10 & ((byte1[x][y])<100))
//t1= "0" + byte1[x][y].toString();
else
t1 = byte1[x][y].toString();
byte2[x][y] = byte2[x][y] / numFrames;
if((byte2[x][y])<10)
t2="0" + byte2[x][y].toString();
//else if((byte2[x][y])>=10 & ((byte2[x][y])<100))
//t2 = "0" + byte2[x][y].toString();
else
t2 = byte2[x][y].toString();
byte3[x][y] = byte3[x][y] / numFrames;
if((byte1[x][y])<10)
t3="0" + byte3[x][y].toString();
else
t3 = byte3[x][y].toString();
pixels[x][y] = (pixels[x][y]/numFrames) * 16581375;
try{
String rgbs = t2+t1+t0;
Integer rgb = Integer.valueOf(rgbs);
//System.out.println("Valor do rgb: " +rgb);
mean.setRGB(x,y,(int)pixels[x][y]);
}catch(NumberFormatException nfe){
System.out.println("ERROR: Integer to STRING: " + nfe);
returnfalse;
}// end of catch

