[Insight-developers] Wrong behaviour of ImageFileReader when reading an itk::VectorImage<itk::FixedArray<double, 6>, 3> ?

Karthik Krishnan karthik.krishnan at kitware.com
Wed Jan 19 01:27:39 EST 2011


I also think adaptors would be slow here. Here is something that might work,
taking advantage of the fact that the memory representation is unchanged.
You can replace the buffer pointer directly under the hood after reading..


In other words read the image as :

  VectorImage< double, 3 > ReaderImageType;


Then later on, cast it brutally by replacing the pixel container under the
hood....

  typedef DiffusionTensor< double > PixelType;
  typedef VectorImage< PixelType, 3 > VectorOfTensorsImageType;

  const unsigned int nTensorsPerVector = readerImage->GetVectorLength() /
6;  // will be 2 for this example
  const unsigned long nVectorsInImage =
readerImage->GetLargestPossibleRegion()->GetNumberOfPixels();
  const unsigned long nTensors = nVectorsInImage * nTensorsPerVector;

  typedef VectorOfTensorsImageType::PixelContainer
VectorOfTensorsPixelContainer;
  VectorOfTensorsPixelContainer::Pointer votPixelContainer =
VectorOfTensorsPixelContainer::New();
  votPixelContainer->SetImportPointer( reinterpret_cast< PixelType *
>(readImage->GetBufferPointer()), nTensors, false ); // keep readerImage
alive since we let it manage the memory.

  VectorOfTensorsImageType::Pointer votImage =
VectorOfTensorsImageType::New();
  votImage->SetVectorLength( readerImage->GetVectorLength() / 6 );
  votImage->PixelContainer( votPixelContainer );

  // Copy metadata from the readerImage.
  votImage->SetSpacing(readerImage->GetSpacing());
  votImage->SetOrigin(readerImage->GetOrigin());
  votImage->SetDirection(readerImage->GetDirection());
  votImage->SetBufferedRegion(readerImage->GetBufferedRegion() );
  votImage->SetRequestedRegion(readerImage->GetRequestedRegion() );


Its untested, but I think it should work :)

--
karthik

On Wed, Jan 19, 2011 at 8:18 AM, Benoit Scherrer
<benoitscherrer at gmail.com>wrote:

