[vtkusers] correction of spacing

Bartosz Wiklak bwiklak at gmail.com
Mon Nov 24 17:58:18 EST 2008


Ok, I did it, and then:

vtkImageReslice *reslice = vtkImageReslice::New();

  reslice->SetInput(image);

  reslice->SetOutputDimensionality(2);

  reslice->SetResliceAxes(resliceAxes);

  reslice->SetInterpolationModeToNearestNeighbor();

Still my resulting image is stretched.

(almost) the whole code:

  vtkGDCMImageReader *reader = vtkGDCMImageReader::New();



	std::vector<std::string> filenames;



	gdcm::Directory d;



	d.Load( "/home/basiek/Dokumenty/dcms/testGLOWA/1", false );



	filenames = d.GetFilenames();

	gdcm::IPPSorter s;

	s.SetComputeZSpacing( true );

	s.SetZSpacingTolerance( 1e-2 );

	bool b = s.Sort( filenames );

	if( !b )

		{

		std::cerr << "Failed to sort:" << "s" << std::endl;

		return 1;

		}



	std::cout << "Sorting succeeded:" << s.GetZSpacing() << std::endl;

	//s.Print( std::cout );



	const std::vector<std::string> & sorted = s.GetFilenames();



	vtkStringArray *files = vtkStringArray::New();

	std::vector< std::string >::const_iterator it = sorted.begin();

	for( ; it != sorted.end(); ++it)

	{

	const std::string &f = *it;

	files->InsertNextValue( f.c_str() );

	}

	reader->SetFileNames( files );

	reader->Update();	

	





  // Calculate the center of the volume

  reader->GetOutput()->UpdateInformation();

  int extent[6];

  double spacing[3];

  double origin[3];

  reader->GetOutput()->GetWholeExtent(extent);

  reader->GetOutput()->GetSpacing(spacing);

  reader->GetOutput()->GetOrigin(origin);



  spacing[2] = 1.5;



  vtkImageData* image = reader->GetOutput();

  image->SetSpacing(spacing);





std::cout << "Sorting succeeded:" << spacing[2] << std::endl;



  double center[3];

  center[0] = origin[0] + spacing[0] * 0.5 * (extent[0] + extent[1]);

  center[1] = origin[1] + spacing[1] * 0.5 * (extent[2] + extent[3]);

  center[2] = origin[2] + spacing[2] * 0.5 * (extent[4] + extent[5]);



  // Matrices for axial, coronal, sagittal, oblique view orientations

  static double axialElements[16] = {

           1, 0, 0, 0,

           0, 1, 0, 0,

           0, 0, 1, 0,

           0, 0, 0, 1 };



  static double coronalElements[16] = {

           1, 0, 0, 0,

           0, 0, 1, 0,

           0,-1, 0, 0,

           0, 0, 0, 1 };



  static double sagittalElements[16] = {

           0, 0,-1, 0,

           1, 0, 0, 0,

           0,-1, 0, 0,

           0, 0, 0, 1 };



  //static double obliqueElements[16] = {

  //         1, 0, 0, 0,

  //         0, 0.866025, -0.5, 0,

  //         0, 0.5, 0.866025, 0,

  //         0, 0, 0, 1 };



  // Set the slice orientation

  vtkMatrix4x4 *resliceAxes = vtkMatrix4x4::New();

  resliceAxes->DeepCopy(coronalElements);

  // Set the point through which to slice

  resliceAxes->SetElement(0, 3, center[0]);

  resliceAxes->SetElement(1, 3, center[1]);

  resliceAxes->SetElement(2, 3, center[2]);



  // Extract a slice in the desired orientation

  vtkImageReslice *reslice = vtkImageReslice::New();

  reslice->SetInput(image);

  reslice->SetOutputDimensionality(2);

  reslice->SetResliceAxes(resliceAxes);

  reslice->SetInterpolationModeToNearestNeighbor();



  // Create a greyscale lookup table

  vtkLookupTable *table = vtkLookupTable::New();

  table->SetRange(0, 200); // image intensity range

  table->SetValueRange(0.0, 1.0); // from black to white

  table->SetSaturationRange(0.0, 0.0); // no color saturation

  table->SetRampToLinear();

  table->Build();



  // Map the image through the lookup table

  vtkImageMapToColors *color = vtkImageMapToColors::New();

  color->SetLookupTable(table);

  color->SetInputConnection(reslice->GetOutputPort());



  // Display the image

  vtkImageActor *actor = vtkImageActor::New();

  actor->SetInput(color->GetOutput());



	// The line definition

	vtkLineSource *seeds = vtkLineSource::New();

	seeds->SetPoint1(0.0,0.0,0.0);

	seeds->SetPoint2(0.0,50.0,0.0);

	seeds->SetResolution(20);



	// The Line Mapper

	vtkPolyDataMapper *seedsMapper = vtkPolyDataMapper::New();

	seedsMapper->SetInputConnection(seeds->GetOutputPort());



	// The line Actor

	vtkActor *seedsActor = vtkActor::New();

	seedsActor->SetMapper(seedsMapper);

	seedsActor->GetProperty()->SetColor(1.0,1.0,1.0);

	seedsActor->GetProperty()->SetRepresentationToPoints();

	seedsActor->GetProperty()->SetOpacity(0.95);





  vtkRenderer *renderer = vtkRenderer::New();

  renderer->AddActor(actor);

  renderer->AddActor(seedsActor);



  vtkRenderWindow *window = vtkRenderWindow::New();

  window->AddRenderer(renderer);



  // Set up the interaction

  vtkInteractorStyleImage *imageStyle = vtkInteractorStyleImage::New();

  vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::New();

  interactor->SetInteractorStyle(imageStyle);

  window->SetInteractor(interactor);

  window->Render();



  vtkImageInteractionCallback *callback = vtkImageInteractionCallback::New();

  callback->SetImageReslice(reslice);

  callback->SetInteractor(interactor);



  imageStyle->AddObserver(vtkCommand::MouseMoveEvent, callback);

  imageStyle->AddObserver(vtkCommand::LeftButtonPressEvent, callback);

  imageStyle->AddObserver(vtkCommand::LeftButtonReleaseEvent, callback);



  // Start interaction

  // The Start() method doesn't return until the window is closed by the user

  interactor->Start();



  // Clean up
