Google Baidu image search & quot; perception Hash Algorithm & quot; C # simple implementation

Source: Internet
Author: User

/// <Summary>
/// Perceive the hash algorithm
/// </Summary>
Public class ImageComparer
{
/// <Summary>
/// Obtain the Hashcode of the image
/// </Summary>
/// <Param name = "imageName"> </param>
/// <Returns> </returns>
Public static string GetImageHashCode (string imageName)
{
Int width = 8;
Int height = 8;
 
// Step 1
// Reduce the image size to 8x8, with a total of 64 pixels. This step is used to remove the image details,
// Only basic information such as structure and brightness is retained, and image differences of different sizes and proportions are discarded.
Bitmap bmp = new Bitmap (Thumb (imageName ));
Int [] pixels = new int [width * height];
 
// Step 2
// Convert the reduced image to 64-level gray scale. That is to say, all pixels have only 64 colors in total.
For (int I = 0; I <width; I ++)
{
For (int j = 0; j {
Color color = bmp. GetPixel (I, j );
Pixels [I * height + j] = RGBToGray (color. ToArgb ());
}
}
 
// Step 3
// Calculate the average gray scale of all 64 pixels.
Int avgPixel = Average (pixels );
 
// Step 4
// Compare the gray scale of each pixel with the average value. If the value is greater than or equal to the average value, it is recorded as 1. If the value is smaller than the average value, it is recorded as 0.
Int [] comps = new int [width * height];
For (int I = 0; I <comps. Length; I ++)
{
If (pixels [I]> = avgPixel)
{
Comps [I] = 1;
}
Else
{
Comps [I] = 0;
}
}
 
// Step 5
// Combine the comparison results in the previous step to form a 64-bit integer, which is the fingerprint of the image. The order of the combination is not important, as long as all images are in the same order.
StringBuilder hashCode = new StringBuilder ();
For (int I = 0; I <comps. Length; I + = 4)
{
Int result = 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 ));
}
Bmp. Dispose ();
Return hashCode. ToString ();
}
 
/// <Summary>
/// Calculate the Hamming distance (Hamming distance ).
/// If the number of different data bits does not exceed 5, the two images are very similar. If the number is greater than 10, the two images are different.
/// </Summary>
/// <Param name = "sourceHashCode"> </param>
/// <Param name = "hashCode"> </param>
/// <Returns> </returns>
Public static int HammingDistance (String sourceHashCode, String hashCode)
{
Int difference = 0;
Int len = sourceHashCode. Length;
 
For (int I = 0; I <len; I ++)
{
If (sourceHashCode [I]! = HashCode [I])
{
Difference ++;
}
}
Return difference;
}
 
/// <Summary>
/// Scale the image www.2cto.com
/// </Summary>
/// <Param name = "imageName"> </param>
/// <Returns> </returns>
Private static Image Thumb (string imageName)
{
Return Image. FromFile (imageName). GetThumbnailImage (8, 8, () =>{ return false ;}, IntPtr. Zero );
}
 
/// <Summary>
/// Convert to 64-level gray scale
/// </Summary>
/// <Param name = "pixels"> </param>
/// <Returns> </returns>
Private static int RGBToGray (int pixels)
{
Int _ red = (pixels> 16) & 0xFF;
Int _ green = (pixels> 8) & 0xFF;
Int _ blue = (pixels) & 0xFF;
Return (int) (0.3 * _ red + 0.59 * _ green + 0.11 * _ blue );
}
 
/// <Summary>
/// Calculate the average value
/// </Summary>
/// <Param name = "pixels"> </param>
/// <Returns> </returns>
Private static int Average (int [] pixels)
{
Float m = 0;
For (int I = 0; I <pixels. Length; ++ I)
{
M + = pixels [I];
}
M = m/pixels. Length;
Return (int) m;
}
 
Private static char BinaryToHex (int binary)
{
Char ch = '';
Switch (binary)
{
Case 0:
Ch = '0 ';
Break;
Case 1:
Ch = '1 ';
Break;
Case 2:
Ch = '2 ';
Break;
Case 3:
Ch = '3 ';
Break;
Case 4:
Ch = '4 ';
Break;
Case 5:
Ch = '5 ';
Break;
Case 6:
Ch = '6 ';
Break;
Case 7:
Ch = '7 ';
Break;
Case 8:
Ch = '8 ';
Break;
Case 9:
Ch = '9 ';
Break;
Case 10:
Ch = 'a ';
Break;
Case 11:
Ch = 'B ';
Break;
Case 12:
Ch = 'C ';
Break;
Case 13:
Ch = 'D ';
Break;
Case 14:
Ch = 'E ';
Break;
Case 15:
Ch = 'F ';
Break;
Default:
Ch = '';
Break;
}
Return ch;
}
}

 


From Query!

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.