def volumeRender(img, tf=[],spacing=[1.0,1.0,1.0]):
importer = numpy2VTK(img,spacing)
# Transfer Functions
opacity_tf = vtk.vtkPiecewiseFunction()
color_tf = vtk.vtkColorTransferFunction()
if len(tf) == 0:
tf.append([img.min(),0,0,0,0])
tf.append([img.max(),1,1,1,1])
for p in tf:
color_tf.AddRGBPoint(p[0], p[1], p[2], p[3])
opacity_tf.AddPoint(p[0], p[4])
# working on the GPU
# volMapper = vtk.vtkGPUVolumeRayCastMapper()
# volMapper.SetInputConnection(importer.GetOutputPort())
# # The property describes how the data will look
# volProperty = vtk.vtkVolumeProperty()
# volProperty.SetColor(color_tf)
# volProperty.SetScalarOpacity(opacity_tf)
# volProperty.ShadeOn()
# volProperty.SetInterpolationTypeToLinear()
# working on the CPU
volMapper = vtk.vtkVolumeRayCastMapper()
compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
compositeFunction.SetCompositeMethodToInterpolateFirst()
volMapper.SetVolumeRayCastFunction(compositeFunction)
volMapper.SetInputConnection(importer.GetOutputPort())
# The property describes how the data will look
volProperty = vtk.vtkVolumeProperty()
volProperty.SetColor(color_tf)
volProperty.SetScalarOpacity(opacity_tf)
volProperty.ShadeOn()
volProperty.SetInterpolationTypeToLinear()
# Do the lines below speed things up?
# pix_diag = 5.0
# volMapper.SetSampleDistance(pix_diag / 5.0)
# volProperty.SetScalarOpacityUnitDistance(pix_diag)
vol = vtk.vtkVolume()
vol.SetMapper(volMapper)
vol.SetProperty(volProperty)
return [vol]
def render_volume_data(self, vtk_img_data):
# Create transfer mapping scalar value to opacity
opacity_transfer_function = vtk.vtkPiecewiseFunction()
opacity_transfer_function.AddPoint(0, 0.0)
opacity_transfer_function.AddPoint(50, 0.0)
opacity_transfer_function.AddPoint(100, 0.8)
opacity_transfer_function.AddPoint(1200, 0.8)
# Create transfer mapping scalar value to color
color_transfer_function = vtk.vtkColorTransferFunction()
color_transfer_function.AddRGBPoint(0, 0.0, 0.0, 0.0)
color_transfer_function.AddRGBPoint(50, 0.0, 0.0, 0.0)
color_transfer_function.AddRGBPoint(100, 1.0, 0.0, 0.0)
color_transfer_function.AddRGBPoint(1200, 1.0, 0.0, 0.0)
# The property describes how the data will look
volume_property = vtk.vtkVolumeProperty()
volume_property.SetColor(color_transfer_function)
volume_property.SetScalarOpacity(opacity_transfer_function)
volume_property.ShadeOff()
volume_property.SetInterpolationTypeToLinear()
# The mapper / ray cast function know how to render the data
compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
volume_mapper = vtk.vtkVolumeRayCastMapper()
volume_mapper.SetVolumeRayCastFunction(compositeFunction)
if vtk.VTK_MAJOR_VERSION <= 5:
volume_mapper.SetInput(vtk_img_data)
else:
volume_mapper.SetInputData(vtk_img_data)
volume_mapper.SetBlendModeToMaximumIntensity()
# The volume holds the mapper and the property and
# can be used to position/orient the volume
volume = vtk.vtkVolume()
volume.SetMapper(volume_mapper)
volume.SetProperty(volume_property)
self.ren.AddVolume(volume)
self.ren.ResetCamera()
self.iren.Initialize()
#.........这里部分代码省略.........
#print 'reslice GetOutputOrigin', reslice.GetOutputOrigin()
#print 'reslice GetOutputExtent',reslice.GetOutputExtent()
#print 'reslice GetOutputSpacing',reslice.GetOutputSpacing()
changeFilter=vtk.vtkImageChangeInformation()
changeFilter.SetInput(reslice.GetOutput())
#changeFilter.SetInput(im)
if center_origin:
changeFilter.SetOutputOrigin(-vol.shape[0]/2.0+0.5,-vol.shape[1]/2.0+0.5,-vol.shape[2]/2.0+0.5)
print 'ChangeFilter ', changeFilter.GetOutputOrigin()
opacity = vtk.vtkPiecewiseFunction()
for i in range(opacitymap.shape[0]):
opacity.AddPoint(opacitymap[i,0],opacitymap[i,1])
color = vtk.vtkColorTransferFunction()
for i in range(colormap.shape[0]):
color.AddRGBPoint(colormap[i,0],colormap[i,1],colormap[i,2],colormap[i,3])
if(maptype==0):
property = vtk.vtkVolumeProperty()
property.SetColor(color)
property.SetScalarOpacity(opacity)
if trilinear:
property.SetInterpolationTypeToLinear()
else:
prop.SetInterpolationTypeToNearest()
if info:
print('mapper VolumeTextureMapper2D')
mapper = vtk.vtkVolumeTextureMapper2D()
if affine == None:
mapper.SetInput(im)
else:
#mapper.SetInput(reslice.GetOutput())
mapper.SetInput(changeFilter.GetOutput())
if (maptype==1):
property = vtk.vtkVolumeProperty()
property.SetColor(color)
property.SetScalarOpacity(opacity)
property.ShadeOn()
if trilinear:
property.SetInterpolationTypeToLinear()
else:
prop.SetInterpolationTypeToNearest()
if iso:
isofunc=vtk.vtkVolumeRayCastIsosurfaceFunction()
isofunc.SetIsoValue(iso_thr)
else:
compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
if info:
print('mapper VolumeRayCastMapper')
mapper = vtk.vtkVolumeRayCastMapper()
if iso:
mapper.SetVolumeRayCastFunction(isofunc)
if info:
print('Isosurface')
else:
mapper.SetVolumeRayCastFunction(compositeFunction)
#mapper.SetMinimumImageSampleDistance(0.2)
if info:
print('Composite')
if affine == None:
mapper.SetInput(im)
else:
#mapper.SetInput(reslice.GetOutput())
mapper.SetInput(changeFilter.GetOutput())
#Return mid position in world space
#im2=reslice.GetOutput()
#index=im2.FindPoint(vol.shape[0]/2.0,vol.shape[1]/2.0,vol.shape[2]/2.0)
#print 'Image Getpoint ' , im2.GetPoint(index)
volum = vtk.vtkVolume()
volum.SetMapper(mapper)
volum.SetProperty(property)
if info :
print 'Origin', volum.GetOrigin()
print 'Orientation', volum.GetOrientation()
print 'OrientationW', volum.GetOrientationWXYZ()
print 'Position', volum.GetPosition()
print 'Center', volum.GetCenter()
print 'Get XRange', volum.GetXRange()
print 'Get YRange', volum.GetYRange()
print 'Get ZRange', volum.GetZRange()
print 'Volume data type', vol.dtype
return volum
开发者ID:arokem,项目名称:Fos,代码行数:101,代码来源:fos.py
示例14: viz
def viz():
opaq = 0.01
# We begin by creating the data we want to render.
# For this tutorial, we create a 3D-image containing three overlaping cubes.
# This data can of course easily be replaced by data from a medical CT-scan or anything else three dimensional.
# The only limit is that the data must be reduced to unsigned 8 bit or 16 bit integers.
img = Image.open('imagen3.png').convert('L')
img = np.asarray(img)
print img.shape
Nx = sqrt(img.shape[0])
Ny = Nx
Nz = img.shape[1]
data_matrix = zeros([Nx, Ny, Nz], dtype=uint8)
for i in range(0,Nz-1):
temp = img[Nx*i:Nx*(i+1),:]
data_matrix[:,:,i] = np.uint8(255)-temp
#for i in range(0,maxcoordZ-1):
# for k in range(0,maxcoord-1):
# data_matrix[k,:,i] = np.uint8(255)-np.array(occupied[i*maxcoord2+k*maxcoord:i*maxcoord2+(k+1)*maxcoord]).astype(np.uint8)
#data_matrix = occupied#data_matrix[20:150, 20:150, 20:150] = randint(0,150)
# For VTK to be able to use the data, it must be stored as a VTK-image. This can be done by the vtkImageImport-class which
# imports raw data and stores it.
dataImporter = vtk.vtkImageImport()
# The preaviusly created array is converted to a string of chars and imported.
data_string = data_matrix.tostring()
dataImporter.CopyImportVoidPointer(data_string, len(data_string))
# The type of the newly imported data is set to unsigned char (uint8)
dataImporter.SetDataScalarTypeToUnsignedChar()
# Because the data that is imported only contains an intensity value (it isnt RGB-coded or someting similar), the importer
# must be told this is the case.
dataImporter.SetNumberOfScalarComponents(1)
# The following two functions describe how the data is stored and the dimensions of the array it is stored in. For this
# simple case, all axes are of length 75 and begins with the first element. For other data, this is probably not the case.
# I have to admit however, that I honestly dont know the difference between SetDataExtent() and SetWholeExtent() although
# VTK complains if not both are used.
dataImporter.SetDataExtent(0, Nx-1, 0, Ny-1, 0, Nz-1)
dataImporter.SetWholeExtent(0, Nx-1, 0, Ny-1, 0, Nz-1)
# The following class is used to store transparencyv-values for later retrival. In our case, we want the value 0 to be
# completly opaque whereas the three different cubes are given different transperancy-values to show how it works.
alphaChannelFunc = vtk.vtkPiecewiseFunction()
alphaChannelFunc.AddPoint(0, 0)
alphaChannelFunc.AddPoint(255, opaq)
# This class stores color data and can create color tables from a few color points. For this demo, we want the three cubes
# to be of the colors red green and blue.
colorFunc = vtk.vtkColorTransferFunction()
colorFunc.AddRGBPoint(0, 0.0, 0.0, 0.0)
colorFunc.AddRGBPoint(255,0.8, 0.7, 0.6)
# The preavius two classes stored properties. Because we want to apply these properties to the volume we want to render,
# we have to store them in a class that stores volume prpoperties.
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colorFunc)
volumeProperty.SetScalarOpacity(alphaChannelFunc)
# This class describes how the volume is rendered (through ray tracing).
compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
# We can finally create our volume. We also have to specify the data for it, as well as how the data will be rendered.
volumeMapper = vtk.vtkVolumeRayCastMapper()
volumeMapper.SetVolumeRayCastFunction(compositeFunction)
volumeMapper.SetInputConnection(dataImporter.GetOutputPort())
# The class vtkVolume is used to pair the preaviusly declared volume as well as the properties to be used when rendering that volume.
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)
# With almost everything else ready, its time to initialize the renderer and window, as well as creating a method for exiting the application
renderer = vtk.vtkRenderer()
renderWin = vtk.vtkRenderWindow()
renderWin.AddRenderer(renderer)
renderInteractor = vtk.vtkRenderWindowInteractor()
renderInteractor.SetRenderWindow(renderWin)
# We add the volume to the renderer ...
renderer.AddVolume(volume)
# ... set background color to white ...
renderer.SetBackground(0,0,0)
# ... and set window size.
renderWin.SetSize(800, 800)
# A simple function to be called when the user decides to quit the application.
def exitCheck(obj, event):
if obj.GetEventPending() != 0:
obj.SetAbortRender(1)
# Tell the application to use the function as an exit check.
renderWin.AddObserver("AbortCheckEvent", exitCheck)
renderInteractor.Initialize()
# Because nothing will be rendered without any input, we order the first render manually before control is handed over to the main-loop.
#.........这里部分代码省略.........
开发者ID:rbaravalle,项目名称:Pysys,代码行数:101,代码来源:viz.py
示例15: show3
def show3(data_matrix = None): # pragma: no coverage
import vtk
# We begin by creating the data we want to render.
# For this tutorial, we create a 3D-image containing three overlaping cubes.
# This data can of course easily be replaced by data from a medical CT-scan or anything else three dimensional.
# The only limit is that the data must be reduced to unsigned 8 bit or 16 bit integers.
import pdb; pdb.set_trace()
if data_matrix == None:
data_matrix = zeros([75, 75, 75], dtype=uint8)
data_matrix[0:35, 0:35, 0:35] = 50
data_matrix[25:55, 25:55, 25:55] = 100
data_matrix[45:74, 45:74, 45:74] = 150
else:
data_matrix[data_matrix==1] = 50
data_matrix[data_matrix==2] = 100
val0 = 0
val1 = 50
val2 = 100
val3 = 150
# For VTK to be able to use the data, it must be stored as a VTK-image. This can be done by the vtkImageImport-class which
# imports raw data and stores it.
dataImporter = vtk.vtkImageImport()
# The preaviusly created array is converted to a string of chars and imported.
data_string = data_matrix.tostring()
dataImporter.CopyImportVoidPointer(data_string, len(data_string))
# The type of the newly imported data is set to unsigned char (uint8)
dataImporter.SetDataScalarTypeToUnsignedChar()
# Because the data that is imported only contains an intensity value (it isnt RGB-coded or someting similar), the importer
# must be told this is the case.
dataImporter.SetNumberOfScalarComponents(1)
# The following two functions describe how the data is stored and the dimensions of the array it is stored in. For this
# simple case, all axes are of length 75 and begins with the first element. For other data, this is probably not the case.
# I have to admit however, that I honestly dont know the difference between SetDataExtent() and SetWholeExtent() although
# VTK complains if not both are used.
#dataImporter.SetDataExtent(0, 74, 0, 74, 0, 74)
#dataImporter.SetWholeExtent(0, 74, 0, 74, 0, 74)
dataImporter.SetDataExtent(0, data_matrix.shape[0]-1, 0, data_matrix.shape[1]-1, 0,data_matrix.shape[2]-1 )
dataImporter.SetWholeExtent(0, data_matrix.shape[0]-1, 0, data_matrix.shape[1]-1, 0,data_matrix.shape[2]-1 )
# The following class is used to store transparencyv-values for later retrival. In our case, we want the value 0 to be
# completly opaque whereas the three different cubes are given different transperancy-values to show how it works.
alphaChannelFunc = vtk.vtkPiecewiseFunction()
alphaChannelFunc.AddPoint(val0, 0.0)
alphaChannelFunc.AddPoint(val1, 0.05)
alphaChannelFunc.AddPoint(val2, 0.1)
alphaChannelFunc.AddPoint(val3, 0.2)
# This class stores color data and can create color tables from a few color points. For this demo, we want the three cubes
# to be of the colors red green and blue.
colorFunc = vtk.vtkColorTransferFunction()
colorFunc.AddRGBPoint(val1, 1.0, 0.0, 0.0)
colorFunc.AddRGBPoint(val2, 0.0, 1.0, 0.0)
colorFunc.AddRGBPoint(val3, 0.0, 0.0, 1.0)
# The preavius two classes stored properties. Because we want to apply these properties to the volume we want to render,
# we have to store them in a class that stores volume prpoperties.
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colorFunc)
volumeProperty.SetScalarOpacity(alphaChannelFunc)
# This class describes how the volume is rendered (through ray tracing).
compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
# We can finally create our volume. We also have to specify the data for it, as well as how the data will be rendered.
volumeMapper = vtk.vtkVolumeRayCastMapper()
volumeMapper.SetVolumeRayCastFunction(compositeFunction)
volumeMapper.SetInputConnection(dataImporter.GetOutputPort())
# The class vtkVolume is used to pair the preaviusly declared volume as well as the properties to be used when rendering that volume.
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)
# With almost everything else ready, its time to initialize the renderer and window, as well as creating a method for exiting the application
renderer = vtk.vtkRenderer()
renderWin = vtk.vtkRenderWindow()
renderWin.AddRenderer(renderer)
renderInteractor = vtk.vtkRenderWindowInteractor()
renderInteractor.SetRenderWindow(renderWin)
# We add the volume to the renderer ...
renderer.AddVolume(volume)
# ... set background color to white ...
renderer.SetBackground(0,0,0)
# ... and set window size.
renderWin.SetSize(400, 400)
# A simple function to be called when the user decides to quit the application.
def exitCheck(obj, event):
if obj.GetEventPending() != 0:
obj.SetAbortRender(1)
# Tell the application to use the function as an exit check.
renderWin.AddObserver("AbortCheckEvent", exitCheck)
renderInteractor.Initialize()
# Because nothing will be rendered without any input, we order the first render manually before control is handed over to the main-loop.
renderWin.Render()
renderInteractor.Start()
#.........这里部分代码省略.........
开发者ID:vlukes,项目名称:lisa,代码行数:101,代码来源:show3.py
示例16: RenderCubeInVTK
#.........这里部分代码省略.........
########################
# Now apply the scalings
ScaledData = vtk.vtkImageShiftScale()
ScaledData.SetInput(CubeData.GetGridOutput())
ScaledData.SetShift(-mindatum)
ScaledData.SetScale((2**ColorDepth-1)/(maxdatum-mindatum))
if ColorDepth == 16:
ScaledData.SetOutputScalarTypeToUnsignedShort()
elif ColorDepth == 8:
ScaledData.SetOutputScalarTypeToUnsignedChar()
else:
print
print "Error! Unsupported color depth given"
print
print "valid values are 8 or 16"
print
raise ValueError
###############################
# Form combined coloring scheme
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colorTransferFunction)
volumeProperty.SetScalarOpacity(opacityTransferFunction)
volumeProperty.SetInterpolationTypeToLinear()
volumeProperty.ShadeOn()
# The mapper / ray cast function know how to render the data
compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
volumeMapper = vtk.vtkVolumeRayCastMapper()
volumeMapper.SetVolumeRayCastFunction(compositeFunction)
volumeMapper.SetInput(ScaledData.GetOutput())
#Create a coarse representation
#Actually a fake - won't display anything
compositeFunction2 = vtk.vtkVolumeRayCastIsosurfaceFunction()
compositeFunction2.SetIsoValue(2**ColorDepth-1)
volumeMapperCoarse = vtk.vtkVolumeRayCastMapper()
volumeMapperCoarse.SetVolumeRayCastFunction(compositeFunction2)
volumeMapperCoarse.SetInput(ScaledData.GetOutput())
# Create volumetric object to be rendered
# Use level of detail prop so that it won't take forever to look around
volume = vtk.vtkLODProp3D()
id1 = volume.AddLOD(volumeMapper, volumeProperty, 0.)
volume.SetLODProperty(id1, volumeProperty)
id2 = volume.AddLOD(volumeMapperCoarse, volumeProperty, 0.)
volume.SetLODProperty(id2, volumeProperty)
# At this point, we can position and orient the volume
#################################
# End of volumetric data pipeline
#################################
#########
#Contours
#########
请发表评论