File size: 1,058 Bytes
63ca2a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""
时序连贯性与光流闪烁修复引擎
Author: XiaoZhe (Commercial Contact: janejulius119@gmail.com / WeChat: julius119)
"""

import cv2
import numpy as np
from typing import List

class TemporalCoherenceEngine:
    """光流分析与时域滤波修复器"""
    def analyze_optical_flow(self, frame1: np.ndarray, frame2: np.ndarray) -> float:
        gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
        gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
        flow = cv2.calcOpticalFlowFarneback(gray1, gray2, None, 0.5, 3, 15, 3, 5, 1.2, 0)
        mag, _ = cv2.cartToPolar(flow[..., 0], flow[..., 1])
        return float(np.mean(mag))

    def repair_flicker(self, frames: List[np.ndarray]) -> List[np.ndarray]:
        if len(frames) < 3:
            return frames
        repaired = [frames[0]]
        for i in range(1, len(frames) - 1):
            window = np.stack([frames[i-1], frames[i], frames[i+1]])
            repaired.append(np.median(window, axis=0).astype(np.uint8))
        repaired.append(frames[-1])
        return repaired