o
    oiS_                     @  s6  d Z ddlmZ ddlmZ ddlZddlmZ ddlm	Z	m
Z
mZ ddlmZmZmZ ddlmZ ddlmZ d	d
lmZmZmZmZmZ d	dlmZmZ d	dlmZmZ 	 g dZ 			dDdEddZ!	dFdGd d!Z"dHdId"d#Z#dHdId$d%Z$	&dJdKd,d-Z%	dHdLd1d2Z&G d3d4 d4e	Z'	5dMdNd<d=Z(dOdBdCZ)dS )Pz8Module containing operators to work on RGB-Depth images.    )annotations)OptionalN)ModuleTensortensor)KORNIA_CHECKKORNIA_CHECK_IS_TENSORKORNIA_CHECK_SHAPE)spatial_gradient)create_meshgrid   )PinholeCamera	cam2pixel	pixel2camproject_pointsunproject_points)normalize_pixel_coordinates normalize_points_with_intrinsics)convert_points_to_homogeneoustransform_points)	DepthWarperdepth_from_disparitydepth_from_plane_equationdepth_to_3ddepth_to_3d_v2depth_to_normals
depth_warpunproject_meshgridwarp_frame_depthFheightintwidthcamera_matrixr   normalize_pointsbooldeviceOptional[torch.device]dtypeOptional[torch.dtype]returnc           
      C  s`   t |g d t| |d||d }|ddddf }t||}t|}	|r.tj|	ddd}	|	S )aS  Compute a 3d point per pixel given its depth value and the camera intrinsics.

    .. tip::

        This function should be used in conjunction with :py:func:`kornia.geometry.depth.depth_to_3d_v2` to cache
        the meshgrid computation when warping multiple frames with the same camera intrinsics.

    Args:
        height: height of image.
        width: width of image.
        camera_matrix: tensor containing the camera intrinsics with shape :math:`(3, 3)`.
        normalize_points: whether to normalize the pointcloud. This must be set to `True` when the depth is
          represented as the Euclidean ray length from the camera position.
        device: device to place the result on.
        dtype: dtype of the result.

    Return:
        tensor with a 3d point per pixel of the same resolution as the input :math:`(*, H, W, 3)`.

    *3r,   F)normalized_coordinatesr%   r'   N   dimp)r	   r   squeezer   r   
kornia_ops	normalize)
r   r!   r"   r#   r%   r'   	points_uvcamera_matrix_tmp	points_xy
points_xyz r:   I/home/ubuntu/.local/lib/python3.10/site-packages/kornia/geometry/depth.pyr   3   s   

r   depthxyz_gridOptional[Tensor]c                 C  sb   t | g d t |g d | jdd \}}|p#t||||| j| j}t |g d || d  S )a  Compute a 3d point per pixel given its depth value and the camera intrinsics.

    .. note::

        This is an alternative implementation of :py:func:`kornia.geometry.depth.depth_to_3d`
        that does not require the creation of a meshgrid.

    Args:
        depth: image tensor containing a depth value per pixel with shape :math:`(*, H, W)`.
        camera_matrix: tensor containing the camera intrinsics with shape :math:`(*, 3, 3)`.
        normalize_points: whether to normalise the pointcloud. This must be set to `True` when the depth is
          represented as the Euclidean ray length from the camera position.
        xyz_grid: explicit xyz point values.

    Return:
        tensor with a 3d point per pixel of the same resolution as the input :math:`(*, H, W, 3)`.

    Example:
        >>> depth = torch.rand(4, 4)
        >>> K = torch.eye(3).repeat(2,1,1)
        >>> depth_to_3d_v2(depth, K).shape
        torch.Size([2, 4, 4, 3])

    r+   HWr*   N)r+   r@   rA   r,   ).N)r	   shaper   r%   r'   )r<   r"   r#   r=   r   r!   r9   r:   r:   r;   r   d   s   r   c           
      C  s   t |  t | t| g d t|g d | j\}}}}t||dd}|| j| j}| dddd}|d	d	d	d	f }t||||d
}	|	ddddS )a  Compute a 3d point per pixel given its depth value and the camera intrinsics.

    .. note::

        This is an alternative implementation of `depth_to_3d` that does not require the creation of a meshgrid.
        In future, we will support only this implementation.

    Args:
        depth: image tensor containing a depth value per pixel with shape :math:`(B, 1, H, W)`.
        camera_matrix: tensor containing the camera intrinsics with shape :math:`(B, 3, 3)`.
        normalize_points: whether to normalise the pointcloud. This must be set to `True` when the depth is
          represented as the Euclidean ray length from the camera position.

    Return:
        tensor with a 3d point per pixel of the same resolution as the input :math:`(B, 3, H, W)`.

    Example:
        >>> depth = torch.rand(1, 1, 4, 4)
        >>> K = torch.eye(3)[None]
        >>> depth_to_3d(depth, K).shape
        torch.Size([1, 3, 4, 4])

    B1r@   rA   rE   r,   r,   Fr-   r   r/      r   N)r5   )	r   r	   rC   r   tor%   r'   permuter   )
r<   r"   r#   _r   r!   	points_2dpoints_depthr7   	points_3dr:   r:   r;   r      s   r   c                 C  s   t |  t | t| g d t|g d t| ||}t|}|dddddf |dddddf }}tjj||dd}tj|dddS )	a  Compute the normal surface per pixel.

    Args:
        depth: image tensor containing a depth value per pixel with shape :math:`(B, 1, H, W)`.
        camera_matrix: tensor containing the camera intrinsics with shape :math:`(B, 3, 3)`.
        normalize_points: whether to normalize the pointcloud. This must be set to `True` when the depth is
        represented as the Euclidean ray length from the camera position.

    Return:
        tensor with a normal surface vector per pixel of the same resolution as the input :math:`(B, 3, H, W)`.

    Example:
        >>> depth = torch.rand(1, 1, 4, 4)
        >>> K = torch.eye(3)[None]
        >>> depth_to_normals(depth, K).shape
        torch.Size([1, 3, 4, 4])

    rD   rG   Nr   r   r1   r/   r0   )	r   r	   r   r
   torchlinalgcrossr4   r5   )r<   r"   r#   xyz	gradientsabnormalsr:   r:   r;   r      s   .r   :0yE>plane_normalsplane_offsetsr6   epsfloatc                 C  s   t | ddg t |ddg t |g d t |g d t||}t|}| d}tj|| dd}t|}	|	|k }
t|
|t| |}|| }|S )	a  Compute depth values from plane equations and pixel coordinates.

    Args:
        plane_normals (Tensor): Plane normal vectors of shape (B, 3).
        plane_offsets (Tensor): Plane offsets of shape (B, 1).
        points_uv (Tensor): Pixel coordinates of shape (B, N, 2).
        camera_matrix (Tensor): Camera intrinsic matrix of shape (B, 3, 3).
        eps: epsilon for numerical stability.

    Returns:
        Tensor: Computed depth values at the given pixels, shape (B, N).

    rE   r,   rF   )rE   N2rG   r   r.   rP   )	r	   r   r   	unsqueezerQ   sumabswheresign)rZ   r[   r6   r"   r\   r8   raysplane_normals_expdenom	denom_abs	zero_maskr<   r:   r:   r;   r      s   


r   	image_src	depth_dstsrc_trans_dstc                 C  s   t | g d t |g d t |g d t |g d t|||}|dddd}t|d	d	d	f |}|d	d	d	d	f }t||}|jd
d	 \}	}
t||	|
}tj| |ddS )ad  Warp a tensor from a source to destination frame by the depth in the destination.

    Compute 3d points from the depth, transform them using given transformation, then project the point cloud to an
    image plane.

    Args:
        image_src: image tensor in the source frame with shape :math:`(B,D,H,W)`.
        depth_dst: depth tensor in the destination frame with shape :math:`(B,1,H,W)`.
        src_trans_dst: transformation matrix from destination to source with shape :math:`(B,4,4)`.
        camera_matrix: tensor containing the camera intrinsics with shape :math:`(B,3,3)`.
        normalize_points: whether to normalize the pointcloud. This must be set to ``True`` when the depth
           is represented as the Euclidean ray length from the camera position.

    Return:
        the warped tensor in the source frame with shape :math:`(B,3,H,W)`.

    )rE   Dr@   rA   rD   )rE   4rn   rG   r   r/   rI   r   NrB   Talign_corners)	r	   r   rK   r   r   rC   r   r4   map_coordinates)rj   rk   rl   r"   r#   points_3d_dstpoints_3d_srcr7   points_2d_srcr   r!   points_2d_src_normr:   r:   r;   r   
  s   
r   c                      sj   e Zd ZdZ			d'd( fddZed)ddZd*ddZd+ddZd,dd Z	d-d"d#Z
d.d%d&Z  ZS )/r   aC  Warp a patch by depth.

    .. math::
        P_{src}^{\{dst\}} = K_{dst} * T_{src}^{\{dst\}}

        I_{src} = \\omega(I_{dst}, P_{src}^{\{dst\}}, D_{src})

    Args:
        pinholes_dst: the pinhole models for the destination frame.
        height: the height of the image to warp.
        width: the width of the image to warp.
        mode: interpolation mode to calculate output values ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: interpolation flag.

    bilinearzerosTpinhole_dstr   r   r    r!   modestrpadding_moderp   r$   r)   Nonec                   sn   t    || _|| _|| _|| _d| _|| _t|t	s%t
dt| || _d | _d | _| ||| _d S )Ngư>z+Expected pinhole_dst as PinholeCamera, got )super__init__r!   r   ry   r{   r\   rp   
isinstancer   	TypeErrortype_pinhole_dst_pinhole_src_dst_proj_src_create_meshgridgrid)selfrx   r   r!   ry   r{   rp   	__class__r:   r;   r~   J  s   
	
zDepthWarper.__init__r   c                 C  s   t | |dd}t|S )NFrH   )r   r   )r   r!   r   r:   r:   r;   r   f  s   zDepthWarper._create_meshgridpinhole_srcc                 C  s  t | jturtdt | j t |tur tdt | |jjdd }|jj}|jj}tj	d||dj
g |ddR   }tj	d||dj
g |ddR   }|jdddddf }|jdddddf }t|d	d}	t|	 |}
|	|dddddf< |
|dddddf< | jjdddddf }| jjdddddf }t||	}t||
| }||dddddf< ||dddddf< t| jj|}|| _|| _| S )
zCCompute the projection matrix from the source to destination frame.zDMember self._pinhole_dst expected to be of class PinholeCamera. Got z@Argument pinhole_src expected to be of class PinholeCamera. Got NrB      r%   r'   .rI   r.   )r   r   r   r   
extrinsicsrC   r%   r'   rQ   eyeexpand
contiguous	transposematmul
intrinsicsr   r   )r   r   batch_shaper%   r'   inv_extrdst_trans_srcsrc_rmatsrc_tvecinv_rmatinv_tvecdst_rmatdst_tveccomposed_rmatcomposed_tvecdst_proj_srcr:   r:   r;   compute_projection_matrixk  s6   ((z%DepthWarper.compute_projection_matrixxr]   yinvdc           	      C  s   | j d u s
| jd u rtdt|g|g|gdggg| j j| j jd}t| j |}d|d d df  }|d d df | }|d d df | }t	||gdS )N'Please, call compute_projection_matrix.      ?r   r/   r   r   )
r   r   
ValueErrorr   r%   r'   rQ   r   r4   concatenate)	r   r   r   r   pointflowz_x_yr:   r:   r;   _compute_projection  s   (zDepthWarper._compute_projectionc                 C  s"  | j du r	tdd}| jd }| jd }d| d| f}tj|||d dg|||d dgg| j j| j jddd	d}| j }t
||}d|dddf  }|dddf | }	|dddf | }
tj|	|
fd	d
}tj|dddf |dddf  dddd }|| }td| S )a  Compute the inverse depth step for sub pixel accurate sampling of the depth cost volume, per camera.

        Szeliski, Richard, and Daniel Scharstein. "Symmetric sub-pixel stereo matching." European Conference on Computer
        Vision. Springer Berlin Heidelberg, 2002.
        Nz=Expected Tensor, but got None Type from the projection matrixg{Gz?r/   r   r   r   )r'   r%   r.   rP   )r2   r1   g       @g      ?)r   RuntimeErrorr!   r   rQ   r   r'   r%   r   r`   r   stacknormmin)r   delta_dcenter_xcenter_yinvdspointsprojr   zsxsysxysdxydxddr:   r:   r;   compute_subpixel_step  s.   



