ó
 ‰\c           @   s¶   d  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 d d l m Z d d g Z d „  Z e d d „ Z d d „ Z d S(   s   Convex Hull.iÿÿÿÿ(   t   productN(   t
   ConvexHulli   (   t   grid_points_in_polyi   (   t   possible_hull(   t   label(   t   unique_rows(   t   warnt   convex_hull_imaget   convex_hull_objectc         C   s_   t  j d |  |  f ƒ } x? t t t |  ƒ d ƒ ƒ D]" \ } \ } } | | | | f <q5 W| S(   Ni   g      à¿g      à?(   g      à¿g      à?(   t   npt   zerost	   enumerateR    t   range(   t   ndimt   offsetst   vertext   axist   offset(    (    s=   lib/python2.7/site-packages/skimage/morphology/convex_hull.pyt   _offsets_diamond   s    .g»½×Ùß|Û=c         C   sß  |  j  } t j |  ƒ d k rD t d t ƒ t j |  j d t j ƒS| d k rk t |  j	 t j
 ƒ ƒ } n= t j t j |  ƒ ƒ } | r¨ t | ƒ } | j | j } n  | rò t |  j  ƒ } | d d … t j d d … f | j d | ƒ } n  t | ƒ } t | ƒ } | j | j } | d k r;t |  j | ƒ }	 n  t j t j t t t |  j ƒ ƒ | d f ƒ }
 t j | j d d … d | … f j |
 ƒ | j d d … | d … f | k  d d ƒ} t j | |  j ƒ }	 |	 S(   s.  Compute the convex hull image of a binary image.

    The convex hull is the set of pixels included in the smallest convex
    polygon that surround all white pixels in the input image.

    Parameters
    ----------
    image : array
        Binary input image. This array is cast to bool before processing.
    offset_coordinates : bool, optional
        If ``True``, a pixel at coordinate, e.g., (4, 7) will be represented
        by coordinates (3.5, 7), (4.5, 7), (4, 6.5), and (4, 7.5). This adds
        some "extent" to a pixel when computing the hull.
    tolerance : float, optional
        Tolerance when determining whether a point is inside the hull. Due
        to numerical floating point errors, a tolerance of 0 can result in
        some points erroneously being classified as being outside the hull.

    Returns
    -------
    hull : (M, N) array of bool
        Binary image with pixels in convex hull set to True.

    References
    ----------
    .. [1] http://blogs.mathworks.com/steve/2011/10/04/binary-image-convex-hull-algorithm-notes/

    i    sI   Input image is entirely zero, no valid convex hull. Returning empty imaget   dtypei   NiÿÿÿÿR   (   R   R	   t   count_nonzeroR   t   UserWarningR
   t   shapet   bool_R   t   astypet   uint8t	   transposet   nonzeroR   t   pointst   verticesR   t   newaxist   reshapeR   R   t   mgridt   tuplet   mapt   slicet   allt	   equationst   dot(   t   imaget   offset_coordinatest	   toleranceR   t   coordst   hull0R   t   hullR   t   maskt
   gridcoordst   coords_in_hull(    (    s=   lib/python2.7/site-packages/skimage/morphology/convex_hull.pyR      s2    	5"+/i   c         C   sÖ   |  j  d k r t d ƒ ‚ n  | d k rE | d k rE t d ƒ ‚ n  t |  | d d ƒ} t j |  j d t ƒ} t j |  j d t ƒ} xE t d	 | j ƒ  d	 ƒ D]* } t	 | | k ƒ } t j
 | | ƒ } q¤ W| S(
   sS  Compute the convex hull image of individual objects in a binary image.

    The convex hull is the set of pixels included in the smallest convex
    polygon that surround all white pixels in the input image.

    Parameters
    ----------
    image : (M, N) array
        Binary input image.
    neighbors : {4, 8}, int
        Whether to use 4- or 8-connectivity.

    Returns
    -------
    hull : ndarray of bool
        Binary image with pixels in convex hull set to True.

    Notes
    -----
    This function uses skimage.morphology.label to define unique objects,
    finds the convex hull of each using convex_hull_image, and combines
    these regions with logical OR. Be aware the convex hulls of unconnected
    objects may overlap in the result. If this is suspected, consider using
    convex_hull_image separately on each object.

    i   s   Input must be a 2D imagei   i   s    Neighbors must be either 4 or 8.t
   backgroundi    R   i   (   R   t
   ValueErrorR   R	   R
   R   t   boolR   t   maxR   t
   logical_or(   R'   t	   neighborst
   labeled_imt
   convex_objt
   convex_imgt   i(    (    s=   lib/python2.7/site-packages/skimage/morphology/convex_hull.pyR   `   s     (   t   __doc__t	   itertoolsR    t   numpyR	   t   scipy.spatialR   t   measure.pnpolyR   t   _convex_hullR   t   measure._labelR   t   utilR   t   _shared.utilsR   t   __all__R   t   TrueR   R   (    (    (    s=   lib/python2.7/site-packages/skimage/morphology/convex_hull.pyt   <module>   s   	K