2

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.

Intern
  • 327
  • 1
  • 7
  • 23
  • 2
    You shouldn't add all of the values together, as two different values of `total` can be completely different, eg. Color 1 = R: 50 G:50 B:50, and Color 2 = R: 150 G: 0 B: 0. These are completely different colors – Kinected Oct 08 '12 at 00:11
  • 2
    What Kinected said. You should be comparing each sample/component independently, not adding them and then comparing the sum. – aroth Oct 08 '12 at 00:15
  • @Kinected: I am aware of this. The only reason I am doing this is because i get one value for RGB and not 3(for easy storage and comparisons). Once I figure out a way to calculate and compare in a good way, I will take care of that issue. – Intern Oct 08 '12 at 00:16
  • You will get innaccurate test results until you fix that, and will never know if it worked. See http://stackoverflow.com/questions/11342251/verify-specific-color-from-24-bpp-image-in-c-sharp/11378502#11378502 (except java) – Kinected Oct 08 '12 at 00:20
  • I've never tried making a photo-mosaic, but there are two issues here, the hardest being what algorithm should I use to create a photo-mosaic and the second is how to implement that. I've never done this, but an algorithm close to what you are doing would require storing the average of the R, G, and B color of an image (separately) and then comparing it to the R, G, and B average of the main image region, maybe a distance like `((R1-R2)^2 + (G1-G2)^2 + (B1-B2)^2)^(1/2)`. If you want to print these out, you need to use CMYK instead of RGB. – Stuart R. Jefferys Oct 08 '12 at 00:37

1 Answers1

3

One of the best ways to compare images in terms of how they differ according to human perception (roughly) is SSIM.

You may also try comparing the R, G, and B values for each pixel, and with a little more effort, weight each color based on human eye sensitivity (e.g. see Von Kries's method). If the images are not very, very close, and the same size, this won't work as well as you might hope.

I wrote a save format recommendation program (PNG vs JPG), ImageGuide, whose (open) source contains a simple image comparison algorithm which works for its purpose. It may help get you started.

Charles Burns
  • 10,310
  • 7
  • 64
  • 81
  • A commercial photo-mosaic program would probably need to do this. But how well will one work that doesn't do this? @Intern: who are your audiences for the mosaic and the program? – Stuart R. Jefferys Oct 08 '12 at 00:42
  • @StuartR.Jefferys: Thank you for your answer. I am checking the links you have given. And the app is not commercial. It is for a project we are working on. – Intern Oct 08 '12 at 00:50