[Insight-users] Reading an mhd file with unknown pixel type

Luis Ibanez luis.ibanez at kitware.com
Thu Dec 2 09:10:08 EST 2010


Hi David

Please see comments below.

On Thu, Dec 2, 2010 at 8:35 AM, David Doria <daviddoria at gmail.com> wrote:
> On Wed, Dec 1, 2010 at 8:13 PM, Luis Ibanez <luis.ibanez at kitware.com> wrote:
>> Hi David,
>>
>> On way to deal with image whose number of components
>> are not known until run time is to use the class.
>>
>>                     itk::VectorImage
>>
>> This image class allows you to set the number of components
>> at run time.
>>
>>
>>      Luis
>
> Arnaud, Luis,
>
> That enum lets you check the pixel type manually (in a loop, comparing
> the GetComponentType() result to all of the entires in the enum, but
> can you do something more like this?
>
>  const ScalarPixelType pixelType = imageIO->GetComponentType();
>  const int numberOfComponents = imageIO->GetNumberOfComponents();
>
>  typedef itk::VectorImage<pixelType, 2> ImageType;
>  image->SetNumberOfComponentsPerPixel(numberOfComponents);
>

Nope you can't do this.
It mixes run time with compilation time.

The expression:

>  typedef itk::VectorImage<pixelType, 2> ImageType;

must be solved at compilation time

while the value of pixelType is only assigned at run time.

The typical way to do this is to use "pixelType" as the
control in a switch statement that instantiate the proper
readers. Such as:

ImageBase::Pointer image ;
switch( pixelType ) [
case 0:
   typedef itk::VectorImage<char, 2> ImageType;
   typedef itk::ImageFileReader<ImageType> ReaderType;
   ReaderType::Pointer reader = ReaderType::New();
   reader->SetFileName( filename );
   reader->Update();
   image = reader->GetOutput();
   break;
case 1:
   typedef itk::VectorImage<int, 2> ImageType;
   typedef itk::ImageFileReader<ImageType> ReaderType;
   ReaderType::Pointer reader = ReaderType::New();
   reader->SetFileName( filename );
   reader->Update();
   image = reader->GetOutput();
   break;
    ...
//  etc for other pixel types.
}

You may find useful to look at:

InsightApplications/Auxiliary/vtk/vtkKWImageIO.cxx

lines 255-324.



     Luis


---------------------------
> Of course it is complaining because the enum value in pixelType is not
> equivalent to the actual word 'float' which would need to be passed as
> the template parameter.
>
> David
>


More information about the Insight-users mailing list