echelon

A personal technology blog about software development and other interesting stuff.

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;
}

4 Comments »

  1. 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 (0×83aa720): initialize: could not open file /Desktop/0004.raw

    Comment by frency — March 24, 2009 @ 20:52

  2. Mh, could it be that you didn’t specified the file path correctly?

    Comment by Sebastian Schaetz — March 24, 2009 @ 21:00

  3. 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

    Comment by frency — March 25, 2009 @ 19:23

  4. 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.

    Comment by Sebastian Schaetz — March 26, 2009 @ 01:03

Leave a comment