[vtkusers] Copy image data into vtkImageData from a textfile

kenichiro yoshimi rccm.kyoshimi at gmail.com
Wed Jul 18 23:38:22 EDT 2018


Hi Lizeth,

Because vtkDelimitedTextReader outputs a vtkTable, you firstly need to
convert it to a vtkPolyData using vtkTableToPolyData. And then you can
use vtkResampleToImage to map the polyData to vtkImageData.

---sample.csv---
0 0 0 60.7635
1 0 0 107.555
2 0 0 80.5241
0 1 0 85.9694
1 1 0 156.706
2 1 0 105.73
0 2 0 37.3531
1 2 0 84.1445
2 2 0 57.1137
0 0 1 100.634
1 0 1 171.37
2 0 1 120.395
0 1 1 149.785
1 1 1 260
2 1 1 169.546
0 2 1 77.2238
1 2 1 147.96
2 2 1 96.9844
0 0 2 60.7635
1 0 2 107.555
2 0 2 80.5241
0 1 2 85.9694
1 1 2 156.706
2 1 2 105.73
0 2 2 37.3531
1 2 2 84.1445
2 2 2 57.1137

---
import vtk


def main():
    colors = vtk.vtkNamedColors()

    reader = vtk.vtkDelimitedTextReader()
    reader.SetFileName('sample.csv')
    reader.DetectNumericColumnsOn()
    reader.SetFieldDelimiterCharacters(' ')
    reader.MergeConsecutiveDelimitersOn()

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

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

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

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


if __name__ == '__main__':
    main()
---

Best
2018年7月19日(木) 3:43 Lizeth Castellanos <castellanoslizan at gmail.com>:
>
> Hi!
>
> I'd like to copy image data from a textfile (csv) into vtkImageData.
> The textfile have voxel values for x,y,z  and image intensity. I already have read the textfile into VTK with the vtkDelimitedTextReader class:
>
> Table loaded from CSV file:
> +-----------+-----------+-----------+------------+
> | Field 0   | Field 1   | Field 2   | Field 3    |
> +-----------+-----------+-----------+------------+
> | 510       | 291       | 0             | 32           |
> | 511       | 291       | 0             | 128         |
> | 510       | 292       | 0             | 104         |
> | 511       | 292       | 0             | 104         |
> | 510       | 293       | 0             | 40           |
> | 511       | 293       | 0             | 240         |
> | 510       | 294       | 0             | 104         |
> | 511       | 294       | 0             | 96           |
> | 506       | 295       | 0             | 64           |
> | 507       | 295       | 0             | 16           |
>  .....
>  .....
> The file is an exported segmented dataset.
>
> I am following the tips from this similar question  http://vtk.1045678.n5.nabble.com/importing-image-data-into-VTK-from-textfile-mimics-td1243332.html
> I have created an appropriate  vtkimagedata (volume):
>
> imageData = vtk.vtkImageData()
> imageData.SetDimensions(512, 512, 192)
> imageData.SetOrigin(0.0, 0.0, 0.0)
> imageData.SetSpacing(1, 1, 1)
> imageData.SetNumberOfScalarComponents(1)
> imageData.Update()
>
> However I don't know how to copy the data loaded from vtkDelimitedTextReader into vtkImageData. How do I copy the x,y,z voxel values into vtkImageData? How do I copy the intensity valuesVtkImageData?
>
> Any help provided for this would be greatly appreciated!
>
> Lizeth
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> https://public.kitware.com/mailman/listinfo/vtkusers


More information about the vtkusers mailing list