Ë
    îì�iÓ8  ã                   óÈ   — d Z ddlmZ ddlmZ ddlZddlmZ	 ddl
mZ ddlmZ dd	lmZ d
dlmZmZ d„ Zddddddej*                  dœd„Zd„ Zddddej*                  dœd„Zy)z,TV-L1 optical flow algorithm implementation.é    )Úpartial)Úcombinations_with_replacementN)Úndimageé   )Úgaussian)Ú_supported_float_type)Úwarpé   )Ú_coarse_to_fineÚ_get_warp_pointsc	                 ó¨  — | j                   }	t        j                  | j                  D �
cg c]  }
t        j                  |
|	¬«      ‘Œ c}
dddœŽ}d| j
                  z  }d}||z  }||z  }|| j                  z  }|x}}t        j                  | j
                  f| j                  z   |	¬«      }t        j                  | j
                  | j
                  f| j                  z   |	¬«      }t        d«      g|j
                  z  }t        d«      g|j
                  z  }t        d«      g|j
                  dz
  z  }t        |«      D �]	  }|r(t        j                  |dg| j
                  d	gz  z   «      }t        |t        ||«      d
¬«      }t        j                  t        j                  |«      «      }||z  j!                  d«      }d||dk(  <   || z
  ||z  j!                  d«      z
  }t        |«      D �]7  }|||z  j!                  d«      z   }t#        |«      ||z  k  }|}|dd…|fxx   ||   |dd…|f   z  ||   z  z  cc<   | }|t        j$                  ||   «      z  }|dd…|fxx   ||dd…|f   z  z  cc<   |j'                  «       }t        | j
                  «      D �]„  }||d<   t        |«      D �]m  }t        | j
                  «      D ]P  } | |d<   t        dd«      || dz   <   t        j(                  ||   | ¬«      |t+        |«      <   t        d«      || dz   <   ŒR t        j,                  |dz  j!                  d«      «      t        j.                  df   }!|!|z  }!|!dz  }!||xx   ||z  z  cc<   ||xx   |!z  cc<   ||   j!                  d«       }"t        | j
                  «      D ]i  } | |d<   t        dd«      || dz   <   t        dd«      || <   |"t+        |«      xx   |t+        |«         z  cc<   t        d«      || dz   <   t        d«      || <   Œk ||   |"z   ||<   �Œp �Œ‡ �Œ: ||z  }||z  j!                  «       |k  r |S |}�Œ |S c c}
w )u¶  TV-L1 solver for optical flow estimation.

    Parameters
    ----------
    reference_image : ndarray, shape (M, N[, P[, ...]])
        The first grayscale image of the sequence.
    moving_image : ndarray, shape (M, N[, P[, ...]])
        The second grayscale image of the sequence.
    flow0 : ndarray, shape (image0.ndim, M, N[, P[, ...]])
        Initialization for the vector field.
    attachment : float
        Attachment parameter. The smaller this parameter is,
        the smoother is the solutions.
    tightness : float
        Tightness parameter. It should have a small value in order to
        maintain attachment and regularization parts in
        correspondence.
    num_warp : int
        Number of times moving_image is warped.
    num_iter : int
        Number of fixed point iteration.
    tol : float
        Tolerance used as stopping criterion based on the LÂ² distance
        between two consecutive values of (u, v).
    prefilter : bool
        Whether to prefilter the estimated optical flow before each
        image warp.

    Returns
    -------
    flow : ndarray, shape (image0.ndim, M, N[, P[, ...]])
        The estimated optical flow components for each axis.

    ©ÚdtypeÚijT©ÚindexingÚsparseg      à?r   Nr
   é   Úedge©Úmoder   éÿÿÿÿ©Úaxis.g      ð?)r   ÚnpÚmeshgridÚshapeÚarangeÚndimÚsizeÚzerosÚsliceÚrangeÚndiÚmedian_filterr	   r   ÚarrayÚgradientÚsumÚabsÚsignÚcopyÚdiffÚtupleÚsqrtÚnewaxis)#Úreference_imageÚmoving_imageÚflow0Ú
