ó
 ‰\c        	   @   s¼   d  d l  Z d  d l m Z d „  Z d „  Z d d „ Z d d „ Z d „  Z	 d	 „  Z
 d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d d d d d e d „ Z d S(   iÿÿÿÿN(   t   distance_transform_edtc   	      C   sË  t  j |  d d d ƒ} | d d … d d … f | d d … d d … f d } | d d … d d … f | d d … d d … f d } | d d … d d … f | d d … d d … f d |  } | d d … d d … f | d d … d d … f d |  } d	 | d d … d d … f | d d … d d … f | d d … d d … f | d d … d d … f } | d | d } | | d d | | | | | d | t  j | ƒ d
 } | S(   s2   Returns the 'curvature' of a level set 'phi'.
    i   t   modet   edgei   Niÿÿÿÿiþÿÿÿg       @g      Ð?g:Œ0âŽyE>(   t   npt   padt   sqrt(	   t   phit   Pt   fyt   fxt   fyyt   fxxt   fxyt   grad2t   K(    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyt   _cv_curvature   s    ::>>n'c         C   s  d } t  j | d d d ƒ} | d d … d d … f | d d … d d … f } | d d … d d … f | d d … d d … f }	 | d d … d d … f | d d … d d … f d	 }
 | d d … d d … f | d d … d d … f } | d d … d d … f | d d … d d … f } | d d … d d … f | d d … d d … f d	 } d
 t  j | | d | d ƒ } d
 t  j | |	 d | d ƒ } d
 t  j | |
 d | d ƒ } d
 t  j | |
 d | d ƒ } | d d … d d … f | | d d … d d … f | | d d … d d … f | | d d … d d … f | } d | d k } t |  | ƒ \ } } | |  | d | |  | d } | | t | ƒ | | | } | d | | t | ƒ | | | | S(   sL   Returns the variation of level set 'phi' based on algorithm parameters.
    g¼‰Ø—²Òœ<i   R   R   iÿÿÿÿi   Niþÿÿÿg       @g      ð?i    (   R   R   R   t   _cv_calculate_averagest	   _cv_delta(   t   imageR   t   mut   lambda1t   lambda2t   dtt   etaR   t   phixpt   phixnt   phix0t   phiypt   phiynt   phiy0t   C1t   C2t   C3t   C4R   t   Hphit   c1t   c2t   difference_from_average_termt   new_phi(    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyt   _cv_calculate_variation   s(    66:66:####zg      ð?c         C   s$   d d d t  j t  j |  | ƒ S(   sV   Returns the result of a regularised heavyside function of the
    input value(s).
    g      à?g      ð?g       @(   R   t   pit   arctan(   t   xt   eps(    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyt   _cv_heavyside4   s    c         C   s   | | d |  d S(   sR   Returns the result of a regularised dirac function of the
    input value(s).
    i   (    (   R*   R+   (    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyR   ;   s    c         C   s   | } d | } t  j | ƒ } t  j | ƒ } t  j |  | ƒ } t  j |  | ƒ } | d k rm | | :} n  | d k r† | | :} n  | | f S(   s7   Returns the average values 'inside' and 'outside'.
    g      ð?i    (   R   t   sum(   R   R"   t   Ht   Hinvt   Hsumt   Hinvsumt
   avg_insidet   avg_oustide(    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyR   B   s    
c         C   sG   t  |  | ƒ \ } } d | } | |  | d | | |  | d | S(   sv   Returns the 'energy' contribution due to the difference from
    the average value within a region at each point.
    g      ð?i   (   R   (   R   R"   t
   lambda_post
   lambda_negR#   R$   R/   (    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyt    _cv_difference_from_average_termR   s    
c         C   s   t  |  ƒ } | | S(   s„   Returns the 'energy' contribution due to the length of the
    edge between regions at each point, multiplied by a factor 'mu'.
    (   R   (   R   R   t   toret(    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyt   _cv_edge_length_term\   s    c         C   sJ   t  | ƒ } t |  | | | ƒ } t | | ƒ } t j | ƒ t j | ƒ S(   sB   Returns the total 'energy' of the current level set function.
    (   R,   R6   R8   R   R-   (   R   R   R   R   R   R.   t	   avgenergyt	   lenenergy(    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyt
   _cv_energyd   s    c         C   s   |  S(   sŒ   This is a placeholder function as resetting the level set is not
    strictly necessary, and has not been done for this implementation.
    (    (   R   (    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyt   _cv_reset_level_setm   s    c         C   sf   t  j |  d ƒ j |  d d ƒ } t  j |  d ƒ } t  j t  j | | ƒ t  j t  j | | ƒ S(   s„   Generates a checkerboard level set function.

    According to Pascal Getreuer, such a level set function has fast convergence.
    i    i   (   R   t   aranget   reshapet   sinR(   (   t
   image_sizet   square_sizet   yvt   xv(    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyt   _cv_checkerboardt   s    #c         C   sv   t  j |  ƒ } t |  d d d ƒ } t |  d d d ƒ } d | | | f <t t | | ƒ ƒ } | t | ƒ | S(   sl   Generates a disk level set function.

    The disk covers the whole image along its smallest dimension.
    i    i   i   g        (   R   t   onest   intt   floatt   mint   distance(   R@   t   rest   centerYt   centerXt   radius(    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyt   _cv_large_disk   s    c         C   s~   t  j |  ƒ } t |  d d d ƒ } t |  d d d ƒ } d | | | f <t t | | ƒ ƒ d } | t | ƒ | d S(   sn   Generates a disk level set function.

    The disk covers half of the image along its smallest dimension.
    i    i   i   g        g       @i   (   R   RE   RF   RG   RH   RI   (   R@   RJ   RK   RL   RM   (    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyt   _cv_small_diskŒ   s    c         C   s   t  |  ƒ t k ru |  d k r0 t | d ƒ } q{ |  d k rK t | ƒ } q{ |  d k rf t | ƒ } q{ t d ƒ ‚ n |  } | S(   sL   Generates an initial level set function conditional on input arguments.
    t   checkerboardi   t   disks
   small disks-   Incorrect name for starting level set preset.(   t   typet   strRD   RN   RO   t
   ValueError(   t   init_level_sett   image_shapeRJ   (    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyt   _cv_init_level_set™   s    g      Ð?gü©ñÒMbP?iô  g      à?RP   c	         C   s©  t  |  j ƒ d k r$ t d ƒ ‚ n  t | |  j ƒ }	 t |	 ƒ t j k s] |	 j |  j k rl t d ƒ ‚ n  |  t j |  ƒ }  t j |  ƒ d k rª |  t j |  ƒ }  n  d }
 t	 |  |	 | | | ƒ } g  } | d } |	 d k } x§ | | k r|
 | k  r|	 } t
 |  |	 | | | | ƒ }	 t |	 ƒ }	 t j |	 | d j ƒ  ƒ } |	 d k } t	 |  |	 | | | ƒ } | j | ƒ | } |
 d 7}
 qç W| r¡| |	 | f S| Sd S(   s  Chan-Vese segmentation algorithm.

    Active contour model by evolving a level set. Can be used to
    segment objects without clearly defined boundaries.

    Parameters
    ----------
    image : (M, N) ndarray
        Grayscale image to be segmented.
    mu : float, optional
        'edge length' weight parameter. Higher `mu` values will
        produce a 'round' edge, while values closer to zero will
        detect smaller objects.
    lambda1 : float, optional
        'difference from average' weight parameter for the output
        region with value 'True'. If it is lower than `lambda2`, this
        region will have a larger range of values than the other.
    lambda2 : float, optional
        'difference from average' weight parameter for the output
        region with value 'False'. If it is lower than `lambda1`, this
        region will have a larger range of values than the other.
    tol : float, positive, optional
        Level set variation tolerance between iterations. If the
        L2 norm difference between the level sets of successive
        iterations normalized by the area of the image is below this
        value, the algorithm will assume that the solution was
        reached.
    max_iter : uint, optional
        Maximum number of iterations allowed before the algorithm
        interrupts itself.
    dt : float, optional
        A multiplication factor applied at calculations for each step,
        serves to accelerate the algorithm. While higher values may
        speed up the algorithm, they may also lead to convergence
        problems.
    init_level_set : str or (M, N) ndarray, optional
        Defines the starting level set used by the algorithm.
        If a string is inputted, a level set that matches the image
        size will automatically be generated. Alternatively, it is
        possible to define a custom level set, which should be an
        array of float values, with the same shape as 'image'.
        Accepted string values are as follows.

        'checkerboard'
            the starting level set is defined as
            sin(x/5*pi)*sin(y/5*pi), where x and y are pixel
            coordinates. This level set has fast convergence, but may
            fail to detect implicit edges.
        'disk'
            the starting level set is defined as the opposite
            of the distance from the center of the image minus half of
            the minimum value between image width and image height.
            This is somewhat slower, but is more likely to properly
            detect implicit edges.
        'small disk'
            the starting level set is defined as the
            opposite of the distance from the center of the image
            minus a quarter of the minimum value between image width
            and image height.
    extended_output : bool, optional
        If set to True, the return value will be a tuple containing
        the three return values (see below). If set to False which
        is the default value, only the 'segmentation' array will be
        returned.

    Returns
    -------
    segmentation : (M, N) ndarray, bool
        Segmentation produced by the algorithm.
    phi : (M, N) ndarray of floats
        Final level set computed by the algorithm.
    energies : list of floats
        Shows the evolution of the 'energy' for each step of the
        algorithm. This should allow to check whether the algorithm
        converged.

    Notes
    -----
    The Chan-Vese Algorithm is designed to segment objects without
    clearly defined boundaries. This algorithm is based on level sets
    that are evolved iteratively to minimize an energy, which is
    defined by weighted values corresponding to the sum of differences
    intensity from the average value outside the segmented region, the
    sum of differences from the average value inside the segmented
    region, and a term which is dependent on the length of the
    boundary of the segmented region.

    This algorithm was first proposed by Tony Chan and Luminita Vese,
    in a publication entitled "An Active Countour Model Without Edges"
    [1]_.

    This implementation of the algorithm is somewhat simplified in the
    sense that the area factor 'nu' described in the original paper is
    not implemented, and is only suitable for grayscale images.

    Typical values for `lambda1` and `lambda2` are 1. If the
    'background' is very different from the segmented object in terms
    of distribution (for example, a uniform black image with figures
    of varying intensity), then these values should be different from
    each other.

    Typical values for mu are between 0 and 1, though higher values
    can be used when dealing with shapes with very ill-defined
    contours.

    The 'energy' which this algorithm tries to minimize is defined
    as the sum of the differences from the average within the region
    squared and weighed by the 'lambda' factors to which is added the
    length of the contour multiplied by the 'mu' factor.

    Supports 2D grayscale images only, and does not implement the area
    term described in the original article.

    References
    ----------
    .. [1] An Active Contour Model without Edges, Tony Chan and
           Luminita Vese, Scale-Space Theories in Computer Vision,
           1999, DOI:10.1007/3-540-48236-9_13
    .. [2] Chan-Vese Segmentation, Pascal Getreuer Image Processing On
           Line, 2 (2012), pp. 214-224,
           DOI:10.5201/ipol.2012.g-cv
    .. [3] The Chan-Vese Algorithm - Project Report, Rami Cohen,
           http://arxiv.org/abs/1107.2782, 2011
    i   s!   Input image should be a 2D array.sI   The dimensions of initial level set do not match the dimensions of image.i    i   N(   t   lent   shapeRT   RW   RR   R   t   ndarrayRH   t   maxR;   R'   R<   R   t   meant   append(   R   R   R   R   t   tolt   max_iterR   RU   t   extended_outputR   t   it
   old_energyt   energiest   phivart   segmentationt   oldphit
   new_energy(    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyt	   chan_veseª   s4    '
(   t   numpyR   t   scipy.ndimageR    RI   R   R'   R,   R   R   R6   R8   R;   R<   RD   RN   RO   RW   t   FalseRh   (    (    (    s>   lib/python2.7/site-packages/skimage/segmentation/_chan_vese.pyt   <module>   s"   		 		
								