
    9iT              	       T   S SK r S SKrS SKJrJr  S SKrS SKJr  SSK	J
r
  SSKJr  S rS rS	 r " S
 S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rS S jr " S S\5      r " S S\5      r " S S\5      r\\\\\\\\S.rS rS rg)!    N)ABCabstractmethod)spatial   )safe_as_int)NP_COPY_IF_NEEDEDc                 F   U R                   nS[        R                  " SSU-  -   5      -   S-  S-
  n[        [        R                  " U5      5      nX#:w  a  [        SU 35      e[        R                  " US-   5      n[        R                  " XUS-   45      USS2SS24'   U$ )z8Affine matrix from linearized (d, d + 1) matrix entries.      r   z2Invalid number of elements for linearized matrix: N)sizenpsqrtintround
ValueErroreyereshape)vnparamddimensionalitymatrixs        \/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/transform/_geometric.py_affine_matrix_from_vectorr      s    VVF	
RWWQV^$	$)A-A!%NCF8L
 	
 VVNQ&'FZZNQ4F#GHF3B36NM    c                    U R                   u  p[        R                  " U SS9nX-
  n[        R                  " [        R                  " US-  5      U-  5      nUS:X  aR  [        R
                  " US-   US-   4[        R                  5      [        R                  " U [        R                  5      4$ [        R                  " U5      U-  nU[        R                  " [        R                  " U5      USS2[        R                  4   * 4SS9-  n[        R                  " US/U-  S/-   /4SS9n[        R                  " U R                  [        R                  " U5      /5      n	X-  R                  n
U
SS2SU24   nXSS2US24   -  nX4$ )a  Center and normalize image points.

The points are transformed in a two-step procedure that is expressed
as a transformation matrix. The matrix of the resulting points is usually
better conditioned than the matrix of the original points.

Center the image points, such that the new coordinate system has its
origin at the centroid of the image points.

Normalize the image points, such that the mean distance from the points
to the origin of the coordinate system is sqrt(D).

If the points are all identical, the returned values will contain nan.

Parameters
----------
points : (N, D) array
    The coordinates of the image points.

Returns
-------
matrix : (D+1, D+1) array_like
    The transformation matrix to obtain the new points.
new_points : (N, D) array
    The transformed image points.

References
----------
.. [1] Hartley, Richard I. "In defense of the eight-point algorithm."
       Pattern Analysis and Machine Intelligence, IEEE Transactions on 19.6
       (1997): 580-593.

r   axisr   r
   N)shaper   meanr   sumfullnan	full_likeconcatenater   newaxisvstackTones)pointsnr   centroidcenteredrmsnorm_factorpart_matrixr   points_hnew_points_h
new_pointss               r   _center_and_normalize_pointsr5      sn   D <<DAwwvA&H H
''"&&1%)
*C axwwAq1u~rvv.VRVV0LLL''!*s"K	Xam,,-A! K ^^   #			
 F yy&((BGGAJ/0H%((La!e$Jq!"u%%Jr   c                    [         R                  " U 5      n [         R                  " U5      nU R                  S   nU R                  S   nU R                  SS9nUR                  SS9nX-
  nX-
  nUR                  U-  U-  n	[         R
                  " U4[         R                  S9n
[         R                  R                  U	5      S:  a  SXS-
  '   [         R                  " US-   [         R                  S9n[         R                  R                  U	5      u  pn[         R                  R                  U	5      nUS:X  a  [         R                  U-  $ XS-
  :X  a  [         R                  R                  U5      [         R                  R                  U5      -  S:  a  X-  USU2SU24'   O`XS-
     nSXS-
  '   U[         R                  " U
5      -  U-  USU2SU24'   UXS-
  '   O%U[         R                  " U
5      -  U-  USU2SU24'   U(       a&  SUR                  SS9R                  5       -  X-  -  nOSnUUUSU2SU24   UR                  -  -  -
  USU2U4'   USU2SU24==   U-  ss'   U$ )aR  Estimate N-D similarity transformation with or without scaling.

Parameters
----------
src : (M, N) array_like
    Source coordinates.
dst : (M, N) array_like
    Destination coordinates.
estimate_scale : bool
    Whether to estimate scaling factor.

Returns
-------
T : (N + 1, N + 1)
    The homogeneous similarity transformation matrix. The matrix contains
    NaN values only if the problem is not well-conditioned.

References
----------
.. [1] "Least-squares estimation of transformation parameters between two
        point patterns", Shinji Umeyama, PAMI 1991, :DOI:`10.1109/34.88573`

r   r
   r   dtyper   Ng      ?)r   asarrayr    r!   r)   r*   float64linalgdetr   svdmatrix_rankr$   diagvarr"   )srcdstestimate_scalenumdimsrc_meandst_mean
src_demean
dst_demeanAr   r)   USVranksscales                     r   _umeyamarQ   i   sD   0 **S/C
**S/C
))A,C
))A,C xxQxHxxQxH JJ 	z!C'A 	bjj)A	yy}}Q!'

sQwbjj)AiimmAGA! 99  #Dqyvvz	q99==biimmA..2EAdsdDSDjM'
AAAgJ
NQ.AdsdDSDjMAAgJBGGAJ*$3$*jnn!n,0022ae<eq#tt}xzz'ABBAdsdCiLdsdDSDjMUMHr   c                   H    \ rS rSrSr\S 5       r\\S 5       5       rS r	Sr
g)_GeometricTransform   z2Abstract base class for geometric transformations.c                     g)zApply forward transformation.

Parameters
----------
coords : (N, 2) array_like
    Source coordinates.

Returns
-------
coords : (N, 2) array
    Destination coordinates.

N selfcoordss     r   __call___GeometricTransform.__call__       r   c                     g)3Return a transform object representing the inverse.NrV   rX   s    r   inverse_GeometricTransform.inverse   r\   r   c                 l    [         R                  " [         R                  " U " U5      U-
  S-  SS95      $ )ak  Determine residuals of transformed destination coordinates.

For each transformed source coordinate the Euclidean distance to the
respective destination coordinate is determined.

Parameters
----------
src : (N, 2) array
    Source coordinates.
dst : (N, 2) array
    Destination coordinates.

Returns
-------
residuals : (N,) array
    Residual for coordinate.

r   r
   r   )r   r   r"   rX   rA   rB   s      r   	residuals_GeometricTransform.residuals   s+    & wwrvvtCy3141=>>r   rV   N)__name__
