export function identifyStaticPixels(
framesData: ImageData[],
roi: { x: number; y: number; width: number; height: number },
rgbaStdDevThreshold: number,
luminanceStdDevThreshold: number,
outlierPercentage: number = 0.1 // 10% of frames can be outliers
): boolean[] {
if (framesData.length < 2) {
console.warn("identifyStaticPixels requires at least 2 frames for meaningful standard deviation analysis. Returning all false.");
return new Array(roi.width * roi.height).fill(false);
}
const staticPixelsMap = new Array(roi.width * roi.height).fill(false); // Initialize all to false
// Iterate over each pixel within the defined ROI
for (let py = 0; py < roi.height; py++) {
for (let px = 0; px < roi.width; px++) {
const currentPixelIdxInRoi = py * roi.width + px;
// Collect all R, G, B, A, and Luminance values for this specific pixel (roi.x+px, roi.y+py)
// across ALL sampled frames.
const rValues: number[] = [];
const gValues: number[] = [];
const bValues: number[] = [];
const aValues: number[] = [];
const lumValues: number[] = [];
for (let i = 0; i < framesData.length; i++) {
const globalX = roi.x + px;
const globalY = roi.y + py;
const pixelColor = getPixelRGBA(framesData[i], globalX, globalY);
rValues.push(pixelColor.r);
gValues.push(pixelColor.g);
bValues.push(pixelColor.b);
aValues.push(pixelColor.a);
lumValues.push(getLuminance(pixelColor.r, pixelColor.g, pixelColor.b));
}
// Calculate the median absolute deviation (MAD) for each collected channel and for luminance.
const rMad = calculateMAD(rValues);
const gMad = calculateMAD(gValues);
const bMad = calculateMAD(bValues);
const aMad = calculateMAD(aValues);
const lumMad = calculateMAD(lumValues);
// Determine the number of allowed outliers
const maxOutliers = Math.floor(framesData.length * outlierPercentage);
// A pixel is considered "static" if the number of frames exceeding the MAD threshold is within the allowed outliers.
const isPixelQuasiStatic =
countExceedingThreshold(rValues, rMad, rgbaStdDevThreshold) <= maxOutliers &&
countExceedingThreshold(gValues, gMad, rgbaStdDevThreshold) <= maxOutliers &&
countExceedingThreshold(bValues, bMad, rgbaStdDevThreshold) <= maxOutliers &&
countExceedingThreshold(aValues, aMad, rgbaStdDevThreshold) <= maxOutliers &&
countExceedingThreshold(lumValues, lumMad, luminanceStdDevThreshold) <= maxOutliers;
staticPixelsMap[currentPixelIdxInRoi] = isPixelQuasiStatic;
}
}
return staticPixelsMap;
}
function calculateMAD(values: number[]): number {
const median = values.sort((a, b) => a - b)[Math.floor(values.length / 2)];
const absoluteDeviations = values.map(value => Math.abs(value - median));
return absoluteDeviations.sort((a, b) => a - b)[Math.floor(absoluteDeviations.length / 2)];
}
function countExceedingThreshold(values: number[], mad: number, threshold: number): number {
return values.filter(value => Math.abs(value - median(values)) > mad * threshold).length;
}
function median(values: number[]): number {
values.sort((a, b) => a - b);
const half = Math.floor(values.length / 2);
return values.length % 2 !== 0 ? values[half] : (values[half - 1] + values[half]) / 2;
}
function getLuminance(r: number, g: number, b: number): number {
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
function getPixelRGBA(imageData: ImageData, x: number, y: number): { r: number, g: number, b: number, a: number } {
const index = (y * imageData.width + x) * 4;
return {
r: imageData.data[index],
g: imageData.data[index + 1],
b: imageData.data[index + 2],
a: imageData.data[index + 3]
};
}