o
    Wbf                     @   sZ   d dl Zd dlZd dlZejfddZG dd deZG dd deZG dd	 d	eZ	dS )
    Nc                 C   s.  t | d j}|d |d kr|d |d kr| S ddg}|d |d  |d< |d |d  |d< t|}t||d  |d< t||d  |d< tj| d t|ddd |d| d< tj| d t|ddd tjd| d< tj| d 	t
jt|ddd tjd| d< | d 	t| d< t|S )	zRezise the sample to ensure the given size. Keeps aspect ratio.

    Args:
        sample (dict): sample
        size (tuple): image size

    Returns:
        tuple: new size
    	disparityr      imageNinterpolationmask)listshapemaxmathceilcv2resizetupleINTER_NEARESTastypenpfloat32bool)samplesizeimage_interpolation_methodr
   scale r   PC:\Users\Kristjan\AppData\Local\Programs\Python\MiDaS-master\midas\transforms.pyapply_min_size   s,   
 


r   c                   @   s@   e Zd ZdZddddejfddZdd
dZdd Zdd Z	d	S )Resizez1Resize sample to given size (width, height).
    TFr   lower_boundc                 C   s.   || _ || _|| _|| _|| _|| _|| _dS )a  Init.

        Args:
            width (int): desired output width
            height (int): desired output height
            resize_target (bool, optional):
                True: Resize the full sample (image, mask, target).
                False: Resize image only.
                Defaults to True.
            keep_aspect_ratio (bool, optional):
                True: Keep the aspect ratio of the input sample.
                Output sample might not have the given width and height, and
                resize behaviour depends on the parameter 'resize_method'.
                Defaults to False.
            ensure_multiple_of (int, optional):
                Output width and height is constrained to be multiple of this parameter.
                Defaults to 1.
            resize_method (str, optional):
                "lower_bound": Output will be at least as large as the given size.
                "upper_bound": Output will be at max as large as the given size. (Output size might be smaller than given size.)
                "minimal": Scale as least as possible.  (Output size might be smaller than given size.)
                Defaults to "lower_bound".
        N)_Resize__width_Resize__height_Resize__resize_target_Resize__keep_aspect_ratio_Resize__multiple_of_Resize__resize_method#_Resize__image_interpolation_method)selfwidthheightresize_targetkeep_aspect_ratioensure_multiple_ofresize_methodr   r   r   r   __init__4   s   !
zResize.__init__r   Nc                 C   sp   t || j | j t}|d ur$||kr$t || j | j t}||k r6t || j | j t}|S N)r   roundr#   r   intfloorr   )r&   xmin_valmax_valyr   r   r   constrain_to_multiple_of^   s   zResize.constrain_to_multiple_ofc                 C   sJ  | j | }| j| }| jrK| jdkr||kr|}n2|}n/| jdkr+||k r(|}n#|}n | jdkrBtd| td| k r?|}n|}n	td| j d| jdkrh| j|| | j d}| j|| | jd}||fS | jdkr| j|| | j d}| j|| | jd}||fS | jdkr| || }| || }||fS td| j d)	Nr   upper_boundminimalr   zresize_method z not implemented)r3   )r4   )r    r   r"   r$   abs
ValueErrorr6   )r&   r'   r(   Zscale_heightZscale_width
new_height	new_widthr   r   r   get_sizei   sN   










	
zResize.get_sizec                 C   s   |  |d jd |d jd \}}tj|d ||f| jd|d< | jrfd|v r7tj|d ||ftjd|d< d|v rJtj|d ||ftjd|d< tj|d tj	||ftjd|d< |d t
|d< |S )Nr   r   r   r   r   depthr   )r=   r
   r   r   r%   r!   r   r   r   r   r   )r&   r   r'   r(   r   r   r   __call__   s4   



zResize.__call__)r   N)
__name__
__module____qualname____doc__r   
INTER_AREAr-   r6   r=   r?   r   r   r   r   r   0   s    

*9r   c                   @       e Zd ZdZdd Zdd ZdS )NormalizeImagez*Normlize image by given mean and std.
    c                 C   s   || _ || _d S r.   Z_NormalizeImage__meanZ_NormalizeImage__std)r&   meanstdr   r   r   r-      s   
zNormalizeImage.__init__c                 C   s   |d | j  | j |d< |S )Nr   rG   )r&   r   r   r   r   r?      s   zNormalizeImage.__call__Nr@   rA   rB   rC   r-   r?   r   r   r   r   rF      s    rF   c                   @   rE   )PrepareForNetz/Prepare sample for usage as network input.
    c                 C   s   d S r.   r   )r&   r   r   r   r-      s   zPrepareForNet.__init__c                 C   s   t |d d}t |t j|d< d|v r*|d t j|d< t |d |d< d|v r=|d t j}t ||d< d|v rP|d t j}t ||d< |S )Nr   )   r   r   r   r   r>   )r   	transposeascontiguousarrayr   r   )r&   r   r   r   r>   r   r   r   r?      s   zPrepareForNet.__call__NrJ   r   r   r   r   rK      s    rK   )
numpyr   r   r   rD   r   objectr   rF   rK   r   r   r   r   <module>   s    * 