You're absolutely right – the current implementation is too strict, treating any deviation in a single frame as a deal-breaker. For transparent watermarks with varying brightness, we need a more forgiving and adaptive approach. Here's a revised strategy that focuses on temporal consistency patterns rather than absolute thresholds:
// watermarkUtils.ts (Enhanced Robust Version)
export function identifyStaticPixels(
framesData: ImageData[],
roi: { x: number; y: number; width: number; height: number },
rgbaConsistencyThreshold: number, // Percentage of frames that must be within stdDev
luminanceConsistencyThreshold: number, // Percentage of frames for luminance
minRequiredFrames: number = 2 // Minimum frames needed for analysis
): boolean[] {
if (framesData.length < minRequiredFrames) {
console.warn(`Insufficient frames (${framesData.length}). Need at least ${minRequiredFrames}. Returning all false.`);
return new Array(roi.width * roi.height).fill(false);
}
const staticPixelsMap = new Array(roi.width * roi.height).fill(false);
const totalFrames = framesData.length;
for (let py = 0; py < roi.height; py++) {
for (let px = 0; px < roi.width; px++) {
const pixelValues: { r: number[], g: number[], b: number[], a: number[], lum: number[] } = {
r: [], g: [], b: [], a: [], lum: []
};
// Collect values across all frames
for (let frame of framesData) {
const { r, g, b, a } = getPixelRGBA(frame, roi.x + px, roi.y + py);
const lum = getLuminance(r, g, b);
pixelValues.r.push(r);
pixelValues.g.push(g);
pixelValues.b.push(b);
pixelValues.a.push(a);
pixelValues.lum.push(lum);
}
// Calculate consistency ratios
const consistency = {
r: calculateConsistencyRatio(pixelValues.r, rgbaConsistencyThreshold),
g: calculateConsistencyRatio(pixelValues.g, rgbaConsistencyThreshold),
b: calculateConsistencyRatio(pixelValues.b, rgbaConsistencyThreshold),
a: calculateConsistencyRatio(pixelValues.a, rgbaConsistencyThreshold),
lum: calculateConsistencyRatio(pixelValues.lum, luminanceConsistencyThreshold)
};
// Adaptive decision making
const isStatic =
(consistency.r + consistency.g + consistency.b) / 3 >= rgbaConsistencyThreshold &&
consistency.a >= rgbaConsistencyThreshold &&
consistency.lum >= luminanceConsistencyThreshold;
staticPixelsMap[py * roi.width + px] = isStatic;
}
}
return staticPixelsMap;
}
// Helper function to calculate consistency ratio
function calculateConsistencyRatio(values: number[], thresholdFactor: number): number {
if (values.length < 2) return 0;
const stdDev = calculateStdDev(values);
const mean = calculateMean(values);
const threshold = mean * thresholdFactor;
const consistentFrames = values.filter(v => Math.abs(v - mean) <= threshold).length;
return consistentFrames / values.length;
}
// Helper functions (assuming these exist or need implementation)
function calculateStdDev(arr: number[]): number { /* ... */ }
function calculateMean(arr: number[]): number { /* ... */ }
function getPixelRGBA(imageData: ImageData, x: number, y: number): { r: number, g: number, b: number, a: number } { /* ... */ }
function getLuminance(r: number, g: number, b: number): number { /* ... */ }