o
    	i                      @  sD   d dl mZ d dlZd dlmZ G dd deZG dd deZdS )    )annotationsN)	Transformc                   @  s2   e Zd ZdZ	ddddZdddZdddZdS )ExtractHEStainsaQ  Class to extract a target stain from an image, using stain deconvolution (see Note).

    Args:
        tli: transmitted light intensity. Defaults to 240.
        alpha: tolerance in percentile for the pseudo-min (alpha percentile)
            and pseudo-max (100 - alpha percentile). Defaults to 1.
        beta: absorbance threshold for transparent pixels. Defaults to 0.15
        max_cref: reference maximum stain concentrations for Hematoxylin & Eosin (H&E).
            Defaults to (1.9705, 1.0308).

    Note:
        For more information refer to:
        - the original paper: Macenko et al., 2009 http://wwwx.cs.unc.edu/~mn/sites/default/files/macenko2009.pdf
        - the previous implementations:

          - MATLAB: https://github.com/mitkovetta/staining-normalization
          - Python: https://github.com/schaugf/HEnorm_python
          333333?gI+?gx$(~?tlifloatalphabetamax_creftuple | np.ndarrayreturnNonec                 C  s"   || _ || _|| _t|| _d S )N)r	   r   r   nparrayr   )selfr	   r   r   r    r   m/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/apps/pathology/transforms/stain/array.py__init__'   s   zExtractHEStains.__init__image
np.ndarrayc                 C  s  t |tjs
td| dk rtd| dkrtd|d}|jtj	ddd	 }t
|j| jd
| j  }|tj|| jkdd }t|dkrQtdtjt|jjtj	dd\}}||ddddf }t|dddf |dddf }t|| j}t|d| j }	|ddddf tjt|t|fgtj	dj}
|ddddf tjt|	t|	fgtj	dj}|
d |d krtj|
dddf |dddf ftj	dj}|S tj|dddf |
dddf ftj	dj}|S )a!  Perform Stain Deconvolution and return stain matrix for the image.

        Args:
            image: uint8 RGB image to perform stain deconvolution on

        Return:
            he: H&E absorbance matrix for the image (first column is H, second column is E, rows are RGB values)
        $Image must be of type numpy.ndarray.r   &Image should not have negative values.   .Image should not have values greater than 255.   F)copy      ?maxr   )axiszAAll pixels of the input image are below the absorbance threshold.Nr   d   dtype)
isinstancer   ndarray	TypeErrormin
ValueErrorr#   reshapeastypefloat32logclipr	   allr   lenlinalgeighcovTdotarctan2
percentiler   r   cossin)r   r   
absorbanceZabsorbance_hat_ZeigvecsZt_hatphiZmin_phiZmax_phiv_minZv_maxher   r   r   _deconvolution_extract_stain/   s0   

$$::..z,ExtractHEStains._deconvolution_extract_stainc                 C  s"   t |tjs
td| |}|S )zPerform stain extraction.

        Args:
            image: uint8 RGB image to extract stain from

        return:
            target_he: H&E absorbance matrix for the image (first column is H, second column is E, rows are RGB values)
        r   )r(   r   r)   r*   rB   )r   r   	target_her   r   r   __call___   s   	
zExtractHEStains.__call__N)r   r   r   r   )
r	   r
   r   r
   r   r
   r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__r   rB   rD   r   r   r   r   r      s    
0r   c                   @  s0   e Zd ZdZ					ddddZdddZdS )NormalizeHEStainsa  Class to normalize patches/images to a reference or target image stain (see Note).

    Performs stain deconvolution of the source image using the ExtractHEStains
    class, to obtain the stain matrix and calculate the stain concentration matrix
    for the image. Then, performs the inverse Beer-Lambert transform to recreate the
    patch using the target H&E stain matrix provided. If no target stain provided, a default
    reference stain is used. Similarly, if no maximum stain concentrations are provided, a
    reference maximum stain concentrations matrix is used.

    Args:
        tli: transmitted light intensity. Defaults to 240.
        alpha: tolerance in percentile for the pseudo-min (alpha percentile) and
            pseudo-max (100 - alpha percentile). Defaults to 1.
        beta: absorbance threshold for transparent pixels. Defaults to 0.15.
        target_he: target stain matrix. Defaults to ((0.5626, 0.2159), (0.7201, 0.8012), (0.4062, 0.5581)).
        max_cref: reference maximum stain concentrations for Hematoxylin & Eosin (H&E).
            Defaults to [1.9705, 1.0308].

    Note:
        For more information refer to:
        - the original paper: Macenko et al., 2009 http://wwwx.cs.unc.edu/~mn/sites/default/files/macenko2009.pdf
        - the previous implementations:

            - MATLAB: https://github.com/mitkovetta/staining-normalization
            - Python: https://github.com/schaugf/HEnorm_python
    r   r   r   )gX ?gQkw?)g/'?g3ı.n?)gH.?g$?r   r	   r
   r   r   rC   r   r   r   r   c                 C  s8   || _ t|| _t|| _t| j ||| jd| _d S )N)r	   r   r   r   )r	   r   r   rC   r   r   stain_extractor)r   r	   r   r   rC   r   r   r   r   r      s   zNormalizeHEStains.__init__r   r   c                 C  sl  t |tjs
td| dk rtd| dkrtd| |}|j\}}}|	d}|
tjd }t|j| jd| j  }t	|dj}tjj||d	d
d }tjt|dd	d	f dt|dd	d	f dgtjd}	tj|	| jtjd}
tj||
d	d	tjf tjd}tj| jt| j| tjd}d||dk< t	|j||df
tj}|S )zPerform stain normalization.

        Args:
            image: uint8 RGB image/patch to be stain normalized, pixel values between 0 and 255

        Return:
            image_norm: stain normalized image/patch
        r   r   r   r   r   r   r!   r"   N)rcondc   r   r&      r   )r(   r   r)   r*   r+   r,   r#   rL   shaper-   r.   r/   r0   r1   r	   r7   r4   lstsqasarrayr:   divider   newaxismultiplyexprC   r8   uint8)r   r   rA   hwr>   r=   yZconcZmax_conctmpZimage_cZ
image_normr   r   r   rD      s(   


< $zNormalizeHEStains.__call__N)r   r   r   rK   r   )r	   r
   r   r
   r   r
   rC   r   r   r   r   r   rE   )rF   rG   rH   rI   r   rD   r   r   r   r   rJ   o   s    rJ   )
__future__r   numpyr   monai.transforms.transformr   r   rJ   r   r   r   r   <module>   s
   \