ó
 ‰\c           @   s*  d  d l  m Z d  d l Z d  d l m Z m Z d  d l Z d  d l m Z 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 l m Z d „  Z d „  Z d „  Z d „  Z d d d d d d „ Z d d d d d e d „ Z d d d d d e d „ Z d S(   iÿÿÿÿ(   t   divisionN(   t   gaussian_filtert   gaussian_laplace(   t   sqrtt   log(   t   spatiali   (   t   img_as_floati   (   t   peak_local_max(   t   _hessian_matrix_det(   t   integral_image(   t	   assert_nDc         C   s#  |  d | d | d d |  | } t  j | d d ƒ } t j | ƒ } |  d | d | d d |  | } t  j | d d ƒ } t j | ƒ } |  | | } |  | | } |  | | }	 |  | | }  | d | | d | d t t | | |	 |  ƒ ƒ }
 |
 t j t | | ƒ d S(   sŽ  
    Compute surface overlap between two disks of radii ``r1`` and ``r2``,
    with centers separated by a distance ``d``.

    Parameters
    ----------
    d : float
        Distance between centers.
    r1 : float
        Radius of the first disk.
    r2 : float
        Radius of the second disk.

    Returns
    -------
    vol: float
        Volume of the overlap between the two disks.
    i   iÿÿÿÿi   g      à?(   t   npt   clipt   matht   acosR   t   abst   pit   min(   t   dt   r1t   r2t   ratio1t   acos1t   ratio2t   acos2t   at   bt   ct   area(    (    s3   lib/python2.7/site-packages/skimage/feature/blob.pyt   _compute_disk_overlap   s    &&#c         C   sy   t  j d |  | | |  d |  d d |  | | d | d | d d | | } | d t  j t | | ƒ d S(   s  
    Compute volume overlap between two spheres of radii ``r1`` and ``r2``,
    with centers separated by a distance ``d``.

    Parameters
    ----------
    d : float
        Distance between centers.
    r1 : float
        Radius of the first sphere.
    r2 : float
        Radius of the second sphere.

    Returns
    -------
    vol: float
        Volume of the overlap between the two spheres.

    Notes
    -----
    See for example http://mathworld.wolfram.com/Sphere-SphereIntersection.html
    for more details.
    i   i   i   i   g      @gUUUUUUõ?(   R   R   R   (   R   R   R   t   vol(    (    s3   lib/python2.7/site-packages/skimage/feature/blob.pyt   _compute_sphere_overlap8   s    ;c         C   s»   t  |  ƒ d } t | ƒ } |  d | } | d | } t t j |  d  | d  d ƒ ƒ } | | | k rq d S| t | | ƒ k r‹ d S| d k r§ t | | | ƒ St | | | ƒ Sd S(   s3  Finds the overlapping area fraction between two blobs.

    Returns a float representing fraction of overlapped area.

    Parameters
    ----------
    blob1 : sequence of arrays
        A sequence of ``(row, col, sigma)`` or ``(pln, row, col, sigma)``,
        where ``row, col`` (or ``(pln, row, col)``) are coordinates
        of blob and ``sigma`` is the standard deviation of the Gaussian kernel
        which detected the blob.
    blob2 : sequence of arrays
        A sequence of ``(row, col, sigma)`` or ``(pln, row, col, sigma)``,
        where ``row, col`` (or ``(pln, row, col)``) are coordinates
        of blob and ``sigma`` is the standard deviation of the Gaussian kernel
        which detected the blob.

    Returns
    -------
    f : float
        Fraction of overlapped area (or volume in 3D).
    i   iÿÿÿÿi   i    N(   t   lenR   R   t   sumR   R   R   (   t   blob1t   blob2t   n_dimt	   root_ndimR   R   R   (    (    s3   lib/python2.7/site-packages/skimage/feature/blob.pyt   _blob_overlapU   s    %c         C   s3  |  d d … d f j  ƒ  } d | t |  j d d ƒ } t j |  d d … d d … f ƒ } t j t | j | ƒ ƒ ƒ } t	 | ƒ d k r” |  Sxl | D]d \ } } |  | |  | } }	 t
 | |	 ƒ | k r› | d |	 d k rò d |	 d <qÿ d | d <q› q› Wt j g  |  D] }
 |
 d d k r|
 ^ qƒ S(   sÒ  Eliminated blobs with area overlap.

    Parameters
    ----------
    blobs_array : ndarray
        A 2d array with each row representing 3 (or 4) values,
        ``(row, col, sigma)`` or ``(pln, row, col, sigma)`` in 3D,
        where ``(row, col)`` (``(pln, row, col)``) are coordinates of the blob
        and ``sigma`` is the standard deviation of the Gaussian kernel which
        detected the blob.
        This array must not have a dimension of size 0.
    overlap : float
        A value between 0 and 1. If the fraction of area overlapping for 2
        blobs is greater than `overlap` the smaller blob is eliminated.

    Returns
    -------
    A : ndarray
        `array` with overlapping blobs removed.
    Niÿÿÿÿi   i   i    (   t   maxR   t   shapeR   t   cKDTreeR   t   arrayt   listt   query_pairsR    R&   (   t   blobs_arrayt   overlapt   sigmat   distancet   treet   pairst   it   jR"   R#   R   (    (    s3   lib/python2.7/site-packages/skimage/feature/blob.pyt   _prune_blobs‚   s    %i2   gš™™™™™ù?g       @g      à?c      
   C   sn  t  |  ƒ }  t t t | ƒ | | ƒ ƒ d } t j g  t | d ƒ D] } | | | ^ qF ƒ } g  | D] }	 t |  |	 ƒ ^ qj }
 g  t | ƒ D]$ } |
 | |
 | d | | ^ q’ } t j | d d ƒ} t	 | d | d t j
 d |  j d ƒ d d d	 t ƒ} | j d
 k r%t j d ƒ S| j t j ƒ } | | d d … d f | d d … d f <t | | ƒ S(   sÕ  Finds blobs in the given grayscale image.

    Blobs are found using the Difference of Gaussian (DoG) method [1]_.
    For each blob found, the method returns its coordinates and the standard
    deviation of the Gaussian kernel that detected the blob.

    Parameters
    ----------
    image : 2D or 3D ndarray
        Input grayscale image, blobs are assumed to be light on dark
        background (white on black).
    min_sigma : float, optional
        The minimum standard deviation for Gaussian Kernel. Keep this low to
        detect smaller blobs.
    max_sigma : float, optional
        The maximum standard deviation for Gaussian Kernel. Keep this high to
        detect larger blobs.
    sigma_ratio : float, optional
        The ratio between the standard deviation of Gaussian Kernels used for
        computing the Difference of Gaussians
    threshold : float, optional.
        The absolute lower bound for scale space maxima. Local maxima smaller
        than thresh are ignored. Reduce this to detect blobs with less
        intensities.
    overlap : float, optional
        A value between 0 and 1. If the area of two blobs overlaps by a
        fraction greater than `threshold`, the smaller blob is eliminated.

    Returns
    -------
    A : (n, image.ndim + 1) ndarray
        A 2d array with each row representing 3 values for a 2D image,
        and 4 values for a 3D image: ``(r, c, sigma)`` or ``(p, r, c, sigma)``
        where ``(r, c)`` or ``(p, r, c)`` are coordinates of the blob and
        ``sigma`` is the standard deviation of the Gaussian kernel which
        detected the blob.

    References
    ----------
    .. [1] http://en.wikipedia.org/wiki/Blob_detection#The_difference_of_Gaussians_approach

    Examples
    --------
    >>> from skimage import data, feature
    >>> feature.blob_dog(data.coins(), threshold=.5, max_sigma=40)
    array([[ 267.      ,  359.      ,   16.777216],
           [ 267.      ,  115.      ,   10.48576 ],
           [ 263.      ,  302.      ,   16.777216],
           [ 263.      ,  245.      ,   16.777216],
           [ 261.      ,  173.      ,   16.777216],
           [ 260.      ,   46.      ,   16.777216],
           [ 198.      ,  155.      ,   10.48576 ],
           [ 196.      ,   43.      ,   10.48576 ],
           [ 195.      ,  102.      ,   16.777216],
           [ 194.      ,  277.      ,   16.777216],
           [ 193.      ,  213.      ,   16.777216],
           [ 185.      ,  347.      ,   16.777216],
           [ 128.      ,  154.      ,   10.48576 ],
           [ 127.      ,  102.      ,   10.48576 ],
           [ 125.      ,  208.      ,   10.48576 ],
           [ 125.      ,   45.      ,   16.777216],
           [ 124.      ,  337.      ,   10.48576 ],
           [ 120.      ,  272.      ,   16.777216],
           [  58.      ,  100.      ,   10.48576 ],
           [  54.      ,  276.      ,   10.48576 ],
           [  54.      ,   42.      ,   16.777216],
           [  52.      ,  216.      ,   16.777216],
           [  52.      ,  155.      ,   16.777216],
           [  45.      ,  336.      ,   16.777216]])

    Notes
    -----
    The radius of each blob is approximately :math:`\sqrt{2}\sigma` for
    a 2-D image and :math:`\sqrt{3}\sigma` for a 3-D image.
    i   t   axisiÿÿÿÿt   threshold_abst	   footprinti   t   threshold_relg        t   exclude_borderi    N(   i   (   i    i   (   R   t   intR   t   floatR   R*   t   rangeR   t   stackR   t   onest   ndimt   Falset   sizet   emptyt   astypet   float64R5   (   t   imaget	   min_sigmat	   max_sigmat   sigma_ratiot	   thresholdR.   t   kR3   t
   sigma_listt   st   gaussian_imagest
   dog_imagest
   image_cubet   local_maximat   lm(    (    s3   lib/python2.7/site-packages/skimage/feature/blob.pyt   blob_dog©   s"    M#	+"4	*i
   gš™™™™™É?c      
   C   s;  t  |  ƒ }  | rI t | d ƒ t | d ƒ } } t j | | | ƒ }	 n t j | | | ƒ }	 g  |	 D] }
 t |  |
 ƒ |
 d ^ qe } t j | d d ƒ} t | d | d t j d |  j	 d ƒ d	 d
 d t
 ƒ} | j d k rò t j d ƒ S| j t j ƒ } |	 | d d … d f | d d … d f <t | | ƒ S(   sÙ  Finds blobs in the given grayscale image.

    Blobs are found using the Laplacian of Gaussian (LoG) method [1]_.
    For each blob found, the method returns its coordinates and the standard
    deviation of the Gaussian kernel that detected the blob.

    Parameters
    ----------
    image : 2D or 3D ndarray
        Input grayscale image, blobs are assumed to be light on dark
        background (white on black).
    min_sigma : float, optional
        The minimum standard deviation for Gaussian Kernel. Keep this low to
        detect smaller blobs.
    max_sigma : float, optional
        The maximum standard deviation for Gaussian Kernel. Keep this high to
        detect larger blobs.
    num_sigma : int, optional
        The number of intermediate values of standard deviations to consider
        between `min_sigma` and `max_sigma`.
    threshold : float, optional.
        The absolute lower bound for scale space maxima. Local maxima smaller
        than thresh are ignored. Reduce this to detect blobs with less
        intensities.
    overlap : float, optional
        A value between 0 and 1. If the area of two blobs overlaps by a
        fraction greater than `threshold`, the smaller blob is eliminated.
    log_scale : bool, optional
        If set intermediate values of standard deviations are interpolated
        using a logarithmic scale to the base `10`. If not, linear
        interpolation is used.

    Returns
    -------
    A : (n, image.ndim + 1) ndarray
        A 2d array with each row representing 3 values for a 2D image,
        and 4 values for a 3D image: ``(r, c, sigma)`` or ``(p, r, c, sigma)``
        where ``(r, c)`` or ``(p, r, c)`` are coordinates of the blob and
        ``sigma`` is the standard deviation of the Gaussian kernel which
        detected the blob.

    References
    ----------
    .. [1] http://en.wikipedia.org/wiki/Blob_detection#The_Laplacian_of_Gaussian

    Examples
    --------
    >>> from skimage import data, feature, exposure
    >>> img = data.coins()
    >>> img = exposure.equalize_hist(img)  # improves detection
    >>> feature.blob_log(img, threshold = .3)
    array([[ 266.        ,  115.        ,   11.88888889],
           [ 263.        ,  302.        ,   17.33333333],
           [ 263.        ,  244.        ,   17.33333333],
           [ 260.        ,  174.        ,   17.33333333],
           [ 198.        ,  155.        ,   11.88888889],
           [ 198.        ,  103.        ,   11.88888889],
           [ 197.        ,   44.        ,   11.88888889],
           [ 194.        ,  276.        ,   17.33333333],
           [ 194.        ,  213.        ,   17.33333333],
           [ 185.        ,  344.        ,   17.33333333],
           [ 128.        ,  154.        ,   11.88888889],
           [ 127.        ,  102.        ,   11.88888889],
           [ 126.        ,  208.        ,   11.88888889],
           [ 126.        ,   46.        ,   11.88888889],
           [ 124.        ,  336.        ,   11.88888889],
           [ 121.        ,  272.        ,   17.33333333],
           [ 113.        ,  323.        ,    1.        ]])

    Notes
    -----
    The radius of each blob is approximately :math:`\sqrt{2}\sigma` for
    a 2-D image and :math:`\sqrt{3}\sigma` for a 3-D image.
    i
   i   R6   iÿÿÿÿR7   R8   i   i   R9   g        R:   i    N(   i   (   i    i   (   R   R   R   t   logspacet   linspaceR   R>   R   R?   R@   RA   RB   RC   RD   RE   R5   (   RF   RG   RH   t	   num_sigmaRJ   R.   t	   log_scalet   startt   stopRL   RM   t	   gl_imagesRP   RQ   RR   (    (    s3   lib/python2.7/site-packages/skimage/feature/blob.pyt   blob_log  s     L+	*i   g{®Gáz„?c      
   C   sA  t  |  d ƒ t |  ƒ }  t |  ƒ }  | rb t | d ƒ t | d ƒ } } t j | | | ƒ }	 n t j | | | ƒ }	 g  |	 D] }
 t |  |
 ƒ ^ q~ } t j | ƒ } t	 | d | d t j
 d | j ƒ d d d t ƒ} | j d	 k rø t j d ƒ S| j t j ƒ } |	 | d
 d
 … d f | d
 d
 … d f <t | | ƒ S(   sÜ  Finds blobs in the given grayscale image.

    Blobs are found using the Determinant of Hessian method [1]_. For each blob
    found, the method returns its coordinates and the standard deviation
    of the Gaussian Kernel used for the Hessian matrix whose determinant
    detected the blob. Determinant of Hessians is approximated using [2]_.

    Parameters
    ----------
    image : 2D ndarray
        Input grayscale image.Blobs can either be light on dark or vice versa.
    min_sigma : float, optional
        The minimum standard deviation for Gaussian Kernel used to compute
        Hessian matrix. Keep this low to detect smaller blobs.
    max_sigma : float, optional
        The maximum standard deviation for Gaussian Kernel used to compute
        Hessian matrix. Keep this high to detect larger blobs.
    num_sigma : int, optional
        The number of intermediate values of standard deviations to consider
        between `min_sigma` and `max_sigma`.
    threshold : float, optional.
        The absolute lower bound for scale space maxima. Local maxima smaller
        than thresh are ignored. Reduce this to detect less prominent blobs.
    overlap : float, optional
        A value between 0 and 1. If the area of two blobs overlaps by a
        fraction greater than `threshold`, the smaller blob is eliminated.
    log_scale : bool, optional
        If set intermediate values of standard deviations are interpolated
        using a logarithmic scale to the base `10`. If not, linear
        interpolation is used.

    Returns
    -------
    A : (n, 3) ndarray
        A 2d array with each row representing 3 values, ``(y,x,sigma)``
        where ``(y,x)`` are coordinates of the blob and ``sigma`` is the
        standard deviation of the Gaussian kernel of the Hessian Matrix whose
        determinant detected the blob.

    References
    ----------
    .. [1] http://en.wikipedia.org/wiki/Blob_detection#The_determinant_of_the_Hessian

    .. [2] Herbert Bay, Andreas Ess, Tinne Tuytelaars, Luc Van Gool,
           "SURF: Speeded Up Robust Features"
           ftp://ftp.vision.ee.ethz.ch/publications/articles/eth_biwi_00517.pdf

    Examples
    --------
    >>> from skimage import data, feature
    >>> img = data.coins()
    >>> feature.blob_doh(img)
    array([[ 270.        ,  363.        ,   30.        ],
           [ 265.        ,  113.        ,   23.55555556],
           [ 262.        ,  243.        ,   23.55555556],
           [ 260.        ,  173.        ,   30.        ],
           [ 197.        ,  153.        ,   20.33333333],
           [ 197.        ,   44.        ,   20.33333333],
           [ 195.        ,  100.        ,   23.55555556],
           [ 193.        ,  275.        ,   23.55555556],
           [ 192.        ,  212.        ,   23.55555556],
           [ 185.        ,  348.        ,   30.        ],
           [ 156.        ,  302.        ,   30.        ],
           [ 126.        ,  153.        ,   20.33333333],
           [ 126.        ,  101.        ,   20.33333333],
           [ 124.        ,  336.        ,   20.33333333],
           [ 123.        ,  205.        ,   20.33333333],
           [ 123.        ,   44.        ,   23.55555556],
           [ 121.        ,  271.        ,   30.        ]])

    Notes
    -----
    The radius of each blob is approximately `sigma`.
    Computation of Determinant of Hessians is independent of the standard
    deviation. Therefore detecting larger blobs won't take more time. In
    methods line :py:meth:`blob_dog` and :py:meth:`blob_log` the computation
    of Gaussians for larger `sigma` takes more time. The downside is that
    this method can't be used for detecting blobs of radius less than `3px`
    due to the box filters used in the approximation of Hessian Determinant.
    i   i
   R7   R8   i   R9   g        R:   i    Niÿÿÿÿ(   i   (   i    i   (   R
   R   R	   R   R   RT   RU   R   t   dstackR   R?   R@   RA   RB   RC   RD   RE   R5   (   RF   RG   RH   RV   RJ   R.   RW   RX   RY   RL   RM   t   hessian_imagesRP   RQ   RR   (    (    s3   lib/python2.7/site-packages/skimage/feature/blob.pyt   blob_doh€  s$    R"	*(   t
   __future__R    t   numpyR   t   scipy.ndimageR   R   R   R   R   t   scipyR   t   utilR   t   peakR   t   _hessian_det_appxR   t	   transformR	   t   _shared.utilsR
   R   R   R&   R5   RS   RA   R[   R^   (    (    (    s3   lib/python2.7/site-packages/skimage/feature/blob.pyt   <module>   s(   	%		-	'mh