
    9i                     H    S SK r S SKrS SKJr  SSKJrJr  S rS r	 SS jr
g)	    N)fftconvolve   )check_nD_supported_float_typec                     [         R                  " U SS9nX!S   S US US   * S-
   -
  n[         R                  " USS9nUS S 2US   S24   US S 2S US   * S-
  24   -
  nU$ )Nr   axis   )npcumsumimagewindow_shape
window_sums      X/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/feature/template.py_window_sum_2dr   	   s    5q)JOb1J?U,q/AQTUAU4VVJ:A.J1l1o**+j<R|A>NQR>R<R9R.SS      c                     [        X5      n[        R                  " USS9nUS S 2S S 2US   S24   US S 2S S 2S US   * S-
  24   -
  nU$ )Nr   r   r
   r   )r   r   r   r   s      r   _window_sum_3dr      sg    4J:A.J1aa2--.
Q1l1o-111
2	3 
 r   c           	         [        U S5        U R                  UR                  :  a  [        S5      e[        R                  " [        R
                  " U R                  UR                  5      5      (       a  [        S5      eU R                  n[        U R                  5      nU R                  USS9n [        S UR                   5       5      nUS:X  a  [        R                  " XX4S9n O[        R                  " XUS	9n U R                  S
:X  a/  [        XR                  5      n[        U S
-  UR                  5      n	O>U R                  S:X  a.  [        XR                  5      n[        U S
-  UR                  5      n	UR                  5       n
[        R                   " UR                  5      n[        R"                  " X-
  S
-  5      nU R                  S
:X  a  [%        XSSS2SSS24   SS9SS2SS24   nO5U R                  S:X  a%  [%        XSSS2SSS2SSS24   SS9SS2SS2SS24   nWWU
-  -
  nW	n[        R&                  " XUS9  [        R(                  " XUS9  X-  nX-  n[        R*                  " USUS9  [        R,                  " XS9  [        R.                  " XS9nU[        R0                  " U5      R2                  :  nUU   UU   -  UU'   / n[5        UR                  5       Hp  nU(       a  UR                  U   S-
  S
-  nUUU   -   nO-UR                  U   S-
  nUUU   -   UR                  U   -
  S-   nUR7                  [9        UU5      5        Mr     U[        U5         $ )a+  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[, P]) array
    2-D or 3-D input image.
template : (m, n[, p]) array
    Template to locate. It must be `(m <= M, n <= N[, p <= P])`.
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              3   (   #    U  H  oU4v   M
     g 7f)N ).0widths     r   	<genexpr>!match_template.<locals>.<genexpr>   s     A.en.s   constant)	pad_widthmodeconstant_values)r!   r"   r   r   Nr
   valid)r"   r   )outr   )dtype)r   ndim
ValueErrorr   anylessshaper   r&   astypetuplepadr   r   meanmathprodsumr   multiplydividemaximumsqrt
zeros_likefinfoepsrangeappendslice)r   template	pad_inputr"   r#   image_shapefloat_dtyper!   image_window_sumimage_window_sum2template_meantemplate_volumetemplate_ssdxcorr	numeratordenominatorresponsemaskslicesid0d1s                         r   match_templaterO   !   s   f UFzzHMM!4
 	
 
vvbggekk8>>233>??++K'4KLL5L1EA(..AAIzT
 u= zzQ)%@*5!8X^^D	q)%@*5!8X^^DMMOMii/O66839:LzzQEDbD$B$J#7gFqtQrTzR	qEDbD$B$",<#=GLbD!B$"
 (=88I#KKK 8HIII5EF#KKJJ{A;/GGK)}}U6H +.222Dt_{4'88HTNF8==!..#a'A-Bk!n$B"Q&Bk!n$x~~a'881<BeBm$ " E&M""r   )Fr    r   )r0   numpyr   scipy.signalr   _shared.utilsr   r   r   r   rO   r   r   r   <module>rS      s(      $ ;		 HIY#r   