facebookresearch / pytorch3d

PyTorch3D is FAIR's library of reusable components for deep learning with 3D data
https://pytorch3d.org/
Other
8.86k stars 1.32k forks source link

Outlier removal for point cloud #511

Open hiyyg opened 3 years ago

hiyyg commented 3 years ago

🚀 Feature and Motivation

Outlier removal is very important for practical usage,libraries like open3d/pcl have CPU support of outlier removal (e.g., http://www.open3d.org/docs/latest/tutorial/Advanced/pointcloud_outlier_removal.html). It would be nice if pytorch3d could add a GPU support of this feature.

bibbygoodwin commented 2 years ago

Hi @hiyyg,

I was wondering the same thing, and I believe we can achieve outlier removal using the knn_points function in pytorch3d.ops: https://pytorch3d.readthedocs.io/en/latest/modules/ops.html#pytorch3d.ops.knn_points

The mean K-nn distances of each point give a good idea of whether they are outliers or not. This is the basis of outlier removal in PCL: https://pcl.readthedocs.io/en/latest/statistical_outlier.html

pytorch3d.ops.knn_points is written to find K nearest neighbours between points in a first pointcloud and a second pointcloud. It can still be used to find the K-nn's of each point in a single pointcloud, though, like this:

import torch
from pytorch3d.ops import utils as oputil
from pytorch3d.ops import knn_points
from pytorch3d.structures.pointclouds import Pointclouds

pcd = Pointclouds(torch.rand((10000, 3))[None,...])
nn_dists, nn_idx, nn = knn_points(oputil.convert_pointclouds_to_tensor(pcd)[0],
                                  oputil.convert_pointclouds_to_tensor(pcd)[0],
                                  K=10)

threshold = 0.1 # you could estimate this based on assuming a Gaussian over K-nn distances as in PCL
pcd_filtered = Pointclouds(pcd[nn_dists[0,:,1:].mean(1) < threshold][None,...])