Skip to content

Instantly share code, notes, and snippets.

@andrevenancio
Last active July 5, 2016 02:36
Show Gist options
  • Save andrevenancio/720fa05d1801289c560138a339528ed8 to your computer and use it in GitHub Desktop.
Save andrevenancio/720fa05d1801289c560138a339528ed8 to your computer and use it in GitHub Desktop.
Returns the correlation between two data sets with the same width and height. 0% completely different, 100% same image. (ES6)
correlation(curData, oldData, minCorrelation) {
const sampledDataWidth = 80;
const sampledDataHeight = 60;
let count = 0;
const total = curData.data.length;
for (let i = 0; i < total; i += 4) {
// sampling the R channel, since the images are grayscaled and threshold samplying any of the channels will give the same results.
if (curData.data[i] !== oldData.data[i]) {
count++;
}
}
const correlation = count / (sampledDataWidth * sampledDataHeight);
if (correlation > minCorrelation) {
// blink detected
}
return correlation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment