[vtkusers] Cut/Paste region of vtkImageData defined by a contour

David Gobbi david.gobbi at gmail.com
Wed Sep 17 11:41:26 EDT 2014

On Wed, Sep 17, 2014 at 8:56 AM, Najzero <adrian.gabriel at 4plus.de> wrote:
> Hello everyone,
> I currently have the joy of refacturing some code from an old project to
> enable new functions.
> It is running on vtk 5.6 with some local patches ( saying that, because I
> can't use vtkLassooStencilSource ).

Why not?  It was never removed from VTK.  E.g. in VTK 6:
Or do you mean that you're planning to stick with VTK 5.6?

> *What I understand there is:*
> - a vtkImageData (filled/scaled from DICOM source)
> - a set of points that is defined as a "contour" drawn by an user ( can be
> in any 2d form, most of the time its a rectangle, but you user can free
> draw)
> - a button saying "cut" to extract the pixels from the vtkImageData,
> creating a new vtkImageData and leaving a black hole in the origin image.
> The old solution uses a bunch of nasty hacks, filling a mask with some magic
> numbers... long story short, only the guy who wrote that stuff 5 years ago
> knows what he did ( then ).

Very common problem, with two solutions: 1) reverse engineer the code,
or 2) rewrite from scratch ;)

The tricky thing is that all the crazy stuff in the original code is probably
there for a reason, so a re-write is likely to be just as crazy (but hopefully
better documented).

> *What I did/tried*
> Found example: http://www.vtk.org/Wiki/VTK/Examples/Cxx/Images/ImageStencil
> hard coded an example by using the vtkImageToImageStencil and simply used
> two stencils; one with ReverseStencil on and one with off.
> Leaving me with the data to update the old plane texture and one to stick to
> the newly object.
> After that success, I thought to change it to use a
> vtkPolyDataToImageStencil and feed it with the contour-points.
> /Pseudo Code:/
> vtkSmartPointer<vtkPolyDataToImageStencil> pol2stenc = ...;
> pol2stenc->SetInput( contourPoly ); + Update();

For vtkPolyDataToImageStencil, you also need to set the following:


Or you can call the SetInformationInput() with an updated image, which
will copy the origin, spacing, and extent from that time.

Also note the following: If you are using VTK 5.6 and cannot upgrade,
then vtkPolyDataToImageStencil will not even work for you.  Originally
vtkPolyDataToImageStencil filter required that the input was a closed
surface (i.e. made of triangle or polygon faces).  It was only much later
(either VTK 5.8 or VTK 5.10, I can't remember which) when it could use
polyline contours.

 - David

More information about the vtkusers mailing list