Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support diff colored visualisation #14

Open
lifeart opened this issue May 23, 2023 · 0 comments
Open

Support diff colored visualisation #14

lifeart opened this issue May 23, 2023 · 0 comments

Comments

@lifeart
Copy link
Owner

lifeart commented May 23, 2023

ref:
telegram-cloud-photo-size-2-5434150931555010562-y
https://www.robots.ox.ac.uk/~vgg/software/imcomp/

Sample:

// Assuming img1 and img2 are your two images and they have the same dimensions
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
canvas.width = img1.width;
canvas.height = img1.height;

ctx.drawImage(img1, 0, 0);
let imgData1 = ctx.getImageData(0, 0, canvas.width, canvas.height);

ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img2, 0, 0);
let imgData2 = ctx.getImageData(0, 0, canvas.width, canvas.height);

let imgDataResult = ctx.createImageData(canvas.width, canvas.height);

for (let i = 0; i < imgData1.data.length; i += 4) {
  let r1 = imgData1.data[i];
  let g1 = imgData1.data[i + 1];
  let b1 = imgData1.data[i + 2];
  
  let r2 = imgData2.data[i];
  let g2 = imgData2.data[i + 1];
  let b2 = imgData2.data[i + 2];
  
  // calculate the color difference
  let diff = Math.abs(r1 - r2) + Math.abs(g1 - g2) + Math.abs(b1 - b2);
  
  if (diff == 0) {
    // the pixels are the same, set to grayscale
    let grayscale = 0.3 * r1 + 0.59 * g1 + 0.11 * b1;
    imgDataResult.data[i] = grayscale;
    imgDataResult.data[i + 1] = grayscale;
    imgDataResult.data[i + 2] = grayscale;
  } else {
    // the pixels are different, set to blue or red
    imgDataResult.data[i] = r1 > r2 ? 255 : 0; // red channel
    imgDataResult.data[i + 1] = 0; // green channel
    imgDataResult.data[i + 2] = r1 < r2 ? 255 : 0; // blue channel
  }
  imgDataResult.data[i + 3] = 255; // alpha channel
}

ctx.putImageData(imgDataResult, 0, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant