ó
 ‰\c           @   sd   d  d l  m Z d  d l Z d  d l m Z d d l m Z d „  Z d „  Z	 e
 d d	 d
 „ Z d S(   iÿÿÿÿ(   t   divisionN(   t   fftconvolvei   (   t	   assert_nDc         C   s“   t  j |  d d ƒ} | | d d !| | d d  } t  j | d d ƒ} | d  d  … | d d … f | d  d  … d  | d d … f } | S(   Nt   axisi    iÿÿÿÿi   (   t   npt   cumsum(   t   imaget   window_shapet
   window_sum(    (    s7   lib/python2.7/site-packages/skimage/feature/template.pyt   _window_sum_2d   s    &c         C   s}   t  |  | ƒ } t j | d d ƒ} | d  d  … d  d  … | d d … f | d  d  … d  d  … d  | d d … f } | S(   NR   i   iÿÿÿÿi   (   R	   R   R   (   R   R   R   (    (    s7   lib/python2.7/site-packages/skimage/feature/template.pyt   _window_sum_3d   s
    &/t   constanti    c         C   s½  t  |  d ƒ |  j | j k  r. t d ƒ ‚ n  t j t j |  j | j ƒ ƒ r^ t d ƒ ‚ n  |  j } t j |  d t j d t	 ƒ}  t
 d „  | j Dƒ ƒ } | d k rÎ t j |  d	 | d
 | d | ƒ}  n t j |  d	 | d
 | ƒ}  |  j d k r#t |  | j ƒ } t |  d | j ƒ } n: |  j d k r]t |  | j ƒ } t |  d | j ƒ } n  | j ƒ  }	 t j | j ƒ }
 t j | |	 d ƒ } |  j d k rët |  | d d d … d d d … f d
 d ƒd d … d d … f } nn |  j d k rYt |  | d d d … d d d … d d d … f d
 d ƒd d … d d … d d … f } n  | | |	 } | } t j | | d | ƒt j | |
 d | ƒ| | 8} | | 9} t j | d d | ƒt j | d | ƒt j | d t j ƒ} | t j t j ƒ j k } | | | | | | <g  } x‡ t | j ƒ D]v } | rg| j | d d } | | | } n. | j | d } | | | | j | d } | j t | | ƒ ƒ q5W| t
 | ƒ S(   sQ  Match a template to a 2-D or 3-D image using normalized correlation.

    The output is an array with values between -1.0 and 1.0. The value at a
    given position corresponds to the correlation coefficient between the image
    and the template.

    For `pad_input=True` matches correspond to the center and otherwise to the
    top-left corner of the template. To find the best match you must search for
    peaks in the response (output) image.

    Parameters
    ----------
    image : (M, N[, D]) array
        2-D or 3-D input image.
    template : (m, n[, d]) array
        Template to locate. It must be `(m <= M, n <= N[, d <= D])`.
    pad_input : bool
        If True, pad `image` so that output is the same size as the image, and
        output values correspond to the template center. Otherwise, the output
        is an array with shape `(M - m + 1, N - n + 1)` for an `(M, N)` image
        and an `(m, n)` template, and matches correspond to origin
        (top-left corner) of the template.
    mode : see `numpy.pad`, optional
        Padding mode.
    constant_values : see `numpy.pad`, optional
        Constant values used in conjunction with ``mode='constant'``.

    Returns
    -------
    output : array
        Response image with correlation coefficients.

    Notes
    -----
    Details on the cross-correlation are presented in [1]_. This implementation
    uses FFT convolutions of the image and the template. Reference [2]_
    presents similar derivations but the approximation presented in this
    reference is not used in our implementation.

    References
    ----------
    .. [1] J. P. Lewis, "Fast Normalized Cross-Correlation", Industrial Light
           and Magic.
    .. [2] Briechle and Hanebeck, "Template Matching using Fast Normalized
           Cross Correlation", Proceedings of the SPIE (2001).
           DOI:10.1117/12.421129

    Examples
    --------
    >>> template = np.zeros((3, 3))
    >>> template[1, 1] = 1
    >>> template
    array([[ 0.,  0.,  0.],
           [ 0.,  1.,  0.],
           [ 0.,  0.,  0.]])
    >>> image = np.zeros((6, 6))
    >>> image[1, 1] = 1
    >>> image[4, 4] = -1
    >>> image
    array([[ 0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  1.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0., -1.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.]])
    >>> result = match_template(image, template)
    >>> np.round(result, 3)
    array([[ 1.   , -0.125,  0.   ,  0.   ],
           [-0.125, -0.125,  0.   ,  0.   ],
           [ 0.   ,  0.   ,  0.125,  0.125],
           [ 0.   ,  0.   ,  0.125, -1.   ]])
    >>> result = match_template(image, template, pad_input=True)
    >>> np.round(result, 3)
    array([[-0.125, -0.125, -0.125,  0.   ,  0.   ,  0.   ],
           [-0.125,  1.   , -0.125,  0.   ,  0.   ,  0.   ],
           [-0.125, -0.125, -0.125,  0.   ,  0.   ,  0.   ],
           [ 0.   ,  0.   ,  0.   ,  0.125,  0.125,  0.125],
           [ 0.   ,  0.   ,  0.   ,  0.125, -1.   ,  0.125],
           [ 0.   ,  0.   ,  0.   ,  0.125,  0.125,  0.125]])
    i   i   sU   Dimensionality of template must be less than or equal to the dimensionality of image.s#   Image must be larger than template.t   dtypet   copyc         s   s   |  ] } | | f Vq d  S(   N(    (   t   .0t   width(    (    s7   lib/python2.7/site-packages/skimage/feature/template.pys	   <genexpr>~   s    R   t	   pad_widtht   modet   constant_valuesNiÿÿÿÿt   validi   t   outi    (   i   i   (   R   t   ndimt
   ValueErrorR   t   anyt   lesst   shapet   arrayt   float64t   Falset   tuplet   padR	   R
   t   meant   prodt   sumR   t   multiplyt   dividet   maximumt   sqrtt
   zeros_liket   finfot   epst   ranget   appendt   slice(   R   t   templatet	   pad_inputR   R   t   image_shapeR   t   image_window_sumt   image_window_sum2t   template_meant   template_volumet   template_ssdt   xcorrt	   numeratort   denominatort   responset   maskt   slicest   it   d0t   d1(    (    s7   lib/python2.7/site-packages/skimage/feature/template.pyt   match_template    s^    R!	("4+

(   t
   __future__R    t   numpyR   t   scipy.signalR   t   _shared.utilsR   R	   R
   R   R=   (    (    (    s7   lib/python2.7/site-packages/skimage/feature/template.pyt   <module>   s   		