Here&#39;s a copy of a small working program in Java that shows the issues that I&#39;m talking about.  The program will spit out 2 lines of text.  I need them both to evaluate to true (that the center and bounds stay the same).  In addition a render window will open up and clearly show that the two circles do not line up with each other (again they should).  Please let me know how I can fix this problem.<div>
<br></div><div>Thanks!!</div><div><br><div><div><div>import vtk.*;</div><div>import java.util.Arrays;</div><div><br></div><div>public class PolyDataToImageToPolyDataTest {</div><div>    static {</div><div>        System.loadLibrary(&quot;vtkCommonJava&quot;);</div>
<div>        System.loadLibrary(&quot;vtkFilteringJava&quot;);</div><div>        System.loadLibrary(&quot;vtkGenericFilteringJava&quot;);</div><div>        System.loadLibrary(&quot;vtkGraphicsJava&quot;);</div><div>        System.loadLibrary(&quot;vtkHybridJava&quot;);</div>
<div>        System.loadLibrary(&quot;vtkImagingJava&quot;);</div><div>        System.loadLibrary(&quot;vtkIOJava&quot;);</div><div>        System.loadLibrary(&quot;vtkRenderingJava&quot;);</div><div>        System.loadLibrary(&quot;vtkVolumeRenderingJava&quot;);</div>
<div>        System.loadLibrary(&quot;vtkWidgetsJava&quot;);</div><div>    }</div><div><br></div><div>    public static void main(String[] args) {</div><div>        vtkRegularPolygonSource polygonSource = new vtkRegularPolygonSource();</div>
<div><br></div><div>        polygonSource.SetNumberOfSides(50);</div><div>        polygonSource.SetRadius(50);</div><div>        polygonSource.GeneratePolygonOff();</div><div>        polygonSource.Update();</div><div><br>
</div><div>        vtkImageData blankImage = new vtkImageData();</div><div>        int[] extent = {0, 512, 0, 512, 0, 100};</div><div>        double[] origin = {-256, -256, 0};</div><div>        double[] spacing = {1, 1, 1};</div>
<div>        blankImage.SetExtent(extent);</div><div>        blankImage.SetOrigin(origin);</div><div>        blankImage.SetSpacing(spacing);</div><div>        blankImage.SetScalarTypeToUnsignedChar();</div><div>        blankImage.AllocateScalars();</div>
<div><br></div><div>        vtkPolyData initialCircle = polygonSource.GetOutput();</div><div>        vtkPolyData drawnCircle = new vtkPolyData();</div><div>        drawnCircle.DeepCopy(initialCircle);</div><div>        // I&#39;m choosing 10 here because this problem gets exacerbated after each run.  By varying this, the problem will get less or more severe.</div>
<div>        for (int i=0; i&lt;10; i++) {</div><div>            vtkLinearExtrusionFilter extruder = new vtkLinearExtrusionFilter();</div><div>            extruder.SetInput(drawnCircle);</div><div>            extruder.SetVector(spacing);</div>
<div>            extruder.Update();</div><div>            vtkPolyData extruderOutput = extruder.GetOutput();</div><div><br></div><div>            vtkPolyDataToImageStencil pol2Stenc = new vtkPolyDataToImageStencil();</div>
<div>            pol2Stenc.SetTolerance(0);</div><div>            pol2Stenc.SetInput(extruderOutput);</div><div>            pol2Stenc.SetInformationInput(blankImage);</div><div>            pol2Stenc.Update();</div><div>            vtkImageStencilData pol2StencOutput = pol2Stenc.GetOutput();</div>
<div><br></div><div>            vtkImageStencil stencil = new vtkImageStencil();</div><div>            stencil.SetInput(blankImage);</div><div>            stencil.ReverseStencilOn();</div><div>            stencil.SetStencil(pol2StencOutput);</div>
<div>            stencil.Update();</div><div><br></div><div>            vtkImageData image = stencil.GetOutput();</div><div>            </div><div>            vtkMarchingSquares marching = new vtkMarchingSquares();</div><div>
            marching.SetInput(image);</div><div>            // For some reason the circle moves in all 3 dimensions, so I need to vary the z-coordinate w.r.t the iteration number</div><div>            marching.SetImageRange(new int[] {0, 512, 0, 512, i+1, i+1});</div>
<div>            marching.SetValue(0, 1);</div><div>            marching.Update();</div><div>            vtkPolyData marchingOutput = marching.GetOutput();</div><div><br></div><div>            vtkStripper stripper = new vtkStripper();</div>
<div>            stripper.SetInput(marchingOutput);</div><div>            stripper.Update();</div><div><br></div><div>            drawnCircle = stripper.GetOutput();</div><div>        }</div><div><br></div><div>        boolean sameCenter = Arrays.equals(drawnCircle.GetCenter(), initialCircle.GetCenter());</div>
<div>        boolean sameBounds = Arrays.equals(drawnCircle.GetBounds(), initialCircle.GetBounds());</div><div><br></div><div>        System.out.println(&quot;Same center: &quot; + sameCenter);</div><div>        System.out.println(&quot;Same bounds: &quot; + sameBounds);</div>
<div><br></div><div>        vtkPolyDataMapper circleMapper = new vtkPolyDataMapper();</div><div>        circleMapper.SetInput(initialCircle);</div><div>        circleMapper.ScalarVisibilityOff();</div><div>        circleMapper.Update();</div>
<div><br></div><div>        vtkActor circleActor = new vtkActor();</div><div>        circleActor.SetMapper(circleMapper);</div><div>        circleActor.GetProperty().SetRepresentationToSurface();</div><div>        circleActor.GetProperty().SetColor(1, 0, 0);</div>
<div><br></div><div>        vtkPolyDataMapper outCircleMapper = new vtkPolyDataMapper();</div><div>        outCircleMapper.SetInput(drawnCircle);</div><div>        outCircleMapper.ScalarVisibilityOff();</div><div>        outCircleMapper.Update();</div>
<div><br></div><div>        vtkActor outCircleActor = new vtkActor();</div><div>        outCircleActor.SetMapper(outCircleMapper);</div><div>        outCircleActor.GetProperty().SetRepresentationToSurface();</div><div>        outCircleActor.GetProperty().SetColor(0, 1, 0);</div>
<div><br></div><div>        vtkRenderer ren = new vtkRenderer();</div><div>        ren.AddActor(circleActor);</div><div>        ren.AddActor(outCircleActor);</div><div>        ren.SetBackground(0.1, 0.2, 0.4);</div><div><br>
</div><div>        vtkRenderWindow renWin = new vtkRenderWindow();</div><div>        renWin.AddRenderer(ren);</div><div><br></div><div>        vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();</div><div>        renWin.SetInteractor(iren);</div>
<div>        renWin.Render();</div><div>        ren.ResetCamera();</div><div>        iren.Start();</div><div>    }</div><div>}</div></div><div><br></div><div><br></div><br><div class="gmail_quote">On Wed, Feb 23, 2011 at 1:31 PM, Jonathan Morra <span dir="ltr">&lt;<a href="mailto:jonmorra@gmail.com">jonmorra@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">I tried it but it doesn&#39;t work, any other ideas?  I really appreciate your help.<div><div></div><div class="h5"><br>
<br><div class="gmail_quote">On Wed, Feb 23, 2011 at 1:01 PM, David Gobbi <span dir="ltr">&lt;<a href="mailto:david.gobbi@gmail.com" target="_blank">david.gobbi@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Jonathan,<br>
<br>
If you convert a contour to a binary image and then back to a contour,<br>
you won&#39;t get the exactly the same contour back again.  That said, I<br>
think that you can get what you want as long as you apply the correct<br>
tolerances.  You can try a contour value of 0.9 for marching squares,<br>
which is fairly tight but not so tight that it will cause the edge<br>
problems that I mentioned in my earlier email. It might work, or it<br>
might not work.<br>
<font color="#888888"><br>
 - David<br>
