o
    i                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ ddddejdfdej	fdd	Z
	dd
ejdedejfddZ	ddejdedejfddZ	ddejdedejfddZdS )    N)	rearrange)logger   FSpectraldepthc           
      C   sL  |   } |    | dk}d| |  | |< |du r)| dkr!d}nt| | |}|du r@| dkr6d}n
t| | d| }||krL|d }|d }tj| }| | ||  dd} d|  } || d dddddddddd	f }	|tjkr|	d d
 tj}	n|tj	ks|tj
kr|	d }	ntd| |r|	||fS |	S )an  
    Visualize a depth map using a colormap.

    Args:
        depth: Input depth map array
        depth_min: Minimum depth value for normalization. If None, uses percentile
        depth_max: Maximum depth value for normalization. If None, uses percentile
        percentile: Percentile for min/max computation if not provided
        ret_minmax: Whether to return min/max depth values
        ret_type: Return array type (uint8 or float)
        cmap: Matplotlib colormap name to use

    Returns:
        Colored depth visualization as numpy array
        If ret_minmax=True, also returns depth_min and depth_max
    r      N
   d   gư>F)bytes   g     o@zInvalid return type: )copysumnp
percentile
matplotlib	colormapsclipuint8astypefloat32float64
ValueError)
r   Z	depth_minZ	depth_maxr   Z
ret_minmaxret_typecmapZ
valid_maskcmZimg_colored_np r   >/data/cameron/da3_repo/src/depth_anything_3/utils/visualize.pyvisualize_depth   s6   
,


r   result	color_mapreturnc              
   C   s   |  ddd  d | }z| | dk dd  d | }W n! ttfyJ } ztd|  t	
|}W Y d}~nd}~ww |  } | | ||  } t| |S )z"
    Color-map the depth map.
    Ni $ gGz?r   g{Gz?z%No valid depth values found. Reason: )reshapefloatquantilelogtoRuntimeErrorr   r   errortorch
zeros_likeapply_color_map_to_image)r   r   ZfarZnearer   r   r   vis_depth_map_tensorT   s   &,
r-   infernoxc                 C   sN   t j|}||   jddd  dd df }tj	|| j
tjdS )Nr   r   )minmax.r   )devicedtype)r   r   get_cmapr#   detachr   cpunumpyr)   tensorr2   r   )r/   r   r   mappedr   r   r   apply_color_mapf   s   .r:   imagec                 C   s   t | |} t| dS )Nz... h w c -> ... c h w)r:   r   )r;   r   r   r   r   r+   s   s   

r+   )r   )r.   )r   r7   r   r)   einopsr   depth_anything_3.utils.loggerr   r   ndarrayr   Tensorstrr-   r:   r+   r   r   r   r   <module>   sL   
?

