code: from typing import SupportsIndex
  
  import numpy as np
  from scipy.ndimage import convolve
  
  
  
  def 							_lowerCamelCase(				__snake_case						,						__snake_case						,						__snake_case	)  ->   np.ndarray:
  		__snake_case		       =     np.stack((horizontal_flow, vertical_flow)						,						2	)
  
  		# Create a grid of all pixel coordinates and subtract the flow to get the
  		# target pixels coordinates
  		__snake_case		       =     np.stack(
  		    np.meshgrid(np.arange(0						,						image.shape[1]	)						,						np.arange(0						,						image.shape[0]	)	)						,						2	)
  		__snake_case		       =     np.round(grid - flow	).astype(np.intaa	)
  
  		# Find the locations outside of the original image
  		__snake_case		       =     (grid < 0) | (grid >= np.array([image.shape[1], image.shape[0]]	))
  		__snake_case		       =     0
  
  		__snake_case		       =     image[grid[:, :, 1], grid[:, :, 0]]
  
  		# Set pixels at invalid locations to 0
  		__snake_case		       =     0
  
  		return warped
  
  
  
  def 							_lowerCamelCase(				__snake_case						,						__snake_case						,						__snake_case						,						__snake_case = None						,						)  ->   tuple[np.ndarray, np.ndarray]:
  		if alpha is None:
  				__snake_case		       =     0.1
  
  		# Initialize flow
  		__snake_case		       =     np.zeros_like(__snake_case	)
  		__snake_case		       =     np.zeros_like(__snake_case	)
  
  		# Prepare kernels for the calculation of the derivatives and the average velocity
  		__snake_case		       =     np.array([[-1, 1], [-1, 1]]	) * 0.2_5
  		__snake_case		       =     np.array([[-1, -1], [1, 1]]	) * 0.2_5
  		__snake_case		       =     np.array([[1, 1], [1, 1]]	) * 0.2_5
  		__snake_case		       =     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(__snake_case	):
  				__snake_case		       =     warp(__snake_case						,						__snake_case						,						__snake_case	)
  				__snake_case		       =     convolve(__snake_case						,						__snake_case	) + convolve(__snake_case						,						__snake_case	)
  				__snake_case		       =     convolve(__snake_case						,						__snake_case	) + convolve(__snake_case						,						__snake_case	)
  				__snake_case		       =     convolve(__snake_case						,						__snake_case	) + convolve(__snake_case						,						-kernel_t	)
  
  				__snake_case		       =     convolve(__snake_case						,						__snake_case	)
  				__snake_case		       =     convolve(__snake_case						,						__snake_case	)
  
  				# This updates the flow as proposed in the paper (Step 12)
  				__snake_case		       =     (
  				    derivative_x * avg_horizontal_velocity
  				    + derivative_y * avg_vertical_velocity
  				    + derivative_t
  				)
  				__snake_case		       =     update / (alpha**2 + derivative_x**2 + derivative_y**2)
  
  				__snake_case		       =     avg_horizontal_velocity - derivative_x * update
  				__snake_case		       =     avg_vertical_velocity - derivative_y * update
  
  		return horizontal_flow, vertical_flow
  
  
  if __name__ == "__main__":
  		import doctest
  
  		doctest.testmod()
  
  
  
  
  
  
