[vtkusers] Copy image data into vtkImageData from a textfile

Lizeth Castellanos castellanoslizan at gmail.com
Mon Aug 6 16:32:57 EDT 2018


Hi,

When I set the dimensions of the data, nothing happens :-(
Are these dimensions too big, maybe?

csvfile="directory path to my csv file/sample.csv"

reader = vtk.vtkDelimitedTextReader()
reader.SetFieldDelimiterCharacters(" ")
reader.DetectNumericColumnsOn()
reader.SetFileName(csvfile)
reader.MergeConsecutiveDelimitersOn()
reader.Update()

tableToPoints = vtk.vtkTableToPolyData()
tableToPoints.SetInputConnection(reader.GetOutputPort())
tableToPoints.SetXColumn('Field 0')
tableToPoints.SetYColumn('Field 1')
tableToPoints.SetZColumn('Field 2')
tableToPoints.Update()
print  tableToPoints.GetInput()

resample = vtk.vtkResampleToImage()
resample.SetInputConnection(tableToPoints.GetOutputPort())
resample.SetSamplingDimensions(512, 512, 192)
resample.Update()

scalar = resample.GetOutput().GetPointData().GetArray('Field 3')
resample.GetOutput().GetPointData().SetScalars(scalar)

range = resample.GetOutput().GetPointData().GetScalars().GetRange()

writer = vtk.vtkMetaImageWriter()
writer.SetInputData(resample.GetOutput())
writer.SetFileName('out.mha')
writer.SetCompression(True)
writer.Write()


colors = vtk.vtkNamedColors()

# Create the standard renderer, render window
# and interactor.
ren = vtk.vtkRenderer()

renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Create transfer mapping scalar value to opacity.
opacityTransferFunction = vtk.vtkPiecewiseFunction()
opacityTransferFunction.AddPoint(range[0], 0.0)
opacityTransferFunction.AddPoint(range[1], 1.0)

# Create transfer mapping scalar value to color.
colorTransferFunction = vtk.vtkColorTransferFunction()
colorTransferFunction.AddRGBPoint(range[0], 0.0, 0.0, 1.0)
colorTransferFunction.AddRGBPoint((range[0]+range[1])*0.5, 0.0, 1.0, 0.0)
colorTransferFunction.AddRGBPoint(range[1], 1.0, 0.0, 0.0)

# The property describes how the data will look.
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colorTransferFunction)
volumeProperty.SetScalarOpacity(opacityTransferFunction)
volumeProperty.SetScalarOpacityUnitDistance(1.73205080757)
volumeProperty.SetInterpolationTypeToLinear()

# The mapper / ray cast function know how to render the data.
volumeMapper = vtk.vtkSmartVolumeMapper()
volumeMapper.SetInputConnection(resample.GetOutputPort())
volumeMapper.SetBlendModeToComposite()

# The volume holds the mapper and the property and
# can be used to position/orient the volume.
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)

ren.AddVolume(volume)

ren.SetBackground(colors.GetColor3d("White"))
ren.GetActiveCamera().ParallelProjectionOn()
ren.ResetCameraClippingRange()
ren.ResetCamera()

renWin.SetSize(600, 600)
renWin.Render()

iren.Start()


This is the output from print  tableToPoints.GetInput()

vtkTable (0x2e87f60)
  Debug: Off
  Modified Time: 439
  Reference Count: 2
  Registered Events: (none)
  Information: 0x2e87950
  Data Released: False
  Global Release Data: Off
  UpdateTime: 459
  Field Data:
    Debug: Off
    Modified Time: 162
    Reference Count: 1
    Registered Events: (none)
    Number Of Arrays: 0
    Number Of Components: 0
    Number Of Tuples: 0
  RowData:
    Debug: Off
    Modified Time: 438
    Reference Count: 1
    Registered Events: (none)
    Number Of Arrays: 4
    Array 0 name = Field 0
    Array 1 name = Field 1
    Array 2 name = Field 2
    Array 3 name = Field 3
    Number Of Components: 4
    Number Of Tuples: 2756596
    Copy Tuple Flags: ( 1 1 1 1 1 0 1 1 )
    Interpolate Flags: ( 1 1 1 1 1 0 0 1 )
    Pass Through Flags: ( 1 1 1 1 1 1 1 1 )
    Scalars: (none)
    Vectors: (none)
    Normals: (none)
    TCoords: (none)
    Tensors: (none)
    GlobalIds: (none)
    PedigreeIds: (none)
    EdgeFlag: (none)