...

What is wrong with it?



On Mon, Nov 24, 2008 at 11:49 PM, Dominik Szczerba <dominik at itis.ethz.ch> wrote:
> you need something like:
>
> vtkImageData* image = reader->GetOutput();
> image->SetSpacing(spacing);
> // check with image->GetSpacing(spacing);
>
> Dominik
>
> Bartosz Wiklak wrote:
>>
>> Oh, I'm really NEW to VTK (2 weeks of heavy learning and some attempts
>> before), so please be more specific.
>> Why should I get mesh out, ist't it the same?:
>>
>> reader->GetOutput()->SetSpacing(...)
>>
>>> mesh = reader->GetOutput();
>>> mesh->SetSpacing(...)
>>
>> ?
>>
>> What I should do further?
>>
>> Really thanks for kind help.
>>
>> On Mon, Nov 24, 2008 at 11:37 PM, Dominik Szczerba <dominik at itis.ethz.ch>
>> wrote:
>>>
>>> Fist of all get the mesh out of the reader:
>>>
>>> mesh = reader->GetOutput();
>>> mesh->SetSpacing(...)
>>>
>>> Dominik
>>>
>>> Bartosz Wiklak wrote:
>>>>
>>>> Hi, thanks for answear
>>>>
>>>> If I understand, you suggested to use reader->Update() or
>>>> reader->GetOutput()->Update().
>>>> It doesn't work for me, I still can see that spacing is wrong.
>>>>
>>>> I'm not sure, but after some actions UpdateInformation is called
>>>> automatically.
>>>> Sample code:
>>>>
>>>>  spacing[2] = 1.5;
>>>>
>>>>  reader->GetOutput()->SetSpacing(spacing);
>>>>
>>>>  reader->Update();
>>>>
>>>>  reader->GetOutput()->UpdateInformation();
>>>>
>>>>  reader->GetOutput()->GetSpacing(spacing);
>>>>
>>>>
>>>> or
>>>>
>>>>  spacing[2] = 1.5;
>>>>
>>>>  reader->GetOutput()->SetSpacing(spacing);
>>>>
>>>>  reader->GetOutput()->Update();
>>>>
>>>>  reader->GetOutput()->UpdateInformation();
>>>>
>>>>  reader->GetOutput()->GetSpacing(spacing);
>>>>
>>>> Gives me spacing[2]==1.0
>>>>
>>>>
>>>> What can I do?
>>>>
>>>> Bartek
>>>>
>>>>
>>>> On Mon, Nov 24, 2008 at 7:58 PM, Dominik Szczerba <dominik at itis.ethz.ch>
>>>> wrote:
>>>>>
>>>>> Haha! - see my recent posts.
>>>>> Try updating the pipeline with the correct spacing.
>>>>>
>>>>> Dominik
>>>>>
>>>>> Bartosz Wiklak wrote:
>>>>>>
>>>>>> Hello,
>>>>>>
>>>>>> I'm using GDCM  ( vtkGDCMImageReader ) to read some MR data.
>>>>>> I switched  SetComputeZSpacing on, set SetZSpacingTolerance to 1e-2
>>>>>> and used IPPSorter.
>>>>>> This is my code:
>>>>>>
>>>>>>  vtkGDCMImageReader *reader = vtkGDCMImageReader::New();
>>>>>>
>>>>>>
>>>>>>
>>>>>>      std::vector<std::string> filenames;
>>>>>>
>>>>>>
>>>>>>
>>>>>>      gdcm::Directory d;
>>>>>>
>>>>>>
>>>>>>
>>>>>>      d.Load( "/home/basiek/Dokumenty/dcms/testGLOWA/1", false );
>>>>>>
>>>>>>
>>>>>>
>>>>>>      filenames = d.GetFilenames();
>>>>>>
>>>>>>      gdcm::IPPSorter s;
>>>>>>
>>>>>>      s.SetComputeZSpacing( true );
>>>>>>
>>>>>>      s.SetZSpacingTolerance( 1e-2 );
>>>>>>
>>>>>>      bool b = s.Sort( filenames );
>>>>>>
>>>>>>      if( !b )
>>>>>>
>>>>>>              {
>>>>>>
>>>>>>              std::cerr << "Failed to sort:" << "s" << std::endl;
>>>>>>
>>>>>>              return 1;
>>>>>>
>>>>>>              }
>>>>>>
>>>>>>
>>>>>>
>>>>>>      std::cout << "Sorting succeeded:" << s.GetZSpacing() <<
>>>>>> std::endl;
>>>>>>
>>>>>>      //s.Print( std::cout );
>>>>>>
>>>>>>
>>>>>>
>>>>>>      const std::vector<std::string> & sorted = s.GetFilenames();
>>>>>>
>>>>>>
>>>>>>
>>>>>>      vtkStringArray *files = vtkStringArray::New();
>>>>>>
>>>>>>      std::vector< std::string >::const_iterator it = sorted.begin();
>>>>>>
>>>>>>      for( ; it != sorted.end(); ++it)
>>>>>>
>>>>>>      {
>>>>>>
>>>>>>      const std::string &f = *it;
>>>>>>
>>>>>>      files->InsertNextValue( f.c_str() );
>>>>>>
>>>>>>      }
>>>>>>
>>>>>>      reader->SetFileNames( files );
>>>>>>
>>>>>>      reader->Update();
>>>>>>
>>>>>> I'm getting zspacing 1.5 whitch is ok, but in the
>>>>>> reader->GetOutput()->GetSpacing(spacing) I'm getiing spacing[2]==1.0 !
>>>>>>
>>>>>> What am I doing wrong?
>>>>>> I tried to set pixelspacing manually but after
>>>>>> reader->GetOutput()->UpdateInformation() I'm with old, wrong value
>>>>>> again.
>>>>>>
>>>>>> When I make reslice in coronal I have eyes shaped like ellipses. I
>>>>>> tired to set pixelspacing for vtkImageReslice:
>>>>>> reslice->SetOutputSpacing( spacing[0], spacing[1], 1.5 );
>>>>>>
>>>>>> but this also makes no difference regardless
>>>>>> reslice->GetOutput()->UpdateInformation().
>>>>>>
>>>>>> Can someone point me in the right direction?
>>>>>> _______________________________________________
>>>>>> This is the private VTK discussion list.
>>>>>> Please keep messages on-topic. Check the FAQ at:
>>>>>> http://www.vtk.org/Wiki/VTK_FAQ
>>>>>> Follow this link to subscribe/unsubscribe:
>>>>>> http://www.vtk.org/mailman/listinfo/vtkusers
>>>>>>
>>>>> --
>>>>> Dominik Szczerba, PhD
>>>>> Computational Physics Group
>>>>> IT'IS Foundation
>>>>> http://www.itis.ethz.ch
>>>>>
>>> --
>>> Dominik Szczerba, PhD
>>> Computational Physics Group
>>> IT'IS Foundation
>>> http://www.itis.ethz.ch
>>>
>>
>
> --
> Dominik Szczerba, PhD
> Computational Physics Group
> IT'IS Foundation
> http://www.itis.ethz.ch
>



More information about the vtkusers mailing list