U
    h;                     @  sp  d dl mZ d dlZd dlmZ d dlmZ d dlZd dlZ	d dl
mZmZmZmZmZmZmZ d dlmZ d dlmZ d dlm  m  mZ d dlmZ d d	lmZmZ d
ddgZ G dd dZ!G dd de!Z"G dd de!Z#G dd deZ$G dd dZ%eeddddddddZ&dddddddZ'eedddddd d
Z(eeddddd!d"dZ)dS )#    )annotationsN)deepcopy)Literal)add_weightedclipclipped
from_floatget_num_channelspreserve_channel_dimto_float)match_histograms)Protocol)PCA)MONO_CHANNEL_DIMENSIONSNUM_MULTI_CHANNEL_DIMENSIONSfourier_domain_adaptationapply_histogramadapt_pixel_distributionc                   @  sZ   e Zd ZddddZdddddZdddd	d
ZdddddZdddddZdS )
BaseScalerNonereturnc                 C  s"   d | _ d | _d | _d | _d | _d S N)data_mindata_maxmeanvarscaleself r    ]/tmp/pip-unpacked-wheel-e8onvpoz/albumentations/augmentations/domain_adaptation/functional.py__init__   s
    zBaseScaler.__init__
np.ndarrayxr   c                 C  s   t d S r   NotImplementedErrorr   r%   r    r    r!   fit    s    zBaseScaler.fitc                 C  s   t d S r   r&   r(   r    r    r!   	transform#   s    zBaseScaler.transformc                 C  s   |  | | |S r   )r)   r*   r(   r    r    r!   fit_transform&   s    
zBaseScaler.fit_transformc                 C  s   t d S r   r&   r(   r    r    r!   inverse_transform*   s    zBaseScaler.inverse_transformN)__name__
__module____qualname__r"   r)   r*   r+   r,   r    r    r    r!   r      s
   r   c                      sV   e Zd Zdddd fddZdddd	d
ZdddddZdddddZ  ZS )MinMaxScalerg        g      ?ztuple[float, float]r   )feature_ranger   c                   s(   t    |d | _|d | _d | _d S )Nr      )superr"   minmax
data_range)r   r2   	__class__r    r!   r"   /   s    


zMinMaxScaler.__init__r#   r$   c                 C  sB   t j|dd| _t j|dd| _| j| j | _d| j| jdk< d S Nr   Zaxisr3   )npr5   r   r6   r   r7   r(   r    r    r!   r)   5   s    zMinMaxScaler.fitc                 C  sL   | j d ks| jd ks| jd kr&td|| j  | j }|| j| j  | j S NzpThis MinMaxScaler instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.)r   r   r7   
ValueErrorr6   r5   r   r%   Zx_stdr    r    r!   r*   <   s    zMinMaxScaler.transformc                 C  sL   | j d ks| jd ks| jd kr&td|| j | j| j  }|| j | j  S r=   )r   r   r7   r>   r5   r6   r?   r    r    r!   r,   E   s    zMinMaxScaler.inverse_transform)r1   r-   r.   r/   r"   r)   r*   r,   __classcell__r    r    r8   r!   r0   .   s   	r0   c                      sR   e Zd Zdd fddZdddddZdddd	d
ZdddddZ  ZS )StandardScalerr   r   c                   s   t    d S r   )r4   r"   r   r8   r    r!   r"   P   s    zStandardScaler.__init__r#   r$   c                 C  sB   t j|dd| _t j|dd| _t | j| _d| j| jdk< d S r:   )r<   r   r   sqrtr   r(   r    r    r!   r)   S   s    zStandardScaler.fitc                 C  s,   | j d ks| jd krtd|| j  | j S NzrThis StandardScaler instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.r   r   r>   r(   r    r    r!   r*   Z   s
    zStandardScaler.transformc                 C  s,   | j d ks| jd krtd|| j | j  S rD   rE   r(   r    r    r!   r,   b   s
    z StandardScaler.inverse_transformr@   r    r    r8   r!   rB   O   s   rB   c                   @  sV   e Zd ZejdddddZejddddddd	Zejdddddd
dZdS )TransformerInterfacer#   r$   c                 C  s   d S r   r    r(   r    r    r!   r,   l   s    z&TransformerInterface.inverse_transformNznp.ndarray | None)r%   yr   c                 C  s   d S r   r    r   r%   rG   r    r    r!   r)   o   s    zTransformerInterface.fitc                 C  s   d S r   r    rH   r    r    r!   r*   r   s    zTransformerInterface.transform)N)N)r-   r.   r/   abcabstractmethodr,   r)   r*   r    r    r    r!   rF   k   s   rF   c                   @  s   e Zd ZdddddddZdddd	d
ZdddddZdddddZdddddddZedddddZ	dddddZ
dS )DomainAdapterNNrF   r#   ztuple[(None, None)])transformerref_imgcolor_conversionsc                 C  s<   |\| _ | _t|| _|| _t|| _| j| | d S r   )	color_in	color_outr   source_transformertarget_transformerr	   num_channelsr)   flatten)r   rM   rN   rO   r    r    r!   r"   w   s
    

