[vtkusers] Convert vtk mesh model to binary image

Dženan Zukić dzenanz at gmail.com
Thu May 23 04:01:50 EDT 2013


Attached is a self-contained program which uses the stencil class. I hope
that ITK stuff does not obscure how vtk classes are used. I was tracking
down some bug in ITK library and then decided to use VTK voxelization class
for the task.


On Wed, May 22, 2013 at 9:38 PM, João Domingos <
joao.sousa.domingos at gmail.com> wrote:

> Hi all,
> Regarding that example, I am trying to replicate it but in Python. For
> that I did the following:
>
>
>                 spacing = np.zeros(3)
>
> whiteImage = vtk.vtkImageData()
>  bounds = self.dsurf.dense_representation_grid.GetBounds()            #
> self.dsurf.dense_representation_grid is my vtkPolyData represented in red
> in the attached image
>
> spacing[0] = 0.0960 # desired volume spacing (.mha info): ElementSpacing
> = 0.0960 0.0968 0.0726
>  spacing[1] = 0.0968
> spacing[2] = 0.0726
>  whiteImage.SetSpacing(spacing)
>  # Compute dimensions:
>  dim = []
> dim.append((bounds[1] - bounds[0])/spacing[0])
>  dim.append((bounds[3] - bounds[2])/spacing[1])
> dim.append((bounds[5] - bounds[4])/spacing[2])
>
> whiteImage.SetDimensions(dim)
> whiteImage.SetExtent(0, dim[0]-1,0,dim[1]-1,0,dim[2]-1)
>
> origin = np.zeros(3)
>
> origin[0] = bounds[0] + spacing[0] / 2;
>   origin[1] = bounds[2] + spacing[1] / 2;
>   origin[2] = bounds[4] + spacing[2] / 2;
>   whiteImage.SetOrigin(origin)
>
> whiteImage.SetScalarTypeToUnsignedChar()
>  whiteImage.AllocateScalars()
>
> inval = 255;
>   outval = 0;
>   count = whiteImage.GetNumberOfPoints()
>   for i in range(0,count):
>     whiteImage.GetPointData().GetScalars().SetTuple1(i, inval)
>
> # polygonal data --> image stencil:
> pol2stenc = vtk.vtkPolyDataToImageStencil()
>  pol2stenc.SetInput(self.dsurf.dense_representation_grid)
> pol2stenc.SetOutputOrigin(origin)
>   pol2stenc.SetOutputSpacing(spacing)
>   pol2stenc.SetOutputWholeExtent(whiteImage.GetExtent())
>   pol2stenc.Update()
>
> # cut the corresponding white image and set the background:
>  imgstenc = vtk.vtkImageStencil()
> imgstenc.SetInput(whiteImage)
>   imgstenc.SetStencil(pol2stenc.GetOutput())
> imgstenc.ReverseStencilOff()
>   imgstenc.SetBackgroundValue(0)
>   imgstenc.Update()
>
>   writer = vtk.vtkMetaImageWriter()
>   writer.SetFileName("SphereVolume.mhd")
>  writer.SetInput(imgstenc.GetOutput())
> writer.Write()
>
>  writer2 = vtk.vtkPNMWriter()
>         writer2.SetInput(imgstenc.GetOutput())
>         writer2.SetFileName("SphereVolume.pgm")
>         writer2.Write()
>
>
> The problem is that the image created at the end is just a small (compared
> to the hxw of the plane I really want in the 3D volume) black image. Can
> anyone tell me, looking at the C++ code of that example, what is wrong here?
> http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataToImageData
>
>
> PS: David G., sorry to add you (2x) to this, but usually you are the one
> who save me when it comes to Python :)
>
>
>
>
>
> On 22 May 2013 11:58, Dženan Zukić <dzenanz at gmail.com> wrote:
>
>> Is this what you are looking for?
>> http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataToImageData
>>
>>
>> On Wed, May 22, 2013 at 12:30 PM, shirani kannangara <
>> mangalika673 at gmail.com> wrote:
>>
>>> HI
>>> I have created a mesh model of liver(.vtk file) with poly data from
>>>             VHP Abdomen.mhd file.Now I want to make a binary image of it.I
>>> checked VTK C++ examples to find a sample program.But so far failed.Can you
>>> please help me to convert this vtk mesh model into a binary image.
>>>
>>> thanks in advance
>>>
>>> regards
>>>
>>> shirani
>>>
>>> _______________________________________________
>>> 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
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.vtk.org/mailman/listinfo/vtkusers
>>>
>>>
>>
>> _______________________________________________
>> 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
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtkusers
>>
>>
>
>
> --
>
> ____________
>
> João Domingos
> DPhil candidate
> Institute of Biomedical Engineering
> Department of Engineering Science
> University of Oxford, UK
>
> Email: domingos.domingos at eng.ox.ac.uk
> Mobile: +44(0) 794 625 2288
>
> Wolfson College
> Linton Road
> Oxford
> OX2 6UD
> United Kingdom
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130523/e6094258/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bugTester.cpp
Type: text/x-c++src
Size: 3585 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130523/e6094258/attachment.cpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bug.mha
Type: application/octet-stream
Size: 425 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130523/e6094258/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bug84.obj
Type: application/octet-stream
Size: 4554 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130523/e6094258/attachment-0001.obj>


More information about the vtkusers mailing list