Question

I can get a measure of the difference between two images loaded in canvas with something like:

<canvas id="canvas1" width="255" height="255"></canvas>
<canvas id="canvas2" width="255" height="255"></canvas>

<script>

// THIS FUNCTION IS THE MOST RELEVANT BIT:
function getSumSq (ctx1, ctx2) {
  var
    i,
    sumSq = 0,
    w = 255, h= 255,
    numPixelComponents = w * h * 4,
    data1 = ctx1.getImageData(0, 0, w, h).data,
    data2 = ctx2.getImageData(0, 0, w, h).data;

  for (i = 0; i < numPixelComponents - 3; i += 4) {
    sumSq += Math.pow(data1[i] - data2[i], 2) +
      Math.pow(data1[i + 1] - data2[i + 1], 2) +
      Math.pow(data1[i + 2] - data2[i + 2], 2);
    // no transparency so ignore i + 3, the alpha channel.
  }

  return sumSq;
}

var c1 = document.getElementById('canvas1');
var ctx1 = c1.getContext('2d');
var img1 = new Image();
img1.src = 'img1.png';
img1.onload = function() {
  ctx1.drawImage(img1, 0, 0);
  var c2 = document.getElementById('canvas2');
  var ctx2 = c2.getContext('2d');
  var img2 = new Image();
  img2.src = 'img2.png';
  img2.onload = function() {
    ctx2.drawImage(img2, 0, 0);
    console.log(getSumSq(ctx1, ctx2));
  };
};

</script>

I want to do this for lots of images, using canvas. Does any browser support a hardware-accelerated way of measuring the difference between two images loaded in canvas?

Was it helpful?

Solution

I can't find a way to do this - guessing the answer is no.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top