Thanks

Lizeth


On Thu, Aug 2, 2018 at 5:59 PM, kenichiro yoshimi <rccm.kyoshimi at gmail.com>
wrote:

> Hello,
>
> Did you set the dimensions based on your own data?  It looks all
> dimensions indicate 3.
>
> Thanks
>
> 2018年8月3日(金) 4:50 Lizeth Castellanos <castellanoslizan at gmail.com>:
>
>> Hi,
>>
>> Thanks for help me.
>> I have confirmed the out.vti using Paraview's volume rendering. I don't
>> know what is wrong, I get a solid cube again :-(
>> I am attaching a screenshot. Could you see, please?
>>
>> Thanks
>> Lizeth
>>
>> On Wed, Aug 1, 2018 at 10:20 PM, kenichiro yoshimi <
>> rccm.kyoshimi at gmail.com> wrote:
>>
>>> Hi,
>>>
>>> Thanks for trying.
>>> Could you confirm if out.vti is visualized properly using ParaView's
>>> volume rendering?
>>>
>>> Regards
>>> 2018年8月2日(木) 6:12 Lizeth Castellanos <castellanoslizan at gmail.com>:
>>> >
>>> > Hi,
>>> >
>>> > I got VTK 7.1.1 in my computer, finally! So, I have tested the two
>>> solutions that you have suggested:
>>> > 1) using vtkProbeFilter
>>> > 2) using vtkResampleToImage
>>> >
>>> > 1) I  have adjusted the image dimensions according to my own data and
>>> I am using  vtkSmartVolumeMapper, instead of vtkVolumeRaycastMapper.
>>> > No errors are generated in the terminal. However, I have been waiting
>>> for several minutes and nothing happens.
>>> > Do you have any idea what is wrong?
>>> >
>>> >
>>> > csvfile="directory path to my csv file/sample.csv"
>>> >
>>> > reader = vtk.vtkDelimitedTextReader()
>>> > reader.SetFieldDelimiterCharacters(" ")
>>> > reader.DetectNumericColumnsOn()
>>> > reader.SetFileName(csvfile)
>>> > reader.MergeConsecutiveDelimitersOn()
>>> > reader.Update()
>>> >
>>> > tableToPoints = vtk.vtkTableToPolyData()
>>> > tableToPoints.SetInputConnection(reader.GetOutputPort())
>>> > tableToPoints.SetXColumn('Field 0')
>>> > tableToPoints.SetYColumn('Field 1')
>>> > tableToPoints.SetZColumn('Field 2')
>>> >
>>> > imageData = vtk.vtkImageData()
>>> > imageData.SetDimensions(512, 512, 192)
>>> > imageData.SetOrigin(0.0, 0.0, 0.0)
>>> > imageData.SetSpacing(1.0, 1.0, 1.0)
>>> >
>>> > probeFilter = vtk.vtkProbeFilter()
>>> > probeFilter.SetSourceConnection(tableToPoints.GetOutputPort())
>>> > probeFilter.SetInputData(imageData)
>>> > probeFilter.Update()
>>> >
>>> > writer = vtk.vtkXMLImageDataWriter()
>>> > writer.SetInputData(probeFilter.GetOutput())
>>> > writer.SetFileName('out.vti')
>>> > writer.Write()
>>> >
>>> > probeFilter.GetOutput().GetPointData().SetActiveScalars('Field 3')
>>> > range = probeFilter.GetOutput().GetPointData().GetScalars().GetRange()
>>> >
>>> > colors = vtk.vtkNamedColors()
>>> >
>>> > # Create the standard renderer, render window
>>> > # and interactor.
>>> > ren = vtk.vtkRenderer()
>>> >
>>> > renWin = vtk.vtkRenderWindow()
>>> > renWin.AddRenderer(ren)
>>> >
>>> > iren = vtk.vtkRenderWindowInteractor()
>>> > iren.SetRenderWindow(renWin)
>>> >
>>> > # Create transfer mapping scalar value to opacity.
>>> > opacityTransferFunction = vtk.vtkPiecewiseFunction()
>>> > opacityTransferFunction.AddPoint(range[0], 0.0)
>>> > opacityTransferFunction.AddPoint(range[1], 1.0)
>>> >
>>> > # Create transfer mapping scalar value to color.
>>> > colorTransferFunction = vtk.vtkColorTransferFunction()
>>> > colorTransferFunction.AddRGBPoint(range[0], 0.0, 0.0, 1.0)
>>> > colorTransferFunction.AddRGBPoint((range[0]+range[1])*0.5, 0.0, 1.0,
>>> 0.0)
>>> > colorTransferFunction.AddRGBPoint(range[1], 1.0, 0.0, 0.0)
>>> >
>>> > # The property describes how the data will look.
>>> > volumeProperty = vtk.vtkVolumeProperty()
>>> > volumeProperty.SetColor(colorTransferFunction)
>>> > volumeProperty.SetScalarOpacity(opacityTransferFunction)
>>> > volumeProperty.SetScalarOpacityUnitDistance(1.73205080757)
>>> > volumeProperty.SetInterpolationTypeToLinear()
>>> >
>>> > # The mapper / ray cast function know how to render the data.
>>> > volumeMapper = vtk.vtkSmartVolumeMapper()
>>> > volumeMapper.SetInputConnection(probeFilter.GetOutputPort())
>>> > volumeMapper.SetBlendModeToComposite()
>>> >
>>> > # The volume holds the mapper and the property and
>>> > # can be used to position/orient the volume.
>>> > volume = vtk.vtkVolume()
>>> > volume.SetMapper(volumeMapper)
>>> > volume.SetProperty(volumeProperty)
>>> >
>>> > ren.AddVolume(volume)
>>> >
>>> > ren.SetBackground(colors.GetColor3d("White"))
>>> > ren.GetActiveCamera().ParallelProjectionOn()
>>> > ren.ResetCameraClippingRange()
>>> > ren.ResetCamera()
>>> >
>>> > renWin.SetSize(600, 600)
>>> > renWin.Render()
>>> > iren.Start()
>>> >
>>> >
>>> > 2) I have tested the vtkResampleToImage class, (the solution that you
>>> suggest at the beginning of this thread). However, I get a solid a cube as
>>> output.
>>> > Do you have any idea why that happens?
>>> >
>>> > csvfile="directory path to my csv file/sample.csv"
>>> >
>>> > reader = vtk.vtkDelimitedTextReader()
>>> > reader.SetFieldDelimiterCharacters(" ")
>>> > reader.DetectNumericColumnsOn()
>>> > reader.SetFileName(csvfile)
>>> > reader.MergeConsecutiveDelimitersOn()
>>> > reader.Update()
>>> >
>>> > tableToPoints = vtk.vtkTableToPolyData()
>>> > tableToPoints.SetInputConnection(reader.GetOutputPort())
>>> > tableToPoints.SetXColumn("Field 0")
>>> > tableToPoints.SetYColumn("Field 1")
>>> > tableToPoints.SetZColumn("Field 2")
>>> > tableToPoints.Update()
>>> >
>>> > resample = vtk.vtkResampleToImage()
>>> > resample.SetInputConnection(tableToPoints.GetOutputPort())
>>> > resample.SetSamplingDimensions(3, 3, 3)
>>> > resample.
>>>
>>
>>
>>
>> --
>>
>>
>>


--
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://public.kitware.com/pipermail/vtkusers/attachments/20180806/90aa134a/attachment.html>


More information about the vtkusers mailing list