


"""simple docstring"""







from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve


def 	lowercase     (							A_    ,  A_    ,  A_			)-> np.ndarray:
  '''simple docstring'''







  a		:						Tuple 				=     np.stack((horizontal_flow, vertical_flow)    ,  2			)

  # Create a grid of all pixel coordinates and subtract the flow to get the
  # target pixels coordinates
  a		:						Tuple 				=     np.stack(
      np.meshgrid(np.arange(0    ,  image.shape[1]			)    ,  np.arange(0    ,  image.shape[0]			)			)    ,  2			)
  a		:						str 				=     np.round(grid - flow			).astype(np.intaa			)

  # Find the locations outside of the original image
  a		:						Any 				=     (grid < 0) | (grid >= np.array([image.shape[1], image.shape[0]]			))
  a		:						Optional[Any] 				=     0

  a		:						List[Any] 				=     image[grid[:, :, 1], grid[:, :, 0]]

  # Set pixels at invalid locations to 0
  a		:						Optional[int] 				=     0

  return warped


def 	lowercase     (							A_    ,  A_    ,  A_    ,  A_ = None    ,  )-> tuple[np.ndarray, np.ndarray]:
  '''simple docstring'''







  if alpha is None:
    a		:						int 				=     0.1

  # Initialize flow
  a		:						str 				=     np.zeros_like(A_			)
  a		:						Union[str, Any] 				=     np.zeros_like(A_			)

  # Prepare kernels for the calculation of the derivatives and the average velocity
  a		:						str 				=     np.array([[-1, 1], [-1, 1]]			) * 0.2_5
  a		:						List[str] 				=     np.array([[-1, -1], [1, 1]]			) * 0.2_5
  a		:						int 				=     np.array([[1, 1], [1, 1]]			) * 0.2_5
  a		:						List[Any] 				=     np.array(
      [[1 / 12, 1 / 6, 1 / 12], [1 / 6, 0, 1 / 6], [1 / 12, 1 / 6, 1 / 12]]			)

  # Iteratively refine the flow
  for _ in range(A_			):
    a		:						Any 				=     warp(A_    ,  A_    ,  A_			)
    a		:						Optional[Any] 				=     convolve(A_    ,  A_			) + convolve(A_    ,  A_			)
    a		:						Optional[Any] 				=     convolve(A_    ,  A_			) + convolve(A_    ,  A_			)
    a		:						List[str] 				=     convolve(A_    ,  A_			) + convolve(A_    ,  -kernel_t			)

    a		:						int 				=     convolve(A_    ,  A_			)
    a		:						List[Any] 				=     convolve(A_    ,  A_			)

    # This updates the flow as proposed in the paper (Step 12)
    a		:						List[Any] 				=     (
        derivative_x * avg_horizontal_velocity
        + derivative_y * avg_vertical_velocity
        + derivative_t
    )
    a		:						str 				=     update / (alpha**2 + derivative_x**2 + derivative_y**2)

    a		:						List[str] 				=     avg_horizontal_velocity - derivative_x * update
    a		:						int 				=     avg_vertical_velocity - derivative_y * update

  return horizontal_flow, vertical_flow


if __name__ == "__main__":
 import doctest

 doctest.testmod()










"""simple docstring"""







from ..utils import DummyObject, requires_backends






class 		_A    (    metaclass=_a    ):







    """simple docstring"""




    UpperCAmelCase			:				int      =			["""torch""", """transformers""", """invisible_watermark"""]






    def __init__(   self							:       int							,					*__UpperCAmelCase							:       List[Any]							,					**__UpperCAmelCase							:       Optional[int]):
      requires_backends(self							,					["torch", "transformers", "invisible_watermark"])






    @classmethod
    def       __snake_case				(   cls							:       Optional[Any]							,					*__UpperCAmelCase							:       Any							,					**__UpperCAmelCase							:       int):
      requires_backends(cls							,					["torch", "transformers", "invisible_watermark"])






    @classmethod
    def       __snake_case				(   cls							:       List[Any]							,					*__UpperCAmelCase							:       Optional[Any]							,					**__UpperCAmelCase							:       Tuple):
      requires_backends(cls							,					["torch", "transformers", "invisible_watermark"])






class 		_A    (    metaclass=_a    ):







    """simple docstring"""




    UpperCAmelCase			:				Optional[int]      =			["""torch""", """transformers""", """invisible_watermark"""]






    def __init__(   self							:       int							,					*__UpperCAmelCase							:       Tuple							,					**__UpperCAmelCase							:       Tuple):
      requires_backends(self							,					["torch", "transformers", "invisible_watermark"])






    @classmethod
    def       __snake_case				(   cls							:       int							,					*__UpperCAmelCase							:       str							,					**__UpperCAmelCase							:       Optional[int]):
      requires_backends(cls							,					["torch", "transformers", "invisible_watermark"])






    @classmethod
    def       __snake_case				(   cls							:       Any							,					*__UpperCAmelCase							:       Optional[int]							,					**__UpperCAmelCase							:       Union[str, Any]):
      requires_backends(cls							,					["torch", "transformers", "invisible_watermark"])






