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