




'''simple docstring'''




from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve




def 						snake_case__    ( lowerCamelCase__				:   np.ndarray			,		lowerCamelCase__				:   np.ndarray			,		lowerCamelCase__				:   np.ndarray			)   ->   np.ndarray:
					A_ :     List[str] =							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_ :     Union[str, Any] =							np.stack(
					    np.meshgrid(np.arange(0			,		image.shape[1]			)			,		np.arange(0			,		image.shape[0]			)			)			,		2			)
					A_ :     Optional[Any] =							np.round(grid - flow			).astype(np.intaa			)

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

					A_ :     Any =							image[grid[:, :, 1], grid[:, :, 0]]

					# Set pixels at invalid locations to 0
					A_ :     int =							0

					return warped




def 						snake_case__    ( lowerCamelCase__				:   np.ndarray			,		lowerCamelCase__				:   np.ndarray			,		lowerCamelCase__				:   SupportsIndex			,		lowerCamelCase__				:   float | None = None			,		)   ->   tuple[np.ndarray, np.ndarray]:
					if alpha is None:
										A_ :     Optional[int] =							0.1

					# Initialize flow
					A_ :     str =							np.zeros_like(lowerCamelCase__			)
					A_ :     Any =							np.zeros_like(lowerCamelCase__			)

					# Prepare kernels for the calculation of the derivatives and the average velocity
					A_ :     int =							np.array([[-1, 1], [-1, 1]]			) * 0.25
					A_ :     Union[str, Any] =							np.array([[-1, -1], [1, 1]]			) * 0.25
					A_ :     Union[str, Any] =							np.array([[1, 1], [1, 1]]			) * 0.25
					A_ :     str =							np.array(
					    [[1 / 1_2, 1 / 6, 1 / 1_2], [1 / 6, 0, 1 / 6], [1 / 1_2, 1 / 6, 1 / 1_2]]			)

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

										A_ :     Any =							convolve(lowerCamelCase__			,		lowerCamelCase__			)
										A_ :     Union[str, Any] =							convolve(lowerCamelCase__			,		lowerCamelCase__			)

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

										A_ :     List[str] =							avg_horizontal_velocity - derivative_x * update
										A_ :     Any =							avg_vertical_velocity - derivative_y * update

					return horizontal_flow, vertical_flow


if __name__ == "__main__":
					import doctest

					doctest.testmod()









'''simple docstring'''




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


snake_case__          =						{"""configuration_nat""": ["""NAT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """NatConfig"""]}


try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							snake_case__          =						[
							    """NAT_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """NatForImageClassification""",
							    """NatModel""",
							    """NatPreTrainedModel""",
							    """NatBackbone""",
							]

if TYPE_CHECKING:
							from .configuration_nat import NAT_PRETRAINED_CONFIG_ARCHIVE_MAP, NatConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_nat import (
														    NAT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    NatBackbone,
														    NatForImageClassification,
														    NatModel,
														    NatPreTrainedModel,
														)

else:
							import sys

							snake_case__          =						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



