def computeDepthImageAndPointCloud(depthBuffer, colorBuffer, camera): """ Returns depth image and pointcloud as vtkImageData and vtkPolyData :param depthBuffer: OpenGL depth buffer :type depthBuffer: :param colorBuffer: OpenGL color buffer :type colorBuffer: :param camera: vtkCamera instance that was used to render the scene :type camera: vtkCamera instance :return: :rtype: vtkImageData, vtkPolyData, numpy array """ depthImage = vtk.vtkImageData() pts = vtk.vtkPoints() ptColors = vtk.vtkUnsignedCharArray() vtk.vtkDepthImageUtils.DepthBufferToDepthImage(depthBuffer, colorBuffer, camera, depthImage, pts, ptColors) pts = vnp.numpy_support.vtk_to_numpy(pts.GetData()) polyData = vnp.numpyToPolyData(pts, createVertexCells=True) ptColors.SetName('rgb') polyData.GetPointData().AddArray(ptColors) return depthImage, polyData, pts
def computeDepthImageAndPointCloud(depthBuffer, colorBuffer, camera): ''' Input args are an OpenGL depth buffer and color buffer as vtkImageData objects, and the vtkCamera instance that was used to render the scene. The function returns returns a depth image and a point cloud as vtkImageData and vtkPolyData. ''' depthImage = vtk.vtkImageData() pts = vtk.vtkPoints() ptColors = vtk.vtkUnsignedCharArray() vtk.vtkDepthImageUtils.DepthBufferToDepthImage(depthBuffer, colorBuffer, camera, depthImage, pts, ptColors) pts = vnp.numpy_support.vtk_to_numpy(pts.GetData()) polyData = vnp.numpyToPolyData(pts, createVertexCells=True) ptColors.SetName('rgb') polyData.GetPointData().AddArray(ptColors) return depthImage, polyData
def computeDepthImageAndPointCloud(depthBuffer, colorBuffer, camera): ''' Input args are an OpenGL depth buffer and color buffer as vtkImageData objects, and the vtkCamera instance that was used to render the scene. The function returns returns a depth image and a point cloud as vtkImageData and vtkPolyData. ''' depthImage = vtk.vtkImageData() pts = vtk.vtkPoints() ptColors = vtk.vtkUnsignedCharArray() vtk.vtkDepthImageUtils.DepthBufferToDepthImage(depthBuffer, colorBuffer, camera, depthImage, pts, ptColors) pts = vnp.numpy_support.vtk_to_numpy(pts.GetData()) polyData = vnp.numpyToPolyData(pts, createVertexCells=True) ptColors.SetName('rgb') polyData.GetPointData().AddArray(ptColors) return depthImage, polyData, pts
def render_depth(renWin, renderer, camera, data_dir, data_dir_name, num_im, out_dir, use_mesh, object_dir, mesh='meshed_scene.ply', keyword=None): actor = vtk.vtkActor() filter1 = vtk.vtkWindowToImageFilter() imageWriter = vtk.vtkPNGWriter() scale = vtk.vtkImageShiftScale() if use_mesh: #use meshed version of scene if not glob.glob(data_dir + "/" + mesh): out = None if glob.glob(data_dir + "/original_log.lcmlog.ply"): out = "original_log.lcmlog.ply" elif glob.glob(data_dir + "/trimmed_log.lcmlog.ply"): out = "trimmed_log.lcmlog.ply" elif glob.glob('*.ply'): out = glob.glob('*.ply')[0] else: return mesher = mesh_wrapper.Mesh(out_dir=data_dir) status = mesher.mesh_cloud(out) print status #blocks until done mapper = vtk.vtkPolyDataMapper() fileReader = vtk.vtkPLYReader() fileReader.SetFileName(data_dir + "/" + mesh) mapper.SetInputConnection(fileReader.GetOutputPort()) actor.SetMapper(mapper) renderer.AddActor(actor) else: #import just the objects objects = common.Objects(data_dir, object_dir) objects.loadObjectMeshes("/registration_result.yaml", renderer, keyword=keyword) #setup filters filter1.SetInput(renWin) filter1.SetMagnification(1) filter1.SetInputBufferTypeToZBuffer() windowToColorBuffer = vtk.vtkWindowToImageFilter() windowToColorBuffer.SetInput(renWin) windowToColorBuffer.SetInputBufferTypeToRGB() scale.SetOutputScalarTypeToUnsignedShort() scale.SetScale(1000) poses = common.CameraPoses(data_dir + "/posegraph.posegraph") for i in range(1, num_im + 1): try: utimeFile = open( data_dir + "/images/" + str(i).zfill(10) + "_utime.txt", 'r') utime = int(utimeFile.read()) #update camera transform cameraToCameraStart = poses.getCameraPoseAtUTime(utime) t = cameraToCameraStart common.setCameraTransform(camera, t) renWin.Render() #update filters filter1.Modified() filter1.Update() windowToColorBuffer.Modified() windowToColorBuffer.Update() #extract depth image depthImage = vtk.vtkImageData() pts = vtk.vtkPoints() ptColors = vtk.vtkUnsignedCharArray() vtk.vtkDepthImageUtils.DepthBufferToDepthImage( filter1.GetOutput(), windowToColorBuffer.GetOutput(), camera, depthImage, pts, ptColors) scale.SetInputData(depthImage) scale.Update() #write out depth image imageWriter.SetFileName(out_dir + str(i).zfill(10) + "_" + data_dir_name + "_depth_ground_truth.png") imageWriter.SetInputConnection(scale.GetOutputPort()) imageWriter.Write() except (IOError): break renderer.RemoveAllViewProps() renWin.Render()
t = cameraToCameraStart common.setCameraTransform(camera, t) common.setCameraTransform(camera1, t) renWin.Render() renSource.Update() #update filters filter1.Modified() filter1.Update() windowToColorBuffer.Modified() windowToColorBuffer.Update() #extract depth image depthImage = vtk.vtkImageData() pts = vtk.vtkPoints() ptColors = vtk.vtkUnsignedCharArray() vtk.vtkDepthImageUtils.DepthBufferToDepthImage( filter1.GetOutput(), windowToColorBuffer.GetOutput(), camera, depthImage, pts, ptColors) scale.SetInputData(depthImage) scale.Update() #source = np.flip(np.reshape(numpy_support.vtk_to_numpy(renSource.GetOutput().GetPointData().GetScalars()),(480,640)),axis=0) #modify this for simulated depth source = np.flip(np.reshape( numpy_support.vtk_to_numpy( renSource.GetOutput().GetPointData().GetScalars()), (480, 640)), axis=0)