Compare RGB of 2 images in Java

╄→гoц情女王★ 提交于 2019-12-13 16:09:37

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/12773925/compare-rgb-of-2-images-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!