
    1-Ph                     \    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ddZ
dS )    N)RectBivariateSpline   )_supported_float_type)img_as_float)sobel{Gz?皙?                 ?	  periodic)boundary_conditionc
                   t          |          }|dk    rt          d          d}g d}|
|vr(t          dd                    |          z   dz             t          |           }t	          | j                  }|                    |d	          }|j        d
k    }|dk    rg|rTt          |dddddf                   t          |dddddf                   t          |dddddf                   g}nt          |          g}ndg}|r-|t          j
        |d          z  |t          |          z  z   }n||z  ||d         z  z   }t          t          j        |j        d                   t          j        |j        d                   |j        ddd          }|dddddf         }|dddf                             |          }|dddf                             |          }t          |          }t          j        ||f|          }t          j        ||f|          }t          j        |t$                    }t          j        |dd          t          j        |dd          z   d|z  z
  }t          j        |dd          t          j        |dd          z   dt          j        |dd          z  z
  dt          j        |dd          z  z
  d|z  z   }| |z  ||z  z   }d}|
                    d          rd|dddf<   d|dddf<   g d|ddd
f<   d}d}|
                    d          rd|dddf<   d|dddf<   g d|dddf<   d}d}|
                    d          r*d|dddf<   g d|ddd
f<   d|dddf<   g d|dddf<   d}d}|
                    d          r*d|dddf<   g d|dddf<   d|dddf<   g d|dddf<   d}t          j                            |||z  z             } |                     |d	          } t1          |          D ]}! |||dd                              |d	          }" |||dd                              |d	          }#|r
d|"d<   d|#d<   |r
d|"d<   d|#d<   |r |"dxx         dz  cc<   |#dxx         dz  cc<   |r |"dxx         dz  cc<   |#dxx         dz  cc<   | ||z  |"z   z  }$| ||z  |#z   z  }%|t          j        |$|z
            z  }&|t          j        |%|z
            z  }'|r
d|&d<   d|'d<   |r
d|&d<   d|'d<   ||&z  }||'z  }|!|dz   z  }(|(|k     r|||(ddf<   |||(ddf<   5t          j        t          j        t          j        ||dddf         z
            t          j        ||dddf         z
            z   d                    })|)|	k     r nt          j        ||gd          S )a  Active contour model.

    Active contours by fitting snakes to features of images. Supports single
    and multichannel 2D images. Snakes can be periodic (for segmentation) or
    have fixed and/or free ends.
    The output snake has the same length as the input boundary.
    As the number of points is constant, make sure that the initial snake
    has enough points to capture the details of the final contour.

    Parameters
    ----------
    image : (M, N) or (M, N, 3) ndarray
        Input image.
    snake : (K, 2) ndarray
        Initial snake coordinates. For periodic boundary conditions, endpoints
        must not be duplicated.
    alpha : float, optional
        Snake length shape parameter. Higher values makes snake contract
        faster.
    beta : float, optional
        Snake smoothness shape parameter. Higher values makes snake smoother.
    w_line : float, optional
        Controls attraction to brightness. Use negative values to attract
        toward dark regions.
    w_edge : float, optional
        Controls attraction to edges. Use negative values to repel snake from
        edges.
    gamma : float, optional
        Explicit time stepping parameter.
    max_px_move : float, optional
        Maximum pixel distance to move per iteration.
    max_num_iter : int, optional
        Maximum iterations to optimize snake shape.
    convergence : float, optional
        Convergence criteria.
    boundary_condition : string, optional
        Boundary conditions for the contour. Can be one of 'periodic',
        'free', 'fixed', 'free-fixed', or 'fixed-free'. 'periodic' attaches
        the two ends of the snake, 'fixed' holds the end-points in place,
        and 'free' allows free movement of the ends. 'fixed' and 'free' can
        be combined by parsing 'fixed-free', 'free-fixed'. Parsing
        'fixed-fixed' or 'free-free' yields same behaviour as 'fixed' and
        'free', respectively.

    Returns
    -------
    snake : (K, 2) ndarray
        Optimised snake, same shape as input parameter.

    References
    ----------
    .. [1]  Kass, M.; Witkin, A.; Terzopoulos, D. "Snakes: Active contour
            models". International Journal of Computer Vision 1 (4): 321
            (1988). :DOI:`10.1007/BF00133570`

    Examples
    --------
    >>> from skimage.draw import circle_perimeter
    >>> from skimage.filters import gaussian

    Create and smooth image:

    >>> img = np.zeros((100, 100))
    >>> rr, cc = circle_perimeter(35, 45, 25)
    >>> img[rr, cc] = 1
    >>> img = gaussian(img, sigma=2, preserve_range=False)

    Initialize spline:

    >>> s = np.linspace(0, 2*np.pi, 100)
    >>> init = 50 * np.array([np.sin(s), np.cos(s)]).T + 50

    Fit spline to image:

    >>> snake = active_contour(img, init, w_edge=0, w_line=1)  # doctest: +SKIP
    >>> dist = np.sqrt((45-snake[:, 0])**2 + (35-snake[:, 1])**2)  # doctest: +SKIP
    >>> int(np.mean(dist))  # doctest: +SKIP
    25

    r   zmax_num_iter should be >0.
   )r   freefixedz