attachmentÚ	tightnessÚnum_warpÚnum_iterÚtolÚ	prefilterr   ÚnÚgridÚdtÚreg_num_iterÚf0Úf1Úflow_currentÚflow_previousÚgÚprojÚs_gÚs_pÚs_dÚ_Úimage1_warpÚgradÚNIÚrho_0ÚrhoÚidxÚflow_auxiliaryÚsrhoÚaxÚnormÚds#                                      ú‡/var/www/ITT-ServiceAPI-Services/ITT-Document-Scan/mrzscanner/mrzenv/lib/python3.12/site-packages/skimage/registration/_optical_flow.pyÚ_tvl1rS      s¤  € ð\ ×!Ñ!€EÜ�;‰;Ø-<×-BÑ-BÖ	C¨Œ"�)‰)�A˜UÖ
#Ò	CØØò€Dð 
ˆ×#Ñ#Ñ	#€BØ€LØ	�iÑ	€BØ	ˆi‰€BØˆ?×ÑÑ€Cà#(Ð(€L�=ä
�‰�/×&Ñ&Ð(¨?×+@Ñ+@Ñ@ÈÔN€AÜ�8‰8à× Ñ Ø× Ñ ð	
ð ×
Ñ
ñ		 ð
 ô€Dô 	ˆd‹ðà	�‰ñ€Cô 	ˆd‹ðà�	‰	ñ€Cô 	ˆd‹ðà	�‰�Q‰ñ€Cô �8‹_ó @%ˆÙÜ×,Ñ,Ø˜q˜c O×$8Ñ$8¸A¸3Ñ$>Ñ>óˆLô ØÔ*¨4°Ó>ÀVô
ˆô �x‰xœŸ™ KÓ0Ó1ˆØ�T‰k×Ñ˜qÓ!ˆØˆˆ2�‰7‰à˜oÑ-°¸Ñ1D×0IÑ0IÈ!Ó0LÑLˆä�x“ó +	@ˆAð ˜4 ,Ñ.×3Ñ3°AÓ6Ñ6ˆCä�c“(˜b 2™gÑ%ˆCà)ˆNàš1˜c˜6Ó" c¨#¡h°²a¸°f±Ñ&=ÀÀ3ÁÑ&GÑGÓ"à�$ˆCØœŸ™  C¡Ó)Ñ)ˆDØš1˜c˜6Ó" d¨T²!°S°&©\Ñ&9Ñ9Ó"ð *×.Ñ.Ó0ˆLä˜_×1Ñ1Ó2ó @�Ø��A‘Ü˜|Ó,ó @�AÜ# O×$8Ñ$8Ó9ò 2˜Ø!#˜˜A™Ü&+¨A¨r£l˜˜B ™F™Ü(*¯©°¸SÑ0AÈÔ(K˜œ% ›*™Ü&+¨D£k˜˜B ™Fšð	2ô Ÿ7™7 A q¡D§:¡:¨a£=Ó1´"·*±*¸c°/ÑB�DØ˜B‘J�DØ˜C‘K�DØ˜“I  a¡Ñ'“IØ˜“I Ñ%“Ið ˜c™Ÿ™ qÓ)Ð)�AÜ# O×$8Ñ$8Ó9ò .˜Ø!#˜˜A™Ü&+¨A¨r£l˜˜B ™F™Ü"'¨¨4£.˜˜B™Øœ% ›*›¨¬e°C«jÑ)9Ñ9›Ü&+¨D£k˜˜B ™F™Ü"'¨£+˜˜Bšð.ð )7°sÑ(;¸aÑ(?�L Ó%ò/@ò@ð%+	@ðZ 	˜Ñ%ˆØ˜MÑ)×.Ñ.Ó0°3Ò6Øð Ðð %ŠðA@%ðD ÐùòI 
Ds   ªQé   g333333Ó?é   é
   g-Cëâ6?F)r3   r4   r5   r6   r7   r8   r   c          	      ó°   — t        t        ||||||¬«      }	t        j                  |«      t	        |«      k7  rd|› d�}
