javascript图片相似度算法实现 js实现直方图和向量算法_javascript技巧

WBOY
Release: 2016-05-16 17:03:59
Original
1849 people have browsed it

复制代码代码如下:

function getHistogram(imageData) {
var arr = [];
for (var i = 0; i arr[i] = 0;
}
var data = imageData.data;
var pow4 = Math.pow(4, 2);
for (var i = 0, len = data.length; i 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 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 var gray = parseInt((data[i] + data[i + 1] + data[i + 2]) / 3);
data[i + 2] = data[i + 1] = data[i] = gray;
}
return imgData;
}

有个问题,假如图片是灰色的跟原图进行比较,那么要比较相似度,需要将图片都转成灰色的,即使用上面代码的gray函数来处理

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!