[Insight-users] [ITK Community] Set the right pixelType

Luis Ibanez luis.ibanez at kitware.com
Sat Jan 11 10:32:26 EST 2014


Hi Massinissa,

You  are mixing two concepts in your code.

They are

             A) The PixelType of an itk::Image. As in
itk::Image<PixelType,3>

                          and

              B) The IOComponentType of an ImageIO class, as defined in:
https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/IO/ImageBase/include/itkImageIOBase.h#L106



The type (A) is expected to be : char, int, short, float, double,.... or a
vector type.

The type (B) is actually an enum, and therefore, closely equivalent to an
int.


What the compilation error is indicating is that:


                        You can't use (B) as an option for (A).




The code that you need, should have the pseudo code structure:


GET_IOCOMPONENT_TYPE

switch( IOTYPE )
{
case UCHAR:
    typedef  proper ImagePixelType
    declare registration type with this type
case CHAR:
    typedef  proper ImagePixelType
    declare registration type with this type
case INT:
    typedef  proper ImagePixelType
    declare registration type with this type
....
etc
}


More explicitly it should be:


class TomoRegistration : public QMainWindow, Ui::TomoRegistrationClass
{
public:
void test(){

        typedef itk::ImageIOBase::IOComponentType ScalarPixelType;

        itk::ImageIOBase::Pointer imageIOsource =
itk::ImageIOFactory::CreateImageIO("writesource.mhd",itk::ImageIOFactory::ReadMode);

        imageIOsource->SetFileName("writesource.mhd");
        imageIOsource->ReadImageInformation();

        itk::ImageIOBase::Pointer imageIOtarget =
itk::ImageIOFactory::CreateImageIO("writetarget.mhd",itk::ImageIOFactory::ReadMode);
        imageIOtarget->SetFileName("writetarget.mhd");
        imageIOtarget->ReadImageInformation();

        ITKRegistration *MI;  // ... etc
        int iteration = 100;

        const itk::ImageIOBase::IOComponentType pixelTypeSource =
imageIOsource->GetComponentType();
        const itk::ImageIOBase::IOComponentType pixelTypeTarget =
imageIOtarget->GetComponentType();

        switch( pixelTypeSource )
        {
        case itk::ImageIOBase::UCHAR:
          {
          typedef unsigned char SourcePixelType;
          SourcePixelType spt;
          switch( pixelTypeTarget )
          {
          case itk::ImageIOBase::UCHAR:
            {
            typedef unsigned char TargetPixelType;
            TargetPixelType tpt;

MI->VolumeRegistration("writesource.mhd","writetarget.mhd",iteration,spt,tpt);
            break;
            }
          case itk::ImageIOBase::CHAR:
            {
            typedef char TargetPixelType;
            TargetPixelType tpt;

MI->VolumeRegistration("writesource.mhd","writetarget.mhd",iteration,spt,tpt);
            break;
            }
          case itk::ImageIOBase::FLOAT:
            {
            typedef float TargetPixelType;
            TargetPixelType tpt;

MI->VolumeRegistration("writesource.mhd","writetarget.mhd",iteration,spt,tpt);
            break;
            }
          }
          break;
          }
        case itk::ImageIOBase::UINT:
          {
          typedef unsigned int SourcePixelType;
          SourcePixelType spt;
          switch( pixelTypeTarget )
          {
          case itk::ImageIOBase::UCHAR:
            {
            typedef unsigned char TargetPixelType;
            TargetPixelType tpt;

MI->VolumeRegistration("writesource.mhd","writetarget.mhd",iteration,spt,tpt);
            break;
            }
          case itk::ImageIOBase::CHAR:
            {
            typedef char TargetPixelType;
            TargetPixelType tpt;

MI->VolumeRegistration("writesource.mhd","writetarget.mhd",iteration,spt,tpt);
            break;
            }
          case itk::ImageIOBase::FLOAT:
            {
            typedef float TargetPixelType;
            TargetPixelType tpt;

MI->VolumeRegistration("writesource.mhd","writetarget.mhd",iteration,spt,tpt);
            break;
            }
          }
          break;
          }
        }

}
};