</font><div><div></div><div><br>
<br>
On Wed, Feb 23, 2011 at 1:07 PM, Jonathan Morra &lt;<a href="mailto:jonmorra@gmail.com" target="_blank">jonmorra@gmail.com</a>&gt; wrote:<br>
&gt; I tried that and now the contour appears to both erode and dilate.  What I&#39;m<br>
&gt; doing it to take as input some contour data.  I then convert it to a binary<br>
&gt; image for internal storage using vtkPolyDataToImageStencil.  I then take a<br>
&gt; slice of the contour and show it to the user using vtkMarchingSquares.  The<br>
&gt; user can then modify the contour.  When the user is done modifying the<br>
&gt; contour I convert it back to binary data for storage using the<br>
&gt; vtkPolyDataToImageStencil, and then again reslice it and use<br>
&gt; vtkMarchingSquares to show the user the contour again.  This process is very<br>
&gt; quick.  What is happening is that the contour appears to be moving on the<br>
&gt; screen as this cycle happens even if no edits are done.<br>
&gt; That is the underlying problem I need to fix.<br>
&gt;<br>
&gt; On Wed, Feb 23, 2011 at 11:59 AM, David Gobbi &lt;<a href="mailto:david.gobbi@gmail.com" target="_blank">david.gobbi@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; I&#39;ve never used marching squares, but I have a guess as<br>
&gt;&gt; to what the problem might be.  If your binary image has<br>
&gt;&gt; values &quot;0&quot; and &quot;1&quot; then you should contour it at &quot;0.5&quot;.<br>
&gt;&gt;<br>
&gt;&gt;  - David<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; On Wed, Feb 23, 2011 at 12:27 PM, Jonathan Morra &lt;<a href="mailto:jonmorra@gmail.com" target="_blank">jonmorra@gmail.com</a>&gt;<br>
&gt;&gt; wrote:<br>
&gt;&gt; &gt; If that&#39;s the case, then maybe I could have issues on the other side<br>
&gt;&gt; &gt; (converting binary images to contours, I do both).  For this I&#39;m using<br>
&gt;&gt; &gt; vtkMarchingSquares followed by vtkStripper<br>
&gt;&gt; &gt; int[] extent = binaryOrgan.GetExtent();<br>
&gt;&gt; &gt;         switch (orientation) {<br>
&gt;&gt; &gt;             case OrthoPanel.ORIENTATION_XY:<br>
&gt;&gt; &gt;                 extent[4] = slice;<br>
&gt;&gt; &gt;                 extent[5] = slice;<br>
&gt;&gt; &gt;                 break;<br>
&gt;&gt; &gt;             case OrthoPanel.ORIENTATION_XZ:<br>
&gt;&gt; &gt;                 extent[2] = slice;<br>
&gt;&gt; &gt;                 extent[3] = slice;<br>
&gt;&gt; &gt;                 break;<br>
&gt;&gt; &gt;             case OrthoPanel.ORIENTATION_YZ:<br>
&gt;&gt; &gt;                 extent[0] = slice;<br>
&gt;&gt; &gt;                 extent[1] = slice;<br>
&gt;&gt; &gt;                 break;<br>
&gt;&gt; &gt;         }<br>
&gt;&gt; &gt; vtkMarchingSquares marching = new vtkMarchingSquares();<br>
&gt;&gt; &gt;         marching.SetInput(binaryOrgan);<br>
&gt;&gt; &gt;         marching.SetImageRange(extent);<br>
&gt;&gt; &gt;         marching.SetValue(0, 1);<br>
&gt;&gt; &gt;         marching.Update();<br>
&gt;&gt; &gt;         vtkPolyData marchingOutput = marching.GetOutput();<br>
&gt;&gt; &gt;         vtkStripper stripper = new vtkStripper();<br>
&gt;&gt; &gt;         stripper.SetInput(marchingOutput);<br>
&gt;&gt; &gt;         stripper.Update();<br>
&gt;&gt; &gt; Does anything look like it could be causing my issues there?<br>
&gt;&gt; &gt; On Wed, Feb 23, 2011 at 11:18 AM, David Gobbi &lt;<a href="mailto:david.gobbi@gmail.com" target="_blank">david.gobbi@gmail.com</a>&gt;<br>
&gt;&gt; &gt; wrote:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; The value &quot;1e-6&quot; is a common tolerance because it is larger than<br>
&gt;&gt; &gt;&gt; most roundoff errors that are likely to occur in the calculations,<br>
&gt;&gt; &gt;&gt; but still small enough that it won&#39;t appreciably increase size of the<br>
&gt;&gt; &gt;&gt; region.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Setting the tolerance to zero does exactly what you noted.  If the<br>
&gt;&gt; &gt;&gt; pixel is exactly on the edge, then it is considered to be inside if the<br>
&gt;&gt; &gt;&gt; edge is a leading edge, or outside if the edge is a trailing edge.<br>
&gt;&gt; &gt;&gt; This is done so that if you have two contours which are adjacent<br>
&gt;&gt; &gt;&gt; (i.e. share an edge), the edge voxels will be considered to be in<br>
&gt;&gt; &gt;&gt; just one of the two contours instead of in both.  If the tolerance is<br>
&gt;&gt; &gt;&gt; set larger than zero, then the edge pixels would always be considered<br>
&gt;&gt; &gt;&gt; to be in both contours.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; If you are dealing with rectangular contours, then the contour<br>
&gt;&gt; &gt;&gt; lines should be made so that they lie halfway between pixels, instead<br>
&gt;&gt; &gt;&gt; of lying directly on top of the pixels.  Then there is no uncertainty<br>
&gt;&gt; &gt;&gt; about whether a pixel lies inside or outside.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;  - David<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; On Wed, Feb 23, 2011 at 11:58 AM, Jonathan Morra &lt;<a href="mailto:jonmorra@gmail.com" target="_blank">jonmorra@gmail.com</a>&gt;<br>
&gt;&gt; &gt;&gt; wrote:<br>
&gt;&gt; &gt;&gt; &gt; I had the tolerance set to 0, and setting it to 1e-6 didn&#39;t fix the<br>
&gt;&gt; &gt;&gt; &gt; problem.<br>
&gt;&gt; &gt;&gt; &gt;  How did you come up with that number?  What&#39;s wrong with setting it<br>
&gt;&gt; &gt;&gt; &gt; to<br>
&gt;&gt; &gt;&gt; &gt; 0?<br>
&gt;&gt; &gt;&gt; &gt;  In my case usually the left and top side of the vtkImageData is<br>
&gt;&gt; &gt;&gt; &gt; being<br>
&gt;&gt; &gt;&gt; &gt; eroded, such that if I call the filter many times, the vtkImageData<br>
&gt;&gt; &gt;&gt; &gt; will<br>
&gt;&gt; &gt;&gt; &gt; eventually be blank because it will all be eroded.   However,<br>
&gt;&gt; &gt;&gt; &gt; sometimes<br>
&gt;&gt; &gt;&gt; &gt; the<br>
&gt;&gt; &gt;&gt; &gt; bottom and right grow in size, I haven&#39;t figured out which situations<br>
&gt;&gt; &gt;&gt; &gt; cause<br>
&gt;&gt; &gt;&gt; &gt; which.<br>
&gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; On Wed, Feb 23, 2011 at 10:54 AM, David Gobbi &lt;<a href="mailto:david.gobbi@gmail.com" target="_blank">david.gobbi@gmail.com</a>&gt;<br>
&gt;&gt; &gt;&gt; &gt; wrote:<br>
&gt;&gt; &gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; &gt;&gt; Hi Jonathan,<br>
&gt;&gt; &gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; &gt;&gt; Whether a pixel is set depends on whether the center of the pixel<br>
&gt;&gt; &gt;&gt; &gt;&gt; is inside or outside the contour, irregardless of what proportion of<br>
&gt;&gt; &gt;&gt; &gt;&gt; the pixel&#39;s volume is inside or outside.<br>
&gt;&gt; &gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; &gt;&gt; The only adjustment is the Tolerance, which should be set to<br>
&gt;&gt; &gt;&gt; &gt;&gt; around 1e-6 so that pixels right on the edge of the contour<br>
&gt;&gt; &gt;&gt; &gt;&gt; are considered to be inside.  The tolerance cannot be negative.<br>
&gt;&gt; &gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; &gt;&gt;  - David<br>
&gt;&gt; &gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; &gt;&gt; On Wed, Feb 23, 2011 at 11:22 AM, Jonathan Morra<br>
&gt;&gt; &gt;&gt; &gt;&gt; &lt;<a href="mailto:jonmorra@gmail.com" target="_blank">jonmorra@gmail.com</a>&gt;<br>
&gt;&gt; &gt;&gt; &gt;&gt; wrote:<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; I am currently using vtkPolyDataToImageStencil to successfully<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; convert<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; contours represented as vtkPolyData to binary vtkImageData&#39;s.<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt;  However,<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; I&#39;m<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; noticing a problem with the output sometimes.  Sometimes the<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; resulting<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; binary images are slightly smaller or slightly bigger than the<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; poly<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; data<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; they were made from.  I assume this is the result of partial<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; volume<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; effects.<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt;  I would like to know 2 things<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; 1.  How does vtkPolyDataToImageStencil handle partial volume.<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; 2.  Is there a way to tune partial volume in<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; vtkPolyDataToImageStencil?<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt;  For<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; instance, a parameter which says if the contour includes less than<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; x<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; percentage of the pixel then that pixel is 0.<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; Thanks,<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; Jon<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; PS If my assumption about partial volume effects is wrong, please<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; let<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; me<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; know.<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; _______________________________________________<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; Visit other Kitware open-source projects at<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; Please keep messages on-topic and check the VTK FAQ at:<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; Follow this link to subscribe/unsubscribe:<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt; <a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;<br>
&gt;<br>
</div></div></blockquote></div><br>
</div></div></blockquote></div><br></div></div>