Here's a copy of a small working program in Java that shows the issues that I'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("vtkCommonJava");</div>
<div> System.loadLibrary("vtkFilteringJava");</div><div> System.loadLibrary("vtkGenericFilteringJava");</div><div> System.loadLibrary("vtkGraphicsJava");</div><div> System.loadLibrary("vtkHybridJava");</div>
<div> System.loadLibrary("vtkImagingJava");</div><div> System.loadLibrary("vtkIOJava");</div><div> System.loadLibrary("vtkRenderingJava");</div><div> System.loadLibrary("vtkVolumeRenderingJava");</div>
<div> System.loadLibrary("vtkWidgetsJava");</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'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<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("Same center: " + sameCenter);</div><div> System.out.println("Same bounds: " + 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"><<a href="mailto:jonmorra@gmail.com">jonmorra@gmail.com</a>></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'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"><<a href="mailto:david.gobbi@gmail.com" target="_blank">david.gobbi@gmail.com</a>></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'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 <<a href="mailto:jonmorra@gmail.com" target="_blank">jonmorra@gmail.com</a>> wrote:<br>
> I tried that and now the contour appears to both erode and dilate. What I'm<br>
> doing it to take as input some contour data. I then convert it to a binary<br>
> image for internal storage using vtkPolyDataToImageStencil. I then take a<br>
> slice of the contour and show it to the user using vtkMarchingSquares. The<br>
> user can then modify the contour. When the user is done modifying the<br>
> contour I convert it back to binary data for storage using the<br>
> vtkPolyDataToImageStencil, and then again reslice it and use<br>
> vtkMarchingSquares to show the user the contour again. This process is very<br>
> quick. What is happening is that the contour appears to be moving on the<br>
> screen as this cycle happens even if no edits are done.<br>
> That is the underlying problem I need to fix.<br>
><br>
> On Wed, Feb 23, 2011 at 11:59 AM, David Gobbi <<a href="mailto:david.gobbi@gmail.com" target="_blank">david.gobbi@gmail.com</a>> wrote:<br>
>><br>
>> I've never used marching squares, but I have a guess as<br>
>> to what the problem might be. If your binary image has<br>
>> values "0" and "1" then you should contour it at "0.5".<br>
>><br>
>> - David<br>
>><br>
>><br>
>> On Wed, Feb 23, 2011 at 12:27 PM, Jonathan Morra <<a href="mailto:jonmorra@gmail.com" target="_blank">jonmorra@gmail.com</a>><br>
>> wrote:<br>
>> > If that's the case, then maybe I could have issues on the other side<br>
>> > (converting binary images to contours, I do both). For this I'm using<br>
>> > vtkMarchingSquares followed by vtkStripper<br>
>> > int[] extent = binaryOrgan.GetExtent();<br>
>> > switch (orientation) {<br>
>> > case OrthoPanel.ORIENTATION_XY:<br>
>> > extent[4] = slice;<br>
>> > extent[5] = slice;<br>
>> > break;<br>
>> > case OrthoPanel.ORIENTATION_XZ:<br>
>> > extent[2] = slice;<br>
>> > extent[3] = slice;<br>
>> > break;<br>
>> > case OrthoPanel.ORIENTATION_YZ:<br>
>> > extent[0] = slice;<br>
>> > extent[1] = slice;<br>
>> > break;<br>
>> > }<br>
>> > vtkMarchingSquares marching = new vtkMarchingSquares();<br>
>> > marching.SetInput(binaryOrgan);<br>
>> > marching.SetImageRange(extent);<br>
>> > marching.SetValue(0, 1);<br>
>> > marching.Update();<br>
>> > vtkPolyData marchingOutput = marching.GetOutput();<br>
>> > vtkStripper stripper = new vtkStripper();<br>
>> > stripper.SetInput(marchingOutput);<br>
>> > stripper.Update();<br>
>> > Does anything look like it could be causing my issues there?<br>
>> > On Wed, Feb 23, 2011 at 11:18 AM, David Gobbi <<a href="mailto:david.gobbi@gmail.com" target="_blank">david.gobbi@gmail.com</a>><br>
>> > wrote:<br>
>> >><br>
>> >> The value "1e-6" is a common tolerance because it is larger than<br>
>> >> most roundoff errors that are likely to occur in the calculations,<br>
>> >> but still small enough that it won't appreciably increase size of the<br>
>> >> region.<br>
>> >><br>
>> >> Setting the tolerance to zero does exactly what you noted. If the<br>
>> >> pixel is exactly on the edge, then it is considered to be inside if the<br>
>> >> edge is a leading edge, or outside if the edge is a trailing edge.<br>
>> >> This is done so that if you have two contours which are adjacent<br>
>> >> (i.e. share an edge), the edge voxels will be considered to be in<br>
>> >> just one of the two contours instead of in both. If the tolerance is<br>
>> >> set larger than zero, then the edge pixels would always be considered<br>
>> >> to be in both contours.<br>
>> >><br>
>> >> If you are dealing with rectangular contours, then the contour<br>
>> >> lines should be made so that they lie halfway between pixels, instead<br>
>> >> of lying directly on top of the pixels. Then there is no uncertainty<br>
>> >> about whether a pixel lies inside or outside.<br>
>> >><br>
>> >> - David<br>
>> >><br>
>> >><br>
>> >> On Wed, Feb 23, 2011 at 11:58 AM, Jonathan Morra <<a href="mailto:jonmorra@gmail.com" target="_blank">jonmorra@gmail.com</a>><br>
>> >> wrote:<br>
>> >> > I had the tolerance set to 0, and setting it to 1e-6 didn't fix the<br>
>> >> > problem.<br>
>> >> > How did you come up with that number? What's wrong with setting it<br>
>> >> > to<br>
>> >> > 0?<br>
>> >> > In my case usually the left and top side of the vtkImageData is<br>
>> >> > being<br>
>> >> > eroded, such that if I call the filter many times, the vtkImageData<br>
>> >> > will<br>
>> >> > eventually be blank because it will all be eroded. However,<br>
>> >> > sometimes<br>
>> >> > the<br>
>> >> > bottom and right grow in size, I haven't figured out which situations<br>
>> >> > cause<br>
>> >> > which.<br>
>> >> ><br>
>> >> > On Wed, Feb 23, 2011 at 10:54 AM, David Gobbi <<a href="mailto:david.gobbi@gmail.com" target="_blank">david.gobbi@gmail.com</a>><br>
>> >> > wrote:<br>
>> >> >><br>
>> >> >> Hi Jonathan,<br>
>> >> >><br>
>> >> >> Whether a pixel is set depends on whether the center of the pixel<br>
>> >> >> is inside or outside the contour, irregardless of what proportion of<br>
>> >> >> the pixel's volume is inside or outside.<br>
>> >> >><br>
>> >> >> The only adjustment is the Tolerance, which should be set to<br>
>> >> >> around 1e-6 so that pixels right on the edge of the contour<br>
>> >> >> are considered to be inside. The tolerance cannot be negative.<br>
>> >> >><br>
>> >> >> - David<br>
>> >> >><br>
>> >> >><br>
>> >> >> On Wed, Feb 23, 2011 at 11:22 AM, Jonathan Morra<br>
>> >> >> <<a href="mailto:jonmorra@gmail.com" target="_blank">jonmorra@gmail.com</a>><br>
>> >> >> wrote:<br>
>> >> >> > I am currently using vtkPolyDataToImageStencil to successfully<br>
>> >> >> > convert<br>
>> >> >> > contours represented as vtkPolyData to binary vtkImageData's.<br>
>> >> >> > However,<br>
>> >> >> > I'm<br>
>> >> >> > noticing a problem with the output sometimes. Sometimes the<br>
>> >> >> > resulting<br>
>> >> >> > binary images are slightly smaller or slightly bigger than the<br>
>> >> >> > poly<br>
>> >> >> > data<br>
>> >> >> > they were made from. I assume this is the result of partial<br>
>> >> >> > volume<br>
>> >> >> > effects.<br>
>> >> >> > I would like to know 2 things<br>
>> >> >> > 1. How does vtkPolyDataToImageStencil handle partial volume.<br>
>> >> >> > 2. Is there a way to tune partial volume in<br>
>> >> >> > vtkPolyDataToImageStencil?<br>
>> >> >> > For<br>
>> >> >> > instance, a parameter which says if the contour includes less than<br>
>> >> >> > x<br>
>> >> >> > percentage of the pixel then that pixel is 0.<br>
>> >> >> > Thanks,<br>
>> >> >> > Jon<br>
>> >> >> > PS If my assumption about partial volume effects is wrong, please<br>
>> >> >> > let<br>
>> >> >> > me<br>
>> >> >> > know.<br>
>> >> >> > _______________________________________________<br>
>> >> >> > Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
>> >> >> ><br>
>> >> >> > Visit other Kitware open-source projects at<br>
>> >> >> > <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
>> >> >> ><br>
>> >> >> > Please keep messages on-topic and check the VTK FAQ at:<br>
>> >> >> > <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
>> >> >> ><br>
>> >> >> > Follow this link to subscribe/unsubscribe:<br>
>> >> >> > <a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>
>> >> >> ><br>
>> >> >> ><br>
>> >> ><br>
>> >> ><br>
>> ><br>
>> ><br>
><br>
><br>
</div></div></blockquote></div><br>
</div></div></blockquote></div><br></div></div>