Image Similarity Principle--java realization

Source: Internet
Author: User
Tags pow

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 an 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

File Inputfile == Imageio.read (inputfile); // reading a 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.

intWidth= 8; Intheight= 8;//Targetw,targeth indicates the target length and width, respectively .intType= Sourceimage.gettype ();//type of pictureBufferedimagethumbimage =NULL;DoubleSx= (Double) Width/sourceimage.getwidth ();DoubleSy= (Double) Height/sourceimage.getheight ();//set the width and height of the image to the same length, whichever is shorterif(b) {if(SX >Sy) {SX=Sy; Width= (int) (SX *sourceimage.getwidth ()); }Else{sy=SX; Height= (int) (SY *sourceimage.getheight ()); }}//Custom Pictureif(type== Bufferedimage.type_custom) {//HandmadeCOLORMODELCM =Sourceimage.getcolormodel (); Writablerasterraster=Cm.createcompatiblewritableraster (width,height); Booleanalphapremultiplied=cm.isalphapremultiplied (); Thumbimage=NewBufferedImage (cm, raster, alphapremultiplied,NULL); } Else {     //known pictures, such as Jpg,png,gifThumbimage=Newbufferedimage (width, height, type);}//call drawing class drawing to reduce the size of the diagramGRAPHICS2DG =target.creategraphics ();//smoother than Exlax:G.setrenderinghint (renderinghints.key_rendering, renderinghints.value_render_quality); G.drawRenderedImage ( Sourceimage,affinetransform.getscaleinstance (SX, SY)); 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.

int[]pixels =New int[Width *height]; for(inti = 0; i < width; i++) {       for(intj = 0; J < height; J + +) {pixels[i* height + j] =Rgbtogray (Thumbimage.getrgb (i, j)); }}/*** Gray Value calculation *@parampixels color RGB Value (red-green-blue red-green-blue) *@returnint Grayscale Value*/ Public Static intRgbtogray (intpixels) {       //int _alpha = (pixels >>) & 0xFF;       int_red = (pixels >>) & 0xFF; int_green = (pixels >> 8) & 0xFF; int_blue = (pixels) & 0xFF; return(int) (0.3 * _red + 0.59 * _green + 0.11 *_blue);}

The third step is to calculate the average.

Calculates a grayscale average of all 64 pixels.

int avgpixel= 0; int m = 0;  for (int i =0; i < pixels.length; + +i)      {+= =m/= 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.

int New int [Width * height];  for (inti = 0; i < comps.length; i++) {    if(pixels[i] >= avgpixel) {        comps[i]= 1  ;    } Else {        comps[i]= 0;    }}

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

New StringBuffer ();  for (inti = 0; i < comps.length; i+= 4) {      = 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];      Hashcode.append (Binarytohex (Result)); // Binary into 16 binary  = 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.

int difference = 0; int len =sourcehashcode.length ();         for (inti = 0; i < len; i++) {   if(Sourcehashcode.charat (i)! = Hashcode.charat (i)) {       Difference+ +;   }}

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.

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

Image Similarity Principle--java realization

Related Article

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.