t        |
«      ‚t        | ||	|¬«      S )uË  Coarse to fine optical flow estimator.

    The TV-L1 solver is applied at each level of the image
    pyramid. TV-L1 is a popular algorithm for optical flow estimation
    introduced by Zack et al. [1]_, improved in [2]_ and detailed in [3]_.

    Parameters
    ----------
    reference_image : ndarray, shape (M, N[, P[, ...]])
        The first grayscale image of the sequence.
    moving_image : ndarray, shape (M, N[, P[, ...]])
        The second grayscale image of the sequence.
    attachment : float, optional
        Attachment parameter (:math:`\lambda` in [1]_). The smaller
        this parameter is, the smoother the returned result will be.
    tightness : float, optional
        Tightness parameter (:math:`\theta` in [1]_). It should have
        a small value in order to maintain attachment and
        regularization parts in correspondence.
    num_warp : int, optional
        Number of times moving_image is warped.
    num_iter : int, optional
        Number of fixed point iteration.
    tol : float, optional
        Tolerance used as stopping criterion based on the LÂ² distance
        between two consecutive values of (u, v).
    prefilter : bool, optional
        Whether to prefilter the estimated optical flow before each
        image warp. When True, a median filter with window size 3
        along each axis is applied. This helps to remove potential
        outliers.
    dtype : dtype, optional
        Output data type: must be floating point. Single precision
        provides good results and saves memory usage and computation
        time compared to double precision.

    Returns
    -------
    flow : ndarray, shape (image0.ndim, M, N[, P[, ...]])
        The estimated optical flow components for each axis.

    Notes
    -----
    Color images are not supported.

    References
    ----------
    .. [1] Zach, C., Pock, T., & Bischof, H. (2007, September). A
       duality based approach for realtime TV-L 1 optical flow. In Joint
       pattern recognition symposium (pp. 214-223). Springer, Berlin,
       Heidelberg. :DOI:`10.1007/978-3-540-74936-3_22`
    .. [2] Wedel, A., Pock, T., Zach, C., Bischof, H., & Cremers,
       D. (2009). An improved algorithm for TV-L 1 optical flow. In
       Statistical and geometrical approaches to visual motion analysis
       (pp. 23-45). Springer, Berlin, Heidelberg.
       :DOI:`10.1007/978-3-642-03061-1_2`
    .. [3] PÃ©rez, J. S., Meinhardt-Llopis, E., & Facciolo,
       G. (2013). TV-L1 optical flow estimation. Image Processing On
       Line, 2013, 137-150. :DOI:`10.5201/ipol.2013.26`

    Examples
    --------
    >>> from skimage.color import rgb2gray
    >>> from skimage.data import stereo_motorcycle
    >>> from skimage.registration import optical_flow_tvl1
    >>> image0, image1, disp = stereo_motorcycle()
    >>> # --- Convert the images to gray level: color is not supported.
    >>> image0 = rgb2gray(image0)
    >>> image1 = rgb2gray(image1)
    >>> flow = optical_flow_tvl1(image1, image0)

    )r3   r4   r5   r6   r7   r8   údtype=ú. is not supported. Try 'float32' or 'float64.'r   )r   rS   r   r   r   Ú
ValueErrorr   )r0   r1   r3   r4   r5   r6   r7   r8   r   ÚsolverÚmsgs              rR   Úoptical_flow_tvl1r]   ¦   sf   € ôj ÜØØØØØØô€Fô 
‡x�x�ƒÔ/°Ó6Ò6Ø�u�gÐKÐLˆÜ˜‹oÐä˜?¨L¸&ÈÔNÐNó    c           	      óÌ  — | j                   }| j                  }d|z  dz   }	|r||	dz  fz  }
t        t        |
d¬«      }n t        t        j
                  ||	fz  d¬«      }|}t        j                  | j                  ||fz   |¬«      }t        j                  | j                  |dfz   |¬«      }t        j                  | j                  D �cg c]  }t        j                  ||¬«      ‘Œ c}dd	d
