U
    ?h                     @   sb   d Z ddlZddlmZ ddlmZ ddlm	Z	 dd Z
d	d
 ZdddZdddejfddZdS )z*Common tools to optical flow algorithms.

    N)ndimage   )pyramid_reduce)_convertc                 C   s2   |  }t| D ]\}}||df  |7  < q|S )a>  Compute warp point coordinates.

    Parameters
    ----------
    grid : iterable
        The sparse grid to be warped (obtained using
        ``np.meshgrid(..., sparse=True)).``)
    flow : ndarray
        The warping motion field.

    Returns
    -------
    out : ndarray
        The warp point coordinates.

    .)copy	enumerate)gridflowoutidxg r   Z/var/www/html/venv/lib/python3.8/site-packages/skimage/registration/_optical_flow_utils.pyget_warp_points   s    r   c                 C   sh   dd t || jdd D }tj|| jd}|D ]}|dtjf }q2|tj| dg| ddd	d
 }|S )a  Rescale the values of the vector field (u, v) to the desired shape.

    The values of the output vector field are scaled to the new
    resolution.

    Parameters
    ----------
    flow : ndarray
        The motion field to be processed.
    shape : iterable
        Couple of integers representing the output shape.

    Returns
    -------
    rflow : ndarray
        The resized and rescaled motion field.

    c                 S   s   g | ]\}}|| qS r   r   ).0nor   r   r   
<listcomp>7   s     zresize_flow.<locals>.<listcomp>   Ndtype.r   ZnearestF)ordermodeZ	prefilter)zipshapenparrayr   ZnewaxisndiZzoom)r	   r   scaleZscale_factor_Zrflowr   r   r   resize_flow#   s     r           @
      c                 C   sf   | g}t | j}d}||k rX||| krXt|d |dd}|| t |j}|d7 }q|ddd S )a  Construct image pyramid.

    Parameters
    ----------
    I : ndarray
        The image to be preprocessed (Gray scale or RGB).
    downscale : float
        The pyramid downscale factor.
    nlevel : int
        The maximum number of pyramid levels.
    min_size : int
        The minimum size for any dimension of the pyramid levels.

    Returns
    -------
    pyramid : list[ndarray]
        The coarse to fine images pyramid.

    r   N)Zchannel_axis)minr   r   append)I	downscalenlevelmin_sizepyramidsizecountJr   r   r   get_pyramidC   s    



r/   c                 C   s   | j |j krtdt|jdkr,tdtttt| ||||tt|||||}tj	|d d j
f|d d j  |d}||d d |d d |}|dd D ]\}	}
||	|
t||	j }q|S )a{  Generic coarse to fine solver.

    Parameters
    ----------
    I0 : ndarray
        The first gray scale image of the sequence.
    I1 : ndarray
        The second gray scale image of the sequence.
    solver : callable
        The solver applied at each pyramid level.
    downscale : float
        The pyramid downscale factor.
    nlevel : int
        The maximum number of pyramid levels.
    min_size : int
        The minimum size for any dimension of the pyramid levels.
    dtype : dtype
        Output data type.

    Returns
    -------
    flow : ndarray
        The estimated optical flow components for each axis.

    z'Input images should have the same shapeZefdgz8Only floating point data type are valid for optical flowr   r   r   N)r   
ValueErrorr   r   charlistr   r/   r   Zzerosndimr    )ZI0ZI1Zsolverr(   r)   r*   r   r+   r	   ZJ0ZJ1r   r   r   coarse_to_finee   s,      
   r4   )r!   r"   r#   )__doc__numpyr   Zscipyr   r   Z	transformr   Z
util.dtyper   r   r    r/   Zfloat32r4   r   r   r   r   <module>   s    
"