[vtkusers] How to create vtkActor2D from vtkContourFilter?

Eric E. Monson emonson at cs.duke.edu
Thu Apr 8 16:42:49 EDT 2010


Hello Andrej,

Well, maybe someone else can give their opinion, too, but personally I would just use a standard vtkRenderWindow, and add all three actors to the vtkRenderer that you add to that render window. 

Each of the images can be shown with a vtkImageActor, and those can be placed anywhere in 3D space with SetPosition(), and then you can just use a regular vtkActor for the contour filter output and place it "in front" of the image actors.

Here's a piece of Python code that does this, if you want to have a look. (Sorry the beginning is more complicated than it should be, I was already playing with VTK/Examples/ImageProcessing/Python/ImageSlicing.py and this was an easy way to get two grayscale images to superimpose...) It takes two images, gives one an orange and one a yellow color map with transparency, then overlays them. (I positioned them separately in space, but I think you can put these on the same Z plane.) Then, I place the contour output "over the top" of the images. (If you change the interactor style to TrackballCamera, you can roll them around and see the separation, which doesn't have to be this extreme.)

Again, maybe others have a better way to do this, but it works for keeping the spatial order as you want it, and they all move together in the window.

Good luck,
-Eric

===================

#!/usr/bin/env python

import vtk

VTK_DATA_ROOT = '/Users/emonson/Programming/VTK_cvs/VTKData'

# Start by loading some data.
reader = vtk.vtkImageReader2()
reader.SetFilePrefix(VTK_DATA_ROOT + "/Data/headsq/quarter")
reader.SetDataExtent(0, 63, 0, 63, 1, 93)
reader.SetDataSpacing(1.0, 1.0, 1.0)
reader.SetDataOrigin(0.0, 0.0, 0.0)
reader.SetDataScalarTypeToUnsignedShort()
reader.UpdateWholeExtent()

# Calculate the center of the volume
reader.GetOutput().UpdateInformation()
(xMin, xMax, yMin, yMax, zMin, zMax) = reader.GetOutput().GetWholeExtent()
(xSpacing, ySpacing, zSpacing) = reader.GetOutput().GetSpacing()
(x0, y0, z0) = reader.GetOutput().GetOrigin()

center = [x0 + xSpacing * 0.5 * (xMin + xMax),
          y0 + ySpacing * 0.5 * (yMin + yMax),
          z0 + zSpacing * 0.5 * (zMin + zMax)]

renderer = vtk.vtkRenderer()
renderer.SetBackground(1,1,1)

resliceList = []
tableList = []
colorList = []
actorList = []

imgOffset = 70

for ii in range(2):
		
	zpos = z0 + zSpacing*(zMin+ii*imgOffset)
	axial = vtk.vtkMatrix4x4()
	axial.DeepCopy((1, 0, 0, center[0],
					0, 1, 0, center[1],
					0, 0, 1, zpos,
					0, 0, 0, 1))
					
	# Extract a slice in the desired orientation
	reslice = vtk.vtkImageReslice()
	reslice.SetInputConnection(reader.GetOutputPort())
	reslice.SetOutputDimensionality(2)
	reslice.SetResliceAxes(axial)
	reslice.SetInterpolationModeToNearestNeighbor()
	reslice.Update()
	
	resliceList.append(reslice)
	
	# Create a greyscale lookup table
	table = vtk.vtkLookupTable()
	table.SetRange(0, 2000) # image intensity range
	table.SetHueRange(ii*0.1,0.1+ii*0.1)
	table.SetValueRange(0.0, 1.0) # from black to white
	table.SetSaturationRange(0.0, 0.8) # no color saturation
	table.SetAlphaRange(0,0.8)
	table.SetRampToLinear()
	table.Build()
	
	tableList.append(table)
	
	# Map the image through the lookup table
	color = vtk.vtkImageMapToColors()
	color.SetLookupTable(tableList[ii])
	color.SetInputConnection(resliceList[ii].GetOutputPort())
	
	colorList.append(color)
	
	# Display the image
	actor = vtk.vtkImageActor()
	actor.SetInput(colorList[ii].GetOutput())
	actor.SetPosition(0, 0, ii*1.0)
	actorList.append(actor)
	
	renderer.AddActor(actorList[ii])
	
