[vtkusers] Question about vtkImageData/Writer

Mathieu Malaterre Mathieu.Malaterre at creatis.insa-lyon.fr
Wed Jul 30 03:40:41 EDT 2003


Paul,

	Add this

  vtkImageReader *reader  = vtkImageReader::New();
...
  reader->SetDataScalarTypeToFloat (); //very important !
...

But I still don't understand why you don't use the 
vtkXMLPImageDataWriter/Reader

HTH
mathieu

Paul McGuinness wrote:
> Hi All,
> 
> The problem I am having is that the scalar values are not being read by
> vtkImageReader properly. I compare the original scalar values that were
> created at the very beginning of the program, and the scalar values the
> where read in my vtkImageReader (remember below I said that I wrote the
> vtkimagedata object with its scalar values to a file using vtkimagewriter
> and read the same file back in using vtkimagereader).
> 
> Does anyone know what I am doing wrong such that I am reading in the wrong
> values for my scalar values?
> I have attached my code below so you can see  what I am trying to achieve.
> I should mention that vtkStructuredPointsReader/Writer works fine but due
> to the nature of my work I must use vtkImageReader/Writer.
> 
> Many Thanks,
> Paul.
> 
> 
> 
> #include "vtkRenderer.h"
> #include "vtkRenderWindow.h"
> #include "vtkRenderWindowInteractor.h"
> #include "vtkImageData.h"
> #include "vtkFloatArray.h"
> #include "vtkContourFilter.h"
> #include "vtkPolyDataMapper.h"
> #include "vtkActor.h"
> #include "vtkProperty.h"
> #include "vtkPointData.h"
> #include "vtkImageWriter.h"
> #include "vtkImageReader.h"
> #include "vtkSphereSource.h"
> #include "vtkStructuredPointsWriter.h"
> #include "vtkStructuredPointsReader.h"
> #include "vtkDataSet.h"
> #include "vtkStructuredData.h"
> #include "vtkImageToStructuredPoints.h"
> #include "vtkStructuredPointsSource.h"
> #include "vtkDataArray.h"
> 
> main ()
> {
> 
>   float sp, z, y, x, s, point[3], point2[3];
> int i, j, k,  kOffset, jOffset, offset,h,t=0;
> float valuerange[2];
> 
> vtkImageData *vol = vtkImageData::New();
>  vol->SetDimensions(26,26,26);
>   vol->SetOrigin(-0.5,-0.5,-0.5);
>   sp =1.0/25.0;
>   vol->SetSpacing(sp,sp,sp);
>   vol->AllocateScalars();
>   vol->SetScalarTypeToFloat();
> 
> vtkFloatArray *scalars =vtkFloatArray::New();
>                     for(k=0;k<26;k++)
>   {
>   z = -0.5 + k*sp;
>   kOffset = k*26*26;
> 
> for(j=0; j<26; j++)
>   {
>   y = -0.5 +j*sp;
>   jOffset = j*26;
> 
> 
>                      for(i=0;i<26;i++)
>                      {
>                      x = -0.5 + i*sp;
>                      s = x*x + y*y + z*z - (0.4*0.4);
>                      offset = i + jOffset + kOffset;
>                     scalars->InsertTuple1(offset,s);
>                   //  scalars->InsertValue(offset,s);
> 		//   if(t<100)  printf("scalars -> %d , %lf\n",offset,s);
> 		 // t++;
>                      }
>                      }
>                      }
> 
>   vol->GetPointData()->SetScalars(scalars);
>    vol->GetPointData()->CopyAllOn();
>   vol->Update();
>   vol->UpdateData();
> 
> vtkImageWriter *writer = vtkImageWriter::New();
>  writer->SetInput(vol);
>  writer->SetFileName("voldataim.vtk");
>  writer->SetFileDimensionality(3);
>  writer->Write();
> 
> vtkImageReader *reader  = vtkImageReader::New();
>  reader->SetFileName("voldataim.vtk");
>  reader->SetFileDimensionality(3);
>  reader->SetDataExtent(0,25, 0, 25, 0, 25);
>  reader->SetDataSpacing(1.0/25.0, 1.0/25.0, 1.0/25.0);
>  reader->SetDataOrigin(-0.5,-0.5,-0.5);
> 
>     reader->GetOutput()->GetScalarRange(valuerange);//new
> 
>  reader->Update();
>  reader->UpdateWholeExtent();
>  reader->UpdateInformation();
> 
>  vtkFloatArray *scalars2 =vtkFloatArray::New();
>  scalars2=(vtkFloatArray *)reader->GetOutput()->GetPointData()->GetScalars();
>  for(i=0;i<100;i++) printf("reader->GetOutput())->GetPointData()->GetScalars(%d) = %lf\n", i, scalars2->GetValue(i));
> 
> 
>   vtkContourFilter *contour = vtkContourFilter::New();
>       contour->SetInput(reader->GetOutput());
>      //   contour->SetInput(vol);
>       contour->SetValue(0,0);
>       contour->Update();
> 
>   vtkPolyDataMapper *volMapper =  vtkPolyDataMapper::New();
>       volMapper->SetInput(contour->GetOutput());
>       volMapper->ScalarVisibilityOff();
>   vtkActor *volActor = vtkActor::New();
>       volActor->SetMapper(volMapper);
>      // volActor->GetProperty()->SetRepresentationToWireframe();
> 
>   vtkRenderer *renderer = vtkRenderer::New();
>   vtkRenderWindow *renWin = vtkRenderWindow::New();
>     renWin->AddRenderer(renderer);
>   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
>     iren->SetRenderWindow(renWin);
> 
> 
>   renderer->AddActor(volActor);
>       renderer->SetBackground(1,1,1);
>   renWin->SetSize(400,400);
> 
>   // interact with data
> 
>   renWin->Render();
>   iren->Start();
> 
>   // Clean up
>   renderer->Delete();
>   renWin->Delete();
>   iren->Delete();
>   vol->Delete();
>   scalars->Delete();
>   contour->Delete();
>   volMapper->Delete();
>   volActor->Delete();
> }
> 
> 
> On Mon, 28 Jul 2003, Paul McGuinness wrote:
> 
> 
>>Hi Users,
>>
>>I have a question that cannot be answered by the book and I hope one of
>>you know the answer.
>>My problem to start with is I am creating a sphere using vtkImageData and
>>using vol->GetPointData()->SetScalars(scalars) to insert the scalar values
>>that define the sphere where scalars is vtkFloatArray. (example Vol.cxx)
>>The pipeline is as follows:
>>
>>vtkImageData -> vtkContourFilter -> vtkPolyDataMapper -> vtkActor
>>->vtkRenderer -> vtkRenderWindow
>>
>>And the sphere appears correctly.
>>
>>Now if I use vtkStructuredPointsWriter/Reader in the pipeline; use
>>structuredpoints writer to write the vtkImageData of a sphere to a file
>>and the structuredpointsreader to read it back in and continue the pipline
>>as before e.g.:
>>
>>vtkImageData -> vtkStructuredPointsWriter ->
>>vtkStructuredPointsReader -> vtkContourFilter -> vtkPolyDataMapper ->
>>vtkActor ->vtkRenderer -> vtkRenderWindow
>>
>>The image will appear correctly on the screen as a sphere.
>>
>>But now if I use vtkImageWriter/Reader instead of
>>vtkStructuredPointsWriter/Reader e.g.:
>>
>>vtkImageData -> vtkImageWriter -> vtkImageReader -> vtkContourFilter ->
>>vtkPolyDataMapper -> vtkActor ->vtkRenderer -> vtkRenderWindow
>>
>>The image will not correctly appear on the screen as a sphere but as a
>>shape that resembles a cubic shaped sponge.
>>
>>Does anyone know why I am getting the wrong shape because I need to use
>>vtkImageWriter's void  SetFilePrefix (char *filePrefix) to create multiple files rather
>>then having all the data in just one file? And if I cannot use
>>vtkImageWriter in the this example because how the vtkImageData object is
>>being defined (vtk example Vol.cxx) how should I create this sphere to
>>use vtkImageWriter?
>>
>>Many Thanks,
>>Paul.
>>
>>_______________________________________________
>>This is the private VTK discussion list.
>>Please keep messages on-topic. Check the FAQ at: <http://public.kitware.com/cgi-bin/vtkfaq>
>>Follow this link to subscribe/unsubscribe:
>>http://www.vtk.org/mailman/listinfo/vtkusers
>>
> 
> 
> _______________________________________________
> This is the private VTK discussion list. 
> Please keep messages on-topic. Check the FAQ at: <http://public.kitware.com/cgi-bin/vtkfaq>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
> 


-- 
Mathieu Malaterre
CREATIS
28 Avenue du Doyen LEPINE
B.P. Lyon-Montchat
69394 Lyon Cedex 03
http://www.creatis.insa-lyon.fr/~malaterre/




More information about the vtkusers mailing list