[Insight-users] Design question when using ITK classes

Bill Lorensen bill.lorensen at gmail.com
Wed Feb 20 08:28:42 EST 2008


Anja,

Yes, at compile time you will need to instantiate each type you expect. At
runtime you can determine which type to use with something like this:

namespace itk
{
  // Description:
  // Get the PixelType and ComponentType from fileName
  void GetImageType (std::string fileName,
                     ImageIOBase::IOPixelType &pixelType,
                     ImageIOBase::IOComponentType &componentType)
    {
      typedef itk::Image<unsigned char, 3> ImageType;
      itk::ImageFileReader<ImageType>::Pointer imageReader =
        itk::ImageFileReader<ImageType>::New();
      imageReader->SetFileName(fileName.c_str());
      imageReader->UpdateOutputInformation();
      pixelType = imageReader->GetImageIO()->GetPixelType();
      componentType = imageReader->GetImageIO()->GetComponentType();
    }
}

Then in your application do something like this:

  itk::ImageIOBase::IOPixelType pixelType;
  itk::ImageIOBase::IOComponentType componentType;
  try
    {
    itk::GetImageType (inputVolume, pixelType, componentType);
    // This filter handles all types

    switch (componentType)
      {
      case itk::ImageIOBase::UCHAR:
        return DoIt( static_cast<unsigned char>(0));
        break;
      case itk::ImageIOBase::CHAR:
        return DoIt( static_cast<char>(0));
        break;
      case itk::ImageIOBase::USHORT:
        return DoIt( static_cast<unsigned short>(0));
        break;
      case itk::ImageIOBase::SHORT:
        return DoIt( static_cast<short>(0));
        break;
      case itk::ImageIOBase::UINT:
        return DoIt( static_cast<unsigned int>(0));
        break;
      case itk::ImageIOBase::INT:
        return DoIt( static_cast<int>(0));
        break;
      case itk::ImageIOBase::ULONG:
        return DoIt( static_cast<unsigned long>(0));
        break;
      case itk::ImageIOBase::LONG:
        return DoIt( static_cast<long>(0));
        break;
      case itk::ImageIOBase::FLOAT:
        return DoIt( static_cast<float>(0));
        break;
      case itk::ImageIOBase::DOUBLE:
        return DoIt( static_cast<double>(0));
        break;
      case itk::ImageIOBase::UNKNOWNCOMPONENTTYPE:
      default:
        std::cout << "unknown component type" << std::endl;
        break;
      }
    }
  catch( itk::ExceptionObject &excep)
    {
    std::cerr << argv[0] << ": exception caught !" << std::endl;
    std::cerr << excep << std::endl;
    return EXIT_FAILURE;
    }
  return EXIT_SUCCESS;
}
where Doit is your method:
template<class T> int DoIt(T )
{
...
}

CAUTION: your compiler may not be able to instantiate all type without
having stack overflow or other problems, You can sometimes combine some of
the types (like unsigned int and int) to reduce the number needed.

Bill



On Wed, Feb 20, 2008 at 7:04 AM, Anja Ende <anja.ende at googlemail.com> wrote:

> Hi everyone,
>
> A quick question about using ITK classes. I am using C++ and am having a
> bit of trouble understanding how I can use the templated classes without
> instantiating for every possible type combination.
>
> A simple example:
>
> I have a custom file reader where the data type and dimensions are
> something that is only available at run time..
>
> So, now if I want to have a class member that uses a reader, I have to
> somehow define these template parameters at runtime... What I wanted was to
> have one reader which has one instance of the ImageImportFilter instance.
> However, to declare this class I need to define the template parameters for
> the ImportImageFilter at design time. So, now if a file is inputted with a
> different input data type and a dimension, I am stuck!
>
> Any ideas??
>
> Cheers,
>
> Anja
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20080220/574465db/attachment.htm


More information about the Insight-users mailing list