JavaScript image similarity algorithm realizes _javascript technique of histogram and vector algorithm in JS

Source: Internet
Author: User

Copy Code code as follows:

function Gethistogram (imagedata) {
var arr = [];
for (var i = 0; i < 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, if the picture is gray to compare with the original, then to compare the similarity, you need to turn the picture into Gray, that is, using the above code gray function to deal with

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.