__module____qualname____firstlineno____doc__r   rZ   propertyr`   rd   __static_attributes__rV   r   r   rS   rS      s:    <  B  B?r   rS   c                   R    \ rS rSrSrSSS.S jjrS r\S 5       rS	 r	S
 r
S rSrg)FundamentalMatrixTransform   a7
  Fundamental matrix transformation.

The fundamental matrix relates corresponding points between a pair of
uncalibrated images. The matrix transforms homogeneous image points in one
image to epipolar lines in the other image.

The fundamental matrix is only defined for a pair of moving images. In the
case of pure rotation or planar scenes, the homography describes the
geometric relation between two images (`ProjectiveTransform`). If the
intrinsic calibration of the images is known, the essential matrix describes
the metric relation between the two images (`EssentialMatrixTransform`).

References
----------
.. [1] Hartley, Richard, and Andrew Zisserman. Multiple view geometry in
       computer vision. Cambridge university press, 2003.

Parameters
----------
matrix : (3, 3) array_like, optional
    Fundamental matrix.

Attributes
----------
params : (3, 3) array
    Fundamental matrix.

Examples
--------
>>> import numpy as np
>>> import skimage as ski
>>> tform_matrix = ski.transform.FundamentalMatrixTransform()

Define source and destination points:

>>> src = np.array([1.839035, 1.924743,
...                 0.543582, 0.375221,
...                 0.473240, 0.142522,
...                 0.964910, 0.598376,
...                 0.102388, 0.140092,
...                15.994343, 9.622164,
...                 0.285901, 0.430055,
...                 0.091150, 0.254594]).reshape(-1, 2)
>>> dst = np.array([1.002114, 1.129644,
...                 1.521742, 1.846002,
...                 1.084332, 0.275134,
...                 0.293328, 0.588992,
...                 0.839509, 0.087290,
...                 1.779735, 1.116857,
...                 0.878616, 0.602447,
...                 0.642616, 1.028681]).reshape(-1, 2)

Estimate the transformation matrix:

>>> tform_matrix.estimate(src, dst)
True
>>> tform_matrix.params
array([[-0.21785884,  0.41928191, -0.03430748],
       [-0.07179414,  0.04516432,  0.02160726],
       [ 0.24806211, -0.42947814,  0.02210191]])

Compute the Sampson distance:

>>> tform_matrix.residuals(src, dst)
array([0.0053886 , 0.00526101, 0.08689701, 0.01850534, 0.09418259,
       0.00185967, 0.06160489, 0.02655136])

Apply inverse transformation:

>>> tform_matrix.inverse(dst)
array([[-0.0513591 ,  0.04170974,  0.01213043],
       [-0.21599496,  0.29193419,  0.00978184],
       [-0.0079222 ,  0.03758889, -0.00915389],
       [ 0.14187184, -0.27988959,  0.02476507],
       [ 0.05890075, -0.07354481, -0.00481342],
       [-0.21985267,  0.36717464, -0.01482408],
       [ 0.01339569, -0.03388123,  0.00497605],
       [ 0.03420927, -0.1135812 ,  0.02228236]])

Nr   r   c                   Uc  [         R                  " US-   5      nOK[         R                  " U5      nUR                  S   S-
  nUR                  US-   US-   4:w  a  [	        S5      eXl        US:w  a  [        U R                   S35      eg )Nr
   r   &Invalid shape of transformation matrixr   zJ is only implemented for 2D coordinates (i.e. 3D transformation matrices).)r   r   r9   r    r   paramsNotImplementedError	__class__rX   r   r   s      r   __init__#FundamentalMatrixTransform.__init__6  s    >VVNQ./FZZ'F#\\!_q0N|| 2NQ4FGG !IJJQ%>>" #5 5  r   c                     [         R                  " U5      n[         R                  " U[         R                  " UR                  S   5      /5      nX R
                  R                  -  $ )zApply forward transformation.

Parameters
----------
coords : (N, 2) array_like
    Source coordinates.

Returns
-------
coords : (N, 3) array
    Epipolar lines in the destination image.

r   )r   r9   column_stackr*   r    rs   r)   )rX   rY   coords_homogeneouss      r   rZ   #FundamentalMatrixTransform.__call__F  sH     F#__fbggfll1o6N-OP!KKMM11r   c                 H    [        U 5      " U R                  R                  S9$ )zReturn a transform object representing the inverse.

See Hartley & Zisserman, Ch. 8: Epipolar Geometry and the Fundamental
Matrix, for an explanation of why F.T gives the inverse.
r   )typers   r)   r_   s    r   r`   "FundamentalMatrixTransform.inverseX  s     Dz//r   c                    [         R                  " U5      n[         R                  " U5      nUR                  UR                  :w  a  [        S5      eUR                  S   S:  a  [        S5      e [	        U5      u  p1[	        U5      u  pB[         R                  " UR                  S   S45      nXSS2SS	24'   USS2SS24==   USS2S[         R                  4   -  ss'   XSS2SS
24'   USS2SS24==   USS2S[         R                  4   -  ss'   XSS2SS24'   [         R                  R                  U5      u    pgUSSS24   R                  SS5      nXU4$ ! [
         aV    [         R                  " S[         R                  5      U l        S[         R                  " S[         R                  5      /-  s $ f = f)aT  Setup and solve the homogeneous epipolar constraint matrix::

    dst' * F * src = 0.

Parameters
----------
src : (N, 2) array_like
    Source coordinates.
dst : (N, 2) array_like
    Destination coordinates.

Returns
-------
F_normalized : (3, 3) array
    The normalized solution to the homogeneous system. If the system
    is not well-conditioned, this matrix contains NaNs.
src_matrix : (3, 3) array
    The transformation matrix to obtain the normalized source
    coordinates.
dst_matrix : (3, 3) array
    The transformation matrix to obtain the normalized destination
    coordinates.

z%src and dst shapes must be identical.r      z,src.shape[0] must be equal or larger than 8.   r   r   	   Nr         r
   r   )r   r9   r    r   r5   ZeroDivisionErrorr#   r$   rs   r*   r'   r;   r=   r   )	rX   rA   rB   
src_matrix
dst_matrixrJ   _rM   F_normalizeds	            r   _setup_constraint_matrix3FundamentalMatrixTransform._setup_constraint_matrixa  s   2 jjojjo99		!DEE99Q<!KLL	1:3?OJ:3?OJ GGSYYq\1%&!RaR%	!RaR%C1bjj())!QqS&		!QqS&	SArzz)**	!QqS&	 ))--"1Qx''1-33! ! 	1''&"&&1DK/000	1s   1E AF?>F?c                     U R                  X5      u  p4n[        R                  R                  U5      u  pgnSUS'   U[        R                  " U5      -  U-  n	UR
                  U	-  U-  U l        g)a  Estimate fundamental matrix using 8-point algorithm.

The 8-point algorithm requires at least 8 corresponding point pairs for
a well-conditioned solution, otherwise the over-determined solution is
estimated.

Parameters
----------
src : (N, 2) array_like
    Source coordinates.
dst : (N, 2) array_like
    Destination coordinates.

Returns
-------
success : bool
    True, if model estimation succeeds.

r   r   Tr   r   r;   r=   r?   r)   rs   )
rX   rA   rB   r   r   r   rK   rL   rM   Fs
             r   estimate#FundamentalMatrixTransform.estimate  sh    * 04/L/LS/V,* ))---a!
