pixelmatch icon indicating copy to clipboard operation
pixelmatch copied to clipboard

Can you please explain what threshold means?

Open gladykov opened this issue 7 months ago • 1 comments

Hi!

Thank for your nice utility! Can you please explain what threshold means ? Is it like 10% of difference ignored when it is 0.1, and 100% when it is set to 1 ?

gladykov avatar May 12 '25 17:05 gladykov

I checked the source and it looks like threshold is the amount that one pixel can be different from another pixel. If it's over the threshold, it's a different pixel and added to the number of different pixels.

If you want to stay within a certain ratio, take the number of different pixels and compare it against the total pixel area, kind of like this:

const numDiffPixels = pixelmatch(
    img1.data,
    img2.data,
    diff.data,
    img1.width,
    img1.height,
    { threshold: 0.4 } // Adjust sensitivity here
);

const pixelArea = img1.width * img1.height;
const maxDiffPixelsByRatio = pixelArea * 0.4;

if (numDiffPixels > maxDiffPixelsByRatio) {
  yellAboutIt();
}

jhned avatar Aug 18 '25 20:08 jhned