Original text: blog.csdn.net/ybdesire/ar…

1. The introduction of

Reference 1 introduces the basic principle of similarity image search, with the help of milvus (Reference 2) such a similarity search engine, we can very quickly achieve similarity search.

But before realizing the search, the image needs to be transformed into feature vector. The mean value hashing introduced in this paper is one of the features of images.

2. Calculation process of mean value hashing

  1. Read the image and convert it to grayscale
import numpy as np
import cv2
img = cv2.imread('Alyson_Hannigan_200512.jpg', cv2.IMREAD_GRAYSCALE)# IMREAD_GRAYSCALE, IMREAD_COLOR
Copy the code

The picture is from Reference 4.

  1. Resize Image size

Images need to be uniformly converted to a fixed size, so that images of different sizes can get a fixed size hash value

img2 = cv2.resize(img, (8.8), interpolation=cv2.INTER_AREA)
Copy the code

In resize, different interpolation method is used and the result will be somewhat different. The optional value is

  • INTER_NEAREST — a nearest-neighbor Interpolation.
  • INTER_LINEAR — a Bilinear Interpolation (Used by Default)
  • INTER_AREA – resampling using pixel area relation. …
  • INTER_CUBIC — a Bicubic Interpolation over 4 x 4 Pixel Neighborhood.
  • INTER_LANCZOS4 — A Lanczos Interpolation over 8 x 8 Pixel Neighborhood.

Here, the image size is fixed at 8×8. If you adjust this size, you get different lengths of hashes.

  1. Take the mean value of gray value
ave = np.mean(img2)
Copy the code
  1. You get the binary hash

If the pixel value is greater than the gray mean, set it to 1; otherwise, set it to 0.

hash_bin = ' '
for i in range(img2.shape[0) :for j in range(img2.shape[1]):
        value = img2[i][j]
        if value>ave:
            hash_bin+='1'
        else:
            hash_bin+='0'
print(hash_bin)# 1110011111000111110010111100001011000100111101001111100011101000
print(len(hash_bin))# 64
Copy the code

As you can see here, since the image size is fixed at 8×8, you end up with a 64-bit hash.

  1. Binary string is converted to hexadecimal string

A binary string can be converted to a hexadecimal string using the following program:

hash_hex = format(int(hash_bin, 2),'x')# bin to hex
hash_hex.rjust(16.'0')# fixed to 16 hex values with zero pending
print(hash_hex)# e7c7cbc2c4f4f8e8
print(len(hash_hex))# 16
Copy the code

The resulting “e7C7CBC2C4F4F8E8” is the mean hash value.

The most similar (closest) image can be found by comparing it with the mean hashing of other images according to Euclidean distance isometric calculation method.

3. Calculation of the distance between two mean hashes

Given two hashing hexadecimal strings, calculate the Hamming distance:

# hex string 
ahash_hex_1 = "e7c7cbc2c4f4f8e8"
ahash_hex_2 = "8a0303f6df3ec8cd"
scale = 16
# convert hex to binary 
ahash_bin_1 = bin(int(ahash_hex_1, scale)).zfill(64) # fixed 64 bit for this paper
ahash_bin_2 = bin(int(ahash_hex_2, scale)).zfill(64) 
# binary difference calculation
diff = 0
for i in range(len(ahash_bin_1)):
    b1 = ahash_bin_1[i]
    b2 = ahash_bin_2[i]
    ifb1! =b2: diff+=1
print(diff) # 27
Copy the code

From this we can see that the difference between the two hashes is 27.

4. The pros and cons of mean hashing

  1. advantages
  • Simple, fast calculation
  • The mean hash value of the image is almost the same after scaling
    • After the final resize, the mean value and each pixel value are almost unchanged
  • Linearly increase or decrease the brightness, the mean value of the image hash is the same
    • Because the difference between each pixel value and the mean is almost constant
  1. disadvantages

A nonlinear increase or decrease in brightness of an image will result in a large change in the mean value of the hash. The nonlinearity mentioned here means that the brightness of all pixel values is not increased/decreased by the same multiple, but for example, the gray value of [0,120] is increased by 50, and the gray value of [121,255] is decreased by 20. Such changes will lead to a great change in the difference between each pixel value and the mean value. For example, after gamma correction of the image, the mean value of the hash changes a lot.

5. To summarize

The average hash algorithm, or aHash, is the simplest perceptual hash algorithm.

This paper gives the source code of the calculation process, and also analyzes its advantages and disadvantages.

More complex hashing procedures will be published later in this article.

For more similar image search methods, see Reference 3. For the methods used in this article and more details, see Reference 4.

6. Reference

  1. www.ruanyifeng.com/blog/2011/0…
  2. milvus.io/
  3. www.ruanyifeng.com/blog/2013/0…
  4. www.hackerfactor.com/blog/index….