zDomainAdapter.__init__)imgr   c                 C  s   | j d kr|S t|| j S r   )rP   cv2cvtColorr   rV   r    r    r!   to_colorspace   s    zDomainAdapter.to_colorspacec                 C  s$   | j d kr|S tt|tj| j S r   )rQ   rW   rX   r   r<   uint8rY   r    r    r!   from_colorspace   s    
zDomainAdapter.from_colorspacec                 C  s    |  |}t|}|d| jS )N)rZ   r   reshaperT   rY   r    r    r!   rU      s    
zDomainAdapter.flattenint)pixelsheightwidthr   c                 C  sJ   t |ddd d}| jdkr4| |||S | |||| jS )Nr   r3      r[   )r<   r   astyperT   r\   r^   )r   r`   ra   rb   r    r    r!   reconstruct   s    
zDomainAdapter.reconstructr$   c                 C  s   t t | jS r   )r<   signtracecomponents_)r%   r    r    r!   	_pca_sign   s    zDomainAdapter._pca_sign)imager   c                 C  s   |j d d \}}| |}| j| t| jdrht| jdrh| | j| | jkrh| j jd9  _| j|}| j	|}| 
|||S )N   rh   r]   )shaperU   rR   r)   hasattrrS   ri   rh   r*   r,   re   )r   rj   ra   rb   r`   Zrepresentationresultr    r    r!   __call__   s    


zDomainAdapter.__call__N)rL   )r-   r.   r/   r"   rZ   r\   rU   re   staticmethodri   ro   r    r    r    r!   rK   v   s    rK   r#   z&Literal[('pca', 'standard', 'minmax')]float)rV   reftransform_typeweightr   c                 C  s   | j |j krtdt| }t|}||kr4td|dkrPt| } t|}| j|jkrxtj|| jd d tjd}| j }|tj	krt
| tj} t
|tj}tttd|  }t||d}|| tj	}	| tj	d|  |	|  }
|tjkr|
S t|
S )Nz9Input image and reference image must have the same dtype.zFInput image and reference image must have the same number of channels.r3   rk   )dsizeinterpolation)ZpcastandardZminmax)rM   rN   )Zdtyper>   r	   r<   squeezerl   rW   resizeZ
INTER_AREAfloat32r   r[   r   rB   r0   rK   rd   r   )rV   rr   rs   rt   Zimg_num_channelsZref_num_channelsZoriginal_dtyperM   adapterZtransformedrn   r    r    r!   r      s(    


)amp_srcamp_trgbetar   c                 C  s   | j d d }ttt|| }t|\}}|\}}tdt|| tt|| | }	}
tdt|| tt|| | }}||	|
||f | |	|
||f< | S )Nrk   r   )rl   r_   r<   floorr5   
fgeometriccenterr6   )r|   r}   r~   Zimage_shapeZborderZcenter_xZcenter_yra   rb   h1h2Zw1Zw2r    r    r!   low_freq_mutate   s    &&$r   )rV   
target_imgr~   r   c                 C  s<  |  tj}| tj}t|jtkr4tj|dd}t|jtkrPtj|dd}|jd }t|}t|D ]}tj	
|dddd|f }tj	
|dddd|f }	tj	|}
tj	|	}t|
t|
 }}t|}t| ||}tj	|td|  }tj	|}t||dddd|f< ql|S )a
  Apply Fourier Domain Adaptation to the input image using a target image.

    This function performs domain adaptation in the frequency domain by modifying the amplitude
    spectrum of the source image based on the target image's amplitude spectrum. It preserves
    the phase information of the source image, which helps maintain its content while adapting
    its style to match the target image.

    Args:
        img (np.ndarray): The source image to be adapted. Can be grayscale or RGB.
        target_img (np.ndarray): The target image used as a reference for adaptation.
            Should have the same dimensions as the source image.
        beta (float): The adaptation strength, typically in the range [0, 1].
            Higher values result in stronger adaptation towards the target image's style.

    Returns:
        np.ndarray: The adapted image with the same shape and type as the input image.

    Raises:
        ValueError: If the source and target images have different shapes.

    Note:
        - Both input images are converted to float32 for processing.
        - The function handles both grayscale (2D) and color (3D) images.
        - For grayscale images, an extra dimension is added to facilitate uniform processing.
        - The adaptation is performed channel-wise for color images.
        - The output is clipped to the valid range and preserves the original number of channels.

    The adaptation process involves the following steps for each channel:
    1. Compute the 2D Fourier Transform of both source and target images.
    2. Shift the zero frequency component to the center of the spectrum.
    3. Extract amplitude and phase information from the source image's spectrum.
    4. Mutate the source amplitude using the target amplitude and the beta parameter.
    5. Combine the mutated amplitude with the original phase.
    6. Perform the inverse Fourier Transform to obtain the adapted channel.

    The `low_freq_mutate` function (not shown here) is responsible for the actual
    amplitude mutation, focusing on low-frequency components which carry style information.

    Example:
        >>> import numpy as np
        >>> import albumentations as A
        >>> source_img = np.random.rand(100, 100, 3).astype(np.float32)
        >>> target_img = np.random.rand(100, 100, 3).astype(np.float32)
        >>> adapted_img = A.fourier_domain_adaptation(source_img, target_img, beta=0.5)
        >>> assert adapted_img.shape == source_img.shape

    References:
        - "FDA: Fourier Domain Adaptation for Semantic Segmentation"
          (Yang and Soatto, 2020, CVPR)
          https://openaccess.thecvf.com/content_CVPR_2020/papers/Yang_FDA_Fourier_Domain_Adaptation_for_Semantic_Segmentation_CVPR_2020_paper.pdf
    r]   r;   Ny              ?)rd   r<   rz   lenrl   r   Zexpand_dimsZ
