VTK read and display 2D raw data
In order to create a simple 3D and 2D data visualization tool I took a look at the Visualization Toolkit. I found everything I needed there – it’s a really great collection of C++ classes with bindings for various other programming languages (TCL, Python). However the documentation is somewhat confusing and lacking simple basic things although a very good document on how to install compile and use it together with Visual Studio can be found here and a great blog aiming at overcoming the lack of tutorials can be found here.
I had quite a hard time figuring out the easiest thing: read raw 2D data and visualize it. After I played around with it for a while I figured it out. It’s actually pretty simple. Here’s the code:
/******************************************************************************* Simple VTK Example A very simple VTK example - a raw 2D datafile is read it's magnified by a factor of 3 and then it's displayed on a window. The pipeline: vtkImageReader->vtkImageMagnify->vtkRenderWindow The image file is a raw file of 64 rows * 64 columns. The values are 2 byte (short int) long. It's a greyscale dataset (NumberOfScalrComponents = 1) *******************************************************************************/ #include "vtkImageReader.h" #include "vtkImageViewer.h" #include "vtkImageMagnify.h" #include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #define IMG_FILE "C:data.raw" int main (void) { vtkImageReader *imgReader = vtkImageReader::New(); imgReader->SetFileDimensionality(2); imgReader->SetFileName(IMG_FILE); imgReader->SetNumberOfScalarComponents(1); imgReader->SetDataExtent(0,63, 0,63, 0,0); vtkImageMagnify *imgMagnify = vtkImageMagnify::New(); imgMagnify->SetInputConnection(imgReader->GetOutputPort()); imgMagnify->SetMagnificationFactors(3, 3, 3); vtkImageViewer* imgViewer = vtkImageViewer::New(); imgViewer->SetInputConnection(imgMagnify->GetOutputPort()); vtkRenderWindow *renWindow = vtkRenderWindow::New(); renWindow->AddRenderer(imgViewer->GetRenderer()); renWindow->SetSize(64*3, 64*3); vtkRenderWindowInteractor *renWindowInteractor = vtkRenderWindowInteractor::New(); renWindowInteractor->SetRenderWindow(renWindow); renWindowInteractor->Initialize(); renWindowInteractor->Start(); return 0; }
9 Comments
Mh, could it be that you didn’t specified the file path correctly?
i did try the 2 data could you pls tell me what all changes shoudl i do for 3d data?
can i use the same code in linux?
my data is 16 bit unsigned int
frency,
loading 3D data should be even easier than 2D data. You could check out http://vtk.org/Wiki/VTK_Online_Tutorials and see if you find something that ansers your question. The same code should rin in linux, yes and your data format should not be a problem.
me too it gives me an error :
ERROR: In C:\InstallationItkVtk\VTK\VTK\IO\vtkImageReader2.cxx, line 618
vtkImageReader (00F63E98): Initialize: Could not open file C:data.raw
please tell me where is a problem ?
Hello,
The image appears in two colors white and black. I checked with “Image J”, it works well. I get the same image with image J if I tick the box little endian byte order. I then tried to add under vtk imgReader->SetDataByteOrderToLittleEndian ();
This has not changed. Will you help me ?
with :
imgReader->SetDataByteOrderToBigEndian ();
the image is appears black
what does setdataextent do in this code? Could you explain please?
@beginner: it’s the shape of your data or the extend of your data – 3 dimensions, x0,x1,y0,y1,z0,z1 – hope this helps.
Sorry, the comment form is closed at this time.
hello
I tried ur program to read a .raw image but it gives me an error
error: in/build/buildd-vtk_5.2.1-2-i386-9xQLQ9/vtk-5.2.1/IO/vtkImagereader2.cxx, line 619
vtkimage Reader (0x83aa720): initialize: could not open file /Desktop/0004.raw