【工具】Java计算图片相似度

【工具】Java图片相似度匹配工具

方案一

通过像素点去匹配


    /**
     * 
     * @param file1Url   图片url
     * @param file2Url   图片url
     * @return
     */
    public static double img相似度Url(String file1Url, String file2Url){
        InputStream inputStream1 = HttpUtil.createGet(file1Url).execute().bodyStream();
        InputStream inputStream2 = HttpUtil.createGet(file2Url).execute().bodyStream();

        BufferedImage image1 = ImgUtil.read(inputStream1);
        BufferedImage image2 = ImgUtil.read(inputStream2);
        try {
            inputStream1.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            inputStream2.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
//
//        String Base641 = Base64.encode(inputStream1);
//        String Base642 = Base64.encode(inputStream2);
//
//
//        if(StrUtil.equals(Base641, Base642)){
//            return 100;
//        }
        
        return WTool.img相似度(image1, image2);
    }


    /**
     * 必须宽高一样
     * 通过像素点逐个去比对
     * @param file1
     * @param file2
     * @return
     */
    public static double img相似度(BufferedImage file1, BufferedImage file2){

        int aheight = file1.getHeight();
        int awidth = file1.getWidth();

        int bHeight = file2.getHeight();
        int bWidth = file2.getWidth();

        if(aheight != bHeight){
            System.out.println("高度不同");
            System.out.println(aheight);
            System.out.println(bHeight);
            return 0;
        }
        if(awidth != bWidth){
            System.out.println("宽度不同");
            System.out.println(awidth);
            System.out.println(bWidth);
            return 0;
        }
        int 不相同点 = 0;
        int sum = aheight * bWidth;
        for (int i = 0; i < aheight; i++) {
            for (int i1 = 0; i1 < bWidth; i1++) {
                int rgba = file1.getRGB(i1, i);
                int rgbb = file2.getRGB(i1, i);

                if(rgba != rgbb){
                    不相同点++;
                }
            }
        }
        BigDecimal 不相同点Big  = new BigDecimal(不相同点);
        BigDecimal sumBig  = new BigDecimal(sum);
        BigDecimal big100  = new BigDecimal(100);
        BigDecimal 相似度Big = big100.subtract(不相同点Big.divide(sumBig, MathContext.DECIMAL32).multiply(new BigDecimal(100)));
        int 相似度 = 相似度Big.intValue();
        log.info("共:{}, 不相同点:{}, 相似度:{}%", sum, 不相同点, 相似度);

        file1.flush();
        file2.flush();
        return 相似度Big.doubleValue();
    }

依赖

        <!--hutool工具集-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.22</version>
        </dependency>

加强优化方案

1 加载两张待比较的图片;
2 将图片转换为灰度图像;
3 对灰度图像进行降噪处理;
4 提取图像特征,例如使用SIFT算法提取关键点和描述子;
4 计算图像相似度,比较两张图片的特征数据;
5 根据相似度的结果进行判断,确定两张图片是否相似。

其他方案

一个常用的库是OpenCV(Open Source Computer Vision Library)

猜你喜欢

转载自blog.csdn.net/G971005287W/article/details/134375936