<div dir="ltr"><div><div><div>Hi,<br><br></div>A much faster way would be to replace the for loop with this:<br>imgvtk.GetPointData().<wbr>GetScalars().Filll(background_value)<br><br></div>-Sujin<br></div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jan 29, 2017 at 2:37 PM, jmerkow <span dir="ltr"><<a href="mailto:jmerkow@gmail.com" target="_blank">jmerkow@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
I am working in creating volume images from PolyData using<br>
vtkPolyDataToImageStencil and vtkImageStencil.<br>
The process is outline here:[1] (for 2D images).<br>
To do this, you need to fill a vtkImageData with a background (or<br>
foreground) value.<br>
Something like this:<br>
<br>
    imgvtk = vtk.vtkImageData()<br>
    imgvtk.SetSpacing(spacing)<br>
    imgvtk.SetOrigin(origin)<br>
    imgvtk.SetExtent(extent)<br>
    imgvtk.AllocateScalars(vtk.<wbr>VTK_UNSIGNED_CHAR, 1)<br>
<br>
    # fill vtk image with background<br>
    count = imgvtk.GetNumberOfPoints()<br>
    for i in range(count):<br>
        imgvtk.GetPointData().<wbr>GetScalars().SetTuple1(i, backgroud_value);<br>
<br>
<br>
However, this is very very slow, by far the slowest part of the process.  Is<br>
there a faster way to do this?<br>
<br>
<br>
Full code:<br>
import vtk<br>
from vtk.util import numpy_support<br>
<br>
def pd_to_numpy_vol(pd, spacing=[1.,1.,1.], shape=None, origin=None<br>
foreground_value=255, backgroud_value = 0):<br>
    if shape is None:<br>
        bnds = np.array(pd.GetBounds())<br>
        shape = np.ceil((bnds[1::2]-bnds[::2])<wbr>/spacing).astype(int)+15<br>
    if origin is None:<br>
        origin = bnds[::2]+(bnds[1::2]-bnds[::<wbr>2])/2<br>
<br>
    #make image<br>
    extent = np.zeros(6).astype(int)<br>
    extent[1::2] = np.array(shape)-1<br>
    imgvtk = vtk.vtkImageData()<br>
    imgvtk.SetSpacing(spacing)<br>
    imgvtk.SetOrigin(origin)<br>
    imgvtk.SetExtent(extent)<br>
    imgvtk.AllocateScalars(vtk.<wbr>VTK_UNSIGNED_CHAR, 1)<br>
<br>
    # fill vtk image with background<br>
    count = imgvtk.GetNumberOfPoints()<br>
    for i in range(count):<br>
        imgvtk.GetPointData().<wbr>GetScalars().SetTuple1(i, backgroud_value);<br>
<br>
    #poly2 stencil<br>
    pol2stenc = vtk.vtkPolyDataToImageStencil(<wbr>)<br>
    pol2stenc.SetInputData(pd)<br>
    pol2stenc.SetOutputSpacing(<wbr>spacing)<br>
    pol2stenc.SetOutputOrigin(<wbr>origin)<br>
    pol2stenc.<wbr>SetOutputWholeExtent(extent)<br>
    pol2stenc.Update()<br>
<br>
    #stencil to image<br>
    imgstenc = vtk.vtkImageStencil()<br>
    imgstenc.SetInputData(imgvtk)<br>
    imgstenc.SetStencilConnection(<wbr>pol2stenc.GetOutputPort())<br>
    imgstenc.ReverseStencilOn()<br>
    imgstenc.SetBackgroundValue(<wbr>foreground_value)<br>
    imgstenc.Update()<br>
<br>
    ndseg =<br>
numpy_support.vtk_to_numpy(<wbr>imgstenc.GetOutputDataObject(<wbr>0).GetPointData().GetArray(0))<br>
    return ndseg.reshape(sitk.<wbr>GetArrayFromImage(seg).shape)<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://vtk.1045678.n5.nabble.com/Faster-way-to-create-a-vtkImageData-with-a-single-value-tp5741978.html" rel="noreferrer" target="_blank">http://vtk.1045678.n5.nabble.<wbr>com/Faster-way-to-create-a-<wbr>vtkImageData-with-a-single-<wbr>value-tp5741978.html</a><br>
Sent from the VTK - Users mailing list archive at Nabble.com.<br>
______________________________<wbr>_________________<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/<wbr>opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the VTK FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ" rel="noreferrer" target="_blank">http://www.vtk.org/Wiki/VTK_<wbr>FAQ</a><br>
<br>
Search the list archives at: <a href="http://markmail.org/search/?q=vtkusers" rel="noreferrer" target="_blank">http://markmail.org/search/?q=<wbr>vtkusers</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/vtkusers" rel="noreferrer" target="_blank">http://public.kitware.com/<wbr>mailman/listinfo/vtkusers</a><br>
</blockquote></div><br></div>