0z!DepthWarper.compute_subpixel_step	depth_srcc           
      C  s   | j du s
| jdu rtdt|jdkrtd|j |j\}}}}|j}|j}| jj||d	|ddd}t
|| j j||d|}t|| j j||d}t|| j| j}	|	S )aT  Compute a grid for warping a given the depth from the reference pinhole camera.

        The function `compute_projection_matrix` has to be called beforehand in order to have precomputed the relative
        projection matrices encoding the relative pose and the intrinsics between the reference and a non reference
        camera.
        Nr   r   z7Input depth_src has to be in the shape of Bx1xHxW. Got r   r.   )r   r   r   lenrC   r%   r'   r   rJ   r   r   intrinsics_inverser   r   r   r!   )
r   r   
batch_sizerL   r%   r'   pixel_coordscam_coords_srcpixel_coords_srcpixel_coords_src_normr:   r:   r;   	warp_grid  s    zDepthWarper.warp_grid	patch_dstc                 C  s    t j|| || j| j| jdS )a  Warp a tensor from destination frame to reference given the depth in the reference frame.

        Args:
            depth_src: the depth in the reference frame. The tensor must have a shape :math:`(B, 1, H, W)`.
            patch_dst: the patch in the destination frame. The tensor must have a shape :math:`(B, C, H, W)`.

        Return:
            the warped patch from destination frame to reference.

        Shape:
            - Output: :math:`(N, C, H, W)` where C = number of channels.

        Example:
            >>> # pinholes camera models
            >>> pinhole_dst = PinholeCamera(torch.randn(1, 4, 4), torch.randn(1, 4, 4),
            ... torch.tensor([32]), torch.tensor([32]))
            >>> pinhole_src = PinholeCamera(torch.randn(1, 4, 4), torch.randn(1, 4, 4),
            ... torch.tensor([32]), torch.tensor([32]))
            >>> # create the depth warper, compute the projection matrix
            >>> warper = DepthWarper(pinhole_dst, 32, 32)
            >>> _ = warper.compute_projection_matrix(pinhole_src)
            >>> # warp the destination frame to reference by depth
            >>> depth_src = torch.ones(1, 1, 32, 32)  # Nx1xHxW
            >>> image_dst = torch.rand(1, 3, 32, 32)  # NxCxHxW
            >>> image_src = warper(depth_src, image_dst)  # NxCxHxW

        )ry   r{   rp   )r4   rq   r   ry   r{   rp   )r   r   r   r:   r:   r;   forward  s   zDepthWarper.forward)rv   rw   T)rx   r   r   r    r!   r    ry   rz   r{   rz   rp   r$   r)   r|   )r   r    r!   r    r)   r   )r   r   r)   r   )r   r]   r   r]   r   r]   r)   r   )r)   r   )r   r   r)   r   )r   r   r   r   r)   r   )__name__
