File size: 922 Bytes
5db43ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
28
import numpy as np

def get_inverse_trans(trans):
    full_matrix = np.vstack([trans, [0, 0, 1]])

    # Compute the inverse of the 3x3 matrix
    inverse_matrix = np.linalg.inv(full_matrix)

    # Extract the top 2 rows for use with cv2.warpAffine
    inverse_transform_matrix = inverse_matrix[:2, :]
    return inverse_transform_matrix

class TemporalSmoothing:
    def __init__(self, c=0.8):
        self.past_trans=None
        self.c=c

    def __call__(self, trans):
        if self.past_trans is None:
            self.past_trans = trans.copy()
            return trans, get_inverse_trans(trans)
        else:
            if trans is not None:
                trans = trans*(1-self.c)+self.past_trans*self.c
                self.past_trans = trans.copy()
                return trans,get_inverse_trans(trans)
            else:
                return self.past_trans.copy(), get_inverse_trans(self.past_trans.copy())