AlekseyCalvin commited on
Commit
924970f
·
verified ·
1 Parent(s): 06a337b

Update deforum_warp.py

Browse files
Files changed (1) hide show
  1. deforum_warp.py +19 -34
deforum_warp.py CHANGED
@@ -12,8 +12,8 @@ def get_border(mode):
12
 
13
  def anim_frame_warp(prev_img, angle, zoom, tx, ty, border_mode):
14
  """
15
- True Deforum 2D Perspective Warp.
16
- Simulates camera movement rather than just image scaling.
17
  """
18
  if prev_img is None: return None
19
 
@@ -21,16 +21,14 @@ def anim_frame_warp(prev_img, angle, zoom, tx, ty, border_mode):
21
  height, width = cv_img.shape[:2]
22
  center_x, center_y = width // 2, height // 2
23
 
24
- # 1. Initialize Identity Matrix (3x3 for Perspective)
25
  matrix = np.identity(3)
26
 
27
- # 2. Apply Translation (Pan X/Y)
28
- # We move the image opposite to the camera movement
29
  matrix[0, 2] = tx
30
  matrix[1, 2] = ty
31
 
32
- # 3. Apply Rotation (Angle)
33
- # Convert degrees to radians
34
  rot_rad = np.radians(angle)
35
  rot_mat = np.identity(3)
36
  rot_mat[0, 0] = np.cos(rot_rad)
@@ -38,42 +36,29 @@ def anim_frame_warp(prev_img, angle, zoom, tx, ty, border_mode):
38
  rot_mat[1, 0] = np.sin(rot_rad)
39
  rot_mat[1, 1] = np.cos(rot_rad)
40
 
41
- # Rotate around center: Translate to origin -> Rotate -> Translate back
42
  center_mat = np.identity(3)
43
  center_mat[0, 2] = -center_x
44
  center_mat[1, 2] = -center_y
 
 
 
45
 
46
- inv_center_mat = np.identity(3)
47
- inv_center_mat[0, 2] = center_x
48
- inv_center_mat[1, 2] = center_y
49
-
50
- matrix = inv_center_mat @ rot_mat @ center_mat @ matrix
51
-
52
- # 4. Apply Zoom (Scale)
53
- # Deforum Zoom is actually scaling around center
54
- # Zoom 1.0 = No change. Zoom 1.1 = Move closer (Image gets bigger)
55
- scale_x = zoom
56
- scale_y = zoom
57
 
 
58
  scale_mat = np.identity(3)
59
- scale_mat[0, 0] = scale_x
60
- scale_mat[1, 1] = scale_y
61
-
62
- # Scale around center
63
- matrix = inv_center_mat @ scale_mat @ center_mat @ matrix
64
 
65
- # 5. Perform Perspective Warp (Homography)
66
- # Use WARP_INVERSE_MAP effectively by applying the matrix directly?
67
- # Standard cv2.warpPerspective applies the transformation matrix.
68
- # Deforum usually applies the inverse to simulate camera move vs object move.
69
- # Here we stick to direct application for WYSIWYG results.
70
 
71
- # Note: cv2.warpPerspective expects a 3x3 matrix
72
  result = cv2.warpPerspective(
73
- cv_img,
74
- matrix,
75
- (width, height),
76
- borderMode=get_border(border_mode),
77
  flags=cv2.INTER_LINEAR
78
  )
79
 
 
12
 
13
  def anim_frame_warp(prev_img, angle, zoom, tx, ty, border_mode):
14
  """
15
+ True Perspective/Homography Warp.
16
+ Simulates camera movement (FOV) via Matrix math.
17
  """
18
  if prev_img is None: return None
19
 
 
21
  height, width = cv_img.shape[:2]
22
  center_x, center_y = width // 2, height // 2
23
 
24
+ # 1. Initialize 3x3 Identity
25
  matrix = np.identity(3)
26
 
27
+ # 2. Translation (Pan)
 
28
  matrix[0, 2] = tx
29
  matrix[1, 2] = ty
30
 
31
+ # 3. Rotation
 
32
  rot_rad = np.radians(angle)
33
  rot_mat = np.identity(3)
34
  rot_mat[0, 0] = np.cos(rot_rad)
 
36
  rot_mat[1, 0] = np.sin(rot_rad)
37
  rot_mat[1, 1] = np.cos(rot_rad)
38
 
39
+ # Center Offset
40
  center_mat = np.identity(3)
41
  center_mat[0, 2] = -center_x
42
  center_mat[1, 2] = -center_y
43
+ inv_center = np.identity(3)
44
+ inv_center[0, 2] = center_x
45
+ inv_center[1, 2] = center_y
46
 
47
+ # Apply Rotation around center
48
+ matrix = inv_center @ rot_mat @ center_mat @ matrix
 
 
 
 
 
 
 
 
 
49
 
50
+ # 4. Zoom (Scale)
51
  scale_mat = np.identity(3)
52
+ scale_mat[0, 0] = zoom
53
+ scale_mat[1, 1] = zoom
 
 
 
54
 
55
+ # Apply Scale around center
56
+ matrix = inv_center @ scale_mat @ center_mat @ matrix
 
 
 
57
 
58
+ # 5. Warp Perspective
59
  result = cv2.warpPerspective(
60
+ cv_img, matrix, (width, height),
61
+ borderMode=get_border(border_mode),
 
 
62
  flags=cv2.INTER_LINEAR
63
  )
64