ó
 ‰\c           @   sß   d  d l  m Z d  d l Z d  d l m Z m Z m Z d  d l m	 Z	 d d l
 m Z d d l m Z d  d l m Z d	 d
 d d g Z d d d „ Z d „  Z d d d d d d „ Z d „  Z d d d d d d „ Z d S(   iÿÿÿÿ(   t   divisionN(   t   fftt   ifftt   fftfreq(   t   interp1di   (   t
   _warp_fast(   t   sart_projection_update(   t   warnt   radont   order_angles_golden_ratiot   iradont   iradon_sartc            s  |  j  d k r t d ƒ ‚ n  | d k r< t j d ƒ } n  | d k r[ t d ƒ t } n  | rÒt |  j ƒ d } t j	 d |  j d … d |  j d … f \ } } | |  j d d d | |  j d d d } | | d k } t j
 | |  d k Bƒ st d ƒ n  g  } x£ d D]› } |  j | t |  j ƒ k r¢|  j | t |  j ƒ }	 | j t t t j |	 d ƒ ƒ t t j |	 d ƒ t |  j ƒ ƒ ƒ ƒ q| j t d ƒ ƒ qWt | ƒ } |  | }
 nt j d ƒ t |  j ƒ } g  |  j D] } t t j | | ƒ ƒ ^ qø} g  t |  j | ƒ D] \ } } | | d ^ q0} g  |  j D] } | d ^ qZ} g  t | | ƒ D] \ } } | | ^ q€} g  t | | ƒ D] \ } } | | | f ^ q¬} t j |  | d d	 d
 d ƒ}
 |
 j d |
 j d k st ‚ t j |
 j d t | ƒ f ƒ } |
 j d d } t j d d | g d d | g d d d g g ƒ ‰  t j d d | g d d | g d d d g g ƒ ‰ ‡  ‡ f d †  } xR t t | ƒ ƒ D]> } t |
 | | | ƒ ƒ } | j d ƒ | d d … | f <qÌW| S(   s  
    Calculates the radon transform of an image given specified
    projection angles.

    Parameters
    ----------
    image : array_like, dtype=float
        Input image. The rotation axis will be located in the pixel with
        indices ``(image.shape[0] // 2, image.shape[1] // 2)``.
    theta : array_like, dtype=float, optional (default np.arange(180))
        Projection angles (in degrees).
    circle : boolean, optional
        Assume image is zero outside the inscribed circle, making the
        width of each projection (the first dimension of the sinogram)
        equal to ``min(image.shape)``.
        The default behavior (None) is equivalent to False.

    Returns
    -------
    radon_image : ndarray
        Radon transform (sinogram).  The tomography rotation axis will lie
        at the pixel index ``radon_image.shape[0] // 2`` along the 0th
        dimension of ``radon_image``.

    References
    ----------
    .. [1] AC Kak, M Slaney, "Principles of Computerized Tomographic
           Imaging", IEEE Press 1988.
    .. [2] B.R. Ramesh, N. Srinivasa, K. Rajgopal, "An Algorithm for Computing
           the Discrete Radon Transform With Some Applications", Proceedings of
           the Fourth IEEE Region 10 International Conference, TENCON '89, 1989

    Notes
    -----
    Based on code of Justin K. Romberg
    (http://www.clear.rice.edu/elec431/projects96/DSP/bpanalysis.html)

    i   s   The input image must be 2-Di´   s[   The default of `circle` in `skimage.transform.radon` will change to `True` in version 0.15.i    i   sE   Radon transform: image must be zero outside the reconstruction circlet   modet   constantt   constant_valuesc            s}   t  j |  ƒ } t  j t  j | ƒ t  j | ƒ d g t  j | ƒ t  j | ƒ d g d d d g g ƒ } ˆ j | ƒ j ˆ  ƒ S(   Ni    i   (   t   npt   deg2radt   arrayt   cost   sint   dot(   t   thetat   Tt   R(   t   shift0t   shift1(    s@   lib/python2.7/site-packages/skimage/transform/radon_transform.pyt   build_rotationh   s
    $N(   i    i   (   t   ndimt
   ValueErrort   NoneR   t   arangeR   t   Falset   mint   shapet   ogridt   allt   appendt   slicet   intt   ceilt   tuplet   sqrtt   maxt   zipt   padt   AssertionErrort   zerost   lenR   t   rangeR   t   sum(   t   imageR   t   circlet   radiust   c0t   c1t   reconstruction_circlet   slicest   dt   excesst   padded_imaget   diagonalt   sR,   t   pt
   new_centert
   old_centert   oct   nct
   pad_beforet   pbt	   pad_widtht   radon_imaget   centerR   t   it   rotated(    (   R   R   s@   lib/python2.7/site-packages/skimage/transform/radon_transform.pyR      s^    '
	3/3 ,2	 "#c         C   s‘   t  t j t j d ƒ |  j d ƒ ƒ } | |  j d } |  j d d } | d } | | } | | | f d f } t j |  | d d d d ƒS(   Ni   i    R   R   R   (   i    i    (   R&   R   R'   R)   R!   R,   (   t   sinogramR<   R,   R@   R?   RC   RE   (    (    s@   lib/python2.7/site-packages/skimage/transform/radon_transform.pyt   _sinogram_circle_to_squareu   s    )

t   rampt   linearc      
   C   sÞ  |  j  d k r t d ƒ ‚ n  | d k rW |  j \ } } t j d d | d t ƒ} n t j | ƒ } t | ƒ |  j d k rŽ t d ƒ ‚ n  d$ } | | k r³ t d | ƒ ‚ n  | sÿ | rÏ |  j d } qÿ t	 t j
 t j |  j d d d ƒ ƒ ƒ } n  | d k rt d ƒ t } n  | r3t |  ƒ }  n  t j d | }	 t d t	 d t j t j d |  j d ƒ ƒ ƒ ƒ }
 d |
 |  j d f d% f } t j |  | d d d d ƒ} t |
 ƒ j d d ƒ } d t j | } d t j | ƒ } | d k r nÚ | d k r6| d t j | d ƒ | d | d )n¤ | d k rX| t j | ƒ 9} n‚ | d k r†| d d t j | d ƒ 9} nT | d k r´| d t j | d ƒ d 9} n& | d k rÊd | (n t d | ƒ ‚ t | d d ƒ| } t j t | d d ƒƒ } | d |  j d … d d … f } t j | | f ƒ } |  j d d } t j d | … d | … f \ } } | t	 | ƒ d } | t	 | ƒ d } xç t t | ƒ ƒ D]Ó } | t j |	 | ƒ | t j |	 | ƒ } t j | j d ƒ | } | d k r@t j | | | d d … | f d d d d ƒ} n= t  | | d d … | f d  | d! t d" d ƒ} | | ƒ } | | 7} q´W| rÅ| d } | d | d | d k } d# | | <n  | t j d t |	 ƒ S(&   s	  
    Inverse radon transform.

    Reconstruct an image from the radon transform, using the filtered
    back projection algorithm.

    Parameters
    ----------
    radon_image : array_like, dtype=float
        Image containing radon transform (sinogram). Each column of
        the image corresponds to a projection along a different angle. The
        tomography rotation axis should lie at the pixel index
        ``radon_image.shape[0] // 2`` along the 0th dimension of
        ``radon_image``.
    theta : array_like, dtype=float, optional
        Reconstruction angles (in degrees). Default: m angles evenly spaced
        between 0 and 180 (if the shape of `radon_image` is (N, M)).
    output_size : int
        Number of rows and columns in the reconstruction.
    filter : str, optional (default ramp)
        Filter used in frequency domain filtering. Ramp filter used by default.
        Filters available: ramp, shepp-logan, cosine, hamming, hann.
        Assign None to use no filter.
    interpolation : str, optional (default 'linear')
        Interpolation method used in reconstruction. Methods available:
        'linear', 'nearest', and 'cubic' ('cubic' is slow).
    circle : boolean, optional
        Assume the reconstructed image is zero outside the inscribed circle.
        Also changes the default output_size to match the behaviour of
        ``radon`` called with ``circle=True``.
        The default behavior (None) is equivalent to False.

    Returns
    -------
    reconstructed : ndarray
        Reconstructed image. The rotation axis will be located in the pixel
        with indices
        ``(reconstructed.shape[0] // 2, reconstructed.shape[1] // 2)``.

    References
    ----------
    .. [1] AC Kak, M Slaney, "Principles of Computerized Tomographic
           Imaging", IEEE Press 1988.
    .. [2] B.R. Ramesh, N. Srinivasa, K. Rajgopal, "An Algorithm for Computing
           the Discrete Radon Transform With Some Applications", Proceedings of
           the Fourth IEEE Region 10 International Conference, TENCON '89, 1989

    Notes
    -----
    It applies the Fourier slice theorem to reconstruct an image by
    multiplying the frequency domain of the filter with the FFT of the
    projection data. This algorithm is called filtered back projection.

    i   s   The input image must be 2-Di    i´   t   endpointi   sP   The given ``theta`` does not match the number of projections in ``radon_image``.RM   t   nearestt   cubics   Unknown interpolation: %sg       @s\   The default of `circle` in `skimage.transform.iradon` will change to `True` in version 0.15.g     €f@i@   R   R   R   iÿÿÿÿRL   s   shepp-logant   cosinet   hamminggHáz®Gá?gq=
×£pÝ?t   hanns   Unknown filter: %st   axisNt   leftt   rightt   kindt   bounds_errort
   fill_valueg        (   RM   RO   RP   (   i    i    (!   R   R   R   R!   R   t   linspaceR   t   asarrayR/   R&   t   floorR)   R   RK   t   piR*   R'   t   log2R,   R   t   reshapet   absR   R   R   t   realR   R.   t   mgridR0   R   t   interpR   (   RF   R   t   output_sizet   filtert   interpolationR3   t   mt   nt   interpolation_typest   tht   projection_size_paddedRE   t   imgt   ft   omegat   fourier_filtert
   projectiont   radon_filteredt   reconstructedt	   mid_indext   Xt   Yt   xprt   yprRH   t   tt   xt   backprojectedt   interpolantR4   R7   (    (    s@   lib/python2.7/site-packages/skimage/transform/radon_transform.pyR
      s€    8
	6*""
#%,""
c         #   s  d ‰  ‡  f d †  } t  t j |  ƒ ƒ } | j d ƒ } |  | } | Vˆ  d t j d ƒ d d } x¯ | r| | ˆ  } t j |  | | ƒ } | d } | t | ƒ k r¶ d n | } | | |  | | ƒ }	 | | |  | | ƒ }
 |	 |
 k  r| j | ƒ Vqj | j | ƒ Vqj Wd S(   s…  
    Order angles to reduce the amount of correlated information
    in subsequent projections.

    Parameters
    ----------
    theta : 1D array of floats
        Projection angles in degrees. Duplicate angles are not allowed.

    Returns
    -------
    indices_generator : generator yielding unsigned integers
        The returned generator yields indices into ``theta`` such that
        ``theta[indices]`` gives the approximate golden ratio ordering
        of the projections. In total, ``len(theta)`` indices are yielded.
        All non-negative integers < ``len(theta)`` are yielded exactly once.

    Notes
    -----
    The method used here is that of the golden ratio introduced
    by T. Kohler.

    References
    ----------
    .. [1] Kohler, T. "A projection access scheme for iterative
           reconstruction based on the golden section." Nuclear Science
           Symposium Conference Record, 2004 IEEE. Vol. 6. IEEE, 2004.
    .. [2] Winkelmann, Stefanie, et al. "An optimal radial profile order
           based on the Golden Ratio for time-resolved MRI."
           Medical Imaging, IEEE Transactions on 26.1 (2007): 68-76.
    i´   c            s,   |  | } t  t | ˆ  ƒ t | ˆ  ƒ ƒ S(   N(   R    R`   (   t   at   bt
   difference(   t   interval(    s@   lib/python2.7/site-packages/skimage/transform/radon_transform.pyt   angle_distance1  s    
i    i   i   i   N(   t   listR   t   argsortt   popR)   t   searchsortedR/   (   R   R€   t	   remainingt   indext   anglet   angle_incrementt   insert_pointt   index_belowt   index_abovet   distance_belowt   distance_above(    (   R   s@   lib/python2.7/site-packages/skimage/transform/radon_transform.pyR	     s"     
	
g333333Ã?c   	      C   sB  |  j  d k r t d ƒ ‚ n  |  j d |  j d f } | d k ri t j d d |  j d d t ƒ} n< | j |  j d f k r¥ t d | j |  j d f ƒ ‚ n  | d k rÌ t j | d t j ƒ} n+ | j | k r÷ t d	 | j | f ƒ ‚ n  | d k r(t j |  j d f d t j ƒ} n< | j |  j d f k rdt d
 | j |  j d f ƒ ‚ n  | d k	 r´t	 | ƒ d k r‘t d ƒ ‚ n  t | d ƒ t | d ƒ f } n  t | ƒ } x{ t
 | ƒ D]m } t | | | |  d d … | f | | ƒ } | | | 7} | d k	 rÍt j | | d | d ƒ } qÍqÍW| S(   s«  
    Inverse radon transform

    Reconstruct an image from the radon transform, using a single iteration of
    the Simultaneous Algebraic Reconstruction Technique (SART) algorithm.

    Parameters
    ----------
    radon_image : 2D array, dtype=float
        Image containing radon transform (sinogram). Each column of
        the image corresponds to a projection along a different angle. The
        tomography rotation axis should lie at the pixel index
        ``radon_image.shape[0] // 2`` along the 0th dimension of
        ``radon_image``.
    theta : 1D array, dtype=float, optional
        Reconstruction angles (in degrees). Default: m angles evenly spaced
        between 0 and 180 (if the shape of `radon_image` is (N, M)).
    image : 2D array, dtype=float, optional
        Image containing an initial reconstruction estimate. Shape of this
        array should be ``(radon_image.shape[0], radon_image.shape[0])``. The
        default is an array of zeros.
    projection_shifts : 1D array, dtype=float
        Shift the projections contained in ``radon_image`` (the sinogram) by
        this many pixels before reconstructing the image. The i'th value
        defines the shift of the i'th column of ``radon_image``.
    clip : length-2 sequence of floats
        Force all values in the reconstructed tomogram to lie in the range
        ``[clip[0], clip[1]]``
    relaxation : float
        Relaxation parameter for the update step. A higher value can
        improve the convergence rate, but one runs the risk of instabilities.
        Values close to or higher than 1 are not recommended.

    Returns
    -------
    reconstructed : ndarray
        Reconstructed image. The rotation axis will be located in the pixel
        with indices
        ``(reconstructed.shape[0] // 2, reconstructed.shape[1] // 2)``.

    Notes
    -----
    Algebraic Reconstruction Techniques are based on formulating the tomography
    reconstruction problem as a set of linear equations. Along each ray,
    the projected value is the sum of all the values of the cross section along
    the ray. A typical feature of SART (and a few other variants of algebraic
    techniques) is that it samples the cross section at equidistant points
    along the ray, using linear interpolation between the pixel values of the
    cross section. The resulting set of linear equations are then solved using
    a slightly modified Kaczmarz method.

    When using SART, a single iteration is usually sufficient to obtain a good
    reconstruction. Further iterations will tend to enhance high-frequency
    information, but will also often increase the noise.

    References
    ----------
    .. [1] AC Kak, M Slaney, "Principles of Computerized Tomographic
           Imaging", IEEE Press 1988.
    .. [2] AH Andersen, AC Kak, "Simultaneous algebraic reconstruction
           technique (SART): a superior implementation of the ART algorithm",
           Ultrasonic Imaging 6 pp 81--94 (1984)
    .. [3] S Kaczmarz, "AngenÃ¤herte auflÃ¶sung von systemen linearer
           gleichungen", Bulletin International de lâ€™Academie Polonaise des
           Sciences et des Lettres 35 pp 355--357 (1937)
    .. [4] Kohler, T. "A projection access scheme for iterative
           reconstruction based on the golden section." Nuclear Science
           Symposium Conference Record, 2004 IEEE. Vol. 6. IEEE, 2004.
    .. [5] Kaczmarz' method, Wikipedia,
           http://en.wikipedia.org/wiki/Kaczmarz_method
    i   s#   radon_image must be two dimensionali    i´   i   RN   sA   Shape of theta (%s) does not match the number of projections (%d)t   dtypesF   Shape of image (%s) does not match first dimension of radon_image (%s)sM   Shape of projection_shifts (%s) does not match the number of projections (%d)s    clip must be a length-2 sequenceN(   R   R   R!   R   R   RZ   R   R.   t   floatR/   R	   R   t   clip(	   RF   R   R2   t   projection_shiftsR   t
   relaxationt   reconstructed_shapet   angle_indext   image_update(    (    s@   lib/python2.7/site-packages/skimage/transform/radon_transform.pyR   I  s>    I%%#$(   t
   __future__R    t   numpyR   t   scipy.fftpackR   R   R   t   scipy.interpolateR   t	   _warps_cyR   t   _radon_transformR   t   warningsR   t   __all__R   R   RK   R
   R	   R   (    (    (    s@   lib/python2.7/site-packages/skimage/transform/radon_transform.pyt   <module>   s   g	
	:	