Javascript image similarity algorithm to implement histogram and vector in javascript _ javascript skills

Source: Internet
Author: User
This article describes how to use javascript to implement image similarity algorithms. The Code is as follows:


Function getHistogram (imageData ){
Var arr = [];
For (var I = 0; I <64; I ++ ){
Arr [I] = 0;
}
Var data = imageData. data;
Var pow4 = Math. pow (4, 2 );
For (var I = 0, len = data. length; I <len; I ++ = 4 ){
Var red = (data [I]/64) | 0;
Var green = (data [I + 1]/64) | 0;
Var blue = (data [I + 2]/64) | 0;
Var index = red * pow4 + green * 4 + blue;
Arr [index] ++;
}

Return arr;
}

Function cosine (arr1, arr2 ){
Var axb = 0,
A = 0,
B = 0;
For (var I = 0, len = arr1.length; I <len; I ++ ){
Axb + = arr1 [I] * arr2 [I];
A + = arr1 [I] * arr1 [I];
B + = arr2 [I] * arr2 [I];
}
Return axb/(Math. sqrt (a) * Math. sqrt (B ));
}
Function gray (imgData ){
Var data = imgData. data;
For (var I = 0, len = data. length; I <len; I ++ = 4 ){
Var gray = parseInt (data [I] + data [I + 1] + data [I + 2])/3 );
Data [I + 2] = data [I + 1] = data [I] = gray;
}
Return imgData;
}

There is a problem: when the fake image is gray and compared with the source image, to compare the similarity, You need to convert the image to gray, that is, use the gray function of the above Code to process it.

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.