Note that, for the sake of brevity, this code above doesn't have all the
combinations of types.
You will have to extend the "case" statements in the "switch" to cover all
the combinations
of pixels types that you are interested in processing.



    Regards


        Luis






On Fri, Jan 10, 2014 at 11:00 PM, Massinissa Bandou <
Massinissa.Bandou at usherbrooke.ca> wrote:

> Dear ITK
>
> I'm trying to use a template function to read 2 unknown images type for a
> mutual information registration. The problem is in pixelType1 & pixelType2
> (specified in test() function of class TomoRegistration). If I replace them
> by float or double or any other format, the program compiles and works
> fine.
> In other hand, I get these errors:
>
> Error   2       error C2146: syntax error : missing ';' before identifier
> 'ComponentType' c:\program
> files\itk\include\itk-4.4\itkDefaultConvertPixelTraits.h        45      1
> TomoRegistration
> Error   4       error C2602:
> 'itk::DefaultConvertPixelTraits<PixelType>::ComponentType' is not a member
> of a base class of 'itk::DefaultConvertPixelTraits<PixelType>'  c:\program
> files\itk\include\itk-4.4\itkDefaultConvertPixelTraits.h        45      1
> TomoRegistration
> Error   1       error C2838: 'ComponentType' : illegal qualified name in
> member
> declaration     c:\program
> files\itk\include\itk-4.4\itkDefaultConvertPixelTraits.h        45      1
> TomoRegistration
> Error   5       error C2868:
> 'itk::DefaultConvertPixelTraits<PixelType>::ComponentType' : illegal syntax
> for using-declaration; expected qualified-name  c:\program
> files\itk\include\itk-4.4\itkDefaultConvertPixelTraits.h        45      1
> TomoRegistration
> Error   3       error C4430: missing type specifier - int assumed. Note:
> C++ does
> not support default-int c:\program
> files\itk\include\itk-4.4\itkDefaultConvertPixelTraits.h        45      1
> TomoRegistration
>
>
>
> class ITKRegistration
> {
> public:
>         ITKRegistration();
>         ~ITKRegistration();
>
> template<typename pixelType1, typename pixelType2>
>         vtkSmartPointer<vtkMatrix4x4> VolumeRegistration(const string
> source, const
> string target,int iteration,pixelType1,pixelType2)
>         {
>                 typedef itk::Image< pixelType1, 3 >  FixedImageType;
>                 typedef itk::Image< pixelType2, 3 >  MovingImageType;
>
>                 typedef itk::VersorRigid3DTransform< double >
> TransformType;
>                 typedef itk::VersorRigid3DTransformOptimizer
> OptimizerType;
>                 //typedef itk::MeanSquaresImageToImageMetric<
> FixedImageType,
> MovingImageType > MetricType;
>                 typedef itk::MutualInformationImageToImageMetric<
> FixedImageType,
> MovingImageType > MetricType;
>                 typedef itk::LinearInterpolateImageFunction<
> MovingImageType, double >
> InterpolatorType;
>                 typedef itk::ImageRegistrationMethod< FixedImageType,
> MovingImageType >
> RegistrationType;
>
>                 MetricType::Pointer         metric        =
> MetricType::New();
>                 OptimizerType::Pointer      optimizer     =
> OptimizerType::New();
>                 InterpolatorType::Pointer   interpolator  =
> InterpolatorType::New();
>                 TransformType::Pointer  transform = TransformType::New();
>
>                 RegistrationType::Pointer   registration  =
> RegistrationType::New();
>                 registration->SetMetric(        metric        );
>                 registration->SetOptimizer(     optimizer     );
>                 registration->SetInterpolator(  interpolator  );
>                 registration->SetTransform( transform );
>
>                 typedef itk::ImageFileReader<FixedImageType>
> FixedImageReaderType;
>                 typedef itk::ImageFileReader<MovingImageType>
> MovingImageReaderType;
>
>                 FixedImageReaderType::Pointer  fixedImageReader  =
> FixedImageReaderType::New();
>                 MovingImageReaderType::Pointer movingImageReader =
> MovingImageReaderType::New();
>                 fixedImageReader->SetFileName(source.c_str());
>                 movingImageReader->SetFileName(target.c_str());
>
>
>                 registration->SetFixedImage(fixedImageReader->GetOutput());
>                 registration->SetMovingImage(
> movingImageReader->GetOutput()   );
>
>                 try{
>                         fixedImageReader->Update();
>                 }catch(itk::ExceptionObject & excp){
>                         std::cerr << excp << std::endl;
>                 }
>
>
>
> registration->SetFixedImageRegion(fixedImageReader->GetOutput()->GetBufferedRegion()
> );
>
>                 typedef
>
> itk::CenteredTransformInitializer<TransformType,FixedImageType,MovingImageType>
> TransformInitializerType;
>                 TransformInitializerType::Pointer initializer =
> TransformInitializerType::New();
>
>                 initializer->SetTransform(transform);
>                 initializer->SetFixedImage(fixedImageReader->GetOutput());
>
> initializer->SetMovingImage(movingImageReader->GetOutput());
>                 initializer->MomentsOn();
>                 initializer->InitializeTransform();
>
>                 typedef TransformType::VersorType  VersorType;
>                 typedef VersorType::VectorType     VectorType;
>                 VersorType     rotation;
>                 VectorType     axis;
>                 axis[0] = 0.0;
>                 axis[1] = 0.0;
>                 axis[2] = 1.0;
>                 const double angle = 0;
>                 rotation.Set(  axis, angle  );
>                 transform->SetRotation( rotation );
>
>                 registration->SetInitialTransformParameters(
> transform->GetParameters() );
>
>                 typedef OptimizerType::ScalesType
> OptimizerScalesType;
>                 OptimizerScalesType optimizerScales(
> transform->GetNumberOfParameters() );
>                 const double translationScale = 1.0 / 1000.0;
>                 optimizerScales[0] = 1.0;
>                 optimizerScales[1] = 1.0;
>                 optimizerScales[2] = 1.0;
>                 optimizerScales[3] = translationScale;
>                 optimizerScales[4] = translationScale;
>                 optimizerScales[5] = translationScale;
>                 optimizer->SetScales( optimizerScales );
>                 optimizer->SetMaximumStepLength( 0.2000  );
>                 optimizer->SetMinimumStepLength( 0.0001 );
>                 optimizer->SetNumberOfIterations( iteration );
>
>                 CommandIterationUpdate::Pointer observer =
> CommandIterationUpdate::New();
>                 observer->GetTomo(this->in);
>                 optimizer->AddObserver(itk::IterationEvent(),observer);
>
>                 try{
>                         registration->Update();
>                         std::cout << "Optimizer stop condition: "<<
> registration->GetOptimizer()->GetStopConditionDescription()<< std::endl;
>                 }
>                 catch( itk::ExceptionObject & err ){
>                         std::cerr << "ExceptionObject caught !" <<
> std::endl;
>                         std::cerr << err << std::endl;
>                 }
>
>                 OptimizerType::ParametersType finalParameters =
> registration->GetLastTransformParameters();
>                 const double versorX              = finalParameters[0];
>                 const double versorY              = finalParameters[1];
>                 const double versorZ              = finalParameters[2];
>                 const double finalTranslationX    = finalParameters[3];
>                 const double finalTranslationY    = finalParameters[4];
>                 const double finalTranslationZ    = finalParameters[5];
>                 const unsigned int numberOfIterations =
> optimizer->GetCurrentIteration();
>                 const double bestValue = optimizer->GetValue();
>
>                 std::cout << std::endl << std::endl;
>                 std::cout << " Result = " << std::endl;
>                 std::cout << " versor X      = " << versorX  << std::endl;
>                 std::cout << " versor Y      = " << versorY  << std::endl;
>                 std::cout << " versor Z      = " << versorZ  << std::endl;
>                 std::cout << " Translation X = " << finalTranslationX  <<
> std::endl;
>                 std::cout << " Translation Y = " << finalTranslationY  <<
> std::endl;
>                 std::cout << " Translation Z = " << finalTranslationZ  <<
> std::endl;
>                 std::cout << " Iterations    = " << numberOfIterations <<
> std::endl;
>                 std::cout << " Metric value  = " << bestValue          <<
> std::endl;
>
>                 transform->SetParameters( finalParameters );
>                 TransformType::MatrixType matrix = transform->GetMatrix();
>                 TransformType::OffsetType offset = transform->GetOffset();
>                 std::cout << "Matrix = " << std::endl << matrix <<
> std::endl;
>                 std::cout << "Offset = " << std::endl << offset <<
> std::endl;
>
>                 vtkSmartPointer<vtkMatrix4x4> matrixOfTransformation =
> vtkSmartPointer<vtkMatrix4x4>::New();
>                 for(unsigned int i=0;i<3;i++){
>                         for(unsigned int j=0;j<3;j++){
>
> matrixOfTransformation->SetElement(i,j,matrix(i,j));
>                         }
>                 }
>                 matrixOfTransformation->SetElement(0,3,versorX);
>                 matrixOfTransformation->SetElement(1,3,versorY);
>                 matrixOfTransformation->SetElement(2,3,versorZ);
>                 matrixOfTransformation->SetElement(3,0,0);
>                 matrixOfTransformation->SetElement(3,1,0);
>                 matrixOfTransformation->SetElement(3,2,0);
>                 matrixOfTransformation->SetElement(3,3,1);
>
>                 cout<<"matrix from mutual information:
> "<<endl<<*matrixOfTransformation<<endl;
>
>                 return matrixOfTransformation;
> }
> };
>
>
>
> class TomoRegistration : public QMainWindow, Ui::TomoRegistrationClass
> {
> public:
> void test(){
> typedef itk::ImageIOBase::IOComponentType ScalarPixelType;
>         itk::ImageIOBase::Pointer imageIOsource =
>
> itk::ImageIOFactory::CreateImageIO("writesource.mhd",itk::ImageIOFactory::ReadMode);
>         imageIOsource->SetFileName("writesource.mhd");
>         imageIOsource->ReadImageInformation();
>         const ScalarPixelType pixelTypeSource =
> imageIOsource->GetComponentType();
>
>         itk::ImageIOBase::Pointer imageIOtarget =
>
> itk::ImageIOFactory::CreateImageIO("writetarget.mhd",itk::ImageIOFactory::ReadMode);
>         imageIOtarget->SetFileName("writetarget.mhd");
>         imageIOtarget->ReadImageInformation();
>         const ScalarPixelType pixelTypeTarget =
> imageIOtarget->GetComponentType();
>
> vtkSmartPointer<vtkMatrix4x4> MatrixFromMutualInformation =
>
> MI->VolumeRegistration("writesource.mhd","writetarget.mhd",iteration,pixelTypeSource,pixelTypeTarget);
> }
> };
>
>
>
>
> --
> View this message in context:
> http://itk-users.7.n7.nabble.com/Set-the-right-pixelType-tp33171.html
> Sent from the ITK - Users mailing list archive at Nabble.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://www.kitware.com/products/protraining.php
>
> 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-users
> _______________________________________________
> Community mailing list
> Community at itk.org
> http://public.kitware.com/cgi-bin/mailman/listinfo/community
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20140111/a351339e/attachment.html>


More information about the Insight-users mailing list