Java 比较两张图片的相似度

问题遇到的现象和发生背景

Java 已知输入为两个BufferedImage类型的图像(可能宽高不同),求两张图像的百分比相似度。
当然,实际输入为两个base64编码的字符串,先处理转为BufferedImage类型的图像了。

运行结果及报错内容

现有的一个思路是比较颜色直方图,但是会被宽高影响。没有达到一个较精确的效果。

参考

可以参考下面两个函数的输入输出,先通过base64编码的字符串输入到getData中获取颜色分布的直方图数组,而后将两张图片的数组数据传入compare中比较,得到一个百分比相似度。。但这个方法的相似度比较的数据不够精确。

    public static int[] getData(String baseStr) {
        try {
            //BufferedImage img = ImageIO.read(new File(name));
            BufferedImage img = base64ToBufferedImage(baseStr);
            BufferedImage slt = new BufferedImage(100, 100,
                    BufferedImage.TYPE_INT_RGB);
            slt.getGraphics().drawImage(img, 0, 0, 100, 100, null);
            // ImageIO.write(slt,"jpeg",new File("slt.jpg"));
            int[] data = new int[256];
            for (int x = 0; x < slt.getWidth(); x++) {
                for (int y = 0; y < slt.getHeight(); y++) {
                    int rgb = slt.getRGB(x, y);
                    Color myColor = new Color(rgb);
                    int r = myColor.getRed();
                    int g = myColor.getGreen();
                    int b = myColor.getBlue();
                    data[(r + g + b) / 3]++;
                }
            }
            // data 就是所谓图形学当中的直方图的概念
            return data;
        } catch (Exception exception) {
            System.out.println("有文件没有找到,请检查文件是否存在或路径是否正确");
            return null;
        }
    }

    public static float compare(int[] s, int[] t) {
        try {
            float result = 0F;
            for (int i = 0; i < 256; i++) {
                int abs = Math.abs(s[i] - t[i]);
                int max = Math.max(s[i], t[i]);
                result += (1 - ((float) abs / (max == 0 ? 1 : max)));
            }
            return (result / 256) * 100;
        } catch (Exception exception) {
            return 0;
        }
    }

可以考虑基于openCV来做图片相似性的比较,可以参考这篇博文来实现。写得比较清楚:https://blog.csdn.net/u014775579/article/details/123534770