java 通过二进制比较图片相似度

        通过将图片转换成二进制来比较图盘的相似度 ! 别的不多说,这个比较简单,直接看代码! 

package com.tgb.image;


import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @author 
 * @Description: 
 * @date
 */
public class App {
    public static void main(String[] args) {
        String imagePath1="/Users/caoxueliang/Desktop/3.jpeg";
        String imagePath2="/Users/caoxueliang/Desktop/4.jpeg";
        try {
            BufferedImage image1 = ImageIO.read(new File(imagePath1));
            BufferedImage image2 = ImageIO.read(new File(imagePath2));
            int width1 = image1.getWidth();
            int height1 = image1.getHeight();

            int width2=image2.getWidth();
            int height2=image2.getHeight();

            if(width1!=width2 || height1!=height2){
                System.out.println("图片大小不一致");
            }else{
                int totalCount=width1*height1;
                int diffCount=0;
                for (int i=0;i<width1;i++){
                    for (int j=0;j<height1;j++){
                        int bit1=image1.getRGB(i,j);
                        int bit2=image2.getRGB(i,j);
                        if(bit1!=bit2){
                            diffCount=diffCount+1;
                        }
                    }
                }
                if(diffCount<totalCount*0.15){
                    System.out.println("是同一个图片,误差为:"+(diffCount*1.0/totalCount*1.0));
                }else{
                    System.out.println("误差过大,不是同一张图片");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值