style_context: from __future__ import annotations
  
  from collections.abc import Iterable
  from dataclasses import dataclass
  from typing import Any
  
  
  
  
  class 					UpperCamelCase	:
  					__UpperCamelCase						=   ("""_front""", """_back""", """_len""")
  
  
  
  
  					@dataclass
  					class 					UpperCamelCase	:
  										__UpperCamelCase						=   None
  										__UpperCamelCase						=   None
  										__UpperCamelCase						=   None
  
  
  
  
  					class 					UpperCamelCase	:
  										__UpperCamelCase						=   ("""_cur""",)
  
  
  
  
  										def __init__( self :					Dict      ,_lowerCAmelCase :					Deque._Node | None						):
  
  
  												"""simple docstring"""
  
  
  
  
  												__snake_case		       =     cur
  
  
  
  
  										def __iter__( self :					Dict						):
  
  
  												"""simple docstring"""
  
  
  
  
  												return self
  
  
  
  
  
  
  
  										def 							UpperCamelCase_						( self :					Optional[Any]						):
  
  
  												"""simple docstring"""
  
  
  
  
  												if self._cur is None:
  														# finished iterating
  														raise StopIteration
  												__snake_case		       =     self._cur.val
  												__snake_case		       =     self._cur.next_node
  
  												return val
  
  
  
  
  					def __init__( self :					Optional[int]      ,_lowerCAmelCase :					Iterable[Any] | None = None						):
  
  
  							"""simple docstring"""
  
  
  
  
  							__snake_case		       =     None
  							__snake_case		       =     None
  							__snake_case		       =     0
  
  							if iterable is not None:
  									# append every value to the deque
  									for val in iterable:
  											self.append(_lowerCAmelCase						)
  
  
  
  
  					def 							UpperCamelCase_						( self :					int      ,_lowerCAmelCase :					Any						):
  
  
  							"""simple docstring"""
  
  
  
  
  							__snake_case		       =     self._Node(_lowerCAmelCase      ,_lowerCAmelCase      ,_lowerCAmelCase						)
  							if self.is_empty():
  									# front = back
  									__snake_case		       =     __snake_case		       =     node
  									__snake_case		       =     1
  							else:
  									# connect nodes
  									__snake_case		       =     node
  									__snake_case		       =     self._back
  									__snake_case		       =     node  # assign new back to the new node
  
  									self._len += 1
  
  									# make sure there were no errors
  									assert not self.is_empty(), "Error on appending value."
  
  
  
  
  					def 							UpperCamelCase_						( self :					Any      ,_lowerCAmelCase :					Any						):
  
  
  							"""simple docstring"""
  
  
  
  
  							__snake_case		       =     self._Node(_lowerCAmelCase      ,_lowerCAmelCase      ,_lowerCAmelCase						)
  							if self.is_empty():
  									# front = back
  									__snake_case		       =     __snake_case		       =     node
  									__snake_case		       =     1
  							else:
  									# connect nodes
  									__snake_case		       =     self._front
  									__snake_case		       =     node
  									__snake_case		       =     node  # assign new front to the new node
  
  									self._len += 1
  
  									# make sure there were no errors
  									assert not self.is_empty(), "Error on appending value."
  
  
  
  
  					def 							UpperCamelCase_						( self :					int      ,_lowerCAmelCase :					Iterable[Any]						):
  
  
  							"""simple docstring"""
  
  
  
  
  							for val in iterable:
  									self.append(_lowerCAmelCase						)
  
  
  
  
  					def 							UpperCamelCase_						( self :					Optional[int]      ,_lowerCAmelCase :					Iterable[Any]						):
  
  
  							"""simple docstring"""
  
  
  
  
  							for val in iterable:
  									self.appendleft(_lowerCAmelCase						)
  
  
  
  
  					def 							UpperCamelCase_						( self :					List[Any]						):
  
  
  							"""simple docstring"""
  
  
  
  
  							assert not self.is_empty(), "Deque is empty."
  
  							__snake_case		       =     self._back
  							__snake_case		       =     self._back.prev_node  # set new back
  							# drop the last node - python will deallocate memory automatically
  							__snake_case		       =     None
  
  							self._len -= 1
  
  							return topop.val
  
  
  
  
  					def 							UpperCamelCase_						( self :					List[str]						):
  
  
  							"""simple docstring"""
  
  
  
  
  							assert not self.is_empty(), "Deque is empty."
  
  							__snake_case		       =     self._front
  							__snake_case		       =     self._front.next_node  # set new front and drop the first node
  							__snake_case		       =     None
  
  							self._len -= 1
  
  							return topop.val
  
  
  
  
  					def 							UpperCamelCase_						( self :					int						):
  
  
  							"""simple docstring"""
  
  
  
  
  							return self._front is None
  
  
  
  
  					def __len__( self :					Tuple						):
  
  
  							"""simple docstring"""
  
  
  
  
  							return self._len
  
  
  
  
  					def __eq__( self :					Union[str, Any]      ,_lowerCAmelCase :					object						):
  
  
  							"""simple docstring"""
  
  
  
  
  							if not isinstance(_lowerCAmelCase      ,_lowerCAmelCase						):
  									return NotImplemented
  
  							__snake_case		       =     self._front
  							__snake_case		       =     other._front
  
  							# if the length of the dequeues are not the same, they are not equal
  							if len(self						) != len(_lowerCAmelCase						):
  									return False
  
  							while me is not None and oth is not None:
  									# compare every value
  									if me.val != oth.val:
  											return False
  									__snake_case		       =     me.next_node
  									__snake_case		       =     oth.next_node
  
  							return True
  
  
  
  
  					def __iter__( self :					Tuple						):
  
  
  							"""simple docstring"""
  
  
  
  
  							return Deque._Iterator(self._front						)
  
  
  
  
  
  
  
  					def __repr__( self :					List[Any]						):
  
  
  							"""simple docstring"""
  
  
  
  
  							__snake_case		       =     []
  							__snake_case		       =     self._front
  							while aux is not None:
  									# append the values in a list to display
  									values_list.append(aux.val						)
  									__snake_case		       =     aux.next_node
  
  							return F"""[{", ".join(repr(_lowerCAmelCase						) for val in values_list						)}]"""
  
  
  if __name__ == "__main__":
  		import doctest
  
  		doctest.testmod()
  
  
  
  
  
  