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

【工具】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();
    }

依赖

XML 复制代码
        <!--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)

相关推荐
andyweike1 小时前
php笔记
开发语言·笔记·php
EatFan1 小时前
Spring Boot 4 落地观察:从 yudao-cloud、matecloud、JPower 看国产脚手架的升级路线与迁移清单
java·spring boot·后端·spring cloud·微服务·后端开发·jdk 21
Maiko Star2 小时前
* LangChain 提示词模板详解:ChatPromptTemplate 的使用与高级特性
java·人工智能·langchain
Circ.2 小时前
Python 调试 Litellm 本地大模型接口:模型列表与对话接口实操踩坑记录
开发语言·python
现任明教教主~3 小时前
Thinkphp站群蜘蛛池SaaS系统YanyvSEO含多用户/积分/六大引擎计费/易支付对接
java·开发语言·spring
Zhou1411364 小时前
SpringSecurity_02_授权与高级功能
开发语言·python
一条破秋裤4 小时前
Linux 线程创建:pthread_create 与基本回收
java·linux·运维
布吉岛的石头4 小时前
Java 程序员第 49 阶段5:BERT 预训练目标 MLM+NSP 的工程含义
java·人工智能·深度学习·bert·transformer
码匠许师傅4 小时前
【C++三方组件】cpp-httplib:一个头文件起 HTTP 服务
开发语言·c++·http