I am writing a photo mosaic in Java. For this, I have to calculate and compare RGB of 2 images and replace the target image with the most appropriate tile image. By most appropriate I mean, if we do not find an exact RGB match, an error of say d
is allowed.
I am using the following code to calculate RGB:
protected static int calcRGBForEachFile(String filePath) throws IOException {
int RGBTotal = 0;
try{
BufferedImage tile = ImageIO.read(new File(filePath));
tileWidth = tile.getWidth();
tileHeight = tile.getHeight();
for(int i=0; i<tileWidth; i++){
for(int j=0; j<tileHeight; j++){
RGBTotal = getPixelData(tile.getRGB(i,j));
}
}
}
catch(IOException e){
System.out.println(e);
}
return RGBTotal;
}
protected static void getPixelData(int rgb) {
int red = (rgb >> 16) & 0xff;
int green = (rgb >> 8) & 0xff;
int blue = (rgb) & 0xff;
}
What it does is that it takes an image from the path given, calculates its RGB and stored it in a HashMap
.
Is there any better way to calculate RGB and compare them for 2 images to get better results?
EDIT: I have edited my question based on some of the comments.