[vtkusers] ImagePlaneWidget mapping ImageData with 3 scalars to rgb

Lars Matthäus lars.matthaeus at web.de
Mon Sep 28 10:29:33 EDT 2015


I have a DataSet with 3 scalar components, e.g. from loading a stack of
rgb images with the vtkJPEGReader. I want to slice through it using a
vtkImagePlaneWidget. Unfortunately, I cannot get the widget to display
the rgb image; I only get a grey image.

I played with the vtkLookupTable::SetVectorModeToRGBColors successfully
to display the image as rgb using a vtkImageSliceMapper, but when I use
the same lookup table for the vtkImagePlaneWidget, I get a grey image again.

I attach some java sample code comparing the vtkImagePlaneWidget and the
vtkImageSliceMapper as well as the output of the program.

Any help appreciated!
Lars
-------------- next part --------------
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import vtk.vtkCanvas;
import vtk.vtkImageData;
import vtk.vtkImagePlaneWidget;
import vtk.vtkImageSlice;
import vtk.vtkImageSliceMapper;
import vtk.vtkJPEGReader;
import vtk.vtkLookupTable;
import vtk.vtkNativeLibrary;


public class SimpleVTK extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JButton exitButton;

	// Load VTK library and print which library was not properly loaded
	static {
		if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
			for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
				if (!lib.IsLoaded()) {
					System.out.println(lib.GetLibraryName() + " not loaded");
				}
			}
		}
		vtkNativeLibrary.DisableOutputWindow(null);
	}
	
	public SimpleVTK() {
		super(new BorderLayout());

		// create vtk display with data
		vtkCanvas renWin = new vtkCanvas();
		createVtkInput(renWin);

		// Add Java UI components
		exitButton = new JButton("Exit");
		exitButton.addActionListener(this);

		add(renWin, BorderLayout.CENTER);
		add(exitButton, BorderLayout.SOUTH);
	}

	void createVtkInput(vtkCanvas renWin) {
		// create input
		vtkJPEGReader reader = new vtkJPEGReader();
		reader.SetFileName("C:/VTKData/Data/beach.jpg");
		vtkImageData id = reader.GetOutput();
		reader.Update();
		int[] dim = id.GetDimensions();

		// lookup table: map the 3 scalars to rgb
		vtkLookupTable table = new vtkLookupTable();
		table.SetRampToLinear();
		table.SetRange(0.0, 255.0);
		table.SetValueRange(0.0, 1.0);
		table.SetSaturationRange(0.0, 0.0);
		table.SetVectorModeToRGBColors();
		table.Build();

		// image plane widget
		vtkImagePlaneWidget ipw = new vtkImagePlaneWidget();
		ipw.SetInteractor(renWin.getRenderWindowInteractor());
		ipw.SetInputData(id);
		ipw.SetPlaneOrientation(2);
		ipw.GetColorMap().SetLookupTable(table);
		ipw.GetColorMap().SetOutputFormatToRGB();
		ipw.On();

		// slice mapper
		vtkImageSliceMapper imageMapper = new vtkImageSliceMapper();
		imageMapper.SetInputData(id);
		vtkImageSlice image = new vtkImageSlice();
		image.SetMapper(imageMapper);
		image.GetProperty().SetLookupTable(table);
		image.SetPosition(1.5 * dim[0], 0, 0); // shift in x direction
		renWin.GetRenderer().AddViewProp(image);
		
		// proper initial display
		renWin.GetRenderer().ResetCamera();
	}


	/** An ActionListener that listens to the button. */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(exitButton)) {
			System.exit(0);
		}
	}

	public static void main(String s[]) {
		JFrame frame = new JFrame("SimpleVTK");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout());
		frame.getContentPane().add(new SimpleVTK(), BorderLayout.CENTER);
		frame.setSize(400, 400);
		frame.setVisible(true);
	}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vtk_ImagePlaneWidget_vs_ImageSliceMapper.jpg
Type: image/jpeg
Size: 18890 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150928/50a91c8e/attachment.jpg>


More information about the vtkusers mailing list