zoukankan      html  css  js  c++  java
  • java 图片相似度简单代码

    package com.cxqy.activity.dto.nyactivity;

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

    /**
    * @Author yjl
    * @Date 2022/1/10 15:39
    * @Version 1.0
    */



    public class ImageUtil {
    public static String[][] getPX(String args) {
    int[] rgb = new int[3];

    File file = new File(args);
    BufferedImage bi = null;
    try {
    bi = ImageIO.read(file);
    } catch (Exception e) {
    e.printStackTrace();
    }

    int width = bi.getWidth();
    int height = bi.getHeight();
    int minx = bi.getMinX();
    int miny = bi.getMinY();
    String[][] list = new String[width][height];
    for (int i = minx; i < width; i++) {
    for (int j = miny; j < height; j++) {
    int pixel = bi.getRGB(i, j);
    rgb[0] = (pixel & 0xff0000) >> 16;
    rgb[1] = (pixel & 0xff00) >> 8;
    rgb[2] = (pixel & 0xff);
    list[i][j] = rgb[0] + "," + rgb[1] + "," + rgb[2];

    }
    }
    return list;

    }

    public static void compareImage(String imgPath1, String imgPath2){
    String[] images = {imgPath1, imgPath2};
    if (images.length == 0) {
    System.out.println("Usage >java BMPLoader ImageFile.bmp");
    System.exit(0);
    }

    // 分析片相似度 begin
    String[][] list1 = getPX(images[0]);
    String[][] list2 = getPX(images[1]);
    int xiangsi = 0;
    int busi = 0;
    int i = 0, j = 0;
    for (String[] strings : list1) {
    if ((i + 1) == list1.length) {
    continue;
    }
    for (int m=0; m<strings.length; m++) {
    try {
    String[] value1 = list1[i][j].toString().split(",");
    String[] value2 = list2[i][j].toString().split(",");
    int k = 0;
    for (int n=0; n<value2.length; n++) {
    if (Math.abs(Integer.parseInt(value1[k]) - Integer.parseInt(value2[k])) < 5) {
    xiangsi++;
    } else {
    busi++;
    }
    }
    } catch (RuntimeException e) {
    continue;
    }
    j++;
    }
    i++;
    }

    list1 = getPX(images[1]);
    list2 = getPX(images[0]);
    i = 0;
    j = 0;
    for (String[] strings : list1) {
    if ((i + 1) == list1.length) {
    continue;
    }
    for (int m=0; m<strings.length; m++) {
    try {
    String[] value1 = list1[i][j].toString().split(",");
    String[] value2 = list2[i][j].toString().split(",");
    int k = 0;
    for (int n=0; n<value2.length; n++) {
    if (Math.abs(Integer.parseInt(value1[k]) - Integer.parseInt(value2[k])) < 5) {
    xiangsi++;
    } else {
    busi++;
    }
    }
    } catch (RuntimeException e) {
    continue;
    }
    j++;
    }
    i++;
    }
    String baifen = "";
    try {
    baifen = ((Double.parseDouble(xiangsi + "") / Double.parseDouble((busi + xiangsi) + "")) + "");
    baifen = baifen.substring(baifen.indexOf(".") + 1, baifen.indexOf(".") + 3);
    } catch (Exception e) {
    baifen = "0";
    }
    if (baifen.length() <= 0) {
    baifen = "0";
    }
    if(busi == 0){
    baifen="100";
    }

    System.out.println("相似像素量:" + xiangsi + " 不相似像素量:" + busi + " 相似率:" + Integer.parseInt(baifen) + "%");

    }

    public static void main(String[] args){
    ImageUtil.compareImage("C:\\Users\\Administrator\\Pictures\\bb\\2.jpg", "C:\\Users\\Administrator\\Pictures\\bb\\55.jpg");
    }
    }
  • 相关阅读:
    word-spacing汉字不起作用的解决方法
    FontAwesome 4.4.0 中完整的585个图标样式CSS参考
    margin-top失效的解决方法
    今天有群友不是很清楚htm直接存数据库的危害,我简单举个例子
    前端:图文混排-怎么在不使用float的情况下实现想要的效果呢?
    前端:圆图头像制作--border-radius : 100%
    样式问题-如何一次性设置网站英文字体样式,中文字体等样式
    8.GitHub实战系列~8.使用GitHub建立自己的免费博客
    牛逼的OSQL----大数据导入
    01.SQLServer性能优化之----强大的文件组----分盘存储
  • 原文地址:https://www.cnblogs.com/yangsanluo/p/15784762.html
Copyright © 2011-2022 走看看