I made a simple image similarity calculator using JAVA.

Source: Internet
Author: User

I made a simple image similarity calculator using JAVA.
Key Algorithms: Copy code 1 // full process 2 public static void main (String [] args) throws IOException {3 // get image 4 File imageFile = new File ("c: /1.jpg"); 5 Image image = ImageIO. read (imageFile); 6 // convert to grayscale 7 image = toGrayscale (image); 8 // scale down to a thumbnail of 32x32 9 image = scale (image ); 10 // obtain the gray pixel array 11 int [] pixels = getPixels (image); 12 // obtain the average gray color 13 int averageColor = getAverageOfPixelArray (pixels ); 14 // obtain the comparison array of gray pixels (that is, the image fingerprint sequence) 15 Pixels = getPixelDeviateWeightsArray (pixels, averageColor, pixels); 18 // calculate the similarity using the Hamming distance. value range: [0.0, 1.0] 19 double similarity = calSimilarity (hammingDistance ); 20} 21 22 // convert any Image type to BufferedImage type to facilitate subsequent operations 23 public static BufferedImage convertToBufferedFrom (Image srcImage) {24 BufferedImage bufferedImage = n Ew BufferedImage (srcImage. getWidth (null), 25 srcImage. getHeight (null), BufferedImage. TYPE_INT_ARGB); 26 Graphics2D g = bufferedImage. createGraphics (); 27g. drawImage (srcImage, null, null); 28g. dispose (); 29 return bufferedImage; 30} 31 32 // convert to grayscale fig 33 public static BufferedImage toGrayscale (Image image) {34 BufferedImage sourceBuffered = convertToBufferedFrom (image ); 35 ColorSpace cs = ColorSpace. getInst Ance (ColorSpace. CS_GRAY); 36 ColorConvertOp op = new ColorConvertOp (cs, null); 37 BufferedImage grayBuffered = op. filter (sourceBuffered, null); 38 return grayBuffered; 39} 40 41 // zoom to 32x32 pixel thumbnail 42 public static Image scale (Image image) {43 image = image. getScaledInstance (32, 32, Image. SCALE_SMOOTH); 44 return image; 45} 46 47 // get the pixel array 48 public static int [] getPixels (Image image) {49 int width = image. g EtWidth (null); 50 int height = image. getHeight (null); 51 int [] pixels = convertToBufferedFrom (image ). getRGB (0, 0, width, height, 52 null, 0, width); 53 return pixels; 54} 55 56 // obtain the average pixel Color value of the grayscale image 57 public static int getAverageOfPixelArray (int [] pixels) {58 color; 59 long sumRed = 0; 60 for (int I = 0; I <pixels. length; I ++) {61 color = new Color (pixels [I], true); 62 sumRed + = color. getRed (); 63} 64 in T averageRed = (int) (sumRed/pixels. length); 65 return averageRed; 66} 67 68 // retrieve the pixel comparison array of the grayscale image (mean deviation) 69 public static int [] getPixelDeviateWeightsArray (int [] pixels, final int averageColor) {70 Color color; 71 int [] dest = new int [pixels. length]; 72 for (int I = 0; I <pixels. length; I ++) {73 color = new Color (pixels [I], true); 74 dest [I] = color. getRed ()-averageColor> 0? 1: 0; 75} 76 return dest; 77} 78 79 // obtain the Hamming distance of the array from the average pixels of the two thumbnails (the larger the distance, the larger the difference) 80 public static int getHammingDistance (int [] a, int [] B) {81 int sum = 0; 82 for (int I = 0; I <. length; I ++) {83 sum + = a [I] = B [I]? 0: 1; 84} 85 return sum; 86} 87 88 // calculate similarity 89 public static double calSimilarity (int hammingDistance) {90 int length = 32*32 using the hammingDistance distance; 91 double similarity = (length-hammingDistance)/(double) length; 92 93 // use an exponential curve to adjust the similarity result 94 similarity = java. lang. math. pow (similarity, 2); 95 return similarity; 96} copy code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.