U
    ?h                     @   sJ   d dl Z d dlZd dlmZ ddlmZmZ dd Zdd Z	dddZ
dS )    N)fftconvolve   )check_nD_supported_float_typec                 C   s|   t j| dd}||d d |d |d  d   }t j|dd}|d d |d df |d d d |d  d f  }|S )Nr   Zaxis   )npcumsumimageZwindow_shapeZ
window_sum r   J/var/www/html/venv/lib/python3.8/site-packages/skimage/feature/template.py_window_sum_2d	   s    r   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   r   r   r   )r   r	   r
   r   r   r   r   _window_sum_3d   s    
"r   Fconstantc                 C   s  t | d | j|jk rtdtt| j|jr<td| j}t| j}| j	|dd} t
dd |jD }|dkrtj| |||d	} ntj| ||d
} | jdkrt| |j}t| d |j}	n&| jdkrt| |j}t| d |j}	| }
t|j}t||
 d }| jdkrNt| |ddddddf ddddddf }nL| jdkrt| |dddddddddf ddddddddf }|||
  }|	}tj|||d tj|||d ||8 }||9 }tj|d|d tj||d tj||d}|t|jk}|| ||  ||< g }t|jD ]d}|rb|j| d d }|||  }n(|j| d }|||  |j|  d }|t|| q8|t
| S )aK  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]])
    )r      zUDimensionality of template must be less than or equal to the dimensionality of image.z#Image must be larger than template.F)copyc                 s   s   | ]}||fV  qd S )Nr   ).0widthr   r   r   	<genexpr>   s     z!match_template.<locals>.<genexpr>r   )	pad_widthmodeconstant_values)r   r   r   r   Nr   Zvalid)r   r   )outr   )dtype)r   ndim
ValueErrorr	   anylessshaper   r   Zastypetuplepadr   r   Zmeanmathprodsumr   multiplydividemaximumsqrtZ
zeros_likeZfinfoepsrangeappendslice)r   templateZ	pad_inputr   r   Zimage_shapeZfloat_dtyper   Zimage_window_sumZimage_window_sum2Ztemplate_meanZtemplate_volumeZtemplate_ssdZxcorr	numeratordenominatorresponsemaskZslicesiZd0Zd1r   r   r   match_template!   s    R




  
"   r4   )Fr   r   )r#   numpyr	   Zscipy.signalr   Z_shared.utilsr   r   r   r   r4   r   r   r   r   <module>   s     