Principles of similar Image Search (2)

Source: Internet
Author: User
Two years ago, I wrote "principles of similar image search" and introduced a simple implementation method. Yesterday, I saw on the isnowfy website that there are two other methods that are also very simple. Here are some notes. 1. the color distribution method each image can generate a color distribution histogram (colorhistogram ). If the histograms of the two images are very close, they can be considered very similar.

Two years ago, I wrote "principles of similar image search" and introduced a simple implementation method.

Yesterday, I saw on the isnowfy website that there are two other methods that are also very simple. Here are some notes.

I. color distribution method

Each image can generate a color histogram ). If the histograms of the two images are very close, they can be considered very similar.

Any color is composed of three primary colors (RGB), red, green, and blue, so there are a total of four histograms (three primary color histogram + The Last merged histogram ).

If 256 values can be set for each primary color, the total color space is 16 million colors (256 to the power of three ). For the comparison histograms of these 16 million colors, the calculation workload is too large. Therefore, a simplified method is required. You can set 0 ~ 255 divided into four zones: 0 ~ Zone 63 is 0th, 64 ~ 127: Zone 1st, 128 ~ 191: Zone 2nd, 192 ~ 255 is zone 3rd. This means that there are four zones in red, green, and blue respectively, and a total of 64 combinations can be formed (the 3rd power of 4 ).

Any color must belong to one of the 64 combinations, so that you can count the number of pixels contained in each combination.

Is the color distribution table of an image. The last column in the table is extracted to form a 64-dimensional vector (7414,230, 0, 0, 8 ,..., 109, 0, 0, 3415,539 29 ). This vector is the feature value of this image or "fingerprint ".

Therefore, searching for similar images becomes the most similar vector. This can be calculated using Pearson correlation coefficient or cosine similarity.

II. content feature method

In addition to color composition, you can also start by comparing the similarity of the image content.

First, convert the source image into a small grayscale image, which is assumed to be 50x50 pixels. Then, determine a threshold value and convert the grayscale image into a black-and-white image.

If the two images are similar, their black and white outlines should be similar. As a result, the question becomes: how to determine a reasonable threshold value for the first step to correctly present the outlines in the photo?

Obviously, the larger the contrast between the foreground color and the background color, the more obvious the outline. This means that if we find a value, we can minimize the "intra-class variance" (minimizing the intra-class variance) of the foreground and background colors ), or "maximizing the inter-class variance", the value is the ideal threshold.

In 1979, the Japanese scholar Dajin demonstrated that "minimum intra-class differences" and "maximum inter-class differences" are the same thing, that is, they correspond to the same threshold. He proposed a simple algorithm to obtain the threshold value, which is called the Otsu's method ). The following is his calculation method.

Assume that an image has n pixels in total, among which n1 are pixels whose gray value is less than the threshold, and n2 are pixels whose gray value is greater than or equal to the threshold (n1 + n2 = n ). W1 and w2 indicate the specific proportions of the two pixels.

W1 = n1/n

W2 = n2/n

Assume that the average and variance of all pixels whose gray values are less than the threshold are μ1 and σ 1, respectively, the average and variance of all pixels whose gray values are greater than or equal to the threshold values are μ2 and σ 2, respectively. Therefore, you can obtain

Intra-class variance = w1 (σ1's Square) + w2 (σ 2's Square)

Inter-class variance = w1w2 (μ1-μ2) ^ 2

It can be proved that the two formulas are equivalent: get the minimum value of "intra-class variance", equivalent to get the maximum value of "inter-class variance. However, in terms of computing difficulty, the latter is easier to calculate.

In the next step, we use the "exhaustive method" to extract the threshold value from the lowest gray-scale value to the highest value, and import them into the formula above. The value that minimizes the "intra-class variance" or "maximum inter-class variance" is the final threshold. For specific examples and Java algorithms, see here.

A 50x50 pixel black-and-white thumbnail is equivalent to a 50x50 0-1 matrix. Each value of the matrix corresponds to a pixel of the source image. 0 indicates black and 1 indicates white. This matrix is the feature matrix of an image.

The fewer differences between the two feature matrices, the more similar the two images are. This can be implemented using the "XOR operation" (that is, if only one of the two values is 1, the calculation result is 1; otherwise, the calculation result is 0 ). Perform "exclusive or operation" on the feature matrix of different images. the less 1 in the result, the more similar the image is.

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.