Google Map-Similar image search principle-Java implementation

Source: Internet
Author: User

The former in Ruan Yi Feng's blog saw this "similar image search principle" blog, there is a kind of impulse to realize these principles.

Google "Similar image search": You can use an image to search all images on the internet that resemble it.

To open the Google Image search page:

Click Use to upload a Angelababy original image:

When you click Search, Google will find a similar image, with the higher the image similarity. Such as:

What is the principle of this technology? How does a computer know that two pictures are similar?

According to Dr. Neal Krawetz's explanation, the key technology for similar image search is called "Perceptual hashing Algorithm" (Perceptualhash algorithm), which generates a "fingerprint" (fingerprint) string for each image. Then compare the fingerprints of the different pictures. The closer the result is, the more similar the picture is.

The following is one of the simplest Java implementations:

Preprocessing: Reading pictures

[Java]View Plaincopyprint?
    1. File inputfile = newFile (filename);
    2. BufferedImage sourceimage = Imageio.read (inputfile); //Read picture file

The first step is to reduce the size.

Reduce the image to 8x8 's size, a total of 64 pixels. The role of this step is to remove the details of the picture, only the structure, shading and other basic information, discard the different sizes, proportions of the picture differences.

[Java]View Plaincopyprint?
    1. int width= 8;
    2. Intheight = 8;
    3. Targetw,targeth indicates the target length and width, respectively.
    4. int type= sourceimage.gettype (); //Picture type
    5. Bufferedimagethumbimage = null;
    6. Double sx= (double) width/sourceimage.getwidth ();
    7. Double sy= (double) height/sourceimage.getheight ();
[Java]View Plaincopyprint?
  1. Set the width and height of the image to the same length, whichever is shorter
  2. if (b) {
  3. if (SX > Sy) {
  4. sx= Sy;
  5. width= (int) (SX * SOURCEIMAGE.GETWIDTH ());
  6. }Else {
  7. sy= SX;
  8. height= (int) (SY * sourceimage.getheight ());
  9. }
  10. }
  11. Custom picture
  12. if (type== bufferedimage.type_custom) { //handmade
  13. COLORMODELCM = Sourceimage.getcolormodel ();
  14. Writablerasterraster = Cm.createcompatiblewritableraster (width,height);
  15. booleanalphapremultiplied = Cm.isalphapremultiplied ();
  16. thumbimage= New BufferedImage (cm, raster, alphapremultiplied, null);
  17. } Else {
  18. //Known pictures, such as Jpg,png,gif
  19. thumbimage= New BufferedImage (width, height, type);
  20. }
  21. Call drawing class drawing to reduce the size of the diagram
  22. GRAPHICS2DG = Target.creategraphics ();
  23. Smoother than Exlax:
  24. G.setrenderinghint (renderinghints.key_rendering, renderinghints.value_render_quality);
  25. G.drawrenderedimage (sourceimage,affinetransform.getscaleinstance (SX, SY));
  26. G.dispose ();

The second step is to simplify the color.

Converts the zoomed-in image to a level 64 grayscale. That is, all pixels have a total of 64 colors.

[HTML]View Plaincopyprint?
  1. int[]pixels = New int[width * height];
  2. for (inti = 0; I < width; i++) {
  3. for (int j = 0; J < height; j + +) {
  4. pixels[i* height + j] = Rgbtogray (Thumbimage.getrgb (i, j));
  5. }
  6. }
  7. /**
  8. * Gray Value Calculation
  9. * @param pixels color RGB value (red-green-blue Red green blue)
  10. * @return int Gray value
  11. */
  12. public static int Rgbtogray (int pixels) {
  13. int _alpha = (pixels >>) & 0xFF;
  14. int _red = (pixels >>) & 0xFF;
  15. int _green = (pixels >> 8) & 0xFF;
  16. int _blue = (pixels) & 0xFF;
  17. return (int) (0.3 * _red + 0.59 * _green + 0.11 * _blue);
  18. }

The third step is to calculate the average.

Calculates a grayscale average of all 64 pixels.

[Java]View Plaincopyprint?
    1. int avgpixel= 0;
    2. int m = 0;
    3. for (int i =0; i < pixels.length; ++i) {
    4. M +=pixels[i];
    5. }
    6. m = m/pixels.length;
    7. Avgpixel = m;

Fourth step, compare the grayscale of the pixel.

The grayscale of each pixel is compared to the average. Greater than or equal to the average, recorded as 1, less than the average, recorded as 0.

[Java]View Plaincopyprint?
    1. Int[] comps= new int[width * height];
    2. for (inti = 0; i < comps.length; i++) {
    3. if (Pixels[i] >= avgpixel) {
    4. comps[i]= 1;
    5. }Else {
    6. comps[i]= 0;
    7. }
    8. }

Fifth step, calculate the hash value.

By combining the results of the previous step, you make up a 64-bit integer, which is the fingerprint of the image. The order of the combinations is not important, just make sure all the pictures are in the same order.

= = 8f373714acfcf4d0

[HTML]View Plaincopyprint?
  1. Stringbufferhashcode = new StringBuffer ();
  2. for (inti = 0; I < comps.length; i+= 4) {
  3. intresult = comps[i] * (int) Math.pow (2, 3) + comps[i + 1] * (int) Math.pow (2, 2) + Comps[i + 2] * (int) Math.pow (2  , 1) + Comps[i + 2];
  4. Hashcode.append (Binarytohex (result));//binary into 16 binary
  5. }
  6. Stringsourcehashcode = hashcode.tostring ();

After getting the fingerprint, you can compare different pictures and see how many of the 64 bits are not the same. In theory, this equates to the calculation of "Hamming distance" (hammingdistance). If the data bits are not more than 5, the two images are similar, and if they are greater than 10, they are two different pictures.

[Java]View Plaincopyprint?
    1. int difference = 0;
    2. int Len =sourcehashcode.length ();
    3. for (inti = 0; i < len; i++) {
    4. if (Sourcehashcode.charat (i)! = Hashcode.charat (i)) {
    5. difference++;
    6. }
    7. }

You can put a few pictures together, and also calculate their Hamming distance comparison, you can see whether the two pictures are similar.

The advantages of this algorithm are simple and fast, not affected by the size of the picture, the disadvantage is that the contents of the picture can not be changed. If you add a few words to the picture, it will not be recognized. So, it's best to use thumbnails to find out the original image.

In practical applications, more powerful phash algorithms and sift algorithms are often used to identify the deformation of images. As long as the degree of deformation does not exceed 25%, they can match the original image. Although these algorithms are more complex, the principle is the same as the simple algorithm above, that is, to first convert the image into a hash string, and then compare.

Most of the above content directly from the Nanyi site copy, want to see the original children's shoes can go to the top link click to see.

Provide source download, source download link: http://download.csdn.net/detail/luohong722/3965112

Reference Links: Magic image processing algorithm, 11 similar image search engine recommendation, to map search will not be difficult, http://insidesearch.blogspot.com/2011/07/teaching-computers-to-see-image.html

Google Map-Similar image search principle-Java implementation

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.