
"""simple docstring"""


from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve



def        _lowerCamelCase(__UpperCamelCase       ,					__UpperCamelCase       ,					__UpperCamelCase			)							->					np.ndarray:
	_lowerCAmelCase								=np.stack((horizontal_flow, vertical_flow)       ,					2			)

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

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

	_lowerCAmelCase								=image[grid[:, :, 1], grid[:, :, 0]]

	# Set pixels at invalid locations to 0
	_lowerCAmelCase								=0

	return warped



def        _lowerCamelCase(__UpperCamelCase       ,					__UpperCamelCase       ,					__UpperCamelCase       ,					__UpperCamelCase = None       ,					)							->					tuple[np.ndarray, np.ndarray]:
	if alpha is None:
		_lowerCAmelCase								=0.1

	# Initialize flow
	_lowerCAmelCase								=np.zeros_like(__UpperCamelCase			)
	_lowerCAmelCase								=np.zeros_like(__UpperCamelCase			)

	# Prepare kernels for the calculation of the derivatives and the average velocity
	_lowerCAmelCase								=np.array([[-1, 1], [-1, 1]]			) * 0.25
	_lowerCAmelCase								=np.array([[-1, -1], [1, 1]]			) * 0.25
	_lowerCAmelCase								=np.array([[1, 1], [1, 1]]			) * 0.25
	_lowerCAmelCase								=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(__UpperCamelCase			):
		_lowerCAmelCase								=warp(__UpperCamelCase       ,					__UpperCamelCase       ,					__UpperCamelCase			)
		_lowerCAmelCase								=convolve(__UpperCamelCase       ,					__UpperCamelCase			) + convolve(__UpperCamelCase       ,					__UpperCamelCase			)
		_lowerCAmelCase								=convolve(__UpperCamelCase       ,					__UpperCamelCase			) + convolve(__UpperCamelCase       ,					__UpperCamelCase			)
		_lowerCAmelCase								=convolve(__UpperCamelCase       ,					__UpperCamelCase			) + convolve(__UpperCamelCase       ,					-kernel_t			)

		_lowerCAmelCase								=convolve(__UpperCamelCase       ,					__UpperCamelCase			)
		_lowerCAmelCase								=convolve(__UpperCamelCase       ,					__UpperCamelCase			)

		# This updates the flow as proposed in the paper (Step 12)
		_lowerCAmelCase								=(
		    derivative_x * avg_horizontal_velocity
		    + derivative_y * avg_vertical_velocity
		    + derivative_t
		)
		_lowerCAmelCase								=update / (alpha**2 + derivative_x**2 + derivative_y**2)

		_lowerCAmelCase								=avg_horizontal_velocity - derivative_x * update
		_lowerCAmelCase								=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


__A					=			None



def        _lowerCamelCase(*__UpperCamelCase       ,					**__UpperCamelCase			)							->					Tuple:
	requires_backends(__UpperCamelCase       ,					["""sentencepiece""", """tokenizers"""]			)



