[vtkusers] Fixing vtkPolyDataToImageStencil

Dr. Roman Grothausmann grothausmann.roman at mh-hannover.de
Tue Jan 6 10:02:17 EST 2015


Many thanks David, that seems to have been the problem. I didn't think of 
uninitialized memory as the artefacts not only appeared in the first few slides 
but also far within the images, and for some setting no artefacts appeared at 
all. Thank You very much for looking into this and for suggesting 
vtkImageStencilToImage which I must have overlooked at that time. It makes the 
whole cold much cleaner.

Thanks again
Roman

On 06/01/15 14:54, David Gobbi wrote:
> Hi Roman,
>
> I think that the artifacts in the image are uninitialized memory.  The
> vtkImageData::AllocateScalars method does not initialize the memory,
> it only allocates it.
>
> In VTK 6, you should be able to use the vtkImageStencilToImage filter
> instead of vtkImageStencil.
>
> I think that VTK really needs a simple vtkImageSource that creates a
> blank image, it is definitely something that a lot of people would use.
> I often create blank images with vtkImageGridSource:
>
> source = vtk.vtkImageGridSource()
> source.SetDataExtent(...)
> source.SetDataOrigin(...)
> source.SetDataSpacing(...)
> source.SetDataScalarTypeToUnsignedChar()
> source.SetFillValue(255)
> source.SetLineValue(255)
> (note: if source image is set to 255, then don't use ReverseStencilOn)
>
> Please let me know if one of these solutions fixes the issue.
>
>   - David
>
> On Tue, Jan 6, 2015 at 1:41 AM, Dr. Roman Grothausmann
> <grothausmann.roman at mh-hannover.de <mailto:grothausmann.roman at mh-hannover.de>>
> wrote:
>
>     Hi David,
>
>     On 05/01/15 16:00, David Gobbi wrote:
>
>         Do the attached images match the artifacts that you see?
>
>
>     Yes.
>
>         The output of vtkPolyDataToImageStencil is binary (each voxel is "on" or
>         "off"), so
>         it is impossible for it, on its own, to produce different shades of grey.
>
>
>     That's a good point, though I thought it only creates the stencil to pass to
>     vtkImageStencil which then should only return a binary image.
>
>         What filters are you using downstream of vtkPolyDataToImageStencil?
>
>
>     Well, basically only vtkImageStencil and the meta-image writer see below:
>
>
>     def voxelizer_vol_naa(selection, scale):
>          """ volume voxelization not anti-aliased """
>
>          # Get selection boundaries.
>          (minX, maxX, minY, maxY, minZ, maxZ) = [int(x*scale) for x in
>     selection.GetBounds()] #convert tuple of floats to ints
>          (minX, maxX, minY, maxY, minZ, maxZ) = (minX-1, maxX+1, minY-1, maxY+1,
>     minZ-1, maxZ+1)
>          print_info(VERB, "  Selection bounds are %s\n"%str((minX, maxX, minY,
>     maxY, minZ, maxZ)) ) #dimensions of the resulting image
>
>          ps1= 1.0/scale  # pixel size for the stencil, make sure it's a float
>     division!
>          ps2= 1.0        # pixel size for the image
>
>          ## Convert a surface mesh into an image stencil that can be used to
>     mask an image with vtkImageStencil.
>          polyToStencilFilter = vtk.vtkPolyDataToImageStencil(__)
>          polyToStencilFilter.__SetInputData(selection)
>          polyToStencilFilter.__SetOutputWholeExtent(minX, maxX, minY, maxY,
>     minZ, maxZ)
>          polyToStencilFilter.__SetOutputSpacing(ps1, ps1, ps1)
>          polyToStencilFilter.__SetOutputOrigin(0.0, 0.0, 0.0)
>          add_progress_observer(__polyToStencilFilter)
>          polyToStencilFilter.Update()
>
>          # Create an empty (3D) image of appropriate size.
>          image = vtk.vtkImageData();
>          image.SetSpacing(ps2, ps2, ps2);
>          image.SetOrigin(0.0, 0.0, 0.0);
>          image.SetExtent(minX, maxX, minY, maxY, minZ, maxZ);
>          # image.__SetScalarTypeToUnsignedChar();
>          # image.AllocateScalars(); # this causes blender to crash if not enough
>     space can be allocated
>          ##see patch:
>     http://vtk.1045678.n5.nabble.__com/Re-patch-for-turning-__almost-all-VTK-errors-into-__Python-exceptions-IMPROVED-__td1251918.html
>     <http://vtk.1045678.n5.nabble.com/Re-patch-for-turning-almost-all-VTK-errors-into-Python-exceptions-IMPROVED-td1251918.html>
>          image.AllocateScalars(vtk.VTK___UNSIGNED_CHAR, 1) #vtk-6.x
>          # Mask the empty image with the image stencil.
>          stencil = vtk.vtkImageStencil()
>          stencil.SetInputData(image)
>          #stencil.SetStencil(__polyToStencilFilter.GetOutput(__))
>          stencil.SetStencilConnection(__polyToStencilFilter.__GetOutputPort())
>     #vtk-6.x
>          stencil.ReverseStencilOn()
>          stencil.SetBackgroundValue(__255)
>
>          add_progress_observer(stencil)
>          stencil.Update()
>
>          return stencil.GetOutput()
>
>
>     The result is then passed to the writer:
>
>
>     def write_image(image, filename):
>          """Write vtk image data to file."""
>          aWriter = vtk.vtkMetaImageWriter()
>          aWriter.SetInputData(image)
>          aWriter.SetFileName(filename)
>          aWriter.SetFileDimensionality(__3) #blender only knows 3D
>          aWriter.SetCompression(False)
>          add_progress_observer(aWriter)
>          aWriter.Write()
>
>
>     Am I doing something inappropriate somewhere that could cause the varying
>     grey values?
>
>     Many thanks for looking into this.
>     Roman
>
>
>
>         On Mon, Jan 5, 2015 at 7:33 AM, Dr. Roman Grothausmann
>         <grothausmann.roman at mh-__hannover.de
>         <mailto:grothausmann.roman at mh-hannover.de>
>         <mailto:grothausmann.roman at mh-__hannover.de
>         <mailto:grothausmann.roman at mh-hannover.de>>>
>         wrote:
>
>              Hi David,
>
>              On 05/01/15 15:20, David Gobbi wrote:
>              > The .mha file that you attached is giving me an error:
>              >
>              >    MetaImage: M_ReadElementsData: data not read completely
>              >       ideal = 1316134911 : actual = 87431
>
>              Sorry, it was compressed with my MHA plugin for fiji which does not
>              recompute CompressedDataSize because ITK happily ignores any mismatches
>              except for reporting;-)
>              Attached the file compressed with ITK itself (possible because the
>         data is
>              less than 4GB, see https://issues.itk.org/jira/____browse/ITK-3321
>         <https://issues.itk.org/jira/__browse/ITK-3321>
>              <https://issues.itk.org/jira/__browse/ITK-3321
>         <https://issues.itk.org/jira/browse/ITK-3321>>).
>
>              Just noticed: it also contains artifacts in slices 130 to 142.
>
>              Many thanks for looking into this.
>              Roman
>
>
>                  On Mon, Jan 5, 2015 at 5:55 AM, Dr. Roman Grothausmann
>                  <grothausmann.roman at mh-____hannover.de
>         <mailto:grothausmann.roman at mh-__hannover.de>
>                  <mailto:grothausmann.roman at mh-__hannover.de
>         <mailto:grothausmann.roman at mh-hannover.de>>
>                  <mailto:grothausmann.roman at mh-____hannover.de
>         <mailto:grothausmann.roman at mh-__hannover.de>
>                  <mailto:grothausmann.roman at mh-__hannover.de
>         <mailto:grothausmann.roman at mh-hannover.de>>>>
>                  wrote:
>
>                       Hi David,
>
>
>                       Many thanks for improving vtkPolyDataToImageStencil. I
>         tested the
>                  git branch
>                       on 141229 in conjunction with my Voxelizer plug-in for Blender
>                       (http://www.midasjournal.org/______browse/publication/882
>         <http://www.midasjournal.org/____browse/publication/882>
>                  <http://www.midasjournal.org/____browse/publication/882
>         <http://www.midasjournal.org/__browse/publication/882>>
>                       <http://www.midasjournal.org/____browse/publication/882
>         <http://www.midasjournal.org/__browse/publication/882>
>                  <http://www.midasjournal.org/__browse/publication/882
>         <http://www.midasjournal.org/browse/publication/882>>>) and I still get some
>                       artifacts in the result (see first two slices of attached
>         MHA) with
>                  e.g. an
>                       icosahedron (scale set to 100) as found in the attachment.
>         Is that
>                  related
>                       to the bugs of vtkPolyDataToImageStencil?
>
>                       Kind regards,
>                       Roman
>
>
>     --
>     Dr. Roman Grothausmann
>
>     Tomographie und Digitale Bildverarbeitung
>     Tomography and Digital Image Analysis
>
>     Institut für Funktionelle und Angewandte Anatomie, OE 4120
>     Medizinische Hochschule Hannover
>     Carl-Neuberg-Str. 1
>     D-30625 Hannover
>
>     Tel. +49 511 532-9574 <tel:%2B49%20511%20532-9574>
>
>

-- 
Dr. Roman Grothausmann

Tomographie und Digitale Bildverarbeitung
Tomography and Digital Image Analysis

Institut für Funktionelle und Angewandte Anatomie, OE 4120
Medizinische Hochschule Hannover
Carl-Neuberg-Str. 1
D-30625 Hannover

Tel. +49 511 532-9574


More information about the vtkusers mailing list