欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA比较两张图片相似度的方法

程序员文章站 2024-03-04 21:53:06
本文实例讲述了java比较两张图片相似度的方法。分享给大家供大家参考。具体如下: 摘要: importjava.awt.image.bufferedimage; im...

本文实例讲述了java比较两张图片相似度的方法。分享给大家供大家参考。具体如下:

摘要:

importjava.awt.image.bufferedimage;
importjava.io.file;
importjavax.imageio.imageio;
/***比较两张图片的相似度*@authorguihua**/publicclassbmploader{//改变成二进制码
publicstaticstring[][]getpx(stringargs){int[]rgb=newint[3];
filefile=newfile(args);
bufferedimagebi=null;
try

主要代码如下:

import java.awt.image.bufferedimage;
import java.io.file;
import javax.imageio.imageio;
/**
 * 比较两张图片的相似度
 * @author guihua
 *
 */
public class bmploader {
  // 改变成二进制码
  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){
    bmploader.compareimage("e:\\12.bmp", "e:\\1.bmp");
  }
}

希望本文所述对大家的java程序设计有所帮助。