java实现图片比对算法-亚博电竞手机版
采用直方图原理算法比对图片的细微差别效果比较好,以下两张区别很小的图片识别效果如下:
识别结果:
主要代码如下:
import javax.imageio.*; import java.awt.image.*; import java.awt.*; import java.io.*; public class photodigest { public static void main(string[] args) throws exception { float percent = compare(getdata("/users/swww.cppcns.comun/downloads/1.jpg"), getdata("/users/sun/downloads/3.jpg")); if (percent == 0) { system.out.println("无法比较"); } else { system.out.println("两张图片的相似度为:" percent "%"); } } public static int[] getdata(string name) { try { bufferedimage img = imageio.read(new file(name)); bufferedimage slt = new bufferedimage(100恰卡编程网, 100, bufferedimage.type_int_rgb); slt.getgraphics().drawimage(img, 0, 0, 100, 100, null); // imageio.write(slt,"jpeg",new file("slt.jpg")); int[] data = new int[256]; for (int x = 0; x < slt.getwidth(); x ) { for (int y = 0; y < slt.getheight(); y ) { int rgb = slt.getrgb(x, y); color mycolor = new color(rgb); int r = mycolor.getred(); int g = mycolor.getgreen(); int b = mycolor.getblue(); data[(r g b) / 3] ; } } // data 就是所谓图形学当中的直方图的概念 return data; } catch (exception exception) { system.out.println("有文件没有找到,请检查文件是否存在或路径是否正确"); return null; } } public static float compare(int[] s, int[] t) { try { float result = 0f; for (int i = 0; i < 256; i ) { int abs = math.abs(s[i] - t[i]); int max = math.max(s[i], t[i]); result = (1 - ((float) abs /www.cppcns.com (max == 0 ? 1 : max))); } return (result / 256) * 100; } catch (exception exception) { return 0; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。