
from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve



def 		UpperCamelCase    (					_UpperCAmelCase     :      np.ndarray   , _UpperCAmelCase     :      np.ndarray   , _UpperCAmelCase     :      np.ndarray     )      -> np.ndarray:






						'''simple docstring'''

						_lowercase					:  int    			=      np.stack((horizontal_flow, vertical_flow)   , 2     )

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

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

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

						# Set pixels at invalid locations to 0
						_lowercase					:  List[str]    			=      0

						return warped



def 		UpperCamelCase    (					_UpperCAmelCase     :      np.ndarray   , _UpperCAmelCase     :      np.ndarray   , _UpperCAmelCase     :      SupportsIndex   , _UpperCAmelCase     :      float | None = None   , )      -> tuple[np.ndarray, np.ndarray]:






						'''simple docstring'''

						if alpha is None:
												_lowercase					:  Dict    			=      0.1

						# Initialize flow
						_lowercase					:  Optional[Any]    			=      np.zeros_like(_UpperCAmelCase     )
						_lowercase					:  Dict    			=      np.zeros_like(_UpperCAmelCase     )

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

												_lowercase					:  Optional[Any]    			=      convolve(_UpperCAmelCase   , _UpperCAmelCase     )
												_lowercase					:  Union[str, Any]    			=      convolve(_UpperCAmelCase   , _UpperCAmelCase     )

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

												_lowercase					:  Union[str, Any]    			=      avg_horizontal_velocity - derivative_x * update
												_lowercase					:  Dict    			=      avg_vertical_velocity - derivative_y * update

						return horizontal_flow, vertical_flow


if __name__ == "__main__":
							import doctest

							doctest.testmod()






def 		UpperCamelCase    (					_UpperCAmelCase     :      int     )      -> bool:






						'''simple docstring'''

						if not isinstance(_UpperCAmelCase   , _UpperCAmelCase     ):
												_lowercase					:  Optional[int]    			=      f"""Input value of [number={number}] must be an integer"""
												raise TypeError(_UpperCAmelCase     )
						if number < 0 or number % 2 == 1:
												return False
						_lowercase					:  List[str]    			=      int(number**0.5     )
						return number == number_sqrt * (number_sqrt + 1)


if __name__ == "__main__":
							import doctest

							doctest.testmod()




