2

我正在用 Java 编写照片马赛克。为此,我必须计算和比较 2 个图像的 RGB,并用最合适的平铺图像替换目标图像。最恰当的意思是,如果我们没有找到精确的 RGB 匹配,d则允许出现 say 错误。

我正在使用以下代码来计算 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;

    }

它的作用是从给定的路径中获取图像,计算其 RGB 并将其存储在HashMap.

有没有更好的方法来计算 RGB 并比较它们以获得更好的结果?

编辑:我根据一些评论编辑了我的问题。

4

1 回答 1

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.

于 2012-10-08T00:34:32.783 回答