
    |h                     F    d dl Z d dlZd dlmZ ddlmZmZ d Zd Z		 ddZ
y)	    N)fftconvolve   )check_nD_supported_float_typec                     t        j                  | d      }||d   d |d |d    dz
   z
  }t        j                  |d      }|d d |d   df   |d d d |d    dz
  f   z
  }|S )Nr   axis   )npcumsumimagewindow_shape
window_sums      W/var/www/html/test/engine/venv/lib/python3.12/site-packages/skimage/feature/template.py_window_sum_2dr   	   s    5q)JLOb1J?U,q/AQTUAU4VVJ:A.J1l1o**+j<R|A>NQR>R<R9R.SS      c                     t        | |      }t        j                  |d      }|d d d d |d   df   |d d d d d |d    dz
  f   z
  }|S )Nr   r   r
   r   )r   r   r   r   s      r   _window_sum_3dr      si    |4J:A.J1aa2--.
Q1l1o-111
2	3 
 r   c           	         t        | d       | j                  |j                  k  rt        d      t        j                  t        j
                  | j                  |j                              rt        d      | j                  }t        | j                        }| j                  |d      } t        d |j                  D              }|dk(  rt        j                  | |||      } nt        j                  | ||	      } | j                  d
k(  r0t        | |j                        }t        | d
z  |j                        }	n>| j                  dk(  r/t        | |j                        }t        | d
z  |j                        }	|j                         }
t        j                   |j                        }t        j"                  ||
z
  d
z        }| j                  d
k(  r#t%        | |ddddddf   d      ddddf   }n8| j                  dk(  r)t%        | |dddddddddf   d      ddddddf   }|
z  z
  }	}t        j&                  |||       t        j(                  |||       ||z  }||z  }t        j*                  |d|       t        j,                  ||       t        j.                  ||      }|t        j0                  |      j2                  kD  }||   ||   z  ||<   g }t5        |j                        D ]j  }|r|j                  |   dz
  d
z  }|||   z   }n-|j                  |   dz
  }|||   z   |j                  |   z
  dz   }|j7                  t9        ||             l |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[, 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   $   K   | ]  }||f 
 y w)N ).0widths     r   	<genexpr>z!match_template.<locals>.<genexpr>   s     AuenAs   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_templaterN   !   s<   f UFzzHMM!4
 	
 
vvbggekk8>>23>??++K'4KLL5L1EA(..AAIzYT?
 u	= zzQ)%@*5!8X^^D	q)%@*5!8X^^DMMOMii/O668m39:LzzQE8DbD$B$J#7gFqtQrTzR	qE8DbD$B$",<#=GLbD!B$"
 (=88I#KKK "28H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   )r/   numpyr   scipy.signalr   _shared.utilsr   r   r   r   rN   r   r   r   <module>rR      s(      $ ;		 HIY#r   