contour = vtk.vtkContourFilter()
contour.SetInputConnection(resliceList[0].GetOutputPort(0))
contour.GenerateValues(5,0,2000)
contour.SetComputeScalars(True)

polyMapper = vtk.vtkPolyDataMapper()
polyMapper.SetInputConnection(contour.GetOutputPort(0))
# Uncomment next two to color contour lines
# polyMapper.SetScalarModeToUsePointData()
# polyMapper.SetScalarRange(0,2000)
polyMapper.SetScalarVisibility(False)
polyActor = vtk.vtkActor()
polyActor.SetMapper(polyMapper)
polyActor.GetProperty().SetColor(0,0,0)
polyActor.GetProperty().SetLineWidth(1)
polyActor.SetPosition(0,0,2.0)

renderer.AddActor(polyActor)

# Set up the interaction
interactorStyle = vtk.vtkInteractorStyleImage()
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetInteractorStyle(interactorStyle)

window = vtk.vtkRenderWindow()
window.AddRenderer(renderer)
window.SetInteractor(interactor)
window.Render()

# Start interaction
interactor.Start()



On Apr 8, 2010, at 9:49 AM, Andrej Gluhov wrote:

> Hi, Eric.
> 
> Thanks for the response. "Images from vtkActor2D gets under 2D images" is a mistake. Of course, I meant vtkActor. The problem is that the contour is not visible when displaying, but if you stretch the window and then vtkActor2D remains in the lower left corner, and the Contours can be seen in the background (under vtkActor2D). I tried to change the position of the coordinate Z, but it does not yield results. vtkActor2D has only x,y coordinates.
> And the window I create in order to then attach vtkImageViewer2 in WindowsForm.
> 
> 8 апреля 2010 г. 17:16 пользователь Eric E. Monson <emonson at cs.duke.edu> написал:
> Hey Andrej,
> 
> I'm trying to understand your problem, but I'm not sure exactly what you mean by "Images from vtkActor2D gets under 2D images". If the problem is that your images show up in front of your contour lines so that the contours are hidden, you can use something like:
> 
> actor.SetPosition(0, 0, 0.01);
> 
> to move the contours out in front of the image, and not have to change the polydata into imagedata.
> 
> Also, you seem to be trying to create a vtkRenderWindow at the beginning of your code but you are using the vtkImageViewer2 later. If you want to view all of your objects in one window, you can use just one or the other (ImageViewer2 creates a render window for you).
> 
> If this doesn't address your problem, be sure to try again to clarify the trouble you're having and I'm sure someone can help.
> 
> Talk to you later,
> -Eric
> 
> ------------------------------------------------------
> Eric E Monson
> Duke Visualization Technology Group
> 
> 
> On Apr 8, 2010, at 3:17 AM, Andrej Gluhov wrote:
> 
> > Hi.
> > I have this problem of combining vtkActor and vtkActor2D for vtkImageViewer2D. Images from vtkActor2D gets under 2D images. I understand that the problem is converting vtkPolyData to vtkImageData, I could use vtkImageData, but do not know how to create it on the contour points.
> >
> >             //Create the RenderWindow, Renderer and RenderWindowInteractor
> >             vtkRenderer ren1 = new vtkRenderer();
> >             vtkRenderWindow renWin = reWin.GetRenderWindow();
> >             renWin.AddRenderer(ren1);
> >             vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
> >             iren.SetRenderWindow(renWin);
> >
> >             vtkDICOMImageReader DicomReader = new vtkDICOMImageReader();
> >             DicomReader.SetFileName(m_strFilePath);
> >             DicomReader.Update();
> >
> >             vtkImageMapper DicomMapper = new vtkImageMapper();
> >             DicomMapper.SetInputConnection(DicomReader.GetOutputPort());
> >             DicomMapper.SetColorWindow(255.0);
> >             DicomMapper.SetColorLevel(127.5);
> >
> >             vtkActor2D DicomActor = new vtkActor2D();
> >             DicomActor.SetMapper(DicomMapper);
> >
> >             vtkImageData vol = new vtkImageData();
> >             vol.SetDimensions(512, 512, 1);
> >             vol.SetSpacing(1, 1, 1);
> >             vol.SetOrigin(0, 0, 0);
> >             vol.SetScalarTypeToFloat();
> >             vol.AllocateScalars();
> >
> >             vtkFloatArray scalars = new vtkFloatArray();
> >             for (int i = 0; i < 512; i++)
> >             {
> >                 for (int j = 0; j < 512; j++)
> >                 {
> >                     scalars.InsertTuple1(i * 512 + j, m_DoseMap[i * 512 + j]);
> >                 }
> >             }
> >
> >             vol.GetPointData().SetScalars(scalars);
> >             vol.Update();
> >
> >             double[] DoseRange = vol.GetScalarRange();
> >
> >             vtkLookupTable LUT = new vtkLookupTable();
> >             LUT.SetTableRange(DoseRange[0], DoseRange[1]);//min dose,max dose
> >             LUT.SetNumberOfColors(1000);
> >             LUT.SetSaturationRange(1, 1);
> >             LUT.SetHueRange(0.67, 0);//blue to red
> >             LUT.SetAlphaRange(0, 0.3);//opacity
> >             LUT.Build();
> >
> >             vtkImageMapToColors mapToRGBA = new vtkImageMapToColors();
> >             mapToRGBA.SetInput(vol);
> >             mapToRGBA.SetOutputFormatToRGBA();
> >             mapToRGBA.SetLookupTable(LUT);
> >
> >             vtkImageMapper ColorMapper = new vtkImageMapper();
> >             ColorMapper.SetInputConnection(mapToRGBA.GetOutputPort());
> >             ColorMapper.SetColorWindow(255.0);
> >             ColorMapper.SetColorLevel(127.5);
> >
> >             vtkActor2D ColorActor = new vtkActor2D();
> >             ColorActor.SetMapper(ColorMapper);
> >
> >             vtkContourFilter cf = new vtkContourFilter();
> >             cf.SetInput(vol);
> >             //cf.SetValue(0, Convert.ToDouble(dataGridViewIsodose.Rows[i].Cells[1].Value));
> >             cf.SetValue(0, 100);
> >             cf.Update();
> >
> >             vtkPolyDataMapper mapper = new vtkPolyDataMapper();
> >             mapper.SetInputConnection(cf.GetOutputPort());
> >             mapper.ScalarVisibilityOff();
> >
> >             vtkActor actor = new vtkActor();
> >             actor.SetMapper(mapper);
> >             actor.GetProperty().SetColor(1, 0, 0);
> >
> >             vtkImageViewer2 viewer = new vtkImageViewer2();
> >             viewer.SetupInteractor(iren);
> >             viewer.GetRenderer().AddActor(DicomActor);
> >             viewer.GetRenderer().AddActor(ColorActor);
> >             viewer.GetRenderer().AddActor(actor);
> >             viewer.GetRenderer().ResetCamera();
> >             viewer.Render();
> >
> > Thanks in advance.
> >
> > --
> > С Уважением,
> > Андрей.
> > Best regards, Andrew
> > _______________________________________________
> > Powered by www.kitware.com
> >
> > Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> >
> > Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
> >
> > Follow this link to subscribe/unsubscribe:
> > http://www.vtk.org/mailman/listinfo/vtkusers
> 
> 
> 
> 
> -- 
> С Уважением,
> Андрей.
> Best regards, Andrew

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100408/1d9fa740/attachment.htm>


More information about the vtkusers mailing list