[vtkusers] Raw data in 2d slice

Saabir Singh singh.saabir at gmail.com
Sun Mar 4 07:38:32 EST 2012


Sir
I am new to vtk and would ask for your help in visualizing visible
human data given over here

http://www.medcatalog.com/F_G/gold_standard_multimedia.htm
A single RAW file consists of 13 bytes of headers and then the image
data. I am using C++ and tried the following code but it gives me a
red cube.

#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkImageDataGeometryFilter.h>
#include <vtkWarpScalar.h>
#include <vtkPolyDataMapper.h>

int main(int argc, char **argv)
{
        char ch;
        long long int i = 0;
        ifstream fin( argv[1], ios::in|ios::binary );
        vtkSmartPointer<vtkImageData> imageData =
vtkSmartPointer<vtkImageData>::New();
        imageData->SetDimensions(500,500,50);
        #if VTK_MAJOR_VERSION <= 5
                imageData->SetNumberOfScalarComponents(1);
                imageData->SetScalarTypeToDouble();
        #else
                imageData->AllocateScalars(VTK_DOUBLE,1);
        #endif
        imageData->SetOrigin(0.0, 0.0, 0.0);
        imageData->SetSpacing(0.33,1,1);
        int* dims = imageData->GetDimensions();
        std::cout << "Dims: " << " x: " << dims[0] << " y: " <<
dims[1] << " z: " << dims[2] << std::endl;
        std::cout << "Number of points: " <<
imageData->GetNumberOfPoints() << std::endl;
        std::cout << "Number of cells: " <<
imageData->GetNumberOfCells() << std::endl;
        fin.seekg( 13 );
        fin.seekg( 1760*1024*914 );
        cout << dims[2] << " " << dims[1] << " " << dims[0] << endl;
        for (int z = 0; z < dims[2]; z++) {
                for (int y = 0; y < dims[1]; y++) {
                       for (int x = 0; x < dims[0]; x++) {
                                double* pixel =
static_cast<double*>(imageData->GetScalarPointer(x,y,z));
                                fin.get(ch);
                                pixel[0] = ch;
                        }
                        fin.seekg( 1260, ios::cur );
                }
                fin.seekg( 524*1760, ios::cur );
        }

        vtkImageDataGeometryFilter *imgfilt = vtkImageDataGeometryFilter::New();
        imgfilt->SetInput(imageData);
        vtkWarpScalar *warp = vtkWarpScalar::New();
        warp->SetInputConnection(imgfilt->GetOutputPort());
        vtkPolyDataMapper *Mapper =vtkPolyDataMapper ::New();
        Mapper->SetInputConnection(warp->GetOutputPort());
        vtkActor *Actor = vtkActor::New();
        Actor->SetMapper( Mapper );
        vtkRenderer *ren1= vtkRenderer::New();
        ren1->AddActor( Actor );
        ren1->SetBackground( 0.0, 0.0, 0.0 );
        vtkRenderWindow *renWin = vtkRenderWindow::New();
        renWin->AddRenderer( ren1 );
        renWin->SetSize( 1366, 768 );
        vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
        iren->SetRenderWindow(renWin);
        iren->Initialize();
        iren->Start();

        return EXIT_SUCCESS;
}



More information about the vtkusers mailing list