pytorch3d.renderer.points.rasterize_points

rasterize_points

pytorch3d.renderer.points.rasterize_points.rasterize_points(pointclouds, image_size: int | List[int] | Tuple[int, int] = 256, radius: float | List | Tuple | Tensor = 0.01, points_per_pixel: int = 8, bin_size: int | None = None, max_points_per_bin: int | None = None)[source]

Each pointcloud is rasterized onto a separate image of shape (H, W) if image_size is a tuple or (image_size, image_size) if it is an int.

If the desired image size is non square (i.e. a tuple of (H, W) where H != W) the aspect ratio needs special consideration. There are two aspect ratios to be aware of:

  • the aspect ratio of each pixel

  • the aspect ratio of the output image

The camera can be used to set the pixel aspect ratio. In the rasterizer, we assume square pixels, but variable image aspect ratio (i.e rectangle images).

In most cases you will want to set the camera aspect ratio to 1.0 (i.e. square pixels) and only vary the image_size (i.e. the output image dimensions in pix

Parameters:
  • pointclouds – A Pointclouds object representing a batch of point clouds to be rasterized. This is a batch of N pointclouds, where each point cloud can have a different number of points; the coordinates of each point are (x, y, z). The coordinates are expected to be in normalized device coordinates (NDC): [-1, 1]^3 with the camera at (0, 0, 0); In the camera coordinate frame the x-axis goes from right-to-left, the y-axis goes from bottom-to-top, and the z-axis goes from back-to-front.

  • image_size – Size in pixels of the output image to be rasterized. Can optionally be a tuple of (H, W) in the case of non square images.

  • radius (Optional) – The radius (in NDC units) of the disk to be rasterized. This can either be a float in which case the same radius is used for each point, or a torch.Tensor of shape (N, P) giving a radius per point in the batch.

  • points_per_pixel (Optional) – We will keep track of this many points per pixel, returning the nearest points_per_pixel points along the z-axis

  • bin_size – Size of bins to use for coarse-to-fine rasterization. Setting bin_size=0 uses naive rasterization; setting bin_size=None attempts to set it heuristically based on the shape of the input. This should not affect the output, but can affect the speed of the forward pass.

  • max_points_per_bin – Only applicable when using coarse-to-fine rasterization (bin_size > 0); this is the maximum number of points allowed within each bin. This should not affect the output values, but can affect the memory usage in the forward pass.

Returns:

3-element tuple containing

  • idx: int32 Tensor of shape (N, image_size, image_size, points_per_pixel) giving the indices of the nearest points at each pixel, in ascending z-order. Concretely idx[n, y, x, k] = p means that points[p] is the kth closest point (along the z-direction) to pixel (y, x) - note that points represents the packed points of shape (P, 3). Pixels that are hit by fewer than points_per_pixel are padded with -1.

  • zbuf: Tensor of shape (N, image_size, image_size, points_per_pixel) giving the z-coordinates of the nearest points at each pixel, sorted in z-order. Concretely, if idx[n, y, x, k] = p then zbuf[n, y, x, k] = points[n, p, 2]. Pixels hit by fewer than points_per_pixel are padded with -1

  • dists2: Tensor of shape (N, image_size, image_size, points_per_pixel) giving the squared Euclidean distance (in NDC units) in the x/y plane for each point closest to the pixel. Concretely if idx[n, y, x, k] = p then dists[n, y, x, k] is the squared distance between the pixel (y, x) and the point (points[n, p, 0], points[n, p, 1]). Pixels hit with fewer than points_per_pixel are padded with -1.

In the case that image_size is a tuple of (H, W) then the outputs will be of shape (N, H, W, …).

pytorch3d.renderer.points.rasterize_points.rasterize_points_python(pointclouds, image_size: int | Tuple[int, int] = 256, radius: float | Tensor = 0.01, points_per_pixel: int = 8)[source]

Naive pure PyTorch implementation of pointcloud rasterization.

Inputs / Outputs: Same as above