NQ llQ&3r   c                 <   [         R                  " U[         R                  " UR                  S   5      /5      n[         R                  " U[         R                  " UR                  S   5      /5      nU R                  UR
                  -  nU R                  R
                  UR
                  -  n[         R                  " XER
                  -  SS9n[         R                  " U5      [         R                  " US   S-  US   S-  -   US   S-  -   US   S-  -   5      -  $ )a  Compute the Sampson distance.

The Sampson distance is the first approximation to the geometric error.

Parameters
----------
src : (N, 2) array
    Source coordinates.
dst : (N, 2) array
    Destination coordinates.

Returns
-------
residuals : (N,) array
    Sampson distance.

r   r
   r   r   )	r   rz   r*   r    rs   r)   r"   absr   )rX   rA   rB   src_homogeneousdst_homogeneousF_srcFt_dst	dst_F_srcs           r   rd   $FundamentalMatrixTransform.residuals  s    $ //3		!0E*FG//3		!0E*FGo///!2!22FF?WW41=	vvi 277!HME!HM)F1IN:VAY!^K$
 
 	
r   rs   N)rf   rg   rh   ri   rj   rw   rZ   rk   r`   r   r   rd   rl   rV   r   r   rn   rn      s?    Oba  2$ 0 044lB
r   rn   c                   @   ^  \ rS rSrSr SSS.U 4S jjjrS rSrU =r$ )	EssentialMatrixTransformi  a	  Essential matrix transformation.

The essential matrix relates corresponding points between a pair of
calibrated images. The matrix transforms normalized, homogeneous image
points in one image to epipolar lines in the other image.

The essential matrix is only defined for a pair of moving images capturing a
non-planar scene. In the case of pure rotation or planar scenes, the
homography describes the geometric relation between two images
(`ProjectiveTransform`). If the intrinsic calibration of the images is
unknown, the fundamental matrix describes the projective relation between
the two images (`FundamentalMatrixTransform`).

References
----------
.. [1] Hartley, Richard, and Andrew Zisserman. Multiple view geometry in
       computer vision. Cambridge university press, 2003.

Parameters
----------
rotation : (3, 3) array_like, optional
    Rotation matrix of the relative camera motion.
translation : (3, 1) array_like, optional
    Translation vector of the relative camera motion. The vector must
    have unit length.
matrix : (3, 3) array_like, optional
    Essential matrix.

Attributes
----------
params : (3, 3) array
    Essential matrix.

Examples
--------
>>> import numpy as np
>>> import skimage as ski
>>>
>>> tform_matrix = ski.transform.EssentialMatrixTransform(
...     rotation=np.eye(3), translation=np.array([0, 0, 1])
... )
>>> tform_matrix.params
array([[ 0., -1.,  0.],
       [ 1.,  0.,  0.],
       [ 0.,  0.,  0.]])
>>> src = np.array([[ 1.839035, 1.924743],
...                 [ 0.543582, 0.375221],
...                 [ 0.47324 , 0.142522],
...                 [ 0.96491 , 0.598376],
...                 [ 0.102388, 0.140092],
...                 [15.994343, 9.622164],
...                 [ 0.285901, 0.430055],
...                 [ 0.09115 , 0.254594]])
>>> dst = np.array([[1.002114, 1.129644],
...                 [1.521742, 1.846002],
...                 [1.084332, 0.275134],
...                 [0.293328, 0.588992],
...                 [0.839509, 0.08729 ],
...                 [1.779735, 1.116857],
...                 [0.878616, 0.602447],
...                 [0.642616, 1.028681]])
>>> tform_matrix.estimate(src, dst)
True
>>> tform_matrix.residuals(src, dst)
array([0.42455187, 0.01460448, 0.13847034, 0.12140951, 0.27759346,
       0.32453118, 0.00210776, 0.26512283])

r   rp   c                8  > [         TU ]  X4S9  UGb2  [        R                  " U5      nUc  [	        S5      e[        R                  " U5      nUR
                  S:w  a  [	        S5      e[        [        R                  R                  U5      S-
  5      S:  a  [	        S5      eUR                  S:w  a  [	        S	5      e[        [        R                  R                  U5      S-
  5      S:  a  [	        S
5      e[        R                  " SUS   * US   US   SUS   * US   * US   S/	5      R                  SS5      nXQ-  U l        g Ub8  [        R                  " U5      nUR
                  S:w  a  [	        S5      eX0l        g [        R                  " S5      U l        g )N)r   r   z&Both rotation and translation requiredr   z Invalid shape of rotation matrixr
   gư>z*Rotation matrix must have unit determinantr   z#Invalid shape of translation vectorz(Translation vector must have unit lengthr   r   rr   )superrw   r   r9   r   r    r   r;   r<   r   normarrayr   rs   r   )rX   rotationtranslationr   r   t_xru   s         r   rw   !EssentialMatrixTransform.__init__  sz    	Fzz(+H" !IJJ**[1K~~' !CDD299==*Q./$6 !MNN1$ !FGG299>>+.23d: !KLL(( ^ONN ^O ^ON
 gam  .DKZZ'F||v% !IJJ K &&)DKr   c                    U R                  X5      u  p4n[        R                  R                  U5      u  pgnUS   US   -   S-  US'   US   US'   SUS'   U[        R                  " U5      -  U-  n	UR
                  U	-  U-  U l        g)a  Estimate essential matrix using 8-point algorithm.

The 8-point algorithm requires at least 8 corresponding point pairs for
a well-conditioned solution, otherwise the over-determined solution is
estimated.

Parameters
----------
src : (N, 2) array_like
    Source coordinates.
dst : (N, 2) array_like
    Destination coordinates.

Returns
-------
success : bool
    True, if model estimation succeeds.

r   r
   g       @r   Tr   )
rX   rA   rB   E_normalizedr   r   rK   rL   rM   Es
             r   r   !EssentialMatrixTransform.estimateF  s    * 04/L/LS/V,* ))---a!qts"!t!!
NQ llQ&3r   r   NNN)	rf   rg   rh   ri   rj   rw   r   rl   __classcell__)ru   s   @r   r   r     s,    CL 7;'$NO'$ '$R! !r   r   c                       \ rS rSrSrSSS.S jjr\S 5       rS rSS	 jr	S
 r
\S 5       rSS jrS rS rS rS r\S 5       rSrg)ProjectiveTransformij  ai  Projective transformation.

Apply a projective transformation (homography) on coordinates.

For each homogeneous coordinate :math:`\mathbf{x} = [x, y, 1]^T`, its
target position is calculated by multiplying with the given matrix,
:math:`H`, to give :math:`H \mathbf{x}`::

  [[a0 a1 a2]
   [b0 b1 b2]
   [c0 c1 1 ]].

E.g., to rotate by theta degrees clockwise, the matrix should be::

  [[cos(theta) -sin(theta) 0]
   [sin(theta)  cos(theta) 0]
   [0            0         1]]

or, to translate x by 10 and y by 20::

  [[1 0 10]
   [0 1 20]
   [0 0 1 ]].

Parameters
----------
matrix : (D+1, D+1) array_like, optional
    Homogeneous transformation matrix.
dimensionality : int, optional
    The number of dimensions of the transform. This is ignored if
    ``matrix`` is not None.

Attributes
----------
params : (D+1, D+1) array
    Homogeneous transformation matrix.

Nr   rp   c                   Uc  [         R                  " US-   5      nOK[         R                  " U5      nUR                  S   S-
  nUR                  US-   US-   4:w  a  [	        S5      eXl        [        UR                  S-
  5      U l        g )Nr
   r   z&invalid shape of transformation matrix)	r   r   r9   r    r   rs   ranger   _coeffsrv   s      r   rw   ProjectiveTransform.__init__  sz    >VVNQ./FZZ'F#\\!_q0N|| 2NQ4FGG !IJJV[[1_-r   c                 T    [         R                  R                  U R                  5      $ r   )r   r;   invrs   r_   s    r   _inv_matrixProjectiveTransform._inv_matrix  s    yy}}T[[))r   c                    UR                   S   S-
  n[        R                  " U[        SS9n[        R                  " U[        R
                  " UR                   S   S45      /SS9nXBR                  -  n[        R                  " [        5      R                  XUS S 2U4   S:H  U4'   US S 2S U24==   US S 2X3S-   24   -  ss'   US S 2S U24   $ )Nr   r
   r   )copyndminr   )
r    r   r   r   r&   r*   r)   finfofloateps)rX   rY   r   ndimrA   rB   s         r   
_apply_matProjectiveTransform._apply_mat  s    ||A"&'8Bnnfbggv||A.B&CD1MHHn
 (*xx':':4LAt#$AuuHQax/001ete8}r   c                 V    Uc  U R                   $ U R                   R                  U5      $ r   )rs   astype)rX   r8   r   s      r   	__array__ProjectiveTransform.__array__  s&    =;;;;%%e,,r   c                 8    U R                  XR                  5      $ )zApply forward transformation.

Parameters
----------
coords : (N, D) array_like
    Source coordinates.

Returns
-------
coords_out : (N, D) array
    Destination coordinates.

)r   rs   rW   s     r   rZ   ProjectiveTransform.__call__  s     v{{33r   c                 4    [        U 5      " U R                  S9$ )r^   r~   )r   r   r_   s    r   r`   ProjectiveTransform.inverse  s     Dz!1!122r   c           
      P   [         R                  " U5      n[         R                  " U5      nUR                  u  pE[        U5      u  pa[        U5      u  pr[         R                  " [         R
                  " Xg-   5      5      (       d3  [         R                  " US-   US-   4[         R                  5      U l        g[         R                  " XE-  US-   S-  45      n[        U5       H  n	XX-  U	S-   U-  2XS-   -  XS-   -  U-   24'   SXU-  U	S-   U-  2XS-   -  U-   4'   XX-  U	S-   U-  2U* S-
  S24'   SXU-  U	S-   U-  2S4'   XU-  U	S-   U-  2U* S-
  S24==   USS2XS-   24   * -  ss'   M     USS2[        U R                  5      S/-   4   nUc#  [         R                  R                  U5      u    pO[         R                  " U5      n[         R                  " [         R                   " [         R"                  " U[         R$                  " U5      -  5      U5      5      n[         R                  R                  X-  5      u    p[         R&                  " US   S5      (       a3  [         R                  " US-   US-   4[         R                  5      U l        g[         R                  " US-   US-   45      nUSSS24   * US   -  UR(                  [        U R                  5      S/-   '   SXU4'   [         R                  R+                  U5      U-  U-  nXS   -  nXl        g)	a}  Estimate the transformation from a set of corresponding points.

You can determine the over-, well- and under-determined parameters
with the total least-squares method.

Number of source and destination coordinates must match.

The transformation is defined as::

    X = (a0*x + a1*y + a2) / (c0*x + c1*y + 1)
    Y = (b0*x + b1*y + b2) / (c0*x + c1*y + 1)

These equations can be transformed to the following form::

    0 = a0*x + a1*y + a2 - c0*x*X - c1*y*X - X
    0 = b0*x + b1*y + b2 - c0*x*Y - c1*y*Y - Y

which exist for each set of corresponding points, so we have a set of
N * 2 equations. The coefficients appear linearly so we can write
A x = 0, where::

    A   = [[x y 1 0 0 0 -x*X -y*X -X]
           [0 0 0 x y 1 -x*Y -y*Y -Y]
            ...
            ...
          ]
    x.T = [a0 a1 a2 b0 b1 b2 c0 c1 c3]

In case of total least-squares the solution of this homogeneous system
of equations is the right singular vector of A which corresponds to the
smallest singular value normed by the coefficient c3.

Weights can be applied to each pair of corresponding points to
indicate, particularly in an overdetermined system, if point pairs have
higher or lower confidence or uncertainties associated with them. From
the matrix treatment of least squares problems, these weight values are
normalised, square-rooted, then built into a diagonal matrix, by which
A is multiplied.

In case of the affine transformation the coefficients c0 and c1 are 0.
Thus the system of equations is::

    A   = [[x y 1 0 0 0 -X]
           [0 0 0 x y 1 -Y]
            ...
            ...
          ]
    x.T = [a0 a1 a2 b0 b1 b2 c3]

Parameters
----------
src : (N, 2) array_like
    Source coordinates.
dst : (N, 2) array_like
    Destination coordinates.
weights : (N,) array_like, optional
    Relative weight values for each pair of points.

Returns
-------
success : bool
    True, if model estimation succeeds.

r
   Fr   r   Nr   r   r   T)r   r9   r    r5   allisfiniter#   r$   rs   zerosr   listr   r;   r=   r?   tiler   maxiscloseflatr   )rX   rA   rB   weightsr,   r   r   r   rJ   ddimr   rM   WHs                 r   r   ProjectiveTransform.estimate  s   B jjojjoyy6s;
6s;
vvbkk*"9:;;''1q5!a%."&&9DK HHaea!e\*+ !HDPSdh$(a'Q$a%.1:L)LLM?@AQh$(a'Q!);;<8;dh$(a'!a"45/1AQh$(a'+,Qh$(a'!a12s1dQh>O;O7P6PP2  adll#rd**+ ?iimmA&GAq!jj)G"&&/(A BAFGAiimmAE*GAq ::ai##''1q5!a%."&&9DKHHa!eQU^$ ./r3B3wZK!F),CtDLL!RD()Q$ IIMM*%)J6 	
vYr   c                     [        U[        5      (       aJ  [        U 5      [        U5      :X  a  U R                  nO[        nU" UR                  U R                  -  5      $ [        S5      e)z)Combine this transformation with another.z2Cannot combine transformations of differing types.)
isinstancer   r   ru   rs   	TypeError)rX   othertforms      r   __add__ProjectiveTransform.__add__I  sT    e011 DzT%[(+344STTr   c                 x    [         R                  " U R                  SS9nS[        R                  " US5      -   nU$ )z.common 'paramstr' used by __str__ and __repr__z, )	separatorzmatrix=
z    )r   array2stringrs   textwrapindent)rX   npstringparamstrs      r   __nice__ProjectiveTransform.__nice__V  s0    ??4;;$?6!BBr   c           
          U R                  5       nU R                  R                  nUnSU SU S[        [	        U 5      5       S3$ )z5Add standard repr formatting around a __nice__ string<(z) at >)r   ru   rf   hexidrX   r   	classnameclassstrs       r   __repr__ProjectiveTransform.__repr__\  sD    ==?NN++	8*AhZuSD]O1==r   c                 d    U R                  5       nU R                  R                  nUnSU SU S3$ )z4Add standard str formatting around a __nice__ stringr   r   z)>)r   ru   rf   r   s       r   __str__ProjectiveTransform.__str__c  s6    ==?NN++	8*AhZr**r   c                 :    U R                   R                  S   S-
  $ )z)The dimensionality of the transformation.r   r
   )rs   r    r_   s    r   r   "ProjectiveTransform.dimensionalityj  s     {{  #a''r   r   rs   r   )NN)rf   rg   rh   ri   rj   rw   rk   r   r   r   rZ   r`   r   r   r   r   r   r   rl   rV   r   r   r   r   j  s{    %N
.a 
. * * -4  3 3zxU>+ ( (r   r   c                   t    \ rS rSrSr     SSS.S jjr\S 5       r\S 5       r\S	 5       r	\S
 5       r
Srg)AffineTransformip  a  Affine transformation.

Has the following form::

    X = a0 * x + a1 * y + a2
      =   sx * x * [cos(rotation) + tan(shear_y) * sin(rotation)]
        - sy * y * [tan(shear_x) * cos(rotation) + sin(rotation)]
        + translation_x

    Y = b0 * x + b1 * y + b2
      =   sx * x * [sin(rotation) - tan(shear_y) * cos(rotation)]
        - sy * y * [tan(shear_x) * sin(rotation) - cos(rotation)]
        + translation_y

where ``sx`` and ``sy`` are scale factors in the x and y directions.

This is equivalent to applying the operations in the following order:

1. Scale
2. Shear
3. Rotate
4. Translate

The homogeneous transformation matrix is::

    [[a0  a1  a2]
     [b0  b1  b2]
     [0   0    1]]

In 2D, the transformation parameters can be given as the homogeneous
transformation matrix, above, or as the implicit parameters, scale,
rotation, shear, and translation in x (a2) and y (b2). For 3D and higher,
only the matrix form is allowed.

In narrower transforms, such as the Euclidean (only rotation and
translation) or Similarity (rotation, translation, and a global scale
factor) transforms, it is possible to specify 3D transforms using implicit
parameters also.

Parameters
----------
matrix : (D+1, D+1) array_like, optional
    Homogeneous transformation matrix. If this matrix is provided, it is an
    error to provide any of scale, rotation, shear, or translation.
scale : {s as float or (sx, sy) as array, list or tuple}, optional
    Scale factor(s). If a single value, it will be assigned to both
    sx and sy. Only available for 2D.

    .. versionadded:: 0.17
       Added support for supplying a single scalar value.
rotation : float, optional
    Rotation angle, clockwise, as radians. Only available for 2D.
shear : float or 2-tuple of float, optional
    The x and y shear angles, clockwise, by which these axes are
    rotated around the origin [2].
    If a single value is given, take that to be the x shear angle, with
    the y angle remaining 0. Only available in 2D.
translation : (tx, ty) as array, list or tuple, optional
    Translation parameters. Only available for 2D.
dimensionality : int, optional
    The dimensionality of the transform. This is not used if any other
    parameters are provided.

Attributes
----------
params : (D+1, D+1) array
    Homogeneous transformation matrix.

Raises
------
ValueError
    If both ``matrix`` and any of the other parameters are provided.

Examples
--------
>>> import numpy as np
>>> import skimage as ski
>>> img = ski.data.astronaut()

Define source and destination points:

>>> src = np.array([[150, 150],
...                 [250, 100],
...                 [150, 200]])
>>> dst = np.array([[200, 200],
...                 [300, 150],
...                 [150, 400]])

Estimate the transformation matrix:

>>> tform = ski.transform.AffineTransform()
>>> tform.estimate(src, dst)
True

Apply the transformation:

>>> warped = ski.transform.warp(img, inverse_map=tform.inverse)

References
----------
.. [1] Wikipedia, "Affine transformation",
       https://en.wikipedia.org/wiki/Affine_transformation#Image_transformation
.. [2] Wikipedia, "Shear mapping",
       https://en.wikipedia.org/wiki/Shear_mapping
Nr   rp   c                T   [        S X#XE4 5       5      n[        XfS-   -  5      U l        U(       a  Ub  [        S5      eU(       a  US:  a  [        S5      eUb  [        R
                  " U5      nUR                  S:w  d   UR                  S   UR                  S   :w  a  [        S5      eUR                  S   S-
  nXfS-   -  n[        U5      U l        Xl        g U(       Ga  Uc  SnUc  SnUc  SnUc  S	n[        R                  " U5      (       a  U=pOUu  p[        R                  " U5      (       a  USpOUu  pU	[        R                  " U5      [        R                  " U5      [        R                  " U5      -  -   -  nU
* [        R                  " U5      [        R                  " U5      -  [        R                  " U5      -   -  nUS   nU	[        R                  " U5      [        R                  " U5      [        R                  " U5      -  -
  -  nU
* [        R                  " U5      [        R                  " U5      -  [        R                  " U5      -
  -  nUS   n[        R                  " XU/UUU// S
Q/5      U l        g [        R                  " US-   5      U l        g )Nc              3   (   #    U  H  oS Lv   M
     g 7fr   rV   .0params     r   	<genexpr>+AffineTransform.__init__.<locals>.<genexpr>  s      
+P%+P   r
   ZYou cannot specify the transformation matrix and the implicit parameters at the same time.r   z(Parameter input is only supported in 2D.r   'Invalid shape of transformation matrix.r
   r
   r   r   r   r   r
   )anyr   r   r   r   r9   r   r    rs   isscalarmathcostansinr   r   )rX   r   rP   r   shearr   r   rs   r   sxsyshear_xshear_ya0a1a2b0b1b2s                      r   rw   AffineTransform.__init__  sN     
,1U+P
 

 ^/ABCf(=  nq(GHHZZ'F{{a6<<?fll1o#E !JKK!'a1!4'A+=> =DL K}}"${{5!!R{{5!!$)1#( txx)DHHW,=@R,RRSB)DHHX,>>(ASSTBQBtxx)DHHW,=@R,RRSB)DHHX,>>(ASSTBQB((RRL2r2,	#JKDK &&!!34DKr   c                    U R                   S:w  aC  [        R                  " [        R                  " U R                  S-  SS95      S U R                    $ [        R                  " U R                  S-  SS9nUS   [
        R                  " U R                  5      S-  S-   -  US'   [        R                  " U5      S U R                    $ )Nr   r   r   r
   )r   r   r   r"   rs   r
  r  r  )rX   sss     r   rP   AffineTransform.scale  s    !#77266$++q.q9:;PT=P=PQQQQ/BqETXXdjj1Q6:;BqE772;4!4!455r   c                     U R                   S:w  a  [        S5      e[        R                  " U R                  S   U R                  S   5      $ )Nr   z<The rotation property is only implemented for 2D transforms.r
   r   r  )r   rt   r
  atan2rs   r_   s    r   r   AffineTransform.rotation%  sE    !#%N  zz$++d+T[[->??r   c                     U R                   S:w  a  [        S5      e[        R                  " U R                  S   * U R                  S   5      nXR
                  -
  $ )Nr   z9The shear property is only implemented for 2D transforms.r   r
   r  )r   rt   r
  r  rs   r   )rX   betas     r   r  AffineTransform.shear-  sT    !#%K  zz4;;t,,dkk$.?@mm##r   c                 P    U R                   SU R                  2U R                  4   $ Nr   rs   r   r_   s    r   r   AffineTransform.translation6  '    {{1t222D4G4GGHHr   r   )NNNNN)rf   rg   rh   ri   rj   rw   rk   rP   r   r  r   rl   rV   r   r   r   r   p  s~    hX ?5 ?5B 6 6 @ @ $ $ I Ir   r   c                   :    \ rS rSrSrS rS rS r\S 5       r	Sr
g)	PiecewiseAffineTransformi;  a  Piecewise affine transformation.

Control points are used to define the mapping. The transform is based on
a Delaunay triangulation of the points to form a mesh. Each triangle is
used to find a local affine transform.

Attributes
----------
affines : list of AffineTransform objects
    Affine transformations for each triangle in the mesh.
inverse_affines : list of AffineTransform objects
    Inverse affine transformations for each triangle in the mesh.

c                 <    S U l         S U l        S U l        S U l        g r   )_tesselation_inverse_tesselationaffinesinverse_affinesr_   s    r   rw   !PiecewiseAffineTransform.__init__K  s!     $(!#r   c           	         [         R                  " U5      n[         R                  " U5      nUR                  S   n[        R                  " U5      U l        Sn/ U l        U R
                  R                   HG  n[        US9nXFR                  XSS24   X%SS24   5      -  nU R                  R                  U5        MI     [        R                  " U5      U l        / U l        U R                  R                   HG  n[        US9nXFR                  X%SS24   XSS24   5      -  nU R                  R                  U5        MI     U$ )aX  Estimate the transformation from a set of corresponding points.

Number of source and destination coordinates must match.

Parameters
----------
src : (N, D) array_like
    Source coordinates.
dst : (N, D) array_like
    Destination coordinates.

Returns
-------
success : bool
    True, if all pieces of the model are successfully estimated.

r
   Trp   N)r   r9   r    r   Delaunayr-  r/  	simplicesr   r   appendr.  r0  )rX   rA   rB   r   successtriaffines          r   r   !PiecewiseAffineTransform.estimateQ  s   $ jjojjoyy| $,,S1 $$..C$D9Fs6{CQK@@GLL' / %,$4$4S$9!!,,66C$D9Fs6{CQK@@G  ''/ 7
 r   c                 t   [         R                  " U5      n[         R                  " U[         R                  5      nU R                  R                  U5      nSX#S:H  SS24'   [        [        U R                  R                  5      5       H*  nU R                  U   nX4:H  nU" XSS24   5      X&SS24'   M,     U$ )zApply forward transformation.

Coordinates outside of the mesh will be set to `- 1`.

Parameters
----------
coords : (N, D) array_like
    Source coordinates.

Returns
-------
coords : (N, 2) array
    Transformed coordinates.

r   N)
r   r9   
empty_liker:   r-  find_simplexr   lenr4  r/  )rX   rY   outsimplexindexr8  
index_masks          r   rZ   !PiecewiseAffineTransform.__call__  s      F#mmFBJJ/ ##008 !#rM13t00::;<E\\%(F )J!'1}(=!>CA = 
r   c                     [        U 5      " 5       nU R                  Ul        U R                  Ul        U R                  Ul        U R                  Ul        U$ )r^   )r   r.  r-  r0  r/  )rX   r   s     r   r`    PiecewiseAffineTransform.inverse  sI     T
!66%)%6%6",, $r   )r.  r-  r/  r0  Nrf   rg   rh   ri   rj   rw   r   rZ   rk   r`   rl   rV   r   r   r+  r+  ;  s,    $-^!F  r   r+  c                 n    [         R                  R                  R                  SXS9R	                  5       $ )a  Produce an Euler rotation matrix from the given intrinsic rotation angles
for the axes x, y and z.

Parameters
----------
angles : array of float, shape (3,)
    The transformation angles in radians.
degrees : bool, optional
    If True, then the given angles are assumed to be in degrees. Default is False.

Returns
-------
R : array of float, shape (3, 3)
    The Euler rotation matrix.
XYZanglesdegrees)r   	transformRotation
from_euler	as_matrixrH  s     r   _euler_rotation_matrixrO    s4      %%00f 1 ikr   c                   R    \ rS rSrSr SSS.S jjrS r\S 5       r\S	 5       r	S
r
g)EuclideanTransformi  a  Euclidean transformation, also known as a rigid transform.

Has the following form::

    X = a0 * x - b0 * y + a1 =
      = x * cos(rotation) - y * sin(rotation) + a1

    Y = b0 * x + a0 * y + b1 =
      = x * sin(rotation) + y * cos(rotation) + b1

where the homogeneous transformation matrix is::

    [[a0 -b0  a1]
     [b0  a0  b1]
     [0   0   1 ]]

The Euclidean transformation is a rigid transformation with rotation and
translation parameters. The similarity transformation extends the Euclidean
transformation with a single scaling factor.

In 2D and 3D, the transformation parameters may be provided either via
`matrix`, the homogeneous transformation matrix, above, or via the
implicit parameters `rotation` and/or `translation` (where `a1` is the
translation along `x`, `b1` along `y`, etc.). Beyond 3D, if the
transformation is only a translation, you may use the implicit parameter
`translation`; otherwise, you must use `matrix`.

Parameters
----------
matrix : (D+1, D+1) array_like, optional
    Homogeneous transformation matrix.
rotation : float or sequence of float, optional
    Rotation angle, clockwise, as radians. If given as
    a vector, it is interpreted as Euler rotation angles [1]_. Only 2D
    (single rotation) and 3D (Euler rotations) values are supported. For
    higher dimensions, you must provide or estimate the transformation
    matrix.
translation : (x, y[, z, ...]) sequence of float, length D, optional
    Translation parameters for each axis.
dimensionality : int, optional
    The dimensionality of the transform.

Attributes
----------
params : (D+1, D+1) array
    Homogeneous transformation matrix.

References
----------
.. [1] https://en.wikipedia.org/wiki/Rotation_matrix#In_three_dimensions
Nr   rp   c                   US L=(       d    US LnU(       a  Ub  [        S5      eUbH  [        R                  " U5      nUR                  S   UR                  S   :w  a  [        S5      eXl        g U(       GaW  Uc@  [        U5      nUS:X  a  SnOeUS:X  a  [        R                  " S5      nOH[        SU S35      e[        R                  " U5      (       d  [        U5      S:w  a  [        SU S35      eUc  S	U-  nUS:X  ax  [        R                  " [        R                  " U5      [        R                  " U5      * S/[        R                  " U5      [        R                  " U5      S// S
Q/5      U l        OBUS:X  a<  [        R                  " US-   5      U l        [        U5      U R                  S U2S U24'   X0R                  SU2U4'   g [        R                  " US-   5      U l        g )Nr  r   r
   r  r   r   z-Parameters cannot be specified for dimension z transformsr   r  )r   r   r9   r    rs   r=  r   r	  r   r
  r  r  r   rO  )rX   r   r   r   r   params_givens         r   rw   EuclideanTransform.__init__  s     t+F{$/FF.=  ZZ'F||A&,,q/1 !JKK K!$[!1!Q& H#q(!xx{H$G)*+7 
 {{8,,X!1C$G)*+7  ""^3" hh(+dhhx.@-@!D(+TXXh-?C!  1$ ff^a%78@VAO^O_n_<= =HKK.(.89 &&!!34DKr   c                     [        XS5      U l        [        R                  " [        R                  " U R                  5      5      (       + $ )  Estimate the transformation from a set of corresponding points.

You can determine the over-, well- and under-determined parameters
with the total least-squares method.

Number of source and destination coordinates must match.

Parameters
----------
src : (N, 2) array_like
    Source coordinates.
dst : (N, 2) array_like
    Destination coordinates.

Returns
-------
success : bool
    True, if model estimation succeeds.

FrQ   rs   r   r  isnanrc   s      r   r   EuclideanTransform.estimate.  s3    * s/ 66"((4;;/000r   c                     U R                   S:X  a1  [        R                  " U R                  S   U R                  S   5      $ U R                   S:X  a  U R                  S S2S S24   $ [	        S5      e)Nr   r  r  r   z3Rotation only implemented for 2D and 3D transforms.)r   r
  r  rs   rt   r_   s    r   r   EuclideanTransform.rotationH  sl    !#::dkk$/T1BCC  A%;;rr2A2v&&%E r   c                 P    U R                   SU R                  2U R                  4   $ r&  r'  r_   s    r   r   EuclideanTransform.translationT  r)  r   r   r   )rf   rg   rh   ri   rj   rw   r   rk   r   r   rl   rV   r   r   rQ  rQ    sJ    2j 7;45NO45l14 	 	 I Ir   rQ  c                   H    \ rS rSrSr    S
SS.S jjrS r\S 5       rS	r	g)SimilarityTransformiY  a+  Similarity transformation.

Has the following form in 2D::

    X = a0 * x - b0 * y + a1 =
      = s * x * cos(rotation) - s * y * sin(rotation) + a1

    Y = b0 * x + a0 * y + b1 =
      = s * x * sin(rotation) + s * y * cos(rotation) + b1

where ``s`` is a scale factor and the homogeneous transformation matrix is::

    [[a0 -b0  a1]
     [b0  a0  b1]
     [0   0   1 ]]

The similarity transformation extends the Euclidean transformation with a
single scaling factor in addition to the rotation and translation
parameters.

Parameters
----------
matrix : (dim+1, dim+1) array_like, optional
    Homogeneous transformation matrix.
scale : float, optional
    Scale factor. Implemented only for 2D and 3D.
rotation : float, optional
    Rotation angle, clockwise, as radians.
    Implemented only for 2D and 3D. For 3D, this is given in ZYX Euler
    angles.
translation : (dim,) array_like, optional
    x, y[, z] translation parameters. Implemented only for 2D and 3D.

Attributes
----------
params : (dim+1, dim+1) array
    Homogeneous transformation matrix.

Nr   rp   c                ,   S U l         [        S X#U4 5       5      nU(       a  Ub  [        S5      eUbi  [        R                  " U5      nUR
                  S:w  d   UR                  S   UR                  S   :w  a  [        S5      eXl         UR                  S   S-
  nU(       a  US;  a  [        S5      e[        R                  " US-   [        S	9nUc  SnUc
  US:X  a  SOS
nUc  SU-  nUS:X  aB  Sn[        R                  " U5      [        R                  " U5      pXXw4'   U	* U	4XUS S S2   4'   O[        U5      US S2S S24'   US U2S U24==   U-  ss'   XAS U2U4'   Xl         g U R                   c  [        R                  " US-   5      U l         g g )Nc              3   (   #    U  H  oS Lv   M
     g 7fr   rV   r   s     r   r   /SimilarityTransform.__init__.<locals>.<genexpr>  s     S4R5$&4Rr  r  r   r   r
   r  )r   r   z(Parameters only supported for 2D and 3D.r7   )r   r   r   rS  r"  r   r   )rs   r  r   r   r9   r   r    r   r   r  r  rO  )
rX   r   rP   r   r   r   rs   axcrO   s
             r   rw   SimilarityTransform.__init__  s    SUk4RSSf(=  ZZ'F{{a6<<?fll1o#E !JKK$!'a1!4V+ !KLLVVNQ.e<F} .! 31""^3"vvh')91!"rv()r1u2dd8|$!7!Arr2A2v?N?O^O34=46A?N?N23 K[[ &&!!34DK !r   c                     [        XSS9U l        [        R                  " [        R                  " U R                  5      5      (       + $ )rW  T)rC   rX  rc   s      r   r   SimilarityTransform.estimate  s3    , s= 66"((4;;/000r   c                 L   U R                   S:X  a=  [        R                  " [        R                  R	                  U R
                  5      5      $ U R                   S:X  a=  [        R                  " [        R                  R	                  U R
                  5      5      $ [        S5      e)Nr   r   z(Scale is only implemented for 2D and 3D.)r   r   r   r;   r<   rs   cbrtrt   r_   s    r   rP   SimilarityTransform.scale  sl     !#77299==566  A%77299==566%&PQQr   r   )NNNN)
rf   rg   rh   ri   rj   rw   r   rk   rP   rl   rV   r   r   r`  r`  Y  sA    &T /5 /5b16 R Rr   r`  c                   J    \ rS rSrSrSSS.S jjrSS jrS r\S	 5       r	S
r
g)PolynomialTransformi  a=  2D polynomial transformation.

Has the following form::

    X = sum[j=0:order]( sum[i=0:j]( a_ji * x**(j - i) * y**i ))
    Y = sum[j=0:order]( sum[i=0:j]( b_ji * x**(j - i) * y**i ))

Parameters
----------
params : (2, N) array_like, optional
    Polynomial coefficients where `N * 2 = (order + 1) * (order + 2)`. So,
    a_ji is defined in `params[0, :]` and b_ji in `params[1, :]`.

Attributes
----------
params : (2, N) array
    Polynomial coefficients where `N * 2 = (order + 1) * (order + 2)`. So,
    a_ji is defined in `params[0, :]` and b_ji in `params[1, :]`.

Nr   rp   c                    US:w  a  [        S5      eUc  [        R                  " / SQ/ SQ/5      nO[        R                  " U5      nUR                  S   S:w  a  [        S5      eXl        g )Nr   z2Polynomial transforms are only implemented for 2D.)r   r
   r   r  r   z*invalid shape of transformation parameters)rt   r   r   r9   r    r   rs   )rX   rs   r   s      r   rw   PolynomialTransform.__init__  sc    Q%D  >XXy)45FZZ'F<<?aIJJr   c           
         [         R                  " U5      n[         R                  " U5      nUSS2S4   nUSS2S4   nUSS2S4   nUSS2S4   nUR                  S   n	[        U5      nUS-   US-   -  n
[         R                  " U	S-  U
S-   45      nSn[        US-   5       HG  n[        US-   5       H2  nX]U-
  -  Xn-  -  USU	2U4'   X]U-
  -  Xn-  -  XS2XS-  -   4'   US-  nM4     MI     X{SU	2S4'   XU	S2S4'   Uc$  [         R                  R                  U5      u    nnO[         R                  " U5      n[         R                  " [         R                  " [         R                  " U[         R                  " U5      -  5      S5      5      n[         R                  R                  UU-  5      u    nnUSSS24   * US   -  nUR                  SU
S-  45      U l        g)a  Estimate the transformation from a set of corresponding points.

You can determine the over-, well- and under-determined parameters
with the total least-squares method.

Number of source and destination coordinates must match.

The transformation is defined as::

    X = sum[j=0:order]( sum[i=0:j]( a_ji * x**(j - i) * y**i ))
    Y = sum[j=0:order]( sum[i=0:j]( b_ji * x**(j - i) * y**i ))

These equations can be transformed to the following form::

    0 = sum[j=0:order]( sum[i=0:j]( a_ji * x**(j - i) * y**i )) - X
    0 = sum[j=0:order]( sum[i=0:j]( b_ji * x**(j - i) * y**i )) - Y

which exist for each set of corresponding points, so we have a set of
N * 2 equations. The coefficients appear linearly so we can write
A x = 0, where::

    A   = [[1 x y x**2 x*y y**2 ... 0 ...             0 -X]
           [0 ...                 0 1 x y x**2 x*y y**2 -Y]
            ...
            ...
          ]
    x.T = [a00 a10 a11 a20 a21 a22 ... ann
           b00 b10 b11 b20 b21 b22 ... bnn c3]

In case of total least-squares the solution of this homogeneous system
of equations is the right singular vector of A which corresponds to the
smallest singular value normed by the coefficient c3.

Weights can be applied to each pair of corresponding points to
indicate, particularly in an overdetermined system, if point pairs have
higher or lower confidence or uncertainties associated with them. From
the matrix treatment of least squares problems, these weight values are
normalised, square-rooted, then built into a diagonal matrix, by which
A is multiplied.

Parameters
----------
src : (N, 2) array_like
    Source coordinates.
dst : (N, 2) array_like
    Destination coordinates.
order : int, optional
    Polynomial order (number of coefficients is order + 1).
weights : (N,) array_like, optional
    Relative weight values for each pair of points.

Returns
-------
success : bool
    True, if model estimation succeeds.

Nr   r
   r   r   r   T)r   r9   r    r   r   r   r;   r=   r?   r   r   r   r   rs   )rX   rA   rB   orderr   xsysxdydrowsurJ   pidxjir   rM   r   rs   s                      r   r   PolynomialTransform.estimate  s   t jjojjoAYAYAYAYyy| E"QY519%HHdQhA&'uqy!A1q5\!#A!6%4%+*,Q-"%*?%Q&'	 " " %4%)$%) ?iimmA&GAq!jj)G"&&/(A BAFGAiimmAE*GAq! BG*qy(nnaa[1r   c           	      N   [         R                  " U5      nUSS2S4   nUSS2S4   n[        U R                  R	                  5       5      n[        S[        R                  " SSSU-
  -  -
  5      -   S-  5      n[         R                  " UR                  5      nSn[        US-   5       Hw  n[        US-   5       Hb  n	USS2S4==   U R                  SU4   X(U	-
  -  -  X9-  -  -  ss'   USS2S4==   U R                  SU4   X(U	-
  -  -  X9-  -  -  ss'   US-  nMd     My     U$ )zApply forward transformation.

Parameters
----------
coords : (N, 2) array_like
    source coordinates

Returns
-------
coords : (N, 2) array
    Transformed coordinates.

Nr   r
   r   r   r   )r   r9   r=  rs   ravelr   r
  r   r   r    r   )
rX   rY   xyrw  rq  rB   rx  ry  rz  s
             r   rZ   PolynomialTransform.__call___  s    F#1a4L1a4L!!#$R$))AQUO449:hhv||$uqy!A1q5\AqD	T[[D1Aa%L@14GG	AqD	T[[D1Aa%L@14GG		 " " 
r   c                     [        S5      e)NzThere is no explicit way to do the inverse polynomial transformation. Instead, estimate the inverse transformation parameters by exchanging source and destination coordinates,then apply the forward transformation.)rt   r_   s    r   r`   PolynomialTransform.inverse~  s    !5
 	
r   r   r   )r   NrE  rV   r   r   rm  rm    s3    *a `D> 
 
r   rm  )	euclidean
similarityr8  zpiecewise-affine
projectivefundamental	essential
polynomialc                     U R                  5       n U [        ;  a  [        SU  S35      e[        U    " UR                  S   S9nUR                  " X/UQ70 UD6  U$ )a4  Estimate 2D geometric transformation parameters.

You can determine the over-, well- and under-determined parameters
with the total least-squares method.

Number of source and destination coordinates must match.

Parameters
----------
ttype : {'euclidean', similarity', 'affine', 'piecewise-affine',              'projective', 'polynomial'}
    Type of transform.
kwargs : array_like or int
    Function parameters (src, dst, n, angle)::

        NAME / TTYPE        FUNCTION PARAMETERS
        'euclidean'         `src, `dst`
        'similarity'        `src, `dst`
        'affine'            `src, `dst`
        'piecewise-affine'  `src, `dst`
        'projective'        `src, `dst`
        'polynomial'        `src, `dst`, `order` (polynomial order,
                                                  default order is 2)

    Also see examples below.

Returns
-------
tform : :class:`_GeometricTransform`
    Transform object containing the transformation parameters and providing
    access to forward and inverse transformation functions.

Examples
--------
>>> import numpy as np
>>> import skimage as ski

>>> # estimate transformation parameters
>>> src = np.array([0, 0, 10, 10]).reshape((2, 2))
>>> dst = np.array([12, 14, 1, -20]).reshape((2, 2))

>>> tform = ski.transform.estimate_transform('similarity', src, dst)

>>> np.allclose(tform.inverse(tform(src)), src)
True

>>> # warp image using the estimated transformation
>>> image = ski.data.camera()

>>> ski.transform.warp(image, inverse_map=tform.inverse) # doctest: +SKIP

>>> # create transformation with explicit parameters
>>> tform2 = ski.transform.SimilarityTransform(scale=1.1, rotation=1,
...     translation=(10, 20))

>>> # unite transformations, applied in order from left to right
>>> tform3 = tform + tform2
>>> np.allclose(tform3(src), tform2(tform(src)))
True

zthe transformation type 'z' is not implementedr
   rp   )lower
TRANSFORMSr   r    r   )ttyperA   rB   argskwargsr   s         r   estimate_transformr    s`    | KKMEJ5eW<QRSSuSYYq\:E	NN3-d-f-Lr   c                 $    [        U5      " U 5      $ )zApply 2D matrix transform.

Parameters
----------
coords : (N, 2) array_like
    x, y coordinates to transform
matrix : (3, 3) array_like
    Homogeneous transformation matrix.

Returns
-------
coords : (N, 2) array
    Transformed coordinates.

)r   )rY   r   s     r   matrix_transformr    s      v&v..r   )F)r
  r   abcr   r   numpyr   scipyr   _shared.utilsr   _shared.compatr   r   r5   rQ   rS   rn   r   r   r   r+  rO  rQ  r`  rm  r  r  r  rV   r   r   <module>r     s      #   ' .K\JZ+?# +?\p
!4 p
fP9 PfC(- C(LHI) HIVp2 pf*SI, SIl}R, }R@l
- l
` $%0%-)%	
EP/r   