ó
 \c           @   s˘   d  d l  m Z d  d l Z d  d l Z d  d l m Z d d l	 m
 Z
 m Z d d l m Z m Z d d l m Z d d	 d
 d d e d e d d e d  Z d S(   i˙˙˙˙(   t   divisionN(   t   ndimagei   (   t   img_as_floatt   regular_grid(   t   _slic_cythont"   _enforce_label_connectivity_cython(   t   rgb2labid   g      $@i
   i    g      ŕ?i   c   #      C   s	  t  |   }  t } |  j d k rC |  t j d t j f }  t } n] |  j d k rt | rt |  t j d f }  t } n, |  j d k r  | r  |  d t j f }  n  | d k rž t j d  } n0 t | t	 t
 f  rî t j | d t j } n  t | t j  s:t j | | | g d t j } | | j t j  } n0 t | t	 t
 f  rjt j | d t j } n  | d k j   r¤t	 |  d g } t j |  |  }  n  | r	| sź| d k r	|  j d d k rä| rät d   q	|  j d d k r	t |   }  q	n  |  j d  \ } } } t j d |  d |  d |  f \ } } } t |  j d  |  } g  | D]* } t | j d k	 r| j n d	  ^ qm\ } } } | | } | | } | | } t j | j |  j d f  } t j | d t j f | d t j f | d t j f | g d
 d j d d |  j d  } t j |  } t t | | | f   } d | } t j |  |  }  t  |  | | | | |  } | rň| | | | }  t |	 |   }! t |
 |   }" t! | |! |"  } n  | r| d } n  | S(   s"  Segments image using k-means clustering in Color-(x,y,z) space.

    Parameters
    ----------
    image : 2D, 3D or 4D ndarray
        Input image, which can be 2D or 3D, and grayscale or multichannel
        (see `multichannel` parameter).
    n_segments : int, optional
        The (approximate) number of labels in the segmented output image.
    compactness : float, optional
        Balances color proximity and space proximity. Higher values give
        more weight to space proximity, making superpixel shapes more
        square/cubic. In SLICO mode, this is the initial compactness.
        This parameter depends strongly on image contrast and on the
        shapes of objects in the image. We recommend exploring possible
        values on a log scale, e.g., 0.01, 0.1, 1, 10, 100, before
        refining around a chosen value.
    max_iter : int, optional
        Maximum number of iterations of k-means.
    sigma : float or (3,) array-like of floats, optional
        Width of Gaussian smoothing kernel for pre-processing for each
        dimension of the image. The same sigma is applied to each dimension in
        case of a scalar value. Zero means no smoothing.
        Note, that `sigma` is automatically scaled if it is scalar and a
        manual voxel spacing is provided (see Notes section).
    spacing : (3,) array-like of floats, optional
        The voxel spacing along each image dimension. By default, `slic`
        assumes uniform spacing (same voxel resolution along z, y and x).
        This parameter controls the weights of the distances along z, y,
        and x during k-means clustering.
    multichannel : bool, optional
        Whether the last axis of the image is to be interpreted as multiple
        channels or another spatial dimension.
    convert2lab : bool, optional
        Whether the input should be converted to Lab colorspace prior to
        segmentation. The input image *must* be RGB. Highly recommended.
        This option defaults to ``True`` when ``multichannel=True`` *and*
        ``image.shape[-1] == 3``.
    enforce_connectivity: bool, optional
        Whether the generated segments are connected or not
    min_size_factor: float, optional
        Proportion of the minimum segment size to be removed with respect
        to the supposed segment size ```depth*width*height/n_segments```
    max_size_factor: float, optional
        Proportion of the maximum connected segment size. A value of 3 works
        in most of the cases.
    slic_zero: bool, optional
        Run SLIC-zero, the zero-parameter mode of SLIC. [2]_

    Returns
    -------
    labels : 2D or 3D array
        Integer mask indicating segment labels.

    Raises
    ------
    ValueError
        If ``convert2lab`` is set to ``True`` but the last array
        dimension is not of length 3.

    Notes
    -----
    * If `sigma > 0`, the image is smoothed using a Gaussian kernel prior to
      segmentation.

    * If `sigma` is scalar and `spacing` is provided, the kernel width is
      divided along each dimension by the spacing. For example, if ``sigma=1``
      and ``spacing=[5, 1, 1]``, the effective `sigma` is ``[0.2, 1, 1]``. This
      ensures sensible smoothing for anisotropic images.

    * The image is rescaled to be in [0, 1] prior to processing.

    * Images of shape (M, N, 3) are interpreted as 2D RGB images by default. To
      interpret them as 3D with the last dimension having length 3, use
      `multichannel=False`.

    References
    ----------
    .. [1] Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi,
        Pascal Fua, and Sabine SĂźsstrunk, SLIC Superpixels Compared to
        State-of-the-art Superpixel Methods, TPAMI, May 2012.
    .. [2] http://ivrg.epfl.ch/research/superpixels#SLICO

    Examples
    --------
    >>> from skimage.segmentation import slic
    >>> from skimage.data import astronaut
    >>> img = astronaut()
    >>> segments = slic(img, n_segments=100, compactness=10)

    Increasing the compactness parameter yields more square regions:

    >>> segments = slic(img, n_segments=100, compactness=20)

    i   .i   t   dtypei    i˙˙˙˙s/   Lab colorspace conversion requires a RGB image.Ni   t   axisg      đ?("   R   t   Falset   ndimt   npt   newaxist   Truet   Nonet   onest
   isinstancet   listt   tuplet   arrayt   doublet   collt   Iterablet   astypet   anyt   ndit   gaussian_filtert   shapet
   ValueErrorR   t   mgridR   t   intt   stept   zerost   concatenatet   reshapet   ascontiguousarrayt   floatt   maxR   R   (#   t   imaget
   n_segmentst   compactnesst   max_itert   sigmat   spacingt   multichannelt   convert2labt   enforce_connectivityt   min_size_factort   max_size_factort	   slic_zerot   is_2dt   deptht   heightt   widtht   grid_zt   grid_yt   grid_xt   slicest   st   step_zt   step_yt   step_xt
   segments_zt
   segments_yt
   segments_xt   segments_colort   segmentsR   t   ratiot   labelst   segment_sizet   min_sizet   max_size(    (    sD   lib/python2.7/site-packages/skimage/segmentation/slic_superpixels.pyt   slic   sp    d		!1=


 	 
(   t
   __future__R    t   collectionsR   t   numpyR   t   scipyR   R   t   utilR   R   t   segmentation._slicR   R   t   colorR   R   R   R	   RH   (    (    (    sD   lib/python2.7/site-packages/skimage/segmentation/slic_superpixels.pyt   <module>   s   		