> Hi,
>
> Thank you very much for all these answers!
>
> I don't know of a way to get the number of components in
>> TOutputImage::PixelType. It would require us to define a NumberOfComponents
>> in the traits of all pixels in itkNumericTraits. The IO class has no way of
>> knowing the specific pixel type you've instantiated. One has to rely on the
>> IO, for instance if the pixel type is an Array, (in which case, the image
>> cannot tell you the number of components in the array. The IO tells you by
>> parsing the file). You have a peculiar situation where you want to get the
>> number of components from the pixel and not from the IO.
>
>
> Yes i think it is really the point. Now i understand that the IO class has
> no way to know the size of the pixel type without
> a NumberOfComponents property in the itkNumericTraits.
>
>  You could use ImageAdaptors for presenting
>> a VectorImage as an Image of Tensors.
>
>
> Thanks, i will look at it (i m a little bit concerned about the speed
> though. but there is no free lunch...)
>
> Also, since
> typedef itk::VectorImage<itk::DiffusionTensor<double>,3>
> ImageOfVariableNumberOfTensorsType;
> seems to work as long as you don't write/load the data, i could also
> convert the output of
> the IO class to my ImageOfVariableNumberOfTensorsType type, knowing that
> each tensor is composed
> of 6 elements.
>
> Thanks
> Benoit
>
>
>>
>> -----------------------------------------------------------------
>> On Mon, Jan 17, 2011 at 10:46 AM, Benoit Scherrer
>> <benoitscherrer at gmail.com> wrote:
>> > Ok thanks!
>> > But so there is no way to use:
>> > typedef itk::VectorImage<itk::DiffusionTensor<double>, 3>
>> TensorVectorImage;
>> >
>> > to read a set of tensors at each voxel ?
>> > ( itk::DiffusionTensor inherits itk::FixedArray)
>> > It would be useful to do for example:
>> > TensorVectorImage tensors;
>> > tensors[i].GetFractionalAnisotropy()
>> > Thanks
>> > Benoit
>> >
>> > On Thu, Jan 13, 2011 at 11:26 PM, Karthik Krishnan
>> > <karthik.krishnan at kitware.com> wrote:
>> >>
>> >>
>> >> On Fri, Jan 14, 2011 at 6:26 AM, Benoit Scherrer
>> >> <benoitscherrer at gmail.com> wrote:
>> >>>
>> >>> Hi,
>> >>> I wanted to use the type:
>> >>> typedef itk::VectorImage<itk::FixedArray<double, 6>, 3>
>> >>> TensorVectorImage;
>> >>> to make a vector image of tensors.
>> >>> Everything compiles well. But when i use the
>> >>> itk::ImageFileReader<TensorVectorImage>
>> >>> it seems that things goes wrong. When trying to iterate on the image :
>> >>> itk::ImageRegionConstIterator<TensorVectorImage>
>> >>>  it(image, image->GetLargestPossibleRegion());
>> >>> while(!it.IsAtEnd())
>> >>> {
>> >>> itk::VariableLengthVector<itk::FixedArray<double, 6> > tensors =
>> >>> it.Get();
>> >>>                (...)
>> >>> ++it;
>> >>> }
>> >>> it compiles but during the execution goes out of the data bounds.
>> >>> By looking into ImageFileReader<TOutputImage,
>> >>> ConvertPixelTraits>::GenerateOutputInformation
>> >>> I found the lines:
>> >>>   if( strcmp( output->GetNameOfClass(), "VectorImage" ) == 0 )
>> >>>     {
>> >>>     typedef typename TOutputImage::AccessorFunctorType
>> >>> AccessorFunctorType;
>> >>>     AccessorFunctorType::SetVectorLength( output,
>> >>> m_ImageIO->GetNumberOfComponents() );
>> >>>     }
>> >>> So here m_ImageIO->GetNumberOfComponents() will returns 6 for a simple
>> >>> tensor file (Dimension
>> >>> of the VectorImage = 1), instead of 1 (after the offsets computed
>> >>> in ImageRegionConstIterator are wrong).
>> >>> So should ImageFileReader divides m_ImageIO->GetNumberOfComponents()
>> by
>> >>> the number of components
>> >>> of TOutputImage::PixelType ? (i manually set it to 1 in the debugger,
>> >>> it's working. However i didn't tried
>> >>> with an image with 12 components, ie two tensors)
>> >>> Or is it a bad idea to use  itk::VectorImage<itk::FixedArray<double,
>> 6>,
>> >>> 3>  ?
>> >>
>> >> To make a VectorImage of tensors, you declare it simply as
>> >> itk::VectorImage< double, 3 > and set the VectorLength on the image to
>> 6.
>> >>
>> >> Or you could use itk::Image< FixedArray< double, 6 >, 3 >.
>> >>
>> >> The two are equivalent.
>> >>
>> >>
>> >>>
>> >>> Thanks a lot
>> >>> Benoit
>> >>>
>> >>> _______________________________________________
>> >>> Powered by www.kitware.com
>> >>>
>> >>> Visit other Kitware open-source projects at
>> >>> http://www.kitware.com/opensource/opensource.html
>> >>>
>> >>> Kitware offers ITK Training Courses, for more information visit:
>> >>> http://kitware.com/products/protraining.html
>> >>>
>> >>> Please keep messages on-topic and check the ITK FAQ at:
>> >>> http://www.itk.org/Wiki/ITK_FAQ
>> >>>
>> >>> Follow this link to subscribe/unsubscribe:
>> >>> http://www.itk.org/mailman/listinfo/insight-developers
>> >>>
>> >>
>> >
>> >
>> >
>> > --
>> > / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
>> / /
>> > / /
>> > Benoit Scherrer
>> > Postdoctoral Research Fellow
>> > Computational Radiology Laboratory,
>> > Harvard Medical School (Boston)
>> >
>> > http://www.crl.med.harvard.edu/
>> > http://www.BenoitScherrer.com
>> > / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
>> / /
>> > / /
>> >
>> > _______________________________________________
>> > Powered by www.kitware.com
>> >
>> > Visit other Kitware open-source projects at
>> > http://www.kitware.com/opensource/opensource.html
>> >
>> > Kitware offers ITK Training Courses, for more information visit:
>> > http://kitware.com/products/protraining.html
>> >
>> > Please keep messages on-topic and check the ITK FAQ at:
>> > http://www.itk.org/Wiki/ITK_FAQ
>> >
>> > Follow this link to subscribe/unsubscribe:
>> > http://www.itk.org/mailman/listinfo/insight-developers
>> >
>> >
>>
>
>
>
> --
> / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
> / /
>
> Benoit Scherrer
> Postdoctoral Research Fellow
> Computational Radiology Laboratory,
> Harvard Medical School (Boston)
>
> http://www.crl.med.harvard.edu/
> http://www.BenoitScherrer.com
> / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
> / /
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20110119/2c590d2e/attachment.htm>


More information about the Insight-developers mailing list