Reputation: 1344
I'm generating a contour in 2D through vtkContourFilter
. (see image attached below) Now I would like to get the region that is inside the contour and save it as vtkImageData
or something similar that would result in an image just with the data inside the contour. Everything else would be black, just to have the same dimensions as the slice.
I don't know how to get the region inside the contour, is there anyway to do it?
This is what I did so far:
import vtk
reader = vtk.vtkXMLImageDataReader()
reader.SetFileName("sample.vti")
reader.GetOutput().SetUpdateExtentToWholeExtent()
reader.Update()
flipYFilter = vtk.vtkImageFlip()
flipYFilter.SetFilteredAxis(1)
flipYFilter.SetInput(reader.GetOutput())
flipYFilter.Update()
image = flipYFilter.GetOutput()
extractSlice = vtk.vtkExtractVOI()
extractSlice.SetInput(image)
extractSlice.SetVOI(image.GetExtent()[0], image.GetExtent()[1], \
image.GetExtent()[2], image.GetExtent()[3], \
5, 5)
extractSlice.SetSampleRate(1, 1, 1)
extractSlice.Update()
contour = vtk.vtkContourFilter()
contour.SetInputConnection(extractSlice.GetOutputPort())
contour.SetValue(1,90)
#How to get the region inside the contour?
Thanks in advance.
Upvotes: 4
Views: 1436
Reputation: 695
You should be able to do this by using vtkPolyDataToImageStencil
followed by vtkImageStencil
, just like in this example
http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataContourToImageData
HTH,
Miro
Upvotes: 2
Reputation: 1858
The vtkContourFilter
is a - in your case - line representation that does not allow for any "inside/outside" filtering. What you want is the vtk.Threshold
threshold = vtk.Threshold()
threshold.SetInputConnection(extractSlice.GetOutputPort())
threshold.ThresholdRange = [37.35310363769531, 276.8288269042969]
The above code is something out of my head, and the two scalars are the min and max values which you apply the threshold to. Have a look at Paraview, which you can use to assemble your visualization and record everything using the Python tracer. This leaves you with python code that you can then use with normal VTK and python, which is great and exactly what you need. But this way, the prototyping process is way faster, than only with python.
Upvotes: 4