def removeOutliers(points, radius, c='k', alpha=1, legend=None): ''' Remove outliers from a cloud of points within radius search ''' isactor = False if isinstance(points, vtk.vtkActor): isactor = True poly = vu.polydata(points) else: src = vtk.vtkPointSource() src.SetNumberOfPoints(len(points)) src.Update() vpts = src.GetOutput().GetPoints() for i, p in enumerate(points): vpts.SetPoint(i, p) poly = src.GetOutput() removal = vtk.vtkRadiusOutlierRemoval() vu.setInput(removal, poly) removal.SetRadius(radius) removal.SetNumberOfNeighbors(5) removal.GenerateOutliersOff() removal.Update() rpoly = removal.GetOutput() print("# of removed outlier points: ", removal.GetNumberOfPointsRemoved(), '/', poly.GetNumberOfPoints()) outpts = [] for i in range(rpoly.GetNumberOfPoints()): outpts.append(list(rpoly.GetPoint(i))) outpts = np.array(outpts) if not isactor: return outpts actor = vs.points(outpts, c=c, alpha=alpha, legend=legend) return actor # return same obj for concatenation
def showPointCloud(modelNodeID): model = slicer.mrmlScene.GetNodeByID(modelNodeID) polydata = model.GetPolyData() # Reuse the locator locator = vtk.vtkStaticPointLocator() locator.SetDataSet(polydata) locator.BuildLocator() # Remove isolated points removal = vtk.vtkRadiusOutlierRemoval() removal.SetInputData(polydata) removal.SetLocator(locator) removal.SetRadius(10) removal.SetNumberOfNeighbors(2) removal.GenerateOutliersOn() # Time execution timer = vtk.vtkTimerLog() timer.StartTimer() removal.Update() timer.StopTimer() time = timer.GetElapsedTime() print("Time to remove points: {0}".format(time)) print(" Number removed: {0}".format(removal.GetNumberOfPointsRemoved()), " (out of: {}".format(polydata.GetNumberOfPoints())) # First output are the non-outliers remMapper = vtk.vtkPointGaussianMapper() remMapper.SetInputConnection(removal.GetOutputPort()) remMapper.EmissiveOff() remMapper.SetScaleFactor(0.0) remActor = vtk.vtkActor() remActor.SetMapper(remMapper) lm = slicer.app.layoutManager() tdWidget = lm.threeDWidget(0) tdView = tdWidget.threeDView() rWin = tdView.renderWindow() rCollection = rWin.GetRenderers() renderer = rCollection.GetItemAsObject(0) # Add the actors to the renderer, set the background and size # renderer.AddActor(remActor)
# create pipeline # points = vtk.vtkBoundedPointSource() points.SetNumberOfPoints(NPts) points.ProduceRandomScalarsOn() points.ProduceCellOutputOff() points.Update() # Reuse the locator locator = vtk.vtkStaticPointLocator() locator.SetDataSet(points.GetOutput()) locator.BuildLocator() # Remove isolated points removal = vtk.vtkRadiusOutlierRemoval() removal.SetInputConnection(points.GetOutputPort()) removal.SetLocator(locator) removal.SetRadius(0.1) removal.SetNumberOfNeighbors(2) removal.GenerateOutliersOn() # Time execution timer = vtk.vtkTimerLog() timer.StartTimer() removal.Update() timer.StopTimer() time = timer.GetElapsedTime() print("Time to remove points: {0}".format(time)) print(" Number removed: {0}".format(removal.GetNumberOfPointsRemoved())) print(" Original number of points: {0}".format(NPts))