
'''simple docstring'''
from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve

def     UpperCamelCase_  (						__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							):
    """simple docstring"""






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

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

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

    # Set pixels at invalid locations to 0
    snake_case_			:     Dict        =    0

    return warped

def     UpperCamelCase_  (						__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE = None,		):
    """simple docstring"""






    if alpha is None:
        snake_case_			:     List[Any]        =    0.1

    # Initialize flow
    snake_case_			:     Optional[Any]        =    np.zeros_like(__SCREAMING_SNAKE_CASE							)
    snake_case_			:     Optional[Any]        =    np.zeros_like(__SCREAMING_SNAKE_CASE							)

    # Prepare kernels for the calculation of the derivatives and the average velocity
    snake_case_			:     Optional[int]        =    np.array([[-1, 1], [-1, 1]]							) * 0.25
    snake_case_			:     Tuple        =    np.array([[-1, -1], [1, 1]]							) * 0.25
    snake_case_			:     Any        =    np.array([[1, 1], [1, 1]]							) * 0.25
    snake_case_			:     Optional[Any]        =    np.array(
        [[1 / 1_2, 1 / 6, 1 / 1_2], [1 / 6, 0, 1 / 6], [1 / 1_2, 1 / 6, 1 / 1_2]]							)

    # Iteratively refine the flow
    for _ in range(__SCREAMING_SNAKE_CASE							):
        snake_case_			:     str        =    warp(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							)
        snake_case_			:     str        =    convolve(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							) + convolve(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							)
        snake_case_			:     Tuple        =    convolve(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							) + convolve(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							)
        snake_case_			:     List[str]        =    convolve(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							) + convolve(__SCREAMING_SNAKE_CASE,		-kernel_t							)

        snake_case_			:     int        =    convolve(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							)
        snake_case_			:     str        =    convolve(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							)

        # This updates the flow as proposed in the paper (Step 12)
        snake_case_			:     int        =    (
            derivative_x * avg_horizontal_velocity
            + derivative_y * avg_vertical_velocity
            + derivative_t
        )
        snake_case_			:     Optional[int]        =    update / (alpha**2 + derivative_x**2 + derivative_y**2)

        snake_case_			:     Union[str, Any]        =    avg_horizontal_velocity - derivative_x * update
        snake_case_			:     str        =    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_           =    {
    "configuration_sam": [
        "SAM_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "SamConfig",
        "SamMaskDecoderConfig",
        "SamPromptEncoderConfig",
        "SamVisionConfig",
    ],
    "processing_sam": ["SamProcessor"],
}


try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  a_           =    [
      "SAM_PRETRAINED_MODEL_ARCHIVE_LIST",
      "SamModel",
      "SamPreTrainedModel",
  ]
try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  a_           =    [
      "TF_SAM_PRETRAINED_MODEL_ARCHIVE_LIST",
      "TFSamModel",
      "TFSamPreTrainedModel",
  ]
try:
  if not is_vision_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  a_           =    ["SamImageProcessor"]


if TYPE_CHECKING:
  from .configuration_sam import (
      SAM_PRETRAINED_CONFIG_ARCHIVE_MAP,
      SamConfig,
      SamMaskDecoderConfig,
      SamPromptEncoderConfig,
      SamVisionConfig,
  )
  from .processing_sam import SamProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_sam import SAM_PRETRAINED_MODEL_ARCHIVE_LIST, SamModel, SamPreTrainedModel

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_sam import TF_SAM_PRETRAINED_MODEL_ARCHIVE_LIST, TFSamModel, TFSamPreTrainedModel

  try:
    if not is_vision_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .image_processing_sam import SamImageProcessor

else:
  import sys

  a_           =    _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)




