Android - Implementation of a Similar Image Search Algorithm



Algorithm

Reduce size.

Scale down the image to 8x8 size, 64 pixels in total. The function of this step is to remove the details of the picture, retain only the basic information such as structure, light and shade, and discard the picture differences caused by different sizes and proportions.

Simplify colors.

Convert the reduced image to 64-level grayscale. That is, there are only 64 colors in total for all pixels.

Calculate the average.

Calculate the grayscale average of all 64 pixels.

Compare the grayscale of pixels.

Compare the grayscale of each pixel with the average. If it is greater than or equal to the average value, it is recorded as 1; if it is less than the average value, it is recorded as 0.

Calculate the hash value.

Combining the comparison results of the previous step together forms a 64-bit integer, which is the fingerprint of this picture. The order of composition is not important, just make sure that all images are in the same order.

Compare fingerprints

to see how many of the 64 bits are different. In theory, this is equivalent to calculating the "Hamming distance". If the different data bits are not more than 5, it means that the two pictures are very similar; if it is greater than 10, it means that these are two different pictures.

Implement key points

Calculate grayscale
private static double calculateGrayValue(int pixel) {
    int red = (pixel >> 16) & 0xFF;
    int green = (pixel >> 8) & 0xFF;
    int blue = (pixel) & 255;
    return 0.3 * red + 0.59 * green + 0.11 * blue;
}

Hamming distance The

final fingerprint is actually a binary number of 0101. For example,

111000
111111

, the Hamming distance of these two numbers is actually the number of 1s after the ^ operation.

private static int hamDist(long finger1, long finger2) {
    int dist = 0;
    long result = finger1 ^ finger2;
    while (result != 0) {
        ++dist;
        result &= result - 1;
    }
    return dist;
}


Source

codehttps ://github.com/gavinliu/SimilarPhotoReferences The principle of similar

image search

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326358992&siteId=291194637
Recommended