ó
 ‰\c           @   sƒ  d  d l  m Z 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 „  Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ i e d 6e d 6e d 6e d 6e d 6e d  6e d! 6e d" 6Z d# „  Z d$ „  Z d S(%   iÿÿÿÿ(   t   divisionN(   t   spatiali   (   t   get_bound_method_classt   safe_as_intc         C   s8   t  d d d d d d ƒ } |  | k r4 | |  }  n  |  S(   sE   Convert from `numpy.pad` mode name to the corresponding ndimage mode.t   edget   nearestt	   symmetrict   reflectt   mirror(   t   dict(   t   modet   mode_translation_dict(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt   _to_ndimage_mode	   s
    	c         C   sS  t  j |  d d ƒ} t j t  j |  | d ƒ |  j d ƒ } t j d ƒ | } t  j | d | | d g d | | | d g d d d g g ƒ } t  j |  j t  j	 |  j d ƒ g ƒ } t  j
 | | ƒ j } | d d … d d … f } | d d … d f c | d d … d f <| d d … d f c | d d … d f <| | f S(   së  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(2).

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

    Returns
    -------
    matrix : (3, 3) array
        The transformation matrix to obtain the new points.
    new_points : (N, 2) 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.

    t   axisi    i   i   N(   t   npt   meant   matht   sqrtt   sumt   shapet   arrayt	   row_stackt   Tt   onest   dot(   t   pointst   centroidt   rmst   norm_factort   matrixt   pointsht   new_pointsht
   new_points(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt   _center_and_normalize_points   s    !+(,,c         C   s½  |  j  d } |  j  d } |  j d d ƒ } | j d d ƒ } |  | } | | } t j | j | ƒ | }	 t j | f d t j ƒ}
 t j j |	 ƒ d k  r¯ d |
 | d <n  t j	 | d d t j ƒ} t j j
 |	 ƒ \ } } } t j j |	 ƒ } | d k rt j | S| | d k rÜt j j | ƒ t j j | ƒ d k rrt j | | ƒ | d | … d | … f <q|
 | d } d |
 | d <t j | t j t j |
 ƒ | ƒ ƒ | d | … d | … f <| |
 | d <n= t j | t j t j |
 ƒ | ƒ ƒ | d | … d | … f <| rNd | j d d ƒ j ƒ  t j | |
 ƒ } n d } | | t j | d | … d | … f | j ƒ | d | … | f <| d | … d | … f c | 9<| S(   sŽ  Estimate N-D similarity transformation with or without scaling.

    Parameters
    ----------
    src : (M, N) array
        Source coordinates.
    dst : (M, N) array
        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

    i    i   R   t   dtypeiÿÿÿÿNg      ð?(   R   R   R   R   R   R   t   doublet   linalgt   dett   eyet   svdt   matrix_rankt   nant   diagt   varR   (   t   srct   dstt   estimate_scalet   numt   dimt   src_meant   dst_meant
   src_demeant
   dst_demeant   At   dR   t   Ut   St   Vt   rankt   st   scale(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt   _umeyamaH   s:    

(+==/C"t   GeometricTransformc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s/   Base class for geometric transformations.

    c         C   s   t  ƒ  ‚ d S(   sð   Apply forward transformation.

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

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

        N(   t   NotImplementedError(   t   selft   coords(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt   __call__—   s    c         C   s   t  ƒ  ‚ d S(   sð   Apply inverse transformation.

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

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

        N(   R?   (   R@   RA   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt   inverse§   s    c         C   s*   t  j t  j |  | ƒ | d d d ƒƒ S(   sÔ  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.

        i   R   i   (   R   R   R   (   R@   R,   R-   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt	   residuals·   s    c         C   s   t  ƒ  ‚ d S(   s3   Combine this transformation with another.

        N(   R?   (   R@   t   other(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt   __add__Ì   s    (   t   __name__t
   __module__t   __doc__RB   RC   RD   RF   (    (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR>   “   s
   			t   FundamentalMatrixTransformc           B   sG   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   sÑ  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, optional
        Fundamental matrix.

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

    c         C   sI   | d  k r t j d ƒ } n  | j d k r< t d ƒ ‚ n  | |  _ d  S(   Ni   s&   Invalid shape of transformation matrix(   i   i   (   t   NoneR   R&   R   t
   ValueErrort   params(   R@   R   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt   __init__ñ   s
    c         C   s;   t  j | t  j | j d ƒ g ƒ } t  j | |  j j ƒ S(   s   Apply forward transformation.

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

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

        i    (   R   t   column_stackR   R   R   RM   R   (   R@   RA   t   coords_homogeneous(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRB   ù   s    %c         C   s8   t  j | t  j | j d ƒ g ƒ } t  j | |  j ƒ S(   s   Apply inverse transformation.

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

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

        i    (   R   RO   R   R   R   RM   (   R@   RA   RP   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRC   
  s    %c   	      C   sÆ  | j  | j  k s t ‚ | j  d d k s1 t ‚ y( t | ƒ \ } } t | ƒ \ } } Wn@ t k
 r› t j d t j ƒ |  _ d t j d t j ƒ g SXt j | j  d d f ƒ } | | d d … d d … f <| d d … d d … f c | d d … d t j	 f 9<| | d d … d d … f <| d d … d d … f c | d d … d	 t j	 f 9<| | d d … d d … f <t j
 j | ƒ \ } } } | d
 d d … f j d d ƒ } | | | f S(   sâ  Setup and solve the homogeneous epipolar constraint matrix::

            dst' * F * src = 0.

        Parameters
        ----------
        src : (N, 2) array
            Source coordinates.
        dst : (N, 2) array
            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.

        i    i   i   i	   Ni   i   i   i   iÿÿÿÿ(   i   i   (   i   i   (   R   t   AssertionErrorR!   t   ZeroDivisionErrorR   t   fullR)   RM   R   t   newaxisR$   R'   t   reshape(	   R@   R,   R-   t
   src_matrixt
   dst_matrixR5   t   _R9   t   F_normalized(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt   _setup_constraint_matrix  s"    88"c   
      C   s   |  j  | | ƒ \ } } } t j j | ƒ \ } } } d | d <t j | t j t j | ƒ | ƒ ƒ }	 t j | j t j |	 | ƒ ƒ |  _ t S(   sû  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
            Source coordinates.
        dst : (N, 2) array
            Destination coordinates.

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

        i    i   (	   RZ   R   R$   R'   R   R*   R   RM   t   True(
   R@   R,   R-   RY   RV   RW   R7   R8   R9   t   F(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt   estimateM  s    
'$c         C   sß   t  j | t  j | j d ƒ g ƒ } t  j | t  j | j d ƒ g ƒ } t  j |  j | j ƒ } t  j |  j j | j ƒ } t  j | | j d d ƒ} t  j | ƒ t  j	 | d d | d d | d d | d d ƒ S(   sy  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.

        i    R   i   i   (
   R   RO   R   R   R   RM   R   R   t   absR   (   R@   R,   R-   t   src_homogeneoust   dst_homogeneoust   F_srct   Ft_dstt	   dst_F_src(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRD   o  s    %%N(
   RG   RH   RI   RK   RN   RB   RC   RZ   R]   RD   (    (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRJ   Ó   s   			2	"t   EssentialMatrixTransformc           B   s)   e  Z d  Z d d d d „ Z d „  Z RS(   sæ  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, optional
        Rotation matrix of the relative camera motion.
    translation : (3, 1) array, optional
        Translation vector of the relative camera motion. The vector must
        have unit length.
    matrix : (3, 3) array, optional
        Essential matrix.

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

    c      
   C   sz  | d  k	 r.| d  k r' t d ƒ ‚ n  | j d k rE t d ƒ ‚ n  t t j j | ƒ d ƒ d k rv t d ƒ ‚ n  | j d k r” t d ƒ ‚ n  t t j j | ƒ d ƒ d k rÅ t d ƒ ‚ n  t j	 d	 | d
 | d | d
 d	 | d	 | d | d	 d	 g	 ƒ j
 d d ƒ } t j | | ƒ |  _ nH | d  k	 rd| j d k rXt d ƒ ‚ n  | |  _ n t j d ƒ |  _ d  S(   Ns&   Both rotation and translation requiredi   s    Invalid shape of rotation matrixi   gíµ ÷Æ°>s*   Rotation matrix must have unit determinants#   Invalid shape of translation vectors(   Translation vector must have unit lengthi    i   s&   Invalid shape of transformation matrix(   i   i   (   i   i   (   RK   RL   R   R^   R   R$   R%   t   sizet   normR   RU   R   RM   R&   (   R@   t   rotationt   translationR   t   t_x(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRN   ±  s(    ""'c   
      C   s·   |  j  | | ƒ \ } } } t j j | ƒ \ } } } | d | d d | d <| d | d <d | d <t j | t j t j | ƒ | ƒ ƒ }	 t j | j t j |	 | ƒ ƒ |  _ t S(   sù  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
            Source coordinates.
        dst : (N, 2) array
            Destination coordinates.

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

        i    i   g       @i   (	   RZ   R   R$   R'   R   R*   R   RM   R[   (
   R@   R,   R-   t   E_normalizedRV   RW   R7   R8   R9   t   E(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR]   Ê  s    
'$N(   RG   RH   RI   RK   RN   R]   (    (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRd     s   "t   ProjectiveTransformc           B   sb   e  Z d  Z e d ƒ Z d	 d „ Z e d „  ƒ Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z RS(
   s?  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 : (3, 3) array, optional
        Homogeneous transformation matrix.

    Attributes
    ----------
    params : (3, 3) array
        Homogeneous transformation matrix.

    i   c         C   sI   | d  k r t j d ƒ } n  | j d k r< t d ƒ ‚ n  | |  _ d  S(   Ni   s&   invalid shape of transformation matrix(   i   i   (   RK   R   R&   R   RL   RM   (   R@   R   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRN     s
    c         C   s   t  j j |  j ƒ S(   N(   R   R$   t   invRM   (   R@   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt   _inv_matrix  s    c         C   sá   t  j | d t d d ƒ} t  j | ƒ \ } } t  j | | t  j | ƒ f ƒ } t  j | j ƒ  | j ƒ  ƒ } | d  d  … d f c | d  d  … d f <| d  d  … d f c | d  d  … d f <| d  d  … d  d … f S(   Nt   copyt   ndmini   i    i   (   R   R   t   Falset	   transposet   vstackt	   ones_likeR   (   R@   RA   R   t   xt   yR,   R-   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt
   _apply_mat"  s    !,,c         C   s   |  j  | |  j ƒ S(   sð   Apply forward transformation.

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

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

        (   Rw   RM   (   R@   RA   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRB   /  s    c         C   s   |  j  | |  j ƒ S(   sð   Apply inverse transformation.

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

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

        (   Rw   Rn   (   R@   RA   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRC   ?  s    c         C   s·  y( t  | ƒ \ } } t  | ƒ \ } } Wn+ t k
 rU t j t j d ƒ |  _ t SX| d d … d f } | d d … d f } | d d … d f } | d d … d f } | j d }	 t j |	 d d f ƒ }
 | |
 d |	 … d f <| |
 d |	 … d f <d |
 d |	 … d f <| | |
 d |	 … d f <| | |
 d |	 … d f <| |
 |	 d … d f <| |
 |	 d … d	 f <d |
 |	 d … d
 f <| | |
 |	 d … d f <| | |
 |	 d … d f <| |
 d |	 … d f <| |
 |	 d … d f <|
 d d … t	 |  j
 ƒ d g f }
 t j j |
 ƒ \ } } } t j d ƒ } | d d d … f | d | j t	 |  j
 ƒ d g <d | d <t j t j j | ƒ t j | | ƒ ƒ } | |  _ t S(   sì  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.

        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
            Source coordinates.
        dst : (N, 2) array
            Destination coordinates.

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

        i   Ni    i   i   i	   i   i   i   i   i   iÿÿÿÿ(   i   i   (   i   i   (   iÿÿÿÿiÿÿÿÿ(   i   i   (   R!   RR   R   R)   t   emptyRM   Rq   R   t   zerost   listt   _coeffsR$   R'   t   flatR   Rm   R[   (   R@   R,   R-   RV   RW   t   xst   yst   xdt   ydt   rowsR5   RX   R9   t   H(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR]   O  s@    9&6
*	c         C   s±   t  | t ƒ rR t |  ƒ t | ƒ k r3 |  j } n t } | | j j |  j ƒ ƒ St | d ƒ r¡ | j d k r¡ t t | ƒ d ƒ r¡ t | j	 j
 j |  j ƒ ƒ St d ƒ ‚ d S(   s3   Combine this transformation with another.

        RG   RC   Rn   s2   Cannot combine transformations of differing types.N(   t
   isinstanceRl   t   typet	   __class__RM   R   t   hasattrRG   R   t   __self__Rn   t	   TypeError(   R@   RE   t   tform(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRF   ¶  s    N(   RG   RH   RI   t   rangeR{   RK   RN   t   propertyRn   Rw   RB   RC   R]   RF   (    (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRl   ï  s   #				gt   AffineTransformc           B   sn   e  Z d  Z e d ƒ Z d d d d d d „ Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 e d „  ƒ Z RS(   sù  2D affine transformation.

    Has the following form::

        X = a0*x + a1*y + a2 =
          = sx*x*cos(rotation) - sy*y*sin(rotation + shear) + a2

        Y = b0*x + b1*y + b2 =
          = sx*x*sin(rotation) + sy*y*cos(rotation + shear) + b2

    where ``sx`` and ``sy`` are scale factors in the x and y directions,
    and the homogeneous transformation matrix is::

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

    Parameters
    ----------
    matrix : (3, 3) array, optional
        Homogeneous transformation matrix.
    scale : (sx, sy) as array, list or tuple, optional
        Scale factors.
    rotation : float, optional
        Rotation angle in counter-clockwise direction as radians.
    shear : float, optional
        Shear angle in counter-clockwise direction as radians.
    translation : (tx, ty) as array, list or tuple, optional
        Translation parameters.

    Attributes
    ----------
    params : (3, 3) array
        Homogeneous transformation matrix.

    i   c   	      C   s„  t  d „  | | | | f Dƒ ƒ } | rC | d  k	 rC t d ƒ ‚ n=| d  k	 ry | j d k rm t d ƒ ‚ n  | |  _ n| rn| d  k r” d	 } n  | d  k r© d } n  | d  k r¾ d } n  | d  k rÓ d
 } n  | \ } } t j | t j | ƒ | t j	 | | ƒ d g | t j	 | ƒ | t j | | ƒ d g d d d g g ƒ |  _ | |  j d d … d f <n t j
 d ƒ |  _ d  S(   Nc         s   s   |  ] } | d  k	 Vq d  S(   N(   RK   (   t   .0t   param(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pys	   <genexpr>õ  s   sZ   You cannot specify the transformation matrix and the implicit parameters at the same time.i   s'   Invalid shape of transformation matrix.i   i    i   (   i   i   (   i   i   (   i    i    (   t   anyRK   RL   R   RM   R   R   R   t   cost   sinR&   (	   R@   R   R<   Rg   t   shearRh   RM   t   sxt   sy(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRN   ó  s0    					+*c         C   s\   t  j |  j d d |  j d d ƒ } t  j |  j d d |  j d d ƒ } | | f S(   Ni    i   i   (   i    i    (   i   i    (   i    i   (   i   i   (   R   R   RM   (   R@   R“   R”   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR<     s    ))c         C   s   t  j |  j d |  j d ƒ S(   Ni   i    (   i   i    (   i    i    (   R   t   atan2RM   (   R@   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRg     s    c         C   s,   t  j |  j d |  j d ƒ } | |  j S(   Ni    i   (   i    i   (   i   i   (   R   R•   RM   Rg   (   R@   t   beta(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR’     s    !c         C   s   |  j  d d … d f S(   Ni    i   (   RM   (   R@   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRh   #  s    N(   RG   RH   RI   RŠ   R{   RK   RN   R‹   R<   Rg   R’   Rh   (    (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRŒ   Ë  s   $ t   PiecewiseAffineTransformc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sõ  2D 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         C   s(   d  |  _ d  |  _ d  |  _ d  |  _ d  S(   N(   RK   t   _tesselationt   _inverse_tesselationt   affinest   inverse_affines(   R@   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRN   8  s    			c         C   sú   t  j | ƒ |  _ g  |  _ x] |  j j D]O } t ƒ  } | j | | d d … f | | d d … f ƒ |  j j | ƒ q( Wt  j | ƒ |  _ g  |  _	 x] |  j j D]O } t ƒ  } | j | | d d … f | | d d … f ƒ |  j	 j | ƒ q£ Wt
 S(   s•  Estimate the transformation from a set of corresponding points.

        Number of source and destination coordinates must match.

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

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

        N(   R   t   DelaunayR˜   Rš   t   verticesRŒ   R]   t   appendR™   R›   R[   (   R@   R,   R-   t   trit   affine(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR]   >  s    		0		0c         C   s¯   t  j | t  j ƒ } |  j j | ƒ } d | | d k d d … f <xe t t |  j j ƒ ƒ D]K } |  j | } | | k } | | | d d … f ƒ | | d d … f <q\ W| S(   s/  Apply forward transformation.

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

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

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

        iÿÿÿÿN(	   R   t
   empty_likeR#   R˜   t   find_simplexRŠ   t   lenR   Rš   (   R@   RA   t   outt   simplext   indexR    t
   index_mask(    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRB   g  s    0c         C   s¯   t  j | t  j ƒ } |  j j | ƒ } d | | d k d d … f <xe t t |  j j ƒ ƒ D]K } |  j | } | | k } | | | d d … f ƒ | | d d … f <q\ W| S(   s/  Apply inverse transformation.

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

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

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

        iÿÿÿÿN(	   R   R¡   R#   R™   R¢   RŠ   R£   R   R›   (   R@   RA   R¤   R¥   R¦   R    R§   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRC   Š  s    0(   RG   RH   RI   RN   R]   RB   RC   (    (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR—   (  s
   		)	#t   EuclideanTransformc           B   sG   e  Z d  Z d d d d „ Z d „  Z e d „  ƒ Z e d „  ƒ Z RS(   sÔ  2D Euclidean transformation.

    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.

    Parameters
    ----------
    matrix : (3, 3) array, optional
        Homogeneous transformation matrix.
    rotation : float, optional
        Rotation angle in counter-clockwise direction as radians.
    translation : (tx, ty) as array, list or tuple, optional
        x, y translation parameters.

    Attributes
    ----------
    params : (3, 3) array
        Homogeneous transformation matrix.

    c         C   s0  t  d „  | | f Dƒ ƒ } | r= | d  k	 r= t d ƒ ‚ nï | d  k	 rs | j d k rg t d ƒ ‚ n  | |  _ n¹ | r| d  k rŽ d } n  | d  k r£ d	 } n  t j t j | ƒ t j	 | ƒ d g t j	 | ƒ t j | ƒ d g d d d g g ƒ |  _ | |  j d d … d f <n t j
 d ƒ |  _ d  S(
   Nc         s   s   |  ] } | d  k	 Vq d  S(   N(   RK   (   R   RŽ   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pys	   <genexpr>Ô  s   sZ   You cannot specify the transformation matrix and the implicit parameters at the same time.i   s'   Invalid shape of transformation matrix.i    i   i   (   i   i   (   i    i    (   R   RK   RL   R   RM   R   R   R   R   R‘   R&   (   R@   R   Rg   Rh   RM   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRN   Ó  s&    			c         C   s   t  | | t ƒ |  _ t S(   s  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
            Source coordinates.
        dst : (N, 2) array
            Destination coordinates.

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

        (   R=   Rq   RM   R[   (   R@   R,   R-   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR]   î  s    c         C   s   t  j |  j d |  j d ƒ S(   Ni   i    (   i   i    (   i   i   (   R   R•   RM   (   R@   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRg     s    c         C   s   |  j  d d … d f S(   Ni    i   (   RM   (   R@   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRh     s    N(	   RG   RH   RI   RK   RN   R]   R‹   Rg   Rh   (    (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR¨   ®  s
   #	t   SimilarityTransformc           B   s;   e  Z d  Z d d d d d „ Z d „  Z e d „  ƒ Z RS(   s	  2D similarity transformation.

    Has the following form::

        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 : (3, 3) array, optional
        Homogeneous transformation matrix.
    scale : float, optional
        Scale factor.
    rotation : float, optional
        Rotation angle in counter-clockwise direction as radians.
    translation : (tx, ty) as array, list or tuple, optional
        x, y translation parameters.

    Attributes
    ----------
    params : (3, 3) array
        Homogeneous transformation matrix.

    c         C   sm  t  d „  | | | f Dƒ ƒ } | r@ | d  k	 r@ t d ƒ ‚ n)| d  k	 rv | j d k rj t d ƒ ‚ n  | |  _ nó | rW| d  k r‘ d } n  | d  k r¦ d } n  | d  k r» d	 } n  t j t j | ƒ t j	 | ƒ d g t j	 | ƒ t j | ƒ d g d d d g g ƒ |  _ |  j d d … d d … f c | 9<| |  j d d … d f <n t j
 d ƒ |  _ d  S(
   Nc         s   s   |  ] } | d  k	 Vq d  S(   N(   RK   (   R   RŽ   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pys	   <genexpr>:  s   sZ   You cannot specify the transformation matrix and the implicit parameters at the same time.i   s'   Invalid shape of transformation matrix.i   i    i   (   i   i   (   i    i    (   R   RK   RL   R   RM   R   R   R   R   R‘   R&   (   R@   R   R<   Rg   Rh   RM   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRN   8  s,    				%c         C   s   t  | | t ƒ |  _ t S(   s  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
            Source coordinates.
        dst : (N, 2) array
            Destination coordinates.

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

        (   R=   R[   RM   (   R@   R,   R-   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR]   W  s    c         C   sX   t  t j |  j ƒ ƒ t j d ƒ k  r7 |  j d } n |  j d t j |  j ƒ } | S(   Ni   i    (   i   i    (   i    i    (   R^   R   R   Rg   R   t   spacingRM   (   R@   R<   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR<   q  s    'N(   RG   RH   RI   RK   RN   R]   R‹   R<   (    (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR©     s
   %		t   PolynomialTransformc           B   s8   e  Z d  Z d d „ Z d d „ Z d „  Z d „  Z RS(   sp  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, 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, :]`.

    c         C   se   | d  k r6 t j d d d g d d d g g ƒ } n  | j d d k rX t d ƒ ‚ n  | |  _ d  S(   Ni    i   i   s*   invalid shape of transformation parameters(   RK   R   R   R   RL   RM   (   R@   RM   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRN   ‘  s
    *i   c         C   sÀ  | d d … d f } | d d … d f } | d d … d f } | d d … d f } | j  d } t | ƒ } | d | d }	 t j | d |	 d f ƒ }
 d } x‘ t | d ƒ D] } xv t | d ƒ D]d } | | | | | |
 d | … | f <| | | | | |
 | d … | |	 d f <| d 7} qÎ Wq· W| |
 d | … d f <| |
 | d … d f <t j j |
 ƒ \ } } } | d d d … f | d } | j d |	 d f ƒ |  _ t	 S(   sÑ  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.

        Parameters
        ----------
        src : (N, 2) array
            Source coordinates.
        dst : (N, 2) array
            Destination coordinates.
        order : int, optional
            Polynomial order (number of coefficients is order + 1).

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

        Ni    i   i   iÿÿÿÿ(   iÿÿÿÿiÿÿÿÿ(
   R   R   R   Ry   RŠ   R$   R'   RU   RM   R[   (   R@   R,   R-   t   orderR}   R~   R   R€   R   t   uR5   t   pidxt   jt   iRX   R9   RM   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR]   ™  s(    1&.c   
   	   C   s@  | d d … d f } | d d … d f } t  |  j j ƒ  ƒ } t d t j d d d | ƒ d ƒ } t j | j ƒ } d } x· t	 | d ƒ D]¥ } xœ t	 | d ƒ D]Š }	 | d d … d f c |  j d | f | | |	 | |	 7<| d d … d f c |  j d | f | | |	 | |	 7<| d 7} qª Wq“ W| S(   sï   Apply forward transformation.

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

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

        Ni    i   iýÿÿÿi	   i   i   (
   R£   RM   t   ravelt   intR   R   R   Ry   R   RŠ   (
   R@   RA   Ru   Rv   R­   R¬   R-   R®   R¯   R°   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRB   é  s    )==c         C   s   t  d ƒ ‚ d  S(   NsÕ   There 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.(   t	   Exception(   R@   RA   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyRC     s    N(   RG   RH   RI   RK   RN   R]   RB   RC   (    (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyR«   {  s
   P	t	   euclideant
   similarityR    s   piecewise-affinet
   projectivet   fundamentalt	   essentialt
   polynomialc         K   sO   |  j  ƒ  }  |  t k r+ t d |  ƒ ‚ n  t |  ƒ  } | j | | |  | S(   så  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 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
    >>> from skimage import transform as tf

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

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

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

    >>> # warp image using the estimated transformation
    >>> from skimage import data
    >>> image = data.camera()

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

    >>> # create transformation with explicit parameters
    >>> tform2 = tf.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

    s.   the transformation type '%s' is notimplemented(   t   lowert
   TRANSFORMSRL   R]   (   t   ttypeR,   R-   t   kwargsR‰   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt   estimate_transform  s    ?c         C   s   t  | ƒ |  ƒ S(   s  Apply 2D matrix transform.

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

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

    (   Rl   (   RA   R   (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt   matrix_transforme  s    (   t
   __future__R    R   t   numpyR   t   scipyR   t   _shared.utilsR   R   R   R!   R=   t   objectR>   RJ   Rd   Rl   RŒ   R—   R¨   R©   R«   R»   R¾   R¿   (    (    (    s;   lib/python2.7/site-packages/skimage/transform/_geometric.pyt   <module>   s6   			6	K@ºbÜ]†cj”
	J