[vtkusers] Copy image data into vtkImageData from a textfile

kenichiro yoshimi rccm.kyoshimi at gmail.com
Fri Jul 27 16:37:22 EDT 2018


Hi,

Thank you for sharing your code. At first glance, the parameters of
imageData have to be set according to your own data.
---
imageData.SetDimensions(512, 512, 192)
imageData.SetOrigin(0.0, 0.0, 0.0)
imageData.SetSpacing(1, 1, 1)
---
And would you try to use vtkSmartVolumeMapper, instead of
vtkVolumeRaycastMapper?

Thanks
2018年7月28日(土) 4:03 Lizeth Castellanos <castellanoslizan at gmail.com>:
>
> Hi!
>
> Thanks for your reply.
> My doubt was about how to pass the 'Field 3' to the probe filter. I made some adjust based in your help. However, the output shows two solid cube instead of the points from my segmented dataset.
>
> I am sending the code and the screenshot of the output. Thanks again :-)
>
> reader = vtk.vtkDelimitedTextReader()
> reader.SetFieldDelimiterCharacters(' ')
> reader.DetectNumericColumnsOn()
> reader.SetFileName('sample.csv')
> 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()
>
> imageData = vtk.vtkImageData()
> imageData.SetDimensions(3, 3, 3)
> imageData.SetOrigin(0.0, 0.0, 0.0)
> imageData.SetSpacing(1.0, 1.0, 1.0)
> imageData.Update()
>
> probeFilter = vtk.vtkProbeFilter()
> probeFilter.SetSourceConnection(tableToPoints.GetOutputPort())
> probeFilter.SetInput(imageData)
> probeFilter.Update()
>
> probeFilter.GetOutput().GetPointData().SetActiveScalars('Field 3')
> scalarRange = probeFilter.GetOutput().GetPointData().GetScalars().GetRange()
>
> writer = vtk.vtkXMLImageDataWriter()
> writer.SetInput(probeFilter.GetOutput())
> writer.SetFileName('out.vti')
> writer.Write()
>
> # Create transfer mapping scalar value to opacity.
> opacityTransferFunction = vtk.vtkPiecewiseFunction()
> opacityTransferFunction.AddPoint(scalarRange[0], 0.0)
> opacityTransferFunction.AddPoint(scalarRange[1], 0.5)
>
> # Create transfer mapping scalar value to color.
> colorTransferFunction = vtk.vtkColorTransferFunction()
> colorTransferFunction.AddRGBPoint(scalarRange[0], 0.0, 0.0, 1.0)
> colorTransferFunction.AddRGBPoint((scalarRange[0]+scalarRange[1])*0.5, 0.0, 1.0, 0.0)
> colorTransferFunction.AddRGBPoint(scalarRange[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()
> volumeProperty.ShadeOn()
>
> # vtkVolumeRayCastMapper need data of type unsigned char or unsigned short
> cast = vtk.vtkImageCast()
> cast.SetInputConnection(probeFilter.GetOutputPort())
> cast.SetOutputScalarTypeToUnsignedChar()
> cast.Update()
>
> ## The mapper / ray cast function know how to render the data
> compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
> volumeMapper = vtk.vtkVolumeRayCastMapper()
> volumeMapper.SetVolumeRayCastFunction(compositeFunction)
> volumeMapper.SetInputConnection(cast.GetOutputPort())
>
>
> #The volume holds the mapper and the property.
> volume = vtk.vtkVolume()
> volume.SetMapper(volumeMapper)
> volume.SetProperty(volumeProperty)
>
> #Create the standard renderer, render window and interactor
> ren = vtk.vtkRenderer()
>
> renWin = vtk.vtkRenderWindow()
> renWin.AddRenderer(ren)
>
> iren = vtk.vtkRenderWindowInteractor()
> iren.SetRenderWindow(renWin)
>
> ren.AddVolume(volume)
> ren.SetBackground(1, 1, 1)
> renWin.SetSize(600, 600)
> renWin.Render()
>
> iren.Initialize()
> renWin.Render()
> iren.Start()
>
>
>
>
> On Thu, Jul 26, 2018 at 9:35 PM, kenichiro yoshimi <rccm.kyoshimi at gmail.com> wrote:
>>
>> Hi,
>>
>> Your question is not clear enough. If you possible, can you post your
>> code? I don't have a problem with volume rendering in my code.
>>
>> ----
>> import vtk
>>
>>
>> def main():
>>
>>     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')
>>
>>     imageData = vtk.vtkImageData()
>>     imageData.SetDimensions(3, 3, 3)
>>     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()
>>
>>
>> if __name__ == '__main__':
>>     main()
>> ----
>>
>> Regards
>> 2018年7月26日(木) 0:15 Lizeth Castellanos <castellanoslizan at gmail.com>:
>> >
>> > Thanks for your answers!
>> >
>> > For the case 2) I have one more question: How can I add the scalar values from the intensity ('Field 3') to the data?
>> >
>> > I have added those two lines to your code:
>> >
>> > scalar = probeFilter.GetOutput().GetPointData().GetArray('Field 3')
>> > probeFilter.GetOutput().GetPointData().SetScalars(scalar)
>> >
>> > But when I render the volume, nothing is displayed in the render window.
>> >
>> > Thanks
>> >
>> > Lizeth
>> >
>> >
>> >
>> >
>> >
>> > On Tue, Jul 24, 2018 at 11:29 PM, kenichiro yoshimi <rccm.kyoshimi at gmail.com> wrote:
>> >>
>> >> Hi,
>> >>
>> >> 1)  You need to specify PYTHON_INCLUDE_DIR and PYTHON_LIBRARY to suit
>> >> your own system in the 'advanced options' in cmake.
>> >>
>> >> PYTHON_INCLUDE_DIR will be the name of the directory containing the Python.h:
>> >>  e.g. /usr/include/python2.7
>> >> PYTHON_LIBRARY will be the full path to libpython2.7.so (or.a):
>> >>  e.g. /usr/lib64/libpython2.7.so
>> >>
>> >> 2) For example, if you apply vtkProbeFilter to sample.csv in the
>> >> previous my post, script is something like below.
>> >>
>> >> ---
>> >> import vtk
>> >>
>> >>
>> >> def main():
>> >>
>> >>     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')
>> >>
>> >>     imageData = vtk.vtkImageData()
>> >>     imageData.SetDimensions(3, 3, 3)
>> >>     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()
>> >>
>> >>
>> >> if __name__ == '__main__':
>> >>     main()
>> >> ---
>> >>
>> >> For your reference.
>> >> 2018年7月25日(水) 4:10 Lizeth Castellanos <castellanoslizan at gmail.com>:
>> >> >
>> >> > Thanks for your help Kenichiro Yoshimi
>> >> >
>> >> > I was able to reproduce some part of your code. However, I encountered new issues.
>> >> >
>> >> > 1)I have all my project under VTK 5.10.1 with python 2.7.12 in linux. I got this error: 'module' object has no attribute 'vtkResampleToImage'.
>> >> >  So I guess that the vtkResampleToImage class is not avaliable for my VTK version.
>> >> >  I tried to build a newer VTK version but I didn't get success. I tried with VTK 7.1.1 and I got  errors related to the python libs Could NOT find PythonLibs (missing: PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS).
>> >> >
>> >> > 2) I read that  vtkResampleToImage  samples a dataset on a uniform grid and it internally uses vtkProbeFilter to do the probing. So,  Is there any way to use vtkProbeFilter instead of vtkResampleToImage?
>> >> > I have tested some examples from vtkProbeFilter  but I am confused about how use it  in my specific problem.
>> >> >
>> >> > Thanks,
>> >> >
>> >> > Lizeth
>> >> >
>> >> > On Thu, Jul 19, 2018 at 12:38 AM, kenichiro yoshimi <rccm.kyoshimi at gmail.com> wrote:
>> >> >>
>> >> >> 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
>> >> >
>> >> >
>> >> >
>> >> >
>> >> > --
>> >> >
>> >> >
>> >> >
>> >
>> >
>> >
>> >
>> > --
>> >
>> >
>> >
>
>
>
>
> --
> Lizeth  Castellanos.
> PhD Student
> Federal University of Rio Grande do Sul (UFRGS)
>
>


More information about the vtkusers mailing list