Perceptual hashing algorithms--Find similar images

Source: Internet
Author: User

Google Image Search function

In Google image search, users can upload a picture, Google shows the internet in the same image or similar images.

For example, I upload a picture to try the effect:

Principle explanation

Referring to this article by Dr. Neal Krawetz, the key technique for implementing this function is called "Perceptual hashing Algorithm" (perceptual hash algorithm), which means that a fingerprint (string format) is generated for the image, and the more similar the two images are, the more similar the two images are. But the key is how to calculate the "fingerprint" according to the picture? Here are the simplest steps to illustrate the principle:

The first step is to reduce the image size

Reduce the image to 8x8 's size, a total of 64 pixels. The effect of this step is to remove the various image sizes and picture proportions of the difference, only the structure, shading and other basic information.

Second step to grayscale image

Convert the reduced image to a 64-level grayscale image.

The third step is to calculate the gray average

Calculates the grayscale average of all pixels in a picture

The fourth step is to compare the grayscale of pixels

The grayscale of each pixel is compared to the average, and if the value is greater than or equal to the average is 1, less than the average is 0.

The fifth step computes the hash value

Combining the results of the previous step, together, constitutes a 64-bit binary integer, which is the fingerprint of this image.

Sixth step contrast picture fingerprint

After you get the fingerprint of the image, you can compare the fingerprints of different images and figure out how many of the 64 bits are not the same. If the number of different data bits is not more than 5, it means that two images are similar, if they are greater than 10, they are two distinct pictures.

Code implementation (C # version)

I use the C # code below to implement the steps described in the previous section.

Using system;using system.io;using System.drawing;namespace similarphoto{class Similarphoto {Image SourceIm        G        Public Similarphoto (String filePath) {sourceimg = Image.FromFile (FilePath);        } public Similarphoto (Stream stream) {sourceimg = Image.fromstream (stream);            } public String Gethash () {Image image = Reducesize ();            byte[] Grayvalues = reducecolor (image);            Byte average = Calcaverage (grayvalues);            String Reslut = computebits (grayvalues, average);        return reslut;            }//Step 1:reduce size to 8*8 private Image reducesize (int width = 8, int height = 8) {            Image image = Sourceimg.getthumbnailimage (width, height, () = = {return false;}, IntPtr.Zero);        return image; }//Step 2:reduce Color private byte[] Reducecolor (image image) {Bitmap Bitmap = new B ItmaP (image); byte[] grayvalues = new Byte[image. Width * image.            Height]; for (int x = 0; x<image. Width; x + +) for (int y = 0; y < image. Height;                    y++) {Color color = bitmap.getpixel (x, y); byte Grayvalue = (byte) ((color). R * + color. G * + color.                    B * 11)/100); Grayvalues[x * image.                Width + y] = Grayvalue;        } return grayvalues;            }//Step 3:average the colors private Byte calcaverage (byte[] values) {int sum = 0; for (int i = 0; i < values. Length;            i++) sum + = (int) values[i]; Return Convert.tobyte (sum/values.        Length);            }//Step 4:compute the bits private String computebits (byte[] values, byte averagevalue) { Char[] result = new char[values.            Length]; for (int i = 0; i < values. Length; i++) {if (VAlues[i] < Averagevalue) Result[i] = ' 0 ';            else result[i] = ' 1 ';        } return new String (result);  }//Compare hash public static Int32 Calcsimilardegree (string A, string b) {if (a.length            ! = b.length) throw new ArgumentException ();            int count = 0;            for (int i = 0; i < a.length; i++) {if (A[i]! = B[i]) count++;        } return count; }    }}

Google Server picture number is company aims Yangzhou other, my Computer picture number of course can not compare, but before did the reptile program, the computer has more than 40,000 people's avatar photos, take them as the comparison result! I calculated the "fingerprints" of these images and put them in a TXT text format as follows.

With ASP. Write a simple page, allow users to upload a picture, the background to calculate the image of the fingerprint, and txt text in the image of the fingerprint comparison, sorting out the results displayed in the page, the effect is as follows:

This address: http://www.cnblogs.com/technology/archive/2012/07/12/Perceptual-Hash-Algorithm.html

Perceptual hashing algorithms--Find similar images

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.