o  i @sDddlmZddlZddlmZGdddeZGdddeZdS)) annotationsN) Transformc@s2eZdZdZ ddddZdddZdddZdS)ExtractHEStainsaQClass 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 +?gx $(~?tlifloatalphabetamax_creftuple | np.ndarrayreturnNonecCs"||_||_||_t||_dS)N)r r r nparrayr )selfr r r r rm/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/apps/pathology/transforms/stain/array.py__init__'szExtractHEStains.__init__image np.ndarrayc Cs t|tjs td|dkrtd|dkrtd|d}|jtj ddd }t |j |j d |j  }|tj ||jkd d }t|dkrQtd tjt|jjtj dd\}}||ddd df}t|ddd f|dddf}t||j}t|d|j} |ddd dftjt|t|fgtj dj} |ddd dftjt| t| fgtj dj} | d| dkrtj| dddf| dddfftj dj} | Stj| dddf| dddfftj dj} | 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.Nrddtype) isinstancerndarray TypeErrormin ValueErrorr#reshapeastypefloat32logclipr allr lenlinalgeighcovTdotarctan2 percentiler rcossin) rr absorbanceZabsorbance_hat_ZeigvecsZt_hatphiZmin_phiZmax_phiv_minZv_maxherrr_deconvolution_extract_stain/s0     $$::..z,ExtractHEStains._deconvolution_extract_staincCs"t|tjs td||}|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(rr)r*rB)rr target_herrr__call___s  zExtractHEStains.__call__N)rrrr) r r r r r r r rrrrrrr)__name__ __module__ __qualname____doc__rrBrDrrrrrs  0rc@s0eZdZdZ     ddddZdddZdS)NormalizeHEStainsaClass 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 rrr)gX?gQkw?)g/' ?g3ı.n?)gH.?g$?rr r r r rCrr rrcCs8||_t||_t||_t|j|||jd|_dS)N)r r r r )r rrrCr rstain_extractor)rr r r rCr rrrrs  zNormalizeHEStains.__init__rrc Cslt|tjs td|dkrtd|dkrtd||}|j\}}}| d}| tj d}t |j |jd|j }t |dj}tjj||d d d}tjt|dd d fd t|d d d fd gtj d } tj| |jtj d } tj|| d d tjftj d } tj|jt|j|  tj d } d| | dk<t | j||df tj} | 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 rrrrrrr!r"N)rcondcrr&r)r(rr)r*r+r,r#rLshaper-r.r/r0r1r r7r4lstsqasarrayr:divider newaxismultiplyexprCr8uint8) rrrAhwr>r=yZconcZmax_conctmpZimage_cZ image_normrrrrDs(      < $ zNormalizeHEStains.__call__N)rrrrKr) r r r r r r rCrr rrrrE)rFrGrHrIrrDrrrrrJos  rJ) __future__rnumpyrmonai.transforms.transformrrrJrrrrs  \