œŽ}t        |«      D �]`  }|rt	        j                  |d|dz  z   «      }t        |t        ||«      d¬«      }t        j                  t        j                   |«      d¬«      }||z  j#                  d¬«      | z   |z
  }t%        t        |«      d«      D ]$  \  }} |||   ||   z  «      x|d||f<   |d||f<   Œ& t        |«      D ]  } |||   |z  «      |d|df<   Œ t'        t        j(                  j+                  |«      «      dk  }t        j,                  ||¬«      ||<   d||<   t        j.                  t        j(                  j1                  ||«      d   |d«      }�Œc |S c c}w )aï  Iterative Lucas-Kanade (iLK) solver for optical flow estimation.

    Parameters
    ----------
    reference_image : ndarray, shape (M, N[, P[, ...]])
        The first grayscale image of the sequence.
    moving_image : ndarray, shape (M, N[, P[, ...]])
        The second grayscale image of the sequence.
    flow0 : ndarray, shape (reference_image.ndim, M, N[, P[, ...]])
        Initialization for the vector field.
    radius : int
        Radius of the window considered around each pixel.
    num_warp : int
        Number of times moving_image is warped.
    gaussian : bool
        if True, a gaussian kernel is used for the local
        integration. Otherwise, a uniform kernel is used.
    prefilter : bool
        Whether to prefilter the estimated optical flow before each
        image warp. This helps to remove potential outliers.

    Returns
    -------
    flow : ndarray, shape (reference_image.ndim, M, N[, P[, ...]])
        The estimated optical flow components for each axis.

    r   r
   é   Úmirror)Úsigmar   )r    r   r   r   Tr   )r
   )r   r   r   r   r   .g›+¡†›„=).r   )r   r   r   Úgaussian_filterr$   Úuniform_filterr   r!   r   r   r   r#   r%   r	   r   Ústackr'   r(   r   r)   ÚlinalgÚdetÚeyeÚmoveaxisÚsolve)r0   r1   r2   Úradiusr5   r   r8   r   r   r    rb   Úfilter_funcÚflowÚAÚbr9   r:   rF   Úmoving_image_warprH   Úerror_imageÚiÚjrL   s                           rR   Ú_ilkrt     s[  € ð8 ×!Ñ!€EØ×Ñ€DØˆv‰:˜‰>€DáØ˜˜q™�{Ñ"ˆÜœo°UÀÔJ‰äœc×0Ñ0°t¸t¸g±~ÈHÔUˆà€Dô 	�‰�×&Ñ&¨$°¨Ñ5¸UÔC€AÜ
�‰�×&Ñ&¨$°¨Ñ2¸%Ô@€Aä�;‰;Ø-<×-BÑ-BÖ	C¨Œ"�)‰)�A˜UÖ
#Ò	CØØò€Dô �8‹_ó CˆÙÜ×$Ñ$ T¨4°$¸±+Ñ+=Ó>ˆDä ØÔ*¨4°Ó6¸Vô
Ðô �x‰xœŸ™Ð$5Ó6¸QÔ?ˆØ˜d‘{×'Ñ'¨QÐ'Ó/°/ÑAÐDUÑUˆô 2´%¸³+¸qÓAò 	I‰DˆAˆqÙ*5°d¸1±gÀÀQÁÑ6GÓ*HÐHˆAˆc�1�aˆi‰L˜1˜S ! Q˜Yš<ð	Iô �t“ò 	>ˆAÙ& t¨A¡w°Ñ'<Ó=ˆAˆc�1�aˆiŠLð	>ô ”"—)‘)—-‘- Ó"Ó# eÑ+ˆÜ—‘˜ EÔ*ˆˆ#‰Øˆˆ#‰ô �{‰{œ2Ÿ9™9Ÿ?™?¨1¨aÓ0°Ñ8¸$ÀÓBŠð/Cð2 €Kùò= 
Ds   Ã
I!é   )rk   r5   r   r8   r   c                ó¬   — t        t        ||||¬«      }t        j                  |«      t	        |«      k7  rd|› d�}t        |«      ‚t        | |||¬«      S )a9
  Coarse to fine optical flow estimator.

    The iterative Lucas-Kanade (iLK) solver is applied at each level
    of the image pyramid. iLK [1]_ is a fast and robust alternative to
    TVL1 algorithm although less accurate for rendering flat surfaces
    and object boundaries (see [2]_).

    Parameters
    ----------
    reference_image : ndarray, shape (M, N[, P[, ...]])
        The first grayscale image of the sequence.
    moving_image : ndarray, shape (M, N[, P[, ...]])
        The second grayscale image of the sequence.
    radius : int, optional
        Radius of the window considered around each pixel.
    num_warp : int, optional
        Number of times moving_image is warped.
    gaussian : bool, optional
        If True, a Gaussian kernel is used for the local
        integration. Otherwise, a uniform kernel is used.
    prefilter : bool, optional
        Whether to prefilter the estimated optical flow before each
        image warp. When True, a median filter with window size 3
        along each axis is applied. This helps to remove potential
        outliers.
    dtype : dtype, optional
        Output data type: must be floating point. Single precision
        provides good results and saves memory usage and computation
        time compared to double precision.

    Returns
    -------
    flow : ndarray, shape (reference_image.ndim, M, N[, P[, ...]])
        The estimated optical flow components for each axis.

    Notes
    -----
    - The implemented algorithm is described in **Table2** of [1]_.
    - Color images are not supported.

    References
    ----------
    .. [1] Le Besnerais, G., & Champagnat, F. (2005, September). Dense
       optical flow by iterative local window registration. In IEEE
       International Conference on Image Processing 2005 (Vol. 1,
       pp. I-137). IEEE. :DOI:`10.1109/ICIP.2005.1529706`
    .. [2] Plyer, A., Le Besnerais, G., & Champagnat,
       F. (2016). Massively parallel Lucas Kanade optical flow for
       real-time video processing applications. Journal of Real-Time
       Image Processing, 11(4), 713-730. :DOI:`10.1007/s11554-014-0423-0`

    Examples
    --------
    >>> from skimage.color import rgb2gray
    >>> from skimage.data import stereo_motorcycle
    >>> from skimage.registration import optical_flow_ilk
    >>> reference_image, moving_image, disp = stereo_motorcycle()
    >>> # --- Convert the images to gray level: color is not supported.
    >>> reference_image = rgb2gray(reference_image)
    >>> moving_image = rgb2gray(moving_image)
    >>> flow = optical_flow_ilk(moving_image, reference_image)

    )rk   r5   r   r8   rX   rY   r   )r   rt   r   r   r   rZ   r   )	r0   r1   rk   r5   r   r8   r   r[   r\   s	            rR   Úoptical_flow_ilkrw   [  s\   € ôT Ü�V h¸ÈYô€Fô 
‡x�x�ƒÔ/°Ó6Ò6Ø�u�gÐKÐLˆÜ˜‹oÐä˜?¨L¸&ÈÔNÐNr^   )Ú__doc__Ú	functoolsr   Ú	itertoolsr   Únumpyr   Úscipyr   r$   Ú_shared.filtersr   rc   Ú_shared.utilsr   Ú	transformr	   Ú_optical_flow_utilsr   r   rS   Úfloat32r]   rt   rw   © r^   rR   ú<module>rƒ      sm   ðÙ 2å Ý 3ã Ý  å 9Ý 1Ý ß BòTðv ØØØØØØ
�*‰*ôcOòLLðf ØØØØ
�*‰*õROr^   