The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.
Rust ffmpegfree-video-editormodel-context-protocolnon-linear-editoroffline-firstopen-source-video-editorself-hostedvideo-editingvideo-editing-automationvideo-editing-softwarevideo-editing-toolsvideo-editorvoice-cloningyoutube-automationyoutube-shorts
test: deflicker experiments
Jareer4eac27a0e07c
diff --git a/test/deflicker/diagnose.py b/test/deflicker/diagnose.pynew file mode 100644index 00000000..66144fcd--- /dev/null+++ b/test/deflicker/diagnose.py@@ -0,0 +1,725 @@+#!/usr/bin/env python3++import sys+import cv2+import numpy as np++RINGS = 20+ANALYSIS_W = 320+SECTORS = 4++SECTOR_NAMES = [+ "RIGHT",+ "BOTTOM",+ "LEFT",+ "TOP",+]+++def luminance(frame):+ f = frame.astype(np.float32) / 255.0++ return (+ 0.0722 * f[:, :, 0]+ + 0.7152 * f[:, :, 1]+ + 0.2126 * f[:, :, 2]+ )+++def robust_median(values):+ if values.size == 0:+ return np.nan++ lo = np.percentile(values, 10)+ hi = np.percentile(values, 90)++ trimmed = values[+ (values >= lo) &+ (values <= hi)+ ]++ if trimmed.size == 0:+ return float(np.median(values))++ return float(np.median(trimmed))+++def make_geometry(h, w):+ """+ Elliptical radius instead of corner-normalized Euclidean radius.++ radius = 1 at:+ left/right image edge+ top/bottom image edge++ This means every radial ring contains meaningful pixels on a+ rectangular video.+ """++ y, x = np.mgrid[0:h, 0:w].astype(np.float32)++ cx = (w - 1) * 0.5+ cy = (h - 1) * 0.5++ nx = (x - cx) / max(cx, 1.0)+ ny = (y - cy) / max(cy, 1.0)++ radius = np.sqrt(+ nx * nx ++ ny * ny+ )++ # Angle:+ #+ # 0 = right+ # pi/2 = bottom+ # +/- pi = left+ # -pi/2 = top++ angle = np.arctan2(+ ny,+ nx+ )++ return radius, angle+++def sector_masks(angle):+ """+ Divide frame into four 90-degree directional sectors.+ """++ pi = np.pi++ right = (+ (angle >= -pi / 4) &+ (angle < pi / 4)+ )++ bottom = (+ (angle >= pi / 4) &+ (angle < 3 * pi / 4)+ )++ left = (+ (angle >= 3 * pi / 4) |+ (angle < -3 * pi / 4)+ )++ top = (+ (angle >= -3 * pi / 4) &+ (angle < -pi / 4)+ )++ return [+ right,+ bottom,+ left,+ top,+ ]+++def safe_std(x):+ x = x[np.isfinite(x)]++ if len(x) == 0:+ return 0.0++ return float(np.std(x))+++def run_pca(X):+ """+ PCA via SVD.++ Rows = frames+ Columns = spatial measurements+ """++ X = X.copy()++ column_mean = np.nanmean(+ X,+ axis=0,+ keepdims=True+ )++ X -= column_mean++ X = np.nan_to_num(+ X,+ nan=0.0,+ posinf=0.0,+ neginf=0.0+ )++ U, S, Vt = np.linalg.svd(+ X,+ full_matrices=False+ )++ variance = S * S++ total = np.sum(variance)++ if total <= 0:+ explained = np.zeros_like(variance)+ else:+ explained = variance / total++ return explained, U, S, Vt+++def correlation(a, b):+ mask = (+ np.isfinite(a) &+ np.isfinite(b)+ )++ a = a[mask]+ b = b[mask]++ if len(a) < 3:+ return 0.0++ if np.std(a) < 1e-8:+ return 0.0++ if np.std(b) < 1e-8:+ return 0.0++ return float(+ np.corrcoef(a, b)[0, 1]+ )+++def main():+ if len(sys.argv) != 2:+ print(+ "Usage: python diagnose.py input.mp4"+ )+ sys.exit(1)++ path = sys.argv[1]++ cap = cv2.VideoCapture(path)++ if not cap.isOpened():+ raise RuntimeError(+ f"Cannot open {path}"+ )++ fps = cap.get(+ cv2.CAP_PROP_FPS+ )++ width = int(+ cap.get(+ cv2.CAP_PROP_FRAME_WIDTH+ )+ )++ height = int(+ cap.get(+ cv2.CAP_PROP_FRAME_HEIGHT+ )+ )++ frame_count = int(+ cap.get(+ cv2.CAP_PROP_FRAME_COUNT+ )+ )++ analysis_h = max(+ 96,+ round(+ ANALYSIS_W *+ height /+ width+ )+ )++ radius, angle = make_geometry(+ analysis_h,+ ANALYSIS_W+ )++ sectors = sector_masks(+ angle+ )++ ring_edges = np.linspace(+ 0.0,+ 1.0,+ RINGS + 1+ )++ radial_profiles = []+ sector_profiles = []++ print()+ print(+ f"Input: {width}x{height} "+ f"@ {fps:.3f} fps"+ )++ print(+ f"Frames: {frame_count}"+ )++ print(+ f"Analysis: "+ f"{ANALYSIS_W}x{analysis_h}"+ )++ print(+ f"Radial rings: {RINGS}"+ )++ print(+ "Directional sectors: "+ "RIGHT / BOTTOM / LEFT / TOP"+ )++ print()+ print("Analyzing...")++ frame_index = 0++ while True:+ ok, frame = cap.read()++ if not ok:+ break++ small = cv2.resize(+ frame,+ (+ ANALYSIS_W,+ analysis_h+ ),+ interpolation=cv2.INTER_AREA+ )++ lum = luminance(+ small+ )++ radial = []++ directional = np.full(+ (+ SECTORS,+ RINGS+ ),+ np.nan,+ dtype=np.float32+ )++ for r in range(RINGS):++ ring_mask = (+ (radius >= ring_edges[r]) &+ (radius < ring_edges[r + 1])+ )++ radial.append(+ robust_median(+ lum[ring_mask]+ )+ )++ for s in range(SECTORS):++ mask = (+ ring_mask &+ sectors[s]+ )++ directional[s, r] = (+ robust_median(+ lum[mask]+ )+ )++ radial_profiles.append(+ radial+ )++ sector_profiles.append(+ directional+ )++ frame_index += 1++ if frame_index % 30 == 0:++ print(+ f"\r{frame_index}/"+ f"{frame_count}",+ end="",+ flush=True+ )++ cap.release()++ print()++ radial_profiles = np.asarray(+ radial_profiles,+ dtype=np.float32+ )++ sector_profiles = np.asarray(+ sector_profiles,+ dtype=np.float32+ )++ # ========================================================+ # Normalize using central region.+ #+ # This removes global exposure variation from the+ # diagnostic and isolates spatial changes.+ # ========================================================++ center = np.nanmedian(+ radial_profiles[:, :3],+ axis=1+ )++ center = np.maximum(+ center,+ 1e-5+ )++ radial_norm = (+ radial_profiles /+ center[:, None]+ )++ sector_norm = (+ sector_profiles /+ center[:, None, None]+ )++ # ========================================================+ # Radial instability+ # ========================================================++ radial_std = np.nanstd(+ radial_norm,+ axis=0+ )++ radial_range = (+ np.nanmax(+ radial_norm,+ axis=0+ )+ -+ np.nanmin(+ radial_norm,+ axis=0+ )+ )++ print()+ print(+ "RADIAL TEMPORAL INSTABILITY"+ )++ print()+ print(+ "Ring Radius STD Range"+ )++ print(+ "-------------------------------------"+ )++ for r in range(RINGS):++ print(+ f"{r:02d} "+ f"{ring_edges[r]:.2f}-"+ f"{ring_edges[r + 1]:.2f} "+ f"{radial_std[r]:.4f} "+ f"{radial_range[r]:.4f}"+ )++ # ========================================================+ # Directional signals+ #+ # Use outer 30% of image.+ # ========================================================++ outer_start = int(+ RINGS * 0.70+ )++ directional_signals = []++ print()+ print(+ "OUTER-REGION TEMPORAL INSTABILITY"+ )++ print()++ for s in range(SECTORS):++ signal = np.nanmedian(+ sector_norm[+ :,+ s,+ outer_start:+ ],+ axis=1+ )++ directional_signals.append(+ signal+ )++ print(+ f" {SECTOR_NAMES[s]:6s}: "+ f"STD {safe_std(signal):.4f}"+ )++ directional_signals = np.asarray(+ directional_signals+ )++ # ========================================================+ # Correlations+ #+ # If all directions strongly correlate, that supports a+ # coherent radial/vignette-like temporal effect.+ # ========================================================++ print()+ print(+ "DIRECTIONAL CORRELATIONS"+ )++ print()++ for a in range(SECTORS):++ for b in range(+ a + 1,+ SECTORS+ ):++ c = correlation(+ directional_signals[a],+ directional_signals[b]+ )++ print(+ f" "+ f"{SECTOR_NAMES[a]:6s} vs "+ f"{SECTOR_NAMES[b]:6s}: "+ f"{c:+.3f}"+ )++ # ========================================================+ # Radial PCA+ # ========================================================++ radial_explained, _, _, radial_modes = (+ run_pca(+ radial_norm+ )+ )++ print()+ print(+ "RADIAL PCA"+ )++ print()++ for i in range(+ min(+ 5,+ len(radial_explained)+ )+ ):++ print(+ f" component {i + 1}: "+ f"{radial_explained[i] * 100:.2f}%"+ )++ # ========================================================+ # Full radial + directional PCA+ #+ # This tells us whether one coherent spatial mode also+ # explains directional variation.+ # ========================================================++ flattened = sector_norm.reshape(+ sector_norm.shape[0],+ -1+ )++ spatial_explained, _, _, spatial_modes = (+ run_pca(+ flattened+ )+ )++ print()+ print(+ "RADIAL + DIRECTIONAL PCA"+ )++ print()++ for i in range(+ min(+ 5,+ len(spatial_explained)+ )+ ):++ print(+ f" component {i + 1}: "+ f"{spatial_explained[i] * 100:.2f}%"+ )++ # ========================================================+ # Interpretation+ # ========================================================++ correlations = []++ for a in range(SECTORS):++ for b in range(+ a + 1,+ SECTORS+ ):++ correlations.append(+ correlation(+ directional_signals[a],+ directional_signals[b]+ )+ )++ median_corr = float(+ np.median(+ correlations+ )+ )++ pc1 = float(+ spatial_explained[0]+ )++ print()+ print(+ "SUMMARY"+ )++ print()+ print(+ f"Median directional correlation: "+ f"{median_corr:+.3f}"+ )++ print(+ f"Dominant spatial PCA mode: "+ f"{pc1 * 100:.2f}%"+ )++ print()++ if (+ median_corr > 0.75+ and pc1 > 0.65+ ):++ print(+ "RESULT:"+ )++ print(+ "Strong evidence for one coherent "+ "time-varying vignette/spatial effect."+ )++ print(+ "A low-dimensional parametric/PCA "+ "remover is worth building."+ )++ elif (+ median_corr > 0.45+ or pc1 > 0.50+ ):++ print(+ "RESULT:"+ )++ print(+ "There is meaningful coherent spatial "+ "flicker, but it is not purely radial."+ )++ print(+ "Use a multi-mode spatial model or "+ "blind neural deflickering."+ )++ else:++ print(+ "RESULT:"+ )++ print(+ "The artifact is not well explained "+ "by a coherent vignette-like model."+ )++ print(+ "Prioritize blind neural deflickering "+ "rather than radial correction."+ )++ # ========================================================+ # Save everything+ # ========================================================++ np.savez(+ "diagnostic_v2.npz",++ radial_profiles=radial_norm,++ sector_profiles=sector_norm,++ radial_pca_modes=radial_modes,++ spatial_pca_modes=spatial_modes,++ directional_signals=directional_signals,++ ring_edges=ring_edges,++ fps=np.float32(fps)+ )++ print()+ print(+ "Saved: diagnostic_v2.npz"+ )+ print()+++if __name__ == "__main__":+ main()\ No newline at end of filediff --git a/test/deflicker/fast_dvd.py b/test/deflicker/fast_dvd.pynew file mode 100644index 00000000..2c1f07db--- /dev/null+++ b/test/deflicker/fast_dvd.py@@ -0,0 +1,172 @@+#!/usr/bin/env python3++import argparse+import subprocess+import tempfile+import shutil+from pathlib import Path+++def run(cmd):+ print("+", " ".join(map(str, cmd)))+ subprocess.run(cmd, check=True)+++def main():+ parser = argparse.ArgumentParser(+ description="Quick FastDVDnet experiment on a real video."+ )+ parser.add_argument("input", help="Input video")+ parser.add_argument("output", help="Output video")+ parser.add_argument(+ "--sigma",+ type=float,+ default=15,+ help="FastDVDnet noise sigma. Try 5, 10, 15, 25."+ )+ parser.add_argument(+ "--cpu",+ action="store_true",+ help="Force CPU instead of CUDA/MPS."+ )+ args = parser.parse_args()++ input_video = Path(args.input).resolve()+ output_video = Path(args.output).resolve()++ if not input_video.exists():+ raise FileNotFoundError(input_video)++ repo = Path(__file__).resolve().parent++ if not (repo / "test_fastdvdnet.py").exists():+ raise RuntimeError(+ "Put this script in the FastDVDnet repository root."+ )++ work = Path(tempfile.mkdtemp(prefix="fastdvdnet_"))++ frames = work / "frames"+ processed = work / "processed"++ frames.mkdir()+ processed.mkdir()++ try:+ # ---------------------------------------------------------+ # 1. Get original FPS+ # ---------------------------------------------------------++ probe = subprocess.run(+ [+ "ffprobe",+ "-v", "error",+ "-select_streams", "v:0",+ "-show_entries", "stream=r_frame_rate",+ "-of", "default=noprint_wrappers=1:nokey=1",+ str(input_video)+ ],+ capture_output=True,+ text=True,+ check=True+ )++ ratio = probe.stdout.strip().split("/")++ if len(ratio) == 2:+ fps = float(ratio[0]) / float(ratio[1])+ else:+ fps = float(ratio[0])++ print(f"\nInput FPS: {fps:.3f}")+ print(f"FastDVDnet sigma: {args.sigma}")++ # ---------------------------------------------------------+ # 2. Decode video losslessly to PNG+ # ---------------------------------------------------------++ run([+ "ffmpeg",+ "-y",+ "-i", str(input_video),+ "-vsync", "0",+ str(frames / "%08d.png")+ ])++ # ---------------------------------------------------------+ # 3. Run FastDVDnet+ # ---------------------------------------------------------++ cmd = [+ "python",+ str(repo / "test_fastdvdnet.py"),+ "--test_path", str(frames),+ "--noise_sigma", str(args.sigma),+ "--save_path", str(processed),+ "--model_file", str(repo / "model.pth"),+ ]++ if args.cpu:+ cmd.append("--no_gpu")++ run(cmd)++ # ---------------------------------------------------------+ # Find where FastDVDnet actually put its output+ # ---------------------------------------------------------++ output_frames = sorted(processed.rglob("*.png"))++ if not output_frames:+ raise RuntimeError(+ f"FastDVDnet produced no PNG frames in {processed}"+ )++ # Normalize filenames because the repo can create+ # its own directory structure.+ normalized = work / "normalized"+ normalized.mkdir()++ for i, frame in enumerate(output_frames, start=1):+ shutil.copy2(+ frame,+ normalized / f"{i:08d}.png"+ )++ print(f"\nProcessed {len(output_frames)} frames.")++ # ---------------------------------------------------------+ # 4. Encode output+ # ---------------------------------------------------------++ run([+ "ffmpeg",+ "-y",+ "-framerate", str(fps),+ "-i", str(normalized / "%08d.png"),+ "-i", str(input_video),+ "-map", "0:v:0",+ "-map", "1:a?",+ "-c:v", "libx264",+ "-crf", "16",+ "-preset", "medium",+ "-pix_fmt", "yuv420p",+ "-c:a", "copy",+ "-shortest",+ str(output_video)+ ])++ print("\n================================")+ print("DONE")+ print("================================")+ print(f"Input : {input_video}")+ print(f"Output: {output_video}")+ print(f"Sigma : {args.sigma}")+ print()++ finally:+ shutil.rmtree(work, ignore_errors=True)+++if __name__ == "__main__":+ main()\ No newline at end of filediff --git a/test/deflicker/fft_pwm.py b/test/deflicker/fft_pwm.pynew file mode 100644index 00000000..e5a602ea--- /dev/null+++ b/test/deflicker/fft_pwm.py@@ -0,0 +1,201 @@+#!/usr/bin/env python3++import sys+import cv2+import numpy as np+++GRID_X = 8+GRID_Y = 5++MIN_FLICKER_HZ = 3.0+MAX_FLICKER_HZ = 14.0++SUPPRESSION = 0.90++MIN_GAIN = 0.65+MAX_GAIN = 1.60+++def luminance(frame):+ f = frame.astype(np.float32) / 255.0++ return (+ 0.0722 * f[:, :, 0] ++ 0.7152 * f[:, :, 1] ++ 0.2126 * f[:, :, 2]+ )+++def main():+ if len(sys.argv) != 3:+ print("Usage: python fft_pwm.py input.mp4 output.mp4")+ sys.exit(1)++ src, dst = sys.argv[1], sys.argv[2]++ cap = cv2.VideoCapture(src)++ if not cap.isOpened():+ raise RuntimeError(f"Cannot open {src}")++ fps = cap.get(cv2.CAP_PROP_FPS)+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))+ count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))++ print(f"Input: {width}x{height} @ {fps:.3f} fps")+ print(f"Frames: {count}")+ print(f"Grid: {GRID_X}x{GRID_Y}")+ print()++ frames = []+ signals = []++ print("Analyzing regional brightness...")++ while True:+ ok, frame = cap.read()++ if not ok:+ break++ frames.append(frame)++ lum = luminance(frame)++ # Downsample directly into regional brightness cells.+ grid = cv2.resize(+ lum,+ (GRID_X, GRID_Y),+ interpolation=cv2.INTER_AREA+ )++ signals.append(grid)++ cap.release()++ signals = np.stack(signals)++ n = signals.shape[0]++ corrected_signals = np.zeros_like(signals)++ frequencies = np.fft.rfftfreq(+ n,+ d=1.0 / fps+ )++ flicker_band = (+ (frequencies >= MIN_FLICKER_HZ) &+ (frequencies <= MAX_FLICKER_HZ)+ )++ print("Filtering temporal frequencies...")++ detected_peaks = []++ for y in range(GRID_Y):+ for x in range(GRID_X):++ signal = signals[:, y, x]++ spectrum = np.fft.rfft(signal)++ magnitude = np.abs(spectrum)++ band_indices = np.where(+ flicker_band+ )[0]++ if len(band_indices):+ strongest = band_indices[+ np.argmax(magnitude[band_indices])+ ]++ detected_peaks.append(+ frequencies[strongest]+ )++ spectrum[flicker_band] *= (+ 1.0 - SUPPRESSION+ )++ corrected = np.fft.irfft(+ spectrum,+ n=n+ )++ corrected_signals[:, y, x] = corrected++ if detected_peaks:+ print(+ "Median strongest frequency: "+ f"{np.median(detected_peaks):.2f} Hz"+ )++ print("Correcting frames...")++ writer = cv2.VideoWriter(+ dst,+ cv2.VideoWriter_fourcc(*"mp4v"),+ fps,+ (width, height)+ )++ for i, frame in enumerate(frames):++ original = signals[i]+ target = corrected_signals[i]++ gain = target / np.maximum(+ original,+ 0.03+ )++ gain = np.clip(+ gain,+ MIN_GAIN,+ MAX_GAIN+ )++ gain = cv2.GaussianBlur(+ gain,+ (0, 0),+ 1.2+ )++ gain_full = cv2.resize(+ gain,+ (width, height),+ interpolation=cv2.INTER_CUBIC+ )++ corrected = (+ frame.astype(np.float32) *+ gain_full[:, :, None]+ )++ corrected = np.clip(+ corrected,+ 0,+ 255+ ).astype(np.uint8)++ writer.write(corrected)++ if (i + 1) % 30 == 0:+ print(+ f"\r{i + 1}/{n}",+ end="",+ flush=True+ )++ writer.release()++ print()+ print(f"Done: {dst}")+++if __name__ == "__main__":+ main()\ No newline at end of filediff --git a/test/deflicker/illumination.py b/test/deflicker/illumination.pynew file mode 100644index 00000000..9ee42fea--- /dev/null+++ b/test/deflicker/illumination.py@@ -0,0 +1,163 @@+#!/usr/bin/env python3++import sys+import cv2+import numpy as np+++ANALYSIS_W = 128+BLUR_SIGMA = 10.0+MAX_GAIN = 2.5+MIN_GAIN = 0.60+REFERENCE_PERCENTILE = 90+++def luminance(frame):+ f = frame.astype(np.float32) / 255.0+ return (+ 0.0722 * f[:, :, 0] ++ 0.7152 * f[:, :, 1] ++ 0.2126 * f[:, :, 2]+ )+++def main():+ if len(sys.argv) != 3:+ print("Usage: python illumination.py input.mp4 output.mp4")+ sys.exit(1)++ src, dst = sys.argv[1], sys.argv[2]++ cap = cv2.VideoCapture(src)++ if not cap.isOpened():+ raise RuntimeError(f"Cannot open {src}")++ fps = cap.get(cv2.CAP_PROP_FPS)+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))+ count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))++ analysis_h = max(+ 64,+ round(ANALYSIS_W * height / width)+ )++ print(f"Input: {width}x{height} @ {fps:.3f} fps")+ print(f"Frames: {count}")+ print(f"Analysis: {ANALYSIS_W}x{analysis_h}")+ print()+ print("Reading frames...")++ small_lums = []+ frames = []++ while True:+ ok, frame = cap.read()++ if not ok:+ break++ frames.append(frame)++ small = cv2.resize(+ frame,+ (ANALYSIS_W, analysis_h),+ interpolation=cv2.INTER_AREA+ )++ lum = luminance(small)++ # Remove high-frequency image detail.+ lum = cv2.GaussianBlur(+ lum,+ (0, 0),+ BLUR_SIGMA+ )++ small_lums.append(lum)++ cap.release()++ lums = np.stack(small_lums)++ print("Building temporal reference...")++ # Bright temporal reference helps reconstruct areas+ # temporarily darkened by the flickering effect.+ reference = np.percentile(+ lums,+ REFERENCE_PERCENTILE,+ axis=0+ ).astype(np.float32)++ reference = cv2.GaussianBlur(+ reference,+ (0, 0),+ BLUR_SIGMA+ )++ print("Correcting frames...")++ fourcc = cv2.VideoWriter_fourcc(*"mp4v")++ writer = cv2.VideoWriter(+ dst,+ fourcc,+ fps,+ (width, height)+ )++ for i, frame in enumerate(frames):++ current = lums[i]++ gain = reference / np.maximum(current, 0.03)++ gain = np.clip(+ gain,+ MIN_GAIN,+ MAX_GAIN+ )++ # Smooth the gain field itself.+ gain = cv2.GaussianBlur(+ gain,+ (0, 0),+ 5.0+ )++ gain_full = cv2.resize(+ gain,+ (width, height),+ interpolation=cv2.INTER_CUBIC+ )++ corrected = (+ frame.astype(np.float32) *+ gain_full[:, :, None]+ )++ corrected = np.clip(+ corrected,+ 0,+ 255+ ).astype(np.uint8)++ writer.write(corrected)++ if (i + 1) % 30 == 0:+ print(+ f"\r{i + 1}/{len(frames)}",+ end="",+ flush=True+ )++ writer.release()++ print()+ print(f"Done: {dst}")+++if __name__ == "__main__":+ main()\ No newline at end of filediff --git a/test/deflicker/main.py b/test/deflicker/main.pynew file mode 100644index 00000000..aea4cf15--- /dev/null+++ b/test/deflicker/main.py@@ -0,0 +1,489 @@+#!/usr/bin/env python3++import sys+import cv2+import numpy as np+++# ============================================================+# Configuration+# ============================================================++ANALYSIS_W = 160++CENTER_RADIUS = 0.28++EDGE_RADIUS_MIN = 0.62+EDGE_RADIUS_MAX = 0.92++CORRECTION_START_RADIUS = 0.25++TARGET_PERCENTILE = 98.0++MAX_GAIN = 2.5+++# ============================================================+# Image analysis+# ============================================================++def luminance(frame):+ """+ BGR uint8 -> luminance float32.+ """+ f = frame.astype(np.float32)++ b = f[:, :, 0]+ g = f[:, :, 1]+ r = f[:, :, 2]++ return (+ 0.0722 * b ++ 0.7152 * g ++ 0.2126 * r+ )+++def radial_map(h, w):+ """+ Normalized radial distance.++ center = 0+ corners = ~1+ """+ y, x = np.mgrid[0:h, 0:w].astype(np.float32)++ cx = (w - 1) * 0.5+ cy = (h - 1) * 0.5++ x = (x - cx) / max(cx, 1.0)+ y = (y - cy) / max(cy, 1.0)++ radius = np.sqrt(x * x + y * y)++ return radius / np.sqrt(2.0)+++def robust_mean(values):+ """+ Trim bright/dark outliers before calculating mean.++ Helps prevent lamps, shadows, skateboarder, etc.+ from dominating the measurement.+ """+ if values.size == 0:+ return 0.0++ lo = np.percentile(values, 20)+ hi = np.percentile(values, 80)++ trimmed = values[+ (values >= lo) &+ (values <= hi)+ ]++ if trimmed.size == 0:+ return float(np.mean(values))++ return float(np.mean(trimmed))+++def measure_vignette(frame, radius_small):+ """+ Measure edge brightness relative to center brightness.+ """++ h, w = radius_small.shape++ small = cv2.resize(+ frame,+ (w, h),+ interpolation=cv2.INTER_AREA+ )++ lum = luminance(small)++ center_mask = radius_small < CENTER_RADIUS++ edge_mask = (+ (radius_small > EDGE_RADIUS_MIN) &+ (radius_small < EDGE_RADIUS_MAX)+ )++ center = robust_mean(+ lum[center_mask]+ )++ edge = robust_mean(+ lum[edge_mask]+ )++ if center < 1.0:+ return 1.0, center, edge++ ratio = edge / center++ return ratio, center, edge+++# ============================================================+# Main+# ============================================================++def main():++ if len(sys.argv) != 3:+ print()+ print("Usage:")+ print(+ " python main.py "+ "input.mp4 output.mp4"+ )+ print()+ sys.exit(1)++ input_path = sys.argv[1]+ output_path = sys.argv[2]++ # --------------------------------------------------------+ # Open source+ # --------------------------------------------------------++ cap = cv2.VideoCapture(input_path)++ if not cap.isOpened():+ raise RuntimeError(+ f"Could not open: {input_path}"+ )++ fps = cap.get(cv2.CAP_PROP_FPS)++ width = int(+ cap.get(cv2.CAP_PROP_FRAME_WIDTH)+ )++ height = int(+ cap.get(cv2.CAP_PROP_FRAME_HEIGHT)+ )++ frame_count = int(+ cap.get(cv2.CAP_PROP_FRAME_COUNT)+ )++ print()+ print(+ f"Input: {width}x{height} "+ f"@ {fps:.3f} fps"+ )++ print(+ f"Frames: {frame_count}"+ )++ # ========================================================+ # PASS 1+ #+ # Analyze the brightness relationship between the+ # center and edges.+ #+ # This operates at very low resolution because we're+ # measuring low-frequency illumination, not detail.+ # ========================================================++ analysis_h = max(+ 64,+ round(+ ANALYSIS_W *+ height /+ width+ )+ )++ analysis_radius = radial_map(+ analysis_h,+ ANALYSIS_W+ )++ ratios = []+ centers = []+ edges = []++ print()+ print("Analyzing vignette...")++ while True:++ ok, frame = cap.read()++ if not ok:+ break++ ratio, center, edge = measure_vignette(+ frame,+ analysis_radius+ )++ ratios.append(ratio)+ centers.append(center)+ edges.append(edge)++ cap.release()++ ratios = np.asarray(+ ratios,+ dtype=np.float32+ )++ if len(ratios) == 0:+ raise RuntimeError(+ "No video frames decoded."+ )++ # --------------------------------------------------------+ # Determine what an almost-unvignetted frame looks like.+ #+ # We use the 98th percentile instead of maximum because+ # one abnormal frame shouldn't determine the target.+ # --------------------------------------------------------++ target_ratio = float(+ np.percentile(+ ratios,+ TARGET_PERCENTILE+ )+ )++ print()+ print("Edge/center ratio:")+ print(+ f" min: "+ f"{ratios.min():.4f}"+ )+ print(+ f" median: "+ f"{np.median(ratios):.4f}"+ )+ print(+ f" max: "+ f"{ratios.max():.4f}"+ )+ print(+ f" target: "+ f"{target_ratio:.4f}"+ )++ # --------------------------------------------------------+ # IMPORTANT:+ #+ # No temporal smoothing.+ #+ # The unwanted vignette itself changes rapidly, so we+ # want to react to each individual frame.+ # --------------------------------------------------------++ measured = ratios++ # --------------------------------------------------------+ # Calculate how much brighter the edge needs to become.+ #+ # Example:+ #+ # target = 0.80+ # frame = 0.40+ #+ # gain = 0.80 / 0.40+ # = 2.0+ # --------------------------------------------------------++ required_edge_gain = (+ target_ratio /+ np.maximum(+ measured,+ 0.10+ )+ )++ required_edge_gain = np.clip(+ required_edge_gain,+ 1.0,+ MAX_GAIN+ )++ print()+ print(+ "Correction gain:"+ )++ print(+ f" min: "+ f"{required_edge_gain.min():.3f}x"+ )++ print(+ f" max: "+ f"{required_edge_gain.max():.3f}x"+ )++ # ========================================================+ # Build full-resolution radial correction weighting.+ # ========================================================++ full_radius = radial_map(+ height,+ width+ )++ radial_weight = (+ full_radius -+ CORRECTION_START_RADIUS+ ) / (+ 1.0 -+ CORRECTION_START_RADIUS+ )++ radial_weight = np.clip(+ radial_weight,+ 0.0,+ 1.0+ )++ # --------------------------------------------------------+ # Smoothstep+ #+ # x²(3 - 2x)+ #+ # Gives us a smooth transition from the untouched center+ # to the corrected edges.+ # --------------------------------------------------------++ radial_weight = (+ radial_weight *+ radial_weight *+ (+ 3.0 -+ 2.0 * radial_weight+ )+ )++ # ========================================================+ # PASS 2+ #+ # Apply per-frame correction.+ # ========================================================++ cap = cv2.VideoCapture(+ input_path+ )++ if not cap.isOpened():+ raise RuntimeError(+ "Could not reopen input video."+ )++ fourcc = cv2.VideoWriter_fourcc(+ *"mp4v"+ )++ writer = cv2.VideoWriter(+ output_path,+ fourcc,+ fps,+ (+ width,+ height+ )+ )++ if not writer.isOpened():+ raise RuntimeError(+ "Could not create output video."+ )++ print()+ print("Correcting frames...")++ frame_index = 0++ while True:++ ok, frame = cap.read()++ if not ok:+ break++ edge_gain = required_edge_gain[+ min(+ frame_index,+ len(required_edge_gain) - 1+ )+ ]++ # ----------------------------------------------------+ # Construct gain map.+ #+ # Center:+ #+ # gain = 1.0+ #+ # Edge:+ #+ # gain = calculated edge correction+ # ----------------------------------------------------++ gain_map = (+ 1.0 ++ radial_weight *+ (+ edge_gain -+ 1.0+ )+ )++ corrected = frame.astype(+ np.float32+ )++ corrected *= gain_map[+ :, :, None+ ]++ corrected = np.clip(+ corrected,+ 0.0,+ 255.0+ )++ corrected = corrected.astype(+ np.uint8+ )++ writer.write(+ corrected+ )++ frame_index += 1++ if frame_index % 30 == 0:++ print(+ f"\r"+ f"{frame_index}/"+ f"{len(ratios)} frames",+ end="",+ flush=True+ )++ print()++ cap.release()+ writer.release()++ print()+ print(+ f"Done: {output_path}"+ )+ print()+++if __name__ == "__main__":+ main()\ No newline at end of filediff --git a/test/deflicker/optical_flow.py b/test/deflicker/optical_flow.pynew file mode 100644index 00000000..5bf2ae97--- /dev/null+++ b/test/deflicker/optical_flow.py@@ -0,0 +1,318 @@+#!/usr/bin/env python3++import sys+import cv2+import numpy as np+++ANALYSIS_W = 320++SPATIAL_SIGMA = 18.0++MIN_GAIN = 0.60+MAX_GAIN = 2.20++CONFIDENCE_THRESHOLD = 0.20+++def luminance(frame):+ f = frame.astype(np.float32) / 255.0++ return (+ 0.0722 * f[:, :, 0] ++ 0.7152 * f[:, :, 1] ++ 0.2126 * f[:, :, 2]+ )+++def flow_warp(source, target):+ """+ Warp SOURCE toward TARGET.++ Flow is calculated from target -> source so that+ remap tells us where each target pixel came from.+ """++ target8 = np.clip(+ target * 255,+ 0,+ 255+ ).astype(np.uint8)++ source8 = np.clip(+ source * 255,+ 0,+ 255+ ).astype(np.uint8)++ flow = cv2.calcOpticalFlowFarneback(+ target8,+ source8,+ None,+ 0.5,+ 4,+ 21,+ 5,+ 7,+ 1.5,+ 0+ )++ h, w = target.shape++ x, y = np.meshgrid(+ np.arange(w),+ np.arange(h)+ )++ map_x = (+ x.astype(np.float32) ++ flow[:, :, 0]+ )++ map_y = (+ y.astype(np.float32) ++ flow[:, :, 1]+ )++ warped = cv2.remap(+ source,+ map_x,+ map_y,+ cv2.INTER_LINEAR,+ borderMode=cv2.BORDER_REFLECT+ )++ return warped+++def main():+ if len(sys.argv) != 3:+ print(+ "Usage: python optical_flow.py "+ "input.mp4 output.mp4"+ )+ sys.exit(1)++ src, dst = sys.argv[1], sys.argv[2]++ cap = cv2.VideoCapture(src)++ if not cap.isOpened():+ raise RuntimeError(f"Cannot open {src}")++ fps = cap.get(cv2.CAP_PROP_FPS)+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))+ count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))++ analysis_h = max(+ 96,+ round(+ ANALYSIS_W *+ height /+ width+ )+ )++ print(+ f"Input: {width}x{height} "+ f"@ {fps:.3f} fps"+ )++ print(f"Frames: {count}")++ print(+ f"Flow analysis: "+ f"{ANALYSIS_W}x{analysis_h}"+ )++ frames = []+ lums = []++ print()+ print("Reading frames...")++ while True:++ ok, frame = cap.read()++ if not ok:+ break++ frames.append(frame)++ small = cv2.resize(+ frame,+ (ANALYSIS_W, analysis_h),+ interpolation=cv2.INTER_AREA+ )++ lums.append(+ luminance(small)+ )++ cap.release()++ n = len(frames)++ writer = cv2.VideoWriter(+ dst,+ cv2.VideoWriter_fourcc(*"mp4v"),+ fps,+ (width, height)+ )++ print()+ print("Motion-compensated correction...")++ for i in range(n):++ current = lums[i]++ ratio_maps = []++ neighbors = []++ if i > 0:+ neighbors.append(i - 1)++ if i + 1 < n:+ neighbors.append(i + 1)++ for j in neighbors:++ warped = flow_warp(+ lums[j],+ current+ )++ # ------------------------------------------------+ # Estimate multiplicative illumination difference.+ # ------------------------------------------------++ ratio = (+ warped + 0.02+ ) / (+ current + 0.02+ )++ # ------------------------------------------------+ # Reject pixels where correspondence is obviously+ # suspicious.+ # ------------------------------------------------++ residual = np.abs(+ warped - current+ )++ confidence = np.exp(+ -residual / 0.12+ )++ valid = (+ confidence >+ CONFIDENCE_THRESHOLD+ )++ ratio = np.where(+ valid,+ ratio,+ 1.0+ )++ ratio_maps.append(+ ratio+ )++ if not ratio_maps:++ writer.write(frames[i])+ continue++ ratios = np.stack(+ ratio_maps+ )++ # Robust consensus between previous/next frames.+ illumination = np.median(+ ratios,+ axis=0+ )++ # ----------------------------------------------------+ # Illumination should be spatially smooth.+ #+ # This is crucial: we don't want to "correct" textures,+ # skateboarder details, etc.+ # ----------------------------------------------------++ illumination = cv2.GaussianBlur(+ illumination,+ (0, 0),+ SPATIAL_SIGMA+ )++ illumination = np.clip(+ illumination,+ MIN_GAIN,+ MAX_GAIN+ )++ # ----------------------------------------------------+ # We only want to remove rapid deviation.+ #+ # Split the difference because neighbors may themselves+ # sit on opposite phases of the flicker.+ # ----------------------------------------------------++ gain = np.sqrt(+ np.maximum(+ illumination,+ 0.01+ )+ )++ gain = np.clip(+ gain,+ MIN_GAIN,+ MAX_GAIN+ )++ gain_full = cv2.resize(+ gain,+ (width, height),+ interpolation=cv2.INTER_CUBIC+ )++ corrected = (+ frames[i].astype(np.float32) *+ gain_full[:, :, None]+ )++ corrected = np.clip(+ corrected,+ 0,+ 255+ ).astype(np.uint8)++ writer.write(+ corrected+ )++ if (i + 1) % 10 == 0:++ print(+ f"\r{i + 1}/{n}",+ end="",+ flush=True+ )++ writer.release()++ print()+ print(f"Done: {dst}")+++if __name__ == "__main__":+ main()\ No newline at end of file