zeros_likerangeZfftZfft2ZfftshiftabsZangler   copyZ	ifftshiftexpZifft2real)rV   r   r~   Zsrc_imgZtrg_imgrT   Z
src_in_trgZ
channel_idZfft_srcZfft_trgZfft_src_shiftedZfft_trg_shiftedr|   Zpha_srcr}   Zmutated_ampZfft_src_mutatedZsrc_in_trg_channelr    r    r!   r      s(    6


)rV   reference_imageblend_ratior   c                 C  s   | j dd |j dd kr:tj|| j d | j d fd}t| } t|}t| || jtkrp| j d dkrpdndd}t||| d| S )a  Apply histogram matching to an input image using a reference image and blend the result.

    This function performs histogram matching between the input image and a reference image,
    then blends the result with the original input image based on the specified blend ratio.

    Args:
        img (np.ndarray): The input image to be transformed. Can be either grayscale or RGB.
            Supported dtypes: uint8, float32 (values should be in [0, 1] range).
        reference_image (np.ndarray): The reference image used for histogram matching.
            Should have the same number of channels as the input image.
            Supported dtypes: uint8, float32 (values should be in [0, 1] range).
        blend_ratio (float): The ratio for blending the matched image with the original image.
            Should be in the range [0, 1], where 0 means no change and 1 means full histogram matching.

    Returns:
        np.ndarray: The transformed image after histogram matching and blending.
            The output will have the same shape and dtype as the input image.

    Supported image types:
        - Grayscale images: 2D arrays
        - RGB images: 3D arrays with 3 channels
        - Multispectral images: 3D arrays with more than 3 channels

    Note:
        - If the input and reference images have different sizes, the reference image
          will be resized to match the input image's dimensions.
        - The function uses `match_histograms` from scikit-image for the core histogram matching.
        - The @clipped and @preserve_channel_dim decorators ensure the output is within
          the valid range and maintains the original number of dimensions.

    Example:
        >>> import numpy as np
        >>> from albumentations.augmentations.domain_adaptation_functional import apply_histogram
        >>> input_image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
        >>> reference_image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
        >>> result = apply_histogram(input_image, reference_image, blend_ratio=0.7)
    Nrk   r3   r   )ru   )Zchannel_axis)	rl   rW   ry   r<   rx   r   ndimr   r   )rV   r   r   matchedr    r    r!   r   ?  s    )

)*
__future__r   rI   r   r   typingr   rW   Znumpyr<   Zalbucorer   r   r   r   r	   r
   r   Zskimage.exposurer   Ztyping_extensionsr   Z1albumentations.augmentations.geometric.functionalZaugmentationsZ	geometricZ
functionalr   Z"albumentations.augmentations.utilsr   Zalbumentations.core.typesr   r   __all__r   r0   rB   rF   rK   r   r   r   r   r    r    r    r!   <module>   s<   $!5$]