[vtkusers] Convert vtk mesh model to binary image

João Domingos joao.sousa.domingos at gmail.com
Wed May 22 15:38:03 EDT 2013


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/20130522/c707c5f0/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: plane & vtkpolydata.jpg
Type: image/jpeg
Size: 47857 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130522/c707c5f0/attachment.jpg>


More information about the vtkusers mailing list