zoukankan      html  css  js  c++  java
  • 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("E:\12.bmp""E:\1.bmp");  
    126.     }  
    127. }  
  • 相关阅读:
    【SPOJ】6779 Can you answer these queries VII
    【SPOJ】1557 Can you answer these queries II
    【SPOJ】2916 Can you answer these queries V
    【CodeForces】86D Powerful array
    【SPOJ】1043 Can you answer these queries I
    【HDU】3727 Jewel
    【HDU】3915 Game
    【SPOJ】1043 Can you answer these queries III
    【SPOJ】2713 Can you answer these queries IV
    成为一名更好的软件工程师的简单方法
  • 原文地址:https://www.cnblogs.com/rosepotato/p/3530112.html
Copyright © 2011-2022 走看看