def writeImage(image, filename):
     """
     Writes given image to filename
     :param image: vtkImageData
     :param filename: full path to file where image should be saved
     :return:
     """
     writer = vtk.vtkPNGWriter()
     writer.SetInput(image)
     writer.SetFileName(filename)
     writer.Write()
Beispiel #2
0
 def writeImage(self, imageName, outFile):
     writer = vtk.vtkPNGWriter()
     writer.SetInput(self.images[imageName])
     writer.SetFileName(outFile)
     writer.Write()
Beispiel #3
0
def render_normals(renWin,
                   renderer,
                   camera,
                   data_dir,
                   data_dir_name,
                   num_im,
                   out_dir,
                   use_mesh,
                   object_dir,
                   mesh='meshed_scene.ply',
                   keyword=None):
    #setup rendering enviornment
    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 = "original_log.lcmlog.ply" if glob.glob(
                data_dir +
                "/original_log.lcmlog.ply") else "trimmed_log.lcmlog.ply"
            mesher = mesh_wrapper.Mesh(out_dir=data_dir)
            status = mesher.mesh_cloud(out)
            print status
            #blocks until done
        mapper = vtk.vtkPolyDataMapper()
        #shading
        #set_material_prop(actor)
        set_shader_input(mapper)
        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,
                                 shader=set_shader_input)

    #setup rendering enviornment
    windowToColorBuffer = vtk.vtkWindowToImageFilter()
    windowToColorBuffer.SetInput(renWin)
    windowToColorBuffer.SetInputBufferTypeToRGB()

    #setup camera calibration
    common.set_up_camera_params(camera)

    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()

            windowToColorBuffer.Modified()
            windowToColorBuffer.Update()

            #write out depth image
            imageWriter.SetFileName(out_dir + str(i).zfill(10) + "_" +
                                    data_dir_name + "_normal_ground_truth.png")
            imageWriter.SetInputConnection(windowToColorBuffer.GetOutputPort())
            imageWriter.Write()
        except (IOError):
            break
    renderer.RemoveAllViewProps()
    renWin.Render()
Beispiel #4
0
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()
Beispiel #5
0
 def writeImage(self, imageName, outFile):
     writer = vtk.vtkPNGWriter()
     writer.SetInput(self.images[imageName])
     writer.SetFileName(outFile)
     writer.Write()