[Insight-users] C++ question

Tom Vercauteren tom.vercauteren at m4x.org
Wed Sep 24 12:34:50 EDT 2008


Hi Luke,

This is also how I do it, except that a complete reader is not
necassary for that. An ImageIOBase is enough.

Here's an example on the dimension. The same can be done on the pixel
type only or on both the pixel type and the dimension.

Regards,
Tom


template <unsigned int Dimension>
void ProcessingFunction( arguments args )
{
   // Declare the types of the images
   typedef float PixelType;
   typedef itk::Image< PixelType, Dimension >  ImageType;

   [...]
}

int main( int argc, char *argv[] )
{
   struct arguments args;
   parseOpts (argc, argv, args);

   // Get the image dimension
   itk::ImageIOBase::Pointer imageIO;
   try
   {
      imageIO = itk::ImageIOFactory::CreateImageIO(
         args.inputImageFile.c_str(), itk::ImageIOFactory::ReadMode);
      if ( imageIO )
      {
         imageIO->SetFileName(args.inputImageFile.c_str());
         imageIO->ReadImageInformation();
      }
      else
      {
         std::cout << "Could not read the input image information." <<
std::endl;
         exit( EXIT_FAILURE );
      }
   }
   catch( itk::ExceptionObject& err )
   {
      std::cout << "Could not read the input image information." << std::endl;
      std::cout << err << std::endl;
      exit( EXIT_FAILURE );
   }

   switch ( imageIO->GetNumberOfDimensions() )
   {
   case 2:
      ProcessingFunction<2>(args);
      break;
   case 3:
      ProcessingFunction<3>(args);
      break;
   default:
      std::cout << "Unsupported dimension" << std::endl;
      exit( EXIT_FAILURE );
   }

   return EXIT_SUCCESS;
}

On Wed, Sep 24, 2008 at 5:47 PM, Karthik Krishnan
<karthik.krishnan at kitware.com> wrote:
> Yes..
>
> You could read the image with the itk::ImageFileReader class in two
> passes. In the first pass, just read the headers. In the second pass,
> read everything including the bulk data.
>
> //pass 1:
> itk::ImageFileReader< itk::Image< double, 3 > > DummyReaderType;
> DummyReaderType::Pointer reader = DummyReaderType::New();
> reader->GenerateOutputInformation();
> pixelComponentType = reader->GetImageIO()->GetComponentType(); //
> assuming one component per pixel.
>
>  switch( pixelType )
>    {
>    case itk::ImageIOBase::CHAR:
>      {
>      typedef itk::Image< char, 3 > ImageType;
>      itk::ImageFileReader< itk::Image< char, 3 > > ReaderType;
>      ReaderType::Pointer bulkReader = ReaderType::New();
>      reader->Update();
>      ImageType::Pointer image = reader->GetOutput();
>      MyTemplatedClass<signed char> runner;
>      runner.Execute(); // do stuff with image
>      break;
>      }
>    case itk::ImageIOBase::UCHAR:
>      {
>      ....
>      }
>
>
> Take a look at InsightApplications/VolViewPlugins/vtkITK*.cxx for
> example . We instantiate all possible pixel types. We have to do this
> since pixeltypes in VTK are specified at run-time, while in ITK are a
> compile time choice.. A similar thing is done in Slicer.
>
>
> On Wed, Sep 24, 2008 at 11:23 AM, Luke Bloy <luke.bloy at gmail.com> wrote:
>> I was worried that would be the answer.
>>
>> How do you guys handle reading image files? is there a way to probe a file
>> to figure out its Pixeltype / dimension? This would be useful for apps that
>> want to work in the native format of the image that is being read.
>>
>> -Luke
>>
>> Karthik Krishnan wrote:
>>>
>>> On Wed, Sep 24, 2008 at 10:55 AM, Luke Bloy <luke.bloy at gmail.com> wrote:
>>>
>>>>
>>>> Hi All,
>>>>
>>>> I'm not, by a long shot, a c++ guru so i have perhaps a pretty basic
>>>> question.
>>>>
>>>> I'd like to be able to do something like this...
>>>>
>>>> int main(int argc, char** argv)
>>>> {
>>>>   typedef double
>>>>  PixelType;
>>>>   unsigned int dimension = 4;
>>>>
>>>>   typedef itk::Image<PixelType, dimension>        ImageType;
>>>> }
>>>>
>>>> This won't compile because dimension isn't const. Ideally i'd like this
>>>> to
>>>> be passed in as a command line argument. Is there a way to get around the
>>>> typedef?
>>>>
>>>
>>> Nope. templates have to be instantiated at compile time, not at run time.
>>>
>>> To get the behaviour you desire, what we tend to do is to instantiate
>>> all possible templates and choose the right template at run-time,
>>>
>>> something like
>>>
>>>  switch (dimension)
>>>    case 3:
>>>      {
>>>      typedef itk::Image<PixelType, 3> ImageType;
>>>      // do stuff
>>>      }
>>>    case 2:
>>>      {
>>>      typedef itk::Image<PixelType, 2> ImageType;
>>>      // do stuff
>>>      }
>>>
>>>
>>>>
>>>> Thanks for any help.
>>>> -Luke
>>>> _______________________________________________
>>>> Insight-users mailing list
>>>> Insight-users at itk.org
>>>> http://www.itk.org/mailman/listinfo/insight-users
>>>>
>>>>
>>>
>>>
>>>
>>>
>>
>
>
>
> --
> Karthik Krishnan
> R&D Engineer,
> Kitware Inc.
> Ph: 518 371 3971 x119
> Fax: 518 371 3971
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
>


More information about the Insight-users mailing list