JAVA 比较两张图片的相似度


  1. import java.awt.image.BufferedImage;  
  2. import java.io.File;  
  3. import javax.imageio.ImageIO;  
  4.   
  5. /** 
  6.  * 比较两张图片的相似度 
  7.  * @author Guihua 
  8.  * 
  9.  */  
  10. public class BMPLoader {  
  11.     // 改变成二进制码  
  12.     public static String[][] getPX(String args) {  
  13.         int[] rgb = new int[3];  
  14.   
  15.         File file = new File(args);  
  16.         BufferedImage bi = null;  
  17.         try {  
  18.             bi = ImageIO.read(file);  
  19.         } catch (Exception e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.   
  23.         int width = bi.getWidth();  
  24.         int height = bi.getHeight();  
  25.         int minx = bi.getMinX();  
  26.         int miny = bi.getMinY();  
  27.         String[][] list = new String[width][height];  
  28.         for (int i = minx; i < width; i++) {  
  29.             for (int j = miny; j < height; j++) {  
  30.                 int pixel = bi.getRGB(i, j);  
  31.                 rgb[0] = (pixel & 0xff0000) >> 16;  
  32.                 rgb[1] = (pixel & 0xff00) >> 8;  
  33.                 rgb[2] = (pixel & 0xff);  
  34.                 list[i][j] = rgb[0] + "," + rgb[1] + "," + rgb[2];  
  35.   
  36.             }  
  37.         }  
  38.         return list;  
  39.   
  40.     }  
  41.       
  42.     public static void compareImage(String imgPath1, String imgPath2){  
  43.         String[] images = {imgPath1, imgPath2};  
  44.         if (images.length == 0) {  
  45.             System.out.println("Usage >java BMPLoader ImageFile.bmp");  
  46.             System.exit(0);  
  47.         }  
  48.   
  49.         // 分析图片相似度 begin  
  50.         String[][] list1 = getPX(images[0]);  
  51.         String[][] list2 = getPX(images[1]);  
  52.         int xiangsi = 0;  
  53.         int busi = 0;  
  54.         int i = 0, j = 0;  
  55.         for (String[] strings : list1) {  
  56.             if ((i + 1) == list1.length) {  
  57.                 continue;  
  58.             }  
  59.             for (int m=0; m<strings.length; m++) {  
  60.                 try {  
  61.                     String[] value1 = list1[i][j].toString().split(",");  
  62.                     String[] value2 = list2[i][j].toString().split(",");  
  63.                     int k = 0;  
  64.                     for (int n=0; n<value2.length; n++) {  
  65.                         if (Math.abs(Integer.parseInt(value1[k]) - Integer.parseInt(value2[k])) < 5) {  
  66.                             xiangsi++;  
  67.                         } else {  
  68.                             busi++;  
  69.                         }  
  70.                     }  
  71.                 } catch (RuntimeException e) {  
  72.                     continue;  
  73.                 }  
  74.                 j++;  
  75.             }  
  76.             i++;  
  77.         }  
  78.   
  79.         list1 = getPX(images[1]);  
  80.         list2 = getPX(images[0]);  
  81.         i = 0;  
  82.         j = 0;  
  83.         for (String[] strings : list1) {  
  84.             if ((i + 1) == list1.length) {  
  85.                 continue;  
  86.             }  
  87.             for (int m=0; m<strings.length; m++) {  
  88.                 try {  
  89.                     String[] value1 = list1[i][j].toString().split(",");  
  90.                     String[] value2 = list2[i][j].toString().split(",");  
  91.                     int k = 0;  
  92.                     for (int n=0; n<value2.length; n++) {  
  93.                         if (Math.abs(Integer.parseInt(value1[k]) - Integer.parseInt(value2[k])) < 5) {  
  94.                             xiangsi++;  
  95.                         } else {  
  96.                             busi++;  
  97.                         }  
  98.                     }  
  99.                 } catch (RuntimeException e) {  
  100.                     continue;  
  101.                 }  
  102.                 j++;  
  103.             }  
  104.             i++;  
  105.         }  
  106.         String baifen = "";  
  107.         try {  
  108.             baifen = ((Double.parseDouble(xiangsi + "") / Double.parseDouble((busi + xiangsi) + "")) + "");  
  109.             baifen = baifen.substring(baifen.indexOf(".") + 1, baifen.indexOf(".") + 3);  
  110.         } catch (Exception e) {  
  111.             baifen = "0";  
  112.         }  
  113.         if (baifen.length() <= 0) {  
  114.             baifen = "0";  
  115.         }  
  116.         if(busi == 0){  
  117.             baifen="100";  
  118.         }  
  119.   
  120.         System.out.println("相似像素数量:" + xiangsi + " 不相似像素数量:" + busi + " 相似率:" + Integer.parseInt(baifen) + "%");  
  121.   
  122.     }  
  123.   
  124.     public static void main(String[] args){  
  125.         BMPLoader.compareImage("D:\\11.bmp""E:\\12.bmp");  
  126.     }  
  127. }  
  128. 原文https://blog.csdn.net/caoliangang/article/details/54972110
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是 Java 中获取图片相似度的代码示例: ``` import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ImageSimilarity { public static void main(String[] args) throws IOException { File img1 = new File("image1.png"); File img2 = new File("image2.png"); BufferedImage image1 = ImageIO.read(img1); BufferedImage image2 = ImageIO.read(img2); double similarity = compareImages(image1, image2); System.out.println("The similarity score is: " + similarity); } private static double compareImages(BufferedImage image1, BufferedImage image2) { int width1 = image1.getWidth(); int width2 = image2.getWidth(); int height1 = image1.getHeight(); int height2 = image2.getHeight(); if ((width1 != width2) || (height1 != height2)) { throw new IllegalArgumentException("Images must have the same dimensions"); } long difference = 0; for (int y = 0; y < height1; y++) { for (int x = 0; x < width1; x++) { int rgb1 = image1.getRGB(x, y); int rgb2 = image2.getRGB(x, y); int red1 = (rgb1 >> 16) & 0xff; int green1 = (rgb1 >> 8) & 0xff; int blue1 = (rgb1) & 0xff; int red2 = (rgb2 >> 16) & 0xff; int green2 = (rgb2 >> 8) & 0xff; int blue2 = (rgb2) & 0xff; difference += Math.abs(red1 - red2); difference += Math.abs(green1 - green2); difference += Math.abs(blue1 - blue2); } } double total_pixels = width1 * height1 * 3; double avg_different_pixels = difference / total_pixels; double similarity = (avg_different_pixels / 255) * 100; return (100 - similarity); } } ``` 该代码使用 Java 中的 `BufferedImage` 类加载两张图片,并计算它们之间的相似度。它通过比较两个像素的 RGB 值来计算不同之处,并将其与总像素数相除以获得平均不同像素数。然后,该代码将平均不同像素数除以 255 并将其乘以 100,以获得相似度百分比。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值