[vtkusers] vtkImageStencil too slow for large images

David Gobbi david.gobbi at gmail.com
Mon Jul 25 08:53:16 EDT 2011


You'll have to wait until VTK 5.8.  It provides a faster
vtkPolyDataToImageStencil that does not require the contours to be extruded.

 - David


On Mon, Jul 25, 2011 at 6:33 AM, Jothy <jothybasu at gmail.com> wrote:

> First I read a seriers of contours and create a vtkPolyData with
> vtkAppendPolyData (e.g.: 100 contours as 1 polydata).
>
> Then, create a vtkImageData with the same spacing  and origin as the dicom
> image and set the extent to the extent of polydata.
>
> Now apply vtkLinearExtrusionFilter to the polydata and then apply
> vtkPolyDataToImageStencil, now finally applying the vtkImageStencil.
>
> I works satisfactorily for vtkPolyData with bounds[2] in the range 0 to 50
> or so. But becomes slow when I need to extrude on all the z slices of the
> image.
>
> here is the code
>
> double *bounds=appendFilter->GetOutput()->GetBounds();
>
>                vtkSmartPointer<vtkImageData> binary_image=vtkSmartPointer<vtkImageData>::New();
>
>                binary_image->SetScalarTypeToUnsignedChar();
>
>                ///Use the smallest mask in which the mesh fits
>
>                // Add two voxels on each side to make sure the mesh fits
>
>                double * samp_origin=imgData->GetOrigin();
>
>                double * spacing=imgData->GetSpacing();
>
>                binary_image->SetSpacing(spacing);
>
>                /// Put the origin on a voxel to avoid small skips
>
>                binary_image->SetOrigin(floor((bounds[0]-samp_origin[0])/spacing[0]-2)*spacing[0]+samp_origin[0],
>
>                                        floor((bounds[2]-samp_origin[1])/spacing[1]-2)*spacing[1]+samp_origin[1],
>
>                                        floor((bounds[4]-samp_origin[2])/spacing[2]-2)*spacing[2]+samp_origin[2]);
>
>                double * origin=binary_image->GetOrigin();
>
>                binary_image->SetExtent(0,ceil((bounds[1]-origin[0])/spacing[0]+4),
>
>                                        0,ceil((bounds[3]-origin[1])/spacing[1]+4),
>
>                                        0,ceil((bounds[5]-origin[2])/spacing[2])+4);
>
>                binary_image->AllocateScalars();
>
>                memset(binary_image->GetScalarPointer(),0,binary_image->GetDimensions()[0]*binary_image->GetDimensions()[1]*binary_image->GetDimensions()[2]*sizeof(unsigned char));
>
>             qDebug()<<binary_image->GetExtent()[0]<<binary_image->GetExtent()[1]<<binary_image->GetExtent()[2]<<
>
>                     binary_image->GetExtent()[3]<<binary_image->GetExtent()[4]<<binary_image->GetExtent()[5]<<"Binary image extent";
>
>                vtkSmartPointer<vtkPolyDataToImageStencil> sts=vtkSmartPointer<vtkPolyDataToImageStencil>::New();
>
>                //The following line is extremely important
>
>                //http://www.nabble.com/Bug-in-vtkPolyDataToImageStencil--td23368312.html#a23370933
>
>                sts->SetTolerance(0);
>
>                sts->SetInformationInput(binary_image);
>
>                vtkSmartPointer<vtkLinearExtrusionFilter> extrude=vtkSmartPointer<vtkLinearExtrusionFilter>::New();
>
>                extrude->SetInput(appendFilter->GetOutput());
>
>                extrude->CappingOn();
>
>                ///We extrude in the -slice_spacing direction to respect the FOCAL convention
>
>                extrude->SetVector(0, 0,-zSpacing);//Correct Z spacing is important
>
>                //qDebug()<<-zSpacing<<"-ve zSpacing";
>
>                sts->SetInput(extrude->GetOutput());
>
>                qDebug()<<t->currentTime()<<"Stencil start";
>
>                vtkSmartPointer<vtkImageStencil> stencil=vtkSmartPointer<vtkImageStencil>::New();
>
>                stencil->SetStencil(sts->GetOutput());
>
>                stencil->SetInput(binary_image);
>
>                stencil->Update();
>
>                qDebug()<<t->currentTime()<<"Stencil stop";
>
>
>
>
>
> The bslowest execution occus only when I do this for the body contour,
> which is there on every dicom image slice.
>
> Can you have a look at the code and suggest ways to improve the
> speed,please. The idea is from vv open source viewer.
>
> Thanks
>
> Jothy
>
>
> On Mon, Jul 25, 2011 at 1:18 PM, David Gobbi <david.gobbi at gmail.com>wrote:
>
>> Hi Jothy,
>>
>> How are you creating the stencil?  The reason that I ask is that
>> vtkImageStencil is a fast filter, so I doubt that it is what is causing
>> the slowdown.  But some of the stencil source filters, especially
>> vtkImplicitFunctionToImageStencil, can be slow depending on how
>> they are used.
>>
>>  - David
>>
>> On Mon, Jul 25, 2011 at 3:09 AM, Jothy <jothybasu at gmail.com> wrote:
>> >
>> > I am running in release mode in QtCreator. I even tried running from
>> outside and also copiling with -o2 and -03 optimization flags, but there is
>> no improvement.
>> >
>> > Jothy
>> >
>> > On Sun, Jul 24, 2011 at 4:18 AM, John Drescher <drescherjm at gmail.com>
>> wrote:
>> >>
>> >> On Sat, Jul 23, 2011 at 3:48 PM, Jothy <jothybasu at gmail.com> wrote:
>> >> > Hi all,
>> >> >
>> >> > In my program vtkImageStencil take alomost 2-3 mins for vtkImageData
>> with
>> >> > 160 slices with 0.9x0.9x2.5 spacing. Is there any way to speed up
>> this?
>> >> >
>> >> > Thanks
>> >> >
>> >>
>> >> Are you running release mode? And also not inside the debugger?
>> >> Running debug mode or inside the Visual Studio can slow down some
>> >> applications considerably ( I am talking about > 10 times slower).
>> >>
>> >> John
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110725/b98303bd/attachment.htm>


More information about the vtkusers mailing list