[vtkusers] Help using vtkImageExport and vtkImageImport

Twobit arapson at gmail.com
Thu Aug 28 04:27:46 EDT 2008


Hello all,

I'm busy doing a fourth year project that requires the use of VTK. While i
am new to this package, I have read both the Kitware books (that i own.
I have done my best in terms of searching the web, the vtkusers mailing list
and read the two books. I have spent two weeks on this issue.

Configuration Details:
Operating System: Windows XP SP3 and Ubuntu 8.04
VTK Version: 5.0.4
Compiler: Using pure C++ with gcc.

The problem i am having is related to my lack of understanding of the
requirements for the vtkImageExport class. I have the need to load images in
general Imaging formats: .jpg, .bmp, .tif, and .png. In order to load these
images I make use of the related images (vtkJPEGReader etc).

I found a great example in the release folder:
VTK_5.0.4/VTK/Imaging/Testing/Cxx/ImportExport.cxx that makes use of the
vtkImageReader. This example works 100% without any problems:

int ImportExport( int argc, char *argv[] )
{
 int i,j,k;

 char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv,
"Data/headsq/quarter");

 vtkImageReader *reader = vtkImageReader::New();
 reader->SetDataByteOrderToLittleEndian();
 reader->SetDataExtent(0,63,0,63,1,93);
 reader->SetFilePrefix(fname);
 reader->SetDataMask(0x7fff);
 delete [] fname;

 // create exporter
 vtkImageExport *exporter = vtkImageExport::New();
 exporter->SetInputConnection(reader->GetOutputPort());
 exporter->ImageLowerLeftOn();

 // get info from exporter and create array to hold data
 int memsize = exporter->GetDataMemorySize();
 int *dimensions = exporter->GetDataDimensions();


 // export the data into the array
 short *data = new short[memsize/sizeof(short)];
 exporter->Export(data);

 // alternative method for getting data
 // short *data = exporter->GetPointerToData();

 // do a little something to the data

 for (i = 0; i < dimensions[2]; i++)
   {
   for (j = 0; j < dimensions[1]; j++)
     {
     for (k = 0; k < dimensions[0]; k++)
       {
       if (k % 10 == 0)
         {
         data[k + dimensions[0]*(j + dimensions[1]*i)] = 0;
         }
       if (j % 10 == 0)
         {
         data[k + dimensions[0]*(j + dimensions[1]*i)] = 1000;
         }
       }
     }
   }

 // create an importer to read the data back in
 vtkImageImport *importer = vtkImageImport::New();
 importer->SetWholeExtent(1,dimensions[0],1,dimensions[1],1,dimensions[2]);
 importer->SetDataExtentToWholeExtent();
 importer->SetDataScalarTypeToShort();
 importer->SetImportVoidPointer(data);


 vtkImageViewer *viewer = vtkImageViewer::New();
 viewer->SetInputConnection(importer->GetOutputPort());
 viewer->SetZSlice(45);
 viewer->SetColorWindow(2000);
 viewer->SetColorLevel(1000);

 viewer->Render();

 int retVal = vtkRegressionTestImage( viewer->GetRenderWindow() );

 viewer->Delete();
 importer->Delete();
 exporter->Delete();
 reader->Delete();

 delete [] data;

 return !retVal;
}

However when i change the image reader to make use of any other reader
(jpeg, bmp, tiff, or png) the image does not
render correctly. Im assume that this is due to the images im using having
three scalar componenets?

vtkJPEGReader *reader = vtkJPEGReader::New();
reader->SetDataByteOrderToLittleEndian();
reader->SetFileName(fileName); // Load the image into the image reader.
reader->ImageLowerLeftOn();
reader->Update();
reader->Print(std::cout);

Im not looking for a full worked solution for anyone, just hoping that
someone could point me in a direction that could help me out alittle.

The reason I need to export the images is to run some external
transformations on the images, after going through several of the amazing
VTK filters. This
needs to be read back into VTK to perform some nice 3D modelling and then
redendering.

When I export then Import (without any modification) the image come back in
red and blue colouring, but the image is not rendereded correctly. Each of
the
rows of pixels get rendered at a strange location on the screen.

Thank you for taking the time to read this email, I hope i gave enough light
into my problem for someone to give some advice.

Regards
Andrew Rapson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20080828/98d7e959/attachment.htm>


More information about the vtkusers mailing list