Java_比较两个图片的相似度
说明:目前使用像素偏移量为5,可根据实际情况相应修改
package com.creditease.fetch.credit.util.similarity; import com.creditease.fetch.credit.util.ImageManager; import sun.misc.BASE64Decoder; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; /** * 比较两张图片的相似度 */ public class SimilarityComparer { // 改变成二进制码 private static String[][] getPX(BufferedImage image) { int[] rgb = new int[3]; int width = image.getWidth(); int height = image.getHeight(); int minx = image.getMinX(); int miny = image.getMinY(); String[][] list = new String[width][height]; for (int i = minx; i < width; i++) { for (int j = miny; j < height; j++) { int pixel = image.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 boolean compareImage(BufferedImage image1, BufferedImage image2) { boolean result = false; // 分析图片相似度 begin String[][] list1 = getPX(image1); String[][] list2 = getPX(image2); 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])) < 3) { xiangsi++; } else { busi++; } } } catch (RuntimeException e) { continue; } j++; } i++; } list1 = getPX(image2); list2 = getPX(image1); 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])) < 3) { xiangsi++; } else { busi++; } } } catch (RuntimeException e) { continue; } j++; } i++; } if (busi == 0) { result = true; } return result; } public static void main(String[] args) throws IOException { InputStream stream = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(ImageManager.numeric.get("6"))); BufferedImage n6 = ImageIO.read(stream); stream = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(ImageManager.numeric.get("9"))); BufferedImage n9 = ImageIO.read(stream); System.out.println(SimilarityComparer.compareImage(n6, n9)); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架