






"""simple docstring"""


from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve


def 					UpperCAmelCase			(				a__    ,			a__    ,			a__   ):





						'''simple docstring'''






						lowerCAmelCase      :Any				  = 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      :str				  = np.stack(
						    np.meshgrid(np.arange(0    ,			image.shape[1]   )    ,			np.arange(0    ,			image.shape[0]   )   )    ,			2   )
						lowerCAmelCase      :Optional[Any]				  = np.round(grid - flow   ).astype(np.intaa   )

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

						lowerCAmelCase      :Dict				  = image[grid[:, :, 1], grid[:, :, 0]]

						# Set pixels at invalid locations to 0
						lowerCAmelCase      :Optional[Any]				  = 0

						return warped


def 					UpperCAmelCase			(				a__    ,			a__    ,			a__    ,			a__ = None    ,			):





						'''simple docstring'''






						if alpha is None:
												lowerCAmelCase      :int				  = 0.1

						# Initialize flow
						lowerCAmelCase      :Tuple				  = np.zeros_like(__A   )
						lowerCAmelCase      :List[str]				  = np.zeros_like(__A   )

						# Prepare kernels for the calculation of the derivatives and the average velocity
						lowerCAmelCase      :List[Any]				  = np.array([[-1, 1], [-1, 1]]   ) * 0.25
						lowerCAmelCase      :int				  = np.array([[-1, -1], [1, 1]]   ) * 0.25
						lowerCAmelCase      :Union[str, Any]				  = np.array([[1, 1], [1, 1]]   ) * 0.25
						lowerCAmelCase      :Dict				  = 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   ):
												lowerCAmelCase      :List[str]				  = warp(__A    ,			__A    ,			__A   )
												lowerCAmelCase      :Tuple				  = convolve(__A    ,			__A   ) + convolve(__A    ,			__A   )
												lowerCAmelCase      :Tuple				  = convolve(__A    ,			__A   ) + convolve(__A    ,			__A   )
												lowerCAmelCase      :List[Any]				  = convolve(__A    ,			__A   ) + convolve(__A    ,			-kernel_t   )

												lowerCAmelCase      :str				  = convolve(__A    ,			__A   )
												lowerCAmelCase      :Union[str, Any]				  = convolve(__A    ,			__A   )

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

												lowerCAmelCase      :Optional[Any]				  = avg_horizontal_velocity - derivative_x * update
												lowerCAmelCase      :List[Any]				  = avg_vertical_velocity - derivative_y * update

						return horizontal_flow, vertical_flow


if __name__ == "__main__":
		import doctest

		doctest.testmod()







"""simple docstring"""


import safs




class 	__UpperCamelCase    (    safs.SaFileSystem	):
					pass