[vtkusers] Filling a polygon in ImageData

Karthik Krishnan karthik.krishnan at kitware.com
Sat Dec 15 11:49:04 EST 2007


As I understand, you are essentially trying to rasterize a bunch of closed

Here are two ways to do this..... (wrong and right)

1. Use vtkPolyDataToImageStencil to generate vtkImageStencilData. And then
using vtkImageStencil to generate the rasterized image
  See Examples/GUI/Tcl/ImageTracerWidget.tcl
On a side note, I have on occassions noted numerical issues with the
rasterization done by vtkPolyDataToImageStencil,  when you have sharp edges
in your polyline, which leads to a streaking effect along an axis..
(see http://www.vtk.org/pipermail/vtkusers/2007-May/091141.html)

That said, this is the quick and dirty approach. The right one (and the
faster one is below)....
OBBTrees are an overkill for something as simple as rasterization. And the
ImageTracerWidget example in VTK misdirects a lot of people into using this

2.  vtkPolygon::PointInPolygon will do the job for you...

Just get the bounds of the closed polyline. For every pixel in your image
that lies within those bounds, check if its world coordinates are within the
Polygon and you are done.

The method is very fast. It just checks the number of times a ray starting
at the point intersects the polygon. If it intersects it, even number of
times, it is outside, odd implies inside. In reality, it throws a bunch of
rays in different directions, and does a voting, to overcome numerical
issues that might arize when one or more rays fall exactly on an edge of the


Good luck :)

Karthik Krishnan
R&D Engineer,
Kitware Inc.

PS: If you choose to use method (2), It would be great if you could wrap it
into a VTK filter (deriving from vtkImageAlgorithm) and contribite it :)

On 12/14/07, kent williams <nkwmailinglists at gmail.com> wrote:
> I need to go from a collection of closed polygons each associate with
> a slice of an image volume, to an image volume defining a mask.
> In other words, for each slice in a volume, I want to set pixels to 1
> if they're inside a polygon in the collection, and set them to zero if
> they're not.
> Suggestions?
> _______________________________________________
> This is the private VTK discussion list.
> Please keep messages on-topic. Check the FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20071215/71e4b291/attachment.htm>

More information about the vtkusers mailing list