The principle is similar image search (ii) a copy of: Ruan Yifeng]

Thanks to the reprint of this Sina blog, I found Dadao Ruan’s blog: http://blog.sina.cn/dpool/blog/s/blog_b906c1070102vjo5.html
I didn’t expect that the technology I used when I was doing a picture sniffing dog I wrote it clearly in my blog several years ago (I used the leftover things I used to play with...)... I feel a little bit "we hate seeing each other late" on this blog.

Many links in this reprint are lost when pasted, you can go directly to the source blog to see:
Principles of Similar Image Search (2)
http://www.ruanyifeng.com/blog/2013/03/similar_image_search_part_ii.html


Author: Ruan Yifeng
Date: March 31, 2013
------------

Two years ago, I wrote "Principles of Similar Image Search" and introduced one of the simplest implementation methods.
(Reprinted note: here is the so-called "Phash", that is, "perceived hash algorithm", this is the "texture" I mentioned in the sniffing dog blog. This post is also very clear, I suggest you click in and read it)

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

Insert picture description here

1. Color distribution method

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

Insert picture description here

Any color is composed of the three primary colors of red, green and blue (RGB), so there are 4 histograms in the above picture (the histogram of the three primary colors + the final composite histogram).

If each primary color can take 256 values, then the entire color space has 16 million colors (256 cubed). For these 16 million color comparison histograms, the amount of calculation is too large, so a simplified method is needed. 0-255 can be divided into four areas: 0-63 is the 0th area, 64-127 is the first area, 128-191 is the second area, and 192-255 is the third area. This means that there are 4 areas for red, green and blue, and a total of 64 combinations (4 to the 3rd power) can be formed.

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

Insert picture description here

The figure above is the color distribution table of a certain picture. The last column in the table is extracted to form a 64-dimensional vector (7414, 230, 0, 0, 8, …, 109, 0, 0, 3415, 53929). This vector is the feature value or "fingerprint" of this picture.

Therefore, finding similar pictures becomes finding the vector that is most similar to it. This can be calculated using Pearson's correlation coefficient or cosine similarity.

2. Content feature method

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

First, convert the original image into a smaller grayscale image, assuming 50x50 pixels. Then, determine a threshold and convert the grayscale image into a black and white image.

Insert picture description here

If two pictures are very similar, their black and white outlines should be similar. So, the question becomes, how to determine a reasonable threshold in the first step to correctly present the contours in the photo?

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

In 1979, Japanese scholar Otsu Zhanzhi proved that "the smallest difference within a class" and "the largest difference between classes" are the same thing, that is, corresponding to the same threshold. He proposed a simple algorithm to find this threshold, which is called "Otsu's method". The following is his calculation method.

Assuming that a picture has n pixels in total, there are n1 pixels with a gray value less than the threshold, and n2 pixels with a gray value greater than or equal to the threshold (n1 + n2 = n). w1 and w2 represent the respective proportions of these two types of pixels.

w1 = n1 / n
w2 = n2 / n

Suppose again that the average value and variance of all pixels with gray values ​​less than the threshold are μ1 and σ1, and the average and variance of all pixels with gray values ​​greater than or equal to the threshold are μ2 and σ2, respectively. So, you can get

Difference within class = w1 (square of σ1) + w2 (square of σ2)
Difference between classes = w1w2(μ1-μ2)^2

It can be proved that these two formulas are equivalent: getting the minimum value of "intra-class difference" is equivalent to getting the maximum value of "inter-class difference". However, from the perspective of calculation difficulty, the latter calculation is easier.

In the next step, use the "exhaustive method" to take the threshold from the lowest value to the highest value of the gray scale, and then substitute them into the above formulas. The value that makes "the smallest difference within a class" or "the largest difference between classes" is the final threshold. For specific examples and Java algorithms, please see here.

Insert picture description here

With a 50x50 pixel black and white thumbnail, it is equivalent to having a 50x50 0-1 matrix. Each value of the matrix corresponds to a pixel of the original image, 0 means black, 1 means white. This matrix is ​​the feature matrix of a picture.

The fewer differences between the two feature matrices, the more similar the two images. This can be achieved with "exclusive OR operation" (that is, if only one of the two values ​​is 1, the operation result is 1, otherwise the operation result is 0). Perform an "exclusive OR" operation on the feature matrix of different pictures, the less 1 in the result, the more similar pictures.

(Finish)

Document information
Copyright notice: Freely reprinted-non-commercial-non-derivative-keep the signature (Creative Commons 3.0 license)
Release date: March 31, 2013

Guess you like

Origin blog.csdn.net/sinat_27382047/article/details/105524885