I believe using vtkImageChangeInformation is the correct way to propagate changes of image spacing in a pipeline. See <a href="http://www.vtk.org/doc/nightly/html/classvtkImageChangeInformation.html" target="_blank">http://www.vtk.org/doc/nightly/html/classvtkImageChangeInformation.html</a>. If you just update the output of the reader, your changes will be overwritten when the object downstream from the reader requests data.<br>
<br>Plug a vtkImageChangeInformation object into your pipeline right after your reader. It'll look something like:<br><br>vtkImageChangeInformation *changer = vtkImageChangeInformation::New();<br>changer->SetOutputSpacing(xSpacing, ySpacing, zSpacing);<br>
changer->SetInputConnection(reader->GetOutputPort());<br>...<br> vtkImageReslice *reslice = vtkImageReslice::New();<br>
reslice->SetInputConnection(changer->GetOutputPort());<br><br>Hope that helps,<br>Cory<br><br><div class="gmail_quote">On Mon, Nov 24, 2008 at 5:58 PM, Bartosz Wiklak <span dir="ltr"><<a href="mailto:bwiklak@gmail.com">bwiklak@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Ok, I did it, and then:<br>
<br>
vtkImageReslice *reslice = vtkImageReslice::New();<br>
<br>
reslice->SetInput(image);<br>
<br>
reslice->SetOutputDimensionality(2);<br>
<br>
reslice->SetResliceAxes(resliceAxes);<br>
<br>
reslice->SetInterpolationModeToNearestNeighbor();<br>
<br>
Still my resulting image is stretched.<br>
<br>
(almost) the whole code:<br>
<div class="Ih2E3d"><br>
vtkGDCMImageReader *reader = vtkGDCMImageReader::New();<br>
<br>
<br>
<br>
std::vector<std::string> filenames;<br>
<br>
<br>
<br>
gdcm::Directory d;<br>
<br>
<br>
<br>
d.Load( "/home/basiek/Dokumenty/dcms/testGLOWA/1", false );<br>
<br>
<br>
<br>
filenames = d.GetFilenames();<br>
<br>
gdcm::IPPSorter s;<br>
<br>
s.SetComputeZSpacing( true );<br>
<br>
s.SetZSpacingTolerance( 1e-2 );<br>
<br>
bool b = s.Sort( filenames );<br>
<br>
if( !b )<br>
<br>
{<br>
<br>
std::cerr << "Failed to sort:" << "s" << std::endl;<br>
<br>
return 1;<br>
<br>
}<br>
<br>
<br>
<br>
std::cout << "Sorting succeeded:" << s.GetZSpacing() << std::endl;<br>
<br>
//s.Print( std::cout );<br>
<br>
<br>
<br>
const std::vector<std::string> & sorted = s.GetFilenames();<br>
<br>
<br>
<br>
vtkStringArray *files = vtkStringArray::New();<br>
<br>
std::vector< std::string >::const_iterator it = sorted.begin();<br>
<br>
for( ; it != sorted.end(); ++it)<br>
<br>
{<br>
<br>
const std::string &f = *it;<br>
<br>
files->InsertNextValue( f.c_str() );<br>
<br>
}<br>
<br>
reader->SetFileNames( files );<br>
<br>
reader->Update();<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div> // Calculate the center of the volume<br>
<br>
reader->GetOutput()->UpdateInformation();<br>
<br>
int extent[6];<br>
<br>
double spacing[3];<br>
<br>
double origin[3];<br>
<br>
reader->GetOutput()->GetWholeExtent(extent);<br>
<br>
reader->GetOutput()->GetSpacing(spacing);<br>
<br>
reader->GetOutput()->GetOrigin(origin);<br>
<br>
<br>
<br>
spacing[2] = 1.5;<br>
<div class="Ih2E3d"><br>
<br>
<br>
vtkImageData* image = reader->GetOutput();<br>
<br>
image->SetSpacing(spacing);<br>
<br>
<br>
<br>
<br>
<br>
</div>std::cout << "Sorting succeeded:" << spacing[2] << std::endl;<br>
<br>
<br>
<br>
double center[3];<br>
<br>
center[0] = origin[0] + spacing[0] * 0.5 * (extent[0] + extent[1]);<br>
<br>
center[1] = origin[1] + spacing[1] * 0.5 * (extent[2] + extent[3]);<br>
<br>
center[2] = origin[2] + spacing[2] * 0.5 * (extent[4] + extent[5]);<br>
<br>
<br>
<br>
// Matrices for axial, coronal, sagittal, oblique view orientations<br>
<br>
static double axialElements[16] = {<br>
<br>
1, 0, 0, 0,<br>
<br>
0, 1, 0, 0,<br>
<br>
0, 0, 1, 0,<br>
<br>
0, 0, 0, 1 };<br>
<br>
<br>
<br>
static double coronalElements[16] = {<br>
<br>
1, 0, 0, 0,<br>
<br>
0, 0, 1, 0,<br>
<br>
0,-1, 0, 0,<br>
<br>
0, 0, 0, 1 };<br>
<br>
<br>
<br>
static double sagittalElements[16] = {<br>
<br>
0, 0,-1, 0,<br>
<br>
1, 0, 0, 0,<br>
<br>
0,-1, 0, 0,<br>
<br>
0, 0, 0, 1 };<br>
<br>
<br>
<br>
//static double obliqueElements[16] = {<br>
<br>
// 1, 0, 0, 0,<br>
<br>
// 0, 0.866025, -0.5, 0,<br>
<br>
// 0, 0.5, 0.866025, 0,<br>
<br>
// 0, 0, 0, 1 };<br>
<br>
<br>
<br>
// Set the slice orientation<br>
<br>
vtkMatrix4x4 *resliceAxes = vtkMatrix4x4::New();<br>
<br>
resliceAxes->DeepCopy(coronalElements);<br>
<br>
// Set the point through which to slice<br>
<br>
resliceAxes->SetElement(0, 3, center[0]);<br>
<br>
resliceAxes->SetElement(1, 3, center[1]);<br>
<br>
resliceAxes->SetElement(2, 3, center[2]);<br>
<br>
<br>
<br>
// Extract a slice in the desired orientation<br>
<br>
vtkImageReslice *reslice = vtkImageReslice::New();<br>
<br>
reslice->SetInput(image);<br>
<br>
reslice->SetOutputDimensionality(2);<br>
<br>
reslice->SetResliceAxes(resliceAxes);<br>
<br>
reslice->SetInterpolationModeToNearestNeighbor();<br>
<br>
<br>
<br>
// Create a greyscale lookup table<br>
<br>
vtkLookupTable *table = vtkLookupTable::New();<br>
<br>
table->SetRange(0, 200); // image intensity range<br>
<br>
table->SetValueRange(0.0, 1.0); // from black to white<br>
<br>
table->SetSaturationRange(0.0, 0.0); // no color saturation<br>
<br>
table->SetRampToLinear();<br>
<br>
table->Build();<br>
<br>
<br>
<br>
// Map the image through the lookup table<br>
<br>
vtkImageMapToColors *color = vtkImageMapToColors::New();<br>
<br>
color->SetLookupTable(table);<br>
<br>
color->SetInputConnection(reslice->GetOutputPort());<br>
<br>
<br>
<br>
// Display the image<br>
<br>
vtkImageActor *actor = vtkImageActor::New();<br>
<br>
actor->SetInput(color->GetOutput());<br>
<br>
<br>
<br>
// The line definition<br>
<br>
vtkLineSource *seeds = vtkLineSource::New();<br>
<br>
seeds->SetPoint1(0.0,0.0,0.0);<br>
<br>
seeds->SetPoint2(0.0,50.0,0.0);<br>
<br>
seeds->SetResolution(20);<br>
<br>
<br>
<br>
// The Line Mapper<br>
<br>
vtkPolyDataMapper *seedsMapper = vtkPolyDataMapper::New();<br>
<br>
seedsMapper->SetInputConnection(seeds->GetOutputPort());<br>
<br>
<br>
<br>
// The line Actor<br>
<br>
vtkActor *seedsActor = vtkActor::New();<br>
<br>
seedsActor->SetMapper(seedsMapper);<br>
<br>
seedsActor->GetProperty()->SetColor(1.0,1.0,1.0);<br>
<br>
seedsActor->GetProperty()->SetRepresentationToPoints();<br>
<br>
seedsActor->GetProperty()->SetOpacity(0.95);<br>
<br>
<br>
<br>
<br>
<br>
vtkRenderer *renderer = vtkRenderer::New();<br>
<br>
renderer->AddActor(actor);<br>
<br>
renderer->AddActor(seedsActor);<br>
<br>
<br>
<br>
vtkRenderWindow *window = vtkRenderWindow::New();<br>
<br>
window->AddRenderer(renderer);<br>
<br>
<br>
<br>
// Set up the interaction<br>
<br>
vtkInteractorStyleImage *imageStyle = vtkInteractorStyleImage::New();<br>
<br>
vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::New();<br>
<br>
interactor->SetInteractorStyle(imageStyle);<br>
<br>
window->SetInteractor(interactor);<br>
<br>
window->Render();<br>
<br>
<br>
<br>
vtkImageInteractionCallback *callback = vtkImageInteractionCallback::New();<br>
<br>
callback->SetImageReslice(reslice);<br>
<br>
callback->SetInteractor(interactor);<br>
<br>
<br>
<br>
imageStyle->AddObserver(vtkCommand::MouseMoveEvent, callback);<br>
<br>
imageStyle->AddObserver(vtkCommand::LeftButtonPressEvent, callback);<br>
<br>
imageStyle->AddObserver(vtkCommand::LeftButtonReleaseEvent, callback);<br>
<br>
<br>
<br>
// Start interaction<br>
<br>
// The Start() method doesn't return until the window is closed by the user<br>
<br>
interactor->Start();<br>
<br>
<br>
<br>
// Clean up<br>
...<br>
<br>
What is wrong with it?<br>
<div><div></div><div class="Wj3C7c"><br>
<br>
<br>
On Mon, Nov 24, 2008 at 11:49 PM, Dominik Szczerba <<a href="mailto:dominik@itis.ethz.ch">dominik@itis.ethz.ch</a>> wrote:<br>
> you need something like:<br>
><br>
> vtkImageData* image = reader->GetOutput();<br>
> image->SetSpacing(spacing);<br>
> // check with image->GetSpacing(spacing);<br>
><br>
> Dominik<br>
><br>
> Bartosz Wiklak wrote:<br>
>><br>
>> Oh, I'm really NEW to VTK (2 weeks of heavy learning and some attempts<br>
>> before), so please be more specific.<br>
>> Why should I get mesh out, ist't it the same?:<br>
>><br>
>> reader->GetOutput()->SetSpacing(...)<br>
>><br>
>>> mesh = reader->GetOutput();<br>
>>> mesh->SetSpacing(...)<br>
>><br>
>> ?<br>
>><br>
>> What I should do further?<br>
>><br>
>> Really thanks for kind help.<br>
>><br>
>> On Mon, Nov 24, 2008 at 11:37 PM, Dominik Szczerba <<a href="mailto:dominik@itis.ethz.ch">dominik@itis.ethz.ch</a>><br>
>> wrote:<br>
>>><br>
>>> Fist of all get the mesh out of the reader:<br>
>>><br>
>>> mesh = reader->GetOutput();<br>
>>> mesh->SetSpacing(...)<br>
>>><br>
>>> Dominik<br>
>>><br>
>>> Bartosz Wiklak wrote:<br>
>>>><br>
>>>> Hi, thanks for answear<br>
>>>><br>
>>>> If I understand, you suggested to use reader->Update() or<br>
>>>> reader->GetOutput()->Update().<br>
>>>> It doesn't work for me, I still can see that spacing is wrong.<br>
>>>><br>
>>>> I'm not sure, but after some actions UpdateInformation is called<br>
>>>> automatically.<br>
>>>> Sample code:<br>
>>>><br>
>>>> spacing[2] = 1.5;<br>
>>>><br>
>>>> reader->GetOutput()->SetSpacing(spacing);<br>
>>>><br>
>>>> reader->Update();<br>
>>>><br>
>>>> reader->GetOutput()->UpdateInformation();<br>
>>>><br>
>>>> reader->GetOutput()->GetSpacing(spacing);<br>
>>>><br>
>>>><br>
>>>> or<br>
>>>><br>
>>>> spacing[2] = 1.5;<br>
>>>><br>
>>>> reader->GetOutput()->SetSpacing(spacing);<br>
>>>><br>
>>>> reader->GetOutput()->Update();<br>
>>>><br>
>>>> reader->GetOutput()->UpdateInformation();<br>
>>>><br>
>>>> reader->GetOutput()->GetSpacing(spacing);<br>
>>>><br>
>>>> Gives me spacing[2]==1.0<br>
>>>><br>
>>>><br>
>>>> What can I do?<br>
>>>><br>
>>>> Bartek<br>
>>>><br>
>>>><br>
>>>> On Mon, Nov 24, 2008 at 7:58 PM, Dominik Szczerba <<a href="mailto:dominik@itis.ethz.ch">dominik@itis.ethz.ch</a>><br>
>>>> wrote:<br>
>>>>><br>
>>>>> Haha! - see my recent posts.<br>
>>>>> Try updating the pipeline with the correct spacing.<br>
>>>>><br>
>>>>> Dominik<br>
>>>>><br>
>>>>> Bartosz Wiklak wrote:<br>
>>>>>><br>
>>>>>> Hello,<br>
>>>>>><br>
>>>>>> I'm using GDCM ( vtkGDCMImageReader ) to read some MR data.<br>
>>>>>> I switched SetComputeZSpacing on, set SetZSpacingTolerance to 1e-2<br>
>>>>>> and used IPPSorter.<br>
>>>>>> This is my code:<br>
>>>>>><br>
>>>>>> vtkGDCMImageReader *reader = vtkGDCMImageReader::New();<br>
>>>>>><br>
>>>>>><br>
>>>>>><br>
>>>>>> std::vector<std::string> filenames;<br>
>>>>>><br>
>>>>>><br>
>>>>>><br>
>>>>>> gdcm::Directory d;<br>
>>>>>><br>
>>>>>><br>
>>>>>><br>
>>>>>> d.Load( "/home/basiek/Dokumenty/dcms/testGLOWA/1", false );<br>
>>>>>><br>
>>>>>><br>
>>>>>><br>
>>>>>> filenames = d.GetFilenames();<br>
>>>>>><br>
>>>>>> gdcm::IPPSorter s;<br>
>>>>>><br>
>>>>>> s.SetComputeZSpacing( true );<br>
>>>>>><br>
>>>>>> s.SetZSpacingTolerance( 1e-2 );<br>
>>>>>><br>
>>>>>> bool b = s.Sort( filenames );<br>
>>>>>><br>
>>>>>> if( !b )<br>
>>>>>><br>
>>>>>> {<br>
>>>>>><br>
>>>>>> std::cerr << "Failed to sort:" << "s" << std::endl;<br>
>>>>>><br>
>>>>>> return 1;<br>
>>>>>><br>
>>>>>> }<br>
>>>>>><br>
>>>>>><br>
>>>>>><br>
>>>>>> std::cout << "Sorting succeeded:" << s.GetZSpacing() <<<br>
>>>>>> std::endl;<br>
>>>>>><br>
>>>>>> //s.Print( std::cout );<br>
>>>>>><br>
>>>>>><br>
>>>>>><br>
>>>>>> const std::vector<std::string> & sorted = s.GetFilenames();<br>
>>>>>><br>
>>>>>><br>
>>>>>><br>
>>>>>> vtkStringArray *files = vtkStringArray::New();<br>
>>>>>><br>
>>>>>> std::vector< std::string >::const_iterator it = sorted.begin();<br>
>>>>>><br>
>>>>>> for( ; it != sorted.end(); ++it)<br>
>>>>>><br>
>>>>>> {<br>
>>>>>><br>
>>>>>> const std::string &f = *it;<br>
>>>>>><br>
>>>>>> files->InsertNextValue( f.c_str() );<br>
>>>>>><br>
>>>>>> }<br>
>>>>>><br>
>>>>>> reader->SetFileNames( files );<br>
>>>>>><br>
>>>>>> reader->Update();<br>
>>>>>><br>
>>>>>> I'm getting zspacing 1.5 whitch is ok, but in the<br>
>>>>>> reader->GetOutput()->GetSpacing(spacing) I'm getiing spacing[2]==1.0 !<br>
>>>>>><br>
>>>>>> What am I doing wrong?<br>
>>>>>> I tried to set pixelspacing manually but after<br>
>>>>>> reader->GetOutput()->UpdateInformation() I'm with old, wrong value<br>
>>>>>> again.<br>
>>>>>><br>
>>>>>> When I make reslice in coronal I have eyes shaped like ellipses. I<br>
>>>>>> tired to set pixelspacing for vtkImageReslice:<br>
>>>>>> reslice->SetOutputSpacing( spacing[0], spacing[1], 1.5 );<br>
>>>>>><br>
>>>>>> but this also makes no difference regardless<br>
>>>>>> reslice->GetOutput()->UpdateInformation().<br>
>>>>>><br>
>>>>>> Can someone point me in the right direction?<br>
>>>>>> _______________________________________________<br>
>>>>>> This is the private VTK discussion list.<br>
>>>>>> Please keep messages on-topic. Check the FAQ at:<br>
>>>>>> <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
>>>>>> Follow this link to subscribe/unsubscribe:<br>
>>>>>> <a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>
>>>>>><br>
>>>>> --<br>
>>>>> Dominik Szczerba, PhD<br>
>>>>> Computational Physics Group<br>
>>>>> IT'IS Foundation<br>
>>>>> <a href="http://www.itis.ethz.ch" target="_blank">http://www.itis.ethz.ch</a><br>
>>>>><br>
>>> --<br>
>>> Dominik Szczerba, PhD<br>
>>> Computational Physics Group<br>
>>> IT'IS Foundation<br>
>>> <a href="http://www.itis.ethz.ch" target="_blank">http://www.itis.ethz.ch</a><br>
>>><br>
>><br>
><br>
> --<br>
> Dominik Szczerba, PhD<br>
> Computational Physics Group<br>
> IT'IS Foundation<br>
> <a href="http://www.itis.ethz.ch" target="_blank">http://www.itis.ethz.ch</a><br>
><br>
_______________________________________________<br>
This is the private VTK discussion list.<br>
Please keep messages on-topic. Check the FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Cory Quammen<br>Center for Computer Integrated Systems for Microscopy and Manipulation (CISMM)<br>Department of Computer Science<br>University of North Carolina at Chapel Hill<br>
<a href="http://www.cs.unc.edu/~cquammen">http://www.cs.unc.edu/~cquammen</a><br>