Calculate the similarity between two black and white images

Source: Internet
Author: User

If there are two black and white images with a resolution of 32x32, what should we do to calculate the similarity between the two images?

Based on thisArticleIntroduction to the 12-cosine theorem of the beautiful mathematical series and the classification of news, we only need to calculate two 1024 bits (32x32 = 1024) the cosine of the angle between vectors. The closer the result is to 1, the higher the similarity.

Well, we have a theoretical foundation. Let's talk about how to store our vectors.

Because there are only two colors in the image, one-bit binary is sufficient. The white point is regarded as 0, and the black point is 1. In this way, each image can be placed in 32 32-bit integers. Each row is represented by an integer, which saves both space and reduces the operation complexity.

Next we will discuss how to calculate.

If the formula below is used for calculation, we need to retrieve the values of the corresponding digits in the integer and then multiply them by square or separately. Obviously, this method is a waste of time.

Let's see if there is any easy way.

Because we only have 0 or 1 values, the corresponding bits in the numerator can be converted to corresponding bits for calculation, and because we use integer storage, therefore, the calculation is further simplified to bitwise AND of two integers.

In the denominator, the bitwise is squared and then the sum is used to save the square operation and add the bitwise operation directly.

Therefore, the entireProgramYou can follow the steps below:

1. Store each image in pixels in a 32-bit 32-bit integer array. Each integer stores one row. Each integer stores one pixel value (0 or 1 );

2. Calculate the numerator by bitwise and the integers in the two arrays according to the corresponding indexes, and then add the calculation results by bitwise;

3. When the denominator is calculated, All integers in each array are added by bit, followed by the root number and then multiplied;

4. Divide the numerator by the denominator to obtain the cosine value.

 

Steps 3, 3, and 4CodeAs follows:

 

Code

Public   Double Getcosine ( Int [] E1, Int [] E2)
{
Int A =   0 ; // Denominator 1
Int B =   0 ; // Denominator 2
Int C =   0 ; // Numerator
For ( Int Y =   0 ; Y <   32 ; ++ Y)
{
// The integers in the two arrays are bitwise AND
Int I = E2 [y] & E1 [y];
// Add by bit
For ( Int X =   1 ; X <   33 ; ++ X)
{
C + = (I > X) &   1 ;
A + = (E2 [y] > X) &   1 ;
B + = (E1 [y] > X) &   1 ;
}
}

//Calculate denominator
IntD=A*B;

ReturnD= 0 ? 0: C/Math. SQRT (d );
}

 

 

If you see this, you already know how to calculate the similarity between the two images. Based on my experience, the calculation result exceeds 0.8, and you can think that the two images are the same.

If you replace the image with the verification code, and you happen to know the value of one of the verification codes, you now know the value of the other verification code.

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.