Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Hash algorithm for Image similarity

Shulou Source: shulou.com Published: 2022-06-01 04:02:58 09月22日 Update

This article mainly introduces the example analysis of image similarity Hash algorithm, which is very detailed and has a certain reference value. Interested friends must read it!

Image similarity Hash algorithm

There are three kinds of Hash algorithms, which are average hash algorithm (aHash), perceptual hash algorithm you (pHash) and differential hash algorithm (dHash).

This paper implements the average hash algorithm.

1 average hash algorithm (aHash) 1.1 algorithm steps

The average hash algorithm is the simplest of the three Hash algorithms. It obtains the hash value of the picture through the following steps: (1) zoom the picture; (2) convert the grayscale image; (3) calculate the pixel mean; and (4) calculate the fingerprint according to the similar mean. The specific algorithm is as follows:

Table 1 aHash algorithm to get the hash value of the picture

Zoom the picture

The size of the input picture is different. In order to unify the input of the picture, the size of the picture is scaled to 8x8, and a total of 64 pixels are obtained.

Convert grayscale image

Some of the input pictures are single-channel grayscale images, some RGB three-channel color images, and some RGBA four-channel color images. In order to unify the next input standard, all non-single-channel images are converted into single-channel grayscale images. Among them, RGB three-channel to single-channel algorithm has the following:

1. Floating-point algorithm: Gray=R0.3+G0.59+B0.11

two。 Integer method: Gray= (R30+G59+B11) / 100

3. Shift method: Gray = (R76+G151+B*28) > > 8

4. Average method: Gray= (R+G+B) / 3

5. Green only: Gray=G

Calculate the pixel mean

Through the previous step, we can get an integer matrix G of 8x8, and calculate the average value of all elements in this matrix, assuming that its value is a.

Calculate fingerprints according to pixel mean

Initialize the ahash of the input picture = ""

Traversing matrix G row by row from left to right if row I j column element G (iMaginj) > = a, then ahash + = "1" if row I j column element G (iMaginj) avg); finally bmp.Free; end; end; 1: / / pHash aware hash algorithm begin end; 2: / / dHash differential hash algorithm begin end; end Result: = ret;end

Calculate the hamming distance function:

Function TForm1.Hamming (Hash2, Hash3: Int64): Integer;var A: Int64;begin Result: = 0; A: = Hash2 xor Hash3; while A0 do begin A: = An and (Amur1); Inc (Result); end;end; is all the contents of this article "sample Analysis of Image similarity Hash algorithm". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Algorithm picture hash similarity pixel grayscale input mean single channel image element average step matrix channel hamming example analysis one line content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple Docker NVidia Shulou Information