__module____qualname____doc__r~   staticmethodr   r   r   r   r   r   __classcell__r:   r:   r   r;   r   7  s    

,

%$r   Trx   r   r   r   r   rp   c                 C  s$   t | |||d}|| |||S )a   Warp a tensor from destination frame to reference given the depth in the reference frame.

    See :class:`~kornia.geometry.warp.DepthWarper` for details.

    Example:
        >>> # pinholes camera models
        >>> pinhole_dst = PinholeCamera(torch.randn(1, 4, 4), torch.randn(1, 4, 4),
        ... torch.tensor([32]), torch.tensor([32]))
        >>> pinhole_src = PinholeCamera(torch.randn(1, 4, 4), torch.randn(1, 4, 4),
        ... torch.tensor([32]), torch.tensor([32]))
        >>> # warp the destination frame to reference by depth
        >>> depth_src = torch.ones(1, 1, 32, 32)  # Nx1xHxW
        >>> image_dst = torch.rand(1, 3, 32, 32)  # NxCxHxW
        >>> image_src = depth_warp(pinhole_dst, pinhole_src, depth_src, image_dst, 32, 32)  # NxCxHxW

    ro   )r   r   )rx   r   r   r   r   r!   rp   warperr:   r:   r;   r     s   

r   	disparitybaselinefloat | Tensorfocalc                 C  s   t | dt|  d t| g d tt|ttfdt|  tt|ttfdt|  t|tr;t|dg t|trFt|dg || | d  S )a  Compute depth from disparity.

    Args:
        disparity: Disparity tensor of shape :math:`(*, H, W)`.
        baseline: float/tensor containing the distance between the two lenses.
        focal: float/tensor containing the focal length.

    Return:
        Depth map of the shape :math:`(*, H, W)`.

    Example:
        >>> disparity = torch.rand(4, 1, 4, 4)
        >>> baseline = torch.rand(1)
        >>> focal = torch.rand(1)
        >>> depth_from_disparity(disparity, baseline, focal).shape
        torch.Size([4, 1, 4, 4])

    z*Input disparity type is not a Tensor. Got .r?   z7Input baseline should be either a float or Tensor. Got z4Input focal should be either a float or Tensor. Got rF   rY   )r   r   r	   r   r   r]   r   )r   r   r   r:   r:   r;   r   3  s   

r   )FNN)r   r    r!   r    r"   r   r#   r$   r%   r&   r'   r(   r)   r   )FN)
r<   r   r"   r   r#   r$   r=   r>   r)   r   )F)r<   r   r"   r   r#   r$   r)   r   )rY   )rZ   r   r[   r   r6   r   r"   r   r\   r]   r)   r   )rj   r   rk   r   rl   r   r"   r   r#   r$   r)   r   )T)rx   r   r   r   r   r   r   r   r   r    r!   r    rp   r$   r)   r   )r   r   r   r   r   r   r)   r   )*r   
__future__r   typingr   rQ   kornia.corecorer4   r   r   r   kornia.core.checkr   r   r	   kornia.filters.sobelr
   kornia.utilsr   camerar   r   r   r   r   conversionsr   r   rR   r   r   __all__r   r   r   r   r   r   r   r   r   r:   r:   r:   r;   <module>   s>   2+.&)- a#