如何使用java比较图像的相似性

新手上路,请多包涵

最近,我有机会在我的一个项目中使用图像处理技术,我的任务是在提供新图像时从图像存储中找到匹配的图像。我从谷歌搜索“如何使用 java 比较图像”开始了我的项目,我得到了一些关于查找两个图像相似性的好文章。几乎所有这些都基于四个基本步骤,它们是:

 1.Locating the Region of Interest (Where the Objects appear in the given image),
2.Re-sizing the ROIs in to a common size,
3.Substracting ROIs,
4.Calculating the Black and White Ratio of the resultant image after subtraction.

虽然这听起来是比较图像的好算法,但在我的项目中使用 JAI 实现它后需要花费大量时间。因此,我必须找到一种替代方法。

有什么建议么?

原文由 sum2000 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 539
1 个回答
    **// This API will compare two image file //
// return true if both image files are equal else return false//**
public static boolean compareImage(File fileA, File fileB) {
    try {
        // take buffer data from botm image files //
        BufferedImage biA = ImageIO.read(fileA);
        DataBuffer dbA = biA.getData().getDataBuffer();
        int sizeA = dbA.getSize();
        BufferedImage biB = ImageIO.read(fileB);
        DataBuffer dbB = biB.getData().getDataBuffer();
        int sizeB = dbB.getSize();
        // compare data-buffer objects //
        if(sizeA == sizeB) {
            for(int i=0; i<sizeA; i++) {
                if(dbA.getElem(i) != dbB.getElem(i)) {
                    return false;
                }
            }
            return true;
        }
        else {
            return false;
        }
    }
    catch (Exception e) {
        System.out.println("Failed to compare image files ...");
        return  false;
    }
}

原文由 Sandip Ganguli 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题