Ë
    ÿ[;je  ã                   ó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      úaG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\skimage/feature/template.pyÚ_window_sum_2dr   	   s‘   € Ü—‘˜5 qÔ)€JØ˜L¨™O¨bÐ1°JÐ?UÀ,ÈqÁ/ÐAQÐTUÑAUÐ4VÑV€Jä—‘˜:¨AÔ.€Jà’1�l 1‘o¨Ð*Ð*Ñ+¨jºÐ<R¸|ÈA¹Ð>NÐQRÑ>RÐ<RÐ9RÑ.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   € Ü  |Ó4€Jä—‘˜:¨AÔ.€Jà’1’a˜ a™¨2Ð-Ð-Ñ.Ø
’QšÐ1˜l¨1™oÐ-°Ñ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±.¨�u˜e”n±.ù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_templaterN   !   s7  € ôf ˆU�FÔà‡z�z�H—M‘MÒ!Üð4ó
ð 	
ô 
‡v�vŒb�g‰g�e—k‘k 8§>¡>Ó2Ô3ÜÐ>Ó?Ð?à—+‘+€Kä'¨¯©Ó4€KØ�L‰L˜¨5ˆLÓ1€EäÑA°(·.².ÓAÓA€IØˆzÒÜ—‘Ø˜Y¨TÀ?ô
‰ô —‘�u¨	¸Ô=ˆð ‡z�z�Q‚Ü)¨%°·±Ó@ÐÜ*¨5°!©8°X·^±^ÓDÑØ	�‰�qŠÜ)¨%°·±Ó@ÐÜ*¨5°!©8°X·^±^ÓDÐà—M‘M“O€MÜ—i‘i §¡Ó/€OÜ—6‘6˜8 mÑ3¸Ñ9Ó:€Là‡z�z�Q‚Ü˜E 8©D¨b¨D±$°B°$¨JÑ#7¸gÔFÀqÈÀtÈQÈrÈTÀzÑR‰Ø	�‰�qŠÜ˜E 8©D¨b¨D±$°B°$¹¸"¸Ð,<Ñ#=ÀGÔLØˆbˆD�!�B�$˜˜"˜Ðñ
ˆð Ð(¨=Ñ8Ñ8€Ià#€KÜ‡K�KÐ Ð"2Ð8HÕIÜ‡I�IÐ Ð5EÕFØÐ#Ñ#€KØ�<Ñ€KÜ‡J�Jˆ{˜A ;Õ/Ü‡G�GˆK˜[Õ)ä�}‰}˜U¨+Ô6€Hð œŸ™ +Ó.×2Ñ2Ñ2€Dà˜t‘_ {°4Ñ'8Ñ8€HˆT�Nà€FÜ�8—=‘=Ö!ˆÙØ—.‘. Ñ# aÑ'¨AÑ-ˆBØ�k !‘nÑ$‰Bà—‘ Ñ" QÑ&ˆBØ�k !‘nÑ$ x§~¡~°aÑ'8Ñ8¸1Ñ<ˆBØ�‰”e˜B “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   