free-fixedz
fixed-freezfixed-fixedz	free-freez.Invalid boundary condition.
Should be one of: z, .F)copy   Nr   r   )axis)kxkys)dtype      r   )r   r   r   Tr   )r   r   r    r   )dxgrid)dyr#   )int
ValueErrorjoinr   r   r   astypendimr   npsumr   arangeshapeTlenemptyeyefloatroll
startswithendswithlinalginvrangetanhminmaxabsstack)*imagesnakealphabetaw_linew_edgegammamax_px_movemax_num_iterconvergencer   convergence_order	valid_bcsimgfloat_dtypeRGBedgeintpsnake_xyxynxsaveysaveeye_nabAsfixedefixedsfreeefreer7   ifxfyxnynr"   r$   jdists*                                             i/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/segmentation/active_contour_model.pyactive_contourre   	   s}   | |$$Lq5666  I **#ii	""# 
 
 	
 u

C'44K
**[u*
-
-C
(a-C {{ 	 #aaaAg,''s111aaa7|)<)<eC111aL>Q>QRDD#JJ<DDs  .rvc****Vc$ii-??slVd1g-- 
	#)A,39Q<!8!8#%A!q  D QQQ"W~HAk**AAk**AAAH'+;???EH'+;???E F1E"""E
r"""RWUBQ%?%?%??!e)K  	r"""
'%!
$
$
$	%
bgeRa(((
(	) bgeRa(((
(	) e)		  

TAXA F$$W-- !QQQ$!QQQ$::!RaR%F""7++ "aaa%"aaa%ZZ"bcc'
E$$V,, !QQQ$::!RaR%!QQQ$!>>!RaR%E""6** "aaa%ZZ"bcc'
"aaa%#^^"bcc'
 )--EEM)
*
*C
**[u*
-
-C <   + +T!Q15)))0050IIT!Q15)))0050II 	BqEBqE 	BrFBrF 	qEEEQJEEEqEEEQJEEE 	rFFFaKFFFrFFFaKFFFEAIN#EAIN# 2726??*2726??* 	BqEBqE 	BrFBrF	R	R "Q&'   E!QQQ$KE!QQQ$KK6rveaaaaj011BF51T111W:;M4N4NNPQRR D k!! " 8QF####    )r   r	   r
   r   r   r   r   r	   )numpyr*   scipy.interpolater   _shared.utilsr   utilr   filtersr   re    rf   rd   <module>rm      s        1 1 1 1 1 1 1 1 1 1 1 1             	
q$ "q$ q$ q$ q$ q$ q$ q$rf   