
    1-Ph                     H    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
dS )    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      X/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/feature/template.py_window_sum_2dr   	   s    5q)))JLOb01J?U,q/AQTUAU?U4VVJ:A...J111l1o**+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      s    |44J:A...J111aaaa2--.
QQQ1l1o-111
2	3 
 r   Fconstantc           	         t          | d           | j        |j        k     rt          d          t          j        t          j        | j        |j                            rt          d          | j        }t          | j                  }| 	                    |d          } t          d |j        D                       }|dk    rt          j        | |||          } nt          j        | ||	          } | j        d
k    r.t          | |j                  }t          | d
z  |j                  }	n8| j        dk    r-t          | |j                  }t          | d
z  |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         }n>| j        dk    r3t%          | |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          j        |          j        k    }||         ||         z  ||<   g }t5          |j                  D ]r}|r|j        |         dz
  d
z  }|||         z   }n,|j        |         dz
  }|||         z   |j        |         z
  dz   }|                    t9          ||                     s|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V  	d S )N ).0widths     r   	<genexpr>z!match_template.<locals>.<genexpr>   s&      AAuenAAAAAAr   r   )	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zHM!!4
 
 	
 
vbgek8>2233 @>???+K'44KLL5L11EAA(.AAAAAIzYT?
 
 
 u	=== zQ)%@@*5!8X^DD	q)%@@*5!8X^DDMMOOMi//O68m39::LzQE8DDbD$$B$J#7gFFFqtQrTzR	qE8DDbD$$B$",<#=GLLLbD!B$"
 (=88I#KK "28HIIIII5EFFFF##K<KJ{A;////GK[))))}U+666H +..22Dt_{4'88HTNF8=!! % % 	=.#a'A-Bk!n$BB"Q&Bk!n$x~a'881<BeBmm$$$$E&MM""r   )Fr   r   )r/   numpyr   scipy.signalr   _shared.utilsr   r   r   r   rN   r   r   r   <module>rR      s         $ $ $ $ $ $ ; ; ; ; ; ; ; ;	 	 		 	 	 HIY# Y# Y# Y# Y# Y#r   