


from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve

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



	"""simple docstring"""






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

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

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

	# Set pixels at invalid locations to 0
	a_     			=   0

	return warped

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



	"""simple docstring"""






	if alpha is None:
		a_     			=   0.1

	# Initialize flow
	a_     			=   np.zeros_like(a__	)
	a_     			=   np.zeros_like(a__	)

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

		a_     			=   convolve(a__     ,					a__	)
		a_     			=   convolve(a__     ,					a__	)

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

		a_     			=   avg_horizontal_velocity - derivative_x * update
		a_     			=   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_tf_available,
    is_torch_available,
    is_vision_available,
)


a__			:						Tuple							=			{
    """configuration_segformer""": ["""SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SegformerConfig""", """SegformerOnnxConfig"""]
}

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							a__			:						List[str]							=			["""SegformerFeatureExtractor"""]
							a__			:						str							=			["""SegformerImageProcessor"""]

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							a__			:						Optional[Any]							=			[
							    """SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """SegformerDecodeHead""",
							    """SegformerForImageClassification""",
							    """SegformerForSemanticSegmentation""",
							    """SegformerLayer""",
							    """SegformerModel""",
							    """SegformerPreTrainedModel""",
							]

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							a__			:						List[Any]							=			[
							    """TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """TFSegformerDecodeHead""",
							    """TFSegformerForImageClassification""",
							    """TFSegformerForSemanticSegmentation""",
							    """TFSegformerModel""",
							    """TFSegformerPreTrainedModel""",
							]


if TYPE_CHECKING:
							from .configuration_segformer import SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SegformerConfig, SegformerOnnxConfig

							try:
														if not is_vision_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .feature_extraction_segformer import SegformerFeatureExtractor
														from .image_processing_segformer import SegformerImageProcessor

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_segformer import (
														    SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
														    SegformerDecodeHead,
														    SegformerForImageClassification,
														    SegformerForSemanticSegmentation,
														    SegformerLayer,
														    SegformerModel,
														    SegformerPreTrainedModel,
														)
							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_segformer import (
														    TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFSegformerDecodeHead,
														    TFSegformerForImageClassification,
														    TFSegformerForSemanticSegmentation,
														    TFSegformerModel,
														    TFSegformerPreTrainedModel,
														)

else:
							import sys

							a__			:						Any							=			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


