[vtkusers] Reading one slice of a volume from file
David Gobbi
david.gobbi at gmail.com
Mon Mar 3 17:50:40 EST 2014
Hi Richard,
Your SetOutput() trick is not going to work no matter what. It's
really not possible to use the VTK pipeline to update just one slice
while leaving the rest unchanged.
If you want to update just one slice, then just use the vtkTIFFReader
normally, i.e. let it create its own output, and _do_not_ set its
update extent. You can then call GetScalarPointer() on its output to
get a pointer to the data for the slice it read. Then, you can use
memcpy() to copy it into the correct slice position of m_imageVolume.
Getting the pointer for the slice within the volume is easy, just use
m_imageVolume->GetScalarPointerForExtent(extent) where "extent"
specifies the slice that you want to write.
David
On Mon, Mar 3, 2014 at 9:14 AM, Richard Whitehead
<richard.whitehead at vivosight.com> wrote:
>
> I am trying to update just one slice of a vtkImageData by reading it from
> file, leaving the rest of the volume unchanged. My code is below. I am
> trying to limit the region updated by the filter by calling SetUpdateExtent,
> but I don't think this is doing anything. When I call UpdateWholeExtent,
> **all** image slices contain a copy of the 2D image read from file. If
> instead I only call Update, then the volume now consists of only one slice.
> Note that I don't want to read a sequence of files with structured
> filenames, I want to read one slice at a time for reasons too tedious to
> explain here!
> Please help! Thanks in advance.
>
>
>
> int SIZE_X = 722;
> int SIZE_Y = 460;
> int SIZE_Z = 100;
>
> // create volume (16-bit)
> m_imageVolume = vtkSmartPointer<vtkImageData>::New();
> m_imageVolume->SetExtent(0, SIZE_X-1, 0, SIZE_Y-1, 0, SIZE_Z-1);
> m_imageVolume->SetSpacing(1.0, 1.0, 1.0);
> m_imageVolume->SetOrigin(0.0, 0.0, 0.0);
> m_imageVolume->AllocateScalars(VTK_UNSIGNED_SHORT, 1);
>
> // set it all mid-grey
> unsigned short* volPtr = static_cast<unsigned short*>(
> m_imageVolume->GetScalarPointer() );
> memset(volPtr, 30000, SIZE_X * SIZE_Y * SIZE_Z * sizeof(unsigned short) );
>
>
> int de[6];
> m_imageVolume->GetExtent( de );
>
> // read data into middle slice
> vtkSmartPointer<vtkTIFFReader> reader =
> vtkSmartPointer<vtkTIFFReader>::New();
> reader->SetOutput( m_imageVolume );
> reader->SetFileName("..... .tif");
> reader->SetFileDimensionality(2);
> reader->SetDataExtent( de );
>
> int updateExtent[6];
> updateExtent[0] = 0;
> updateExtent[1] = SIZE_X-1;
> updateExtent[2] = 0;
> updateExtent[3] = SIZE_Y-1;
> updateExtent[4] = SIZE_Z/2;
> updateExtent[5] = SIZE_Z/2;
> reader->SetUpdateExtent( updateExtent );
>
> reader->SetOrientationType( ORIENTATION_BOTLEFT ); // otherwise it comes
> out upside down
> //reader->Update(); // volume has only one slice
> reader->UpdateWholeExtent(); // volume has many slices all containing the
> image read from file
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
More information about the vtkusers
mailing list