Python VTK - eiichiromomma/CVMLAB GitHub Wiki

(Python) VTK

PythonからVTKを使う

C++よりもソースが見易い

VTKとOpenNI2を使ったPoint Cloud

取り敢えずDepthを拾ってそれらしい場所に白点を打つのみ。 Stereoのアナグリフは確認。QuadroでもOpenGLなStereoscopicは今のところ成功してない。

# VTKONIPC
# package requirements: vtk, primesense, numpy
# by Eiichiro Momma

import vtk
from primesense import openni2
import numpy as np
  
class vtkTimerCallback():
    def __init__(self):
        self.timer_count = 0
        self.depth_stream = 0
        self.polyData = vtk.vtkPolyData()
 
    def execute(self,obj,event):
        #print self.timer_count
        self.polyData.Initialize()
        frame = self.depth_stream.read_frame()
        frame_data = frame.get_buffer_as_uint16()
        depth_array = np.ndarray((frame.height,frame.width),dtype=np.uint16,buffer=frame_data)
        depth_array = np.flipud(np.fliplr(depth_array))
        validMask = depth_array != 0
        points = vtk.vtkPoints()
        carray = vtk.vtkCellArray()
        colors = vtk.vtkUnsignedCharArray()
        colors.SetName('Colors')
        colors.SetNumberOfComponents(3)
        offset_x = -frame.width/2
        offset_y = -frame.height/2
        fx = 580.0
        fy = 580.0
        x_array = np.arange(depth_array.shape[1])
        y_array = np.arange(depth_array.shape[0])
        x_array = (x_array+offset_x)*2./fx
        y_array = (y_array+offset_y)*2./fy
        x_map = np.tile(x_array,(depth_array.shape[0],1))
        y_map = np.tile(y_array,(depth_array.shape[1],1)).T
        x_map = x_map*depth_array
        y_map = y_map*depth_array
        for y in range(depth_array.shape[0]):
            for x in range(depth_array.shape[1]):
                if validMask[y,x]:
                    dp = depth_array[y,x]
                    id = points.InsertNextPoint(x_map[y,x],y_map[y,x],dp)
                    carray.InsertNextCell(1)
                    carray.InsertCellPoint(id)
                    colors.InsertNextTuple3(255,255,255)
        self.polyData.SetPoints(points)
        self.polyData.SetVerts(carray)
        self.polyData.GetCellData().SetScalars(colors);
        self.polyData.Modified()
        self.polyData.Update()
        iren = obj
        iren.GetRenderWindow().Render()
        self.timer_count += 1
 
 
def main():
    cb = vtkTimerCallback()
    #openni2
    openni2.initialize()
    dev = openni2.Device.open_any()
    cb.depth_stream = dev.create_depth_stream()
    cb.depth_stream.start()
    
 
    #Create a mapper and actor
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInput(cb.polyData)
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
 
    # Setup a renderer, render window, and interactor
    renderer = vtk.vtkRenderer()
    renderWindow = vtk.vtkRenderWindow()
 
    renderWindow.AddRenderer(renderer);
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)
 
    #Add the actor to the scene
    renderer.AddActor(actor)
    renderer.SetBackground(0,0,0) # Background color white
    
    # use StereoScopic
    renderWindow.SetFullScreen(1)
    renderWindow.SetStereoCapableWindow(1)
    renderWindow.StereoRenderOn()
    #renderWindow.SetStereoTypeToAnaglyph()
    #renderWindow.SetStereoTypeToInterlaced()
    #renderWindow.SetStereoTypeToDresden()
    renderWindow.SetStereoTypeToCrystalEyes()
    renderWindow.StereoUpdate()
    
    #set Camera
    cam = vtk.vtkCamera()
    cam.SetViewUp(0,1,0)
    cam.SetPosition(0,0,-2000)
    cam.SetFocalPoint(0,0,-1500)
    cam.ComputeViewPlaneNormal()
    renderer.SetActiveCamera(cam)
    renderer.ResetCamera()
    renderer.ResetCameraClippingRange(-320,320,-240,240,0,4096)
 
    #Render and interact
    renderWindow.SetSize(1680,1050)
    renderWindow.Render()
 
    # Initialize must be called prior to creating timer events.
    renderWindowInteractor.Initialize()
 
    # Sign up to receive TimerEvent
    cb.actor = actor
    renderWindowInteractor.AddObserver('TimerEvent', cb.execute)
    timerId = renderWindowInteractor.CreateRepeatingTimer(33);
 
    #start the interaction and timer
    renderWindowInteractor.Start()
    openni2.unload()
 
if __name__ == '__main__':
    main()