[Insight-users] using NumericTraits in a templated function

Dr. Roman Grothausmann grothausmann.roman at mh-hannover.de
Fri Apr 19 08:04:39 EDT 2013


Dear mailing list members,


Is it possible to use NumericTraits in a templated function?
E.g. using it like this:

template<typename InputPixelType, size_t Dimension>
int DoIt(int argc, char *argv[]){

     arg= argv[3];
     if (!strcmp(arg, "max")){
       printf("%s: identified as max\n", arg);
       th_l= itk::NumericTraits<InputPixelType>::max();
       std::cout << "Max: " << itk::NumericTraits< float >::max() << std::endl;
       }
     else if (!strcmp(arg, "min")){
       printf("%s: identified as min\n", arg);
       th_l= itk::NumericTraits<InputPixelType>::min();
       std::cout << "Min: " << itk::NumericTraits<InputPixelType>::min() << 
std::endl;
       std::cout << "Is -1 negative? " << 
itk::NumericTraits<InputPixelType>::IsNegative(-1) << std::endl;
       std::cout << "Is 1 negative? " << 
itk::NumericTraits<InputPixelType>::IsNegative(1) << std::endl;
       }
     else
       th_l= InputPixelType(atof(arg));

...
calling it in main with:

...
     try
         {
         GetImageType(ifn, pixelType, componentType, dimensionType);

         switch (componentType)
             {
             case itk::ImageIOBase::UCHAR:{
                 typedef unsigned char InputPixelType;
                 switch (dimensionType){
                 case 1:
		    DoIt<InputPixelType, 1>(argc, argv);
                     break;
                 case 2:
                     DoIt<InputPixelType, 2>(argc, argv);
                     break;
                 case 3:
                     DoIt<InputPixelType, 3>(argc, argv);
                     break;
                 default:
                     std::cout << "Images of dimension " << dimensionType << " 
are not supported!" << std::endl;
                     break;
                     }
                 break;
                 }
             case itk::ImageIOBase::CHAR:{
                 typedef char InputPixelType;
                 switch (dimensionType){
                 case 1:
		    DoIt<InputPixelType, 1>(argc, argv);
                     break;
                 case 2:
                     DoIt<InputPixelType, 2>(argc, argv);
                     break;
                 case 3:
                     DoIt<InputPixelType, 3>(argc, argv);
                     break;
                 default:
                     std::cout << "Images of dimension " << dimensionType << " 
are not supported!" << std::endl;
                     break;
                     }
                 break;
                 }
             case itk::ImageIOBase::USHORT:{
                 typedef unsigned short InputPixelType;
                 switch (dimensionType){
                 case 1:
                     DoIt<InputPixelType, 1>(argc, argv);
                     break;
                 case 2:
                     DoIt<InputPixelType, 2>(argc, argv);
                     break;
                 case 3:
                     DoIt<InputPixelType, 3>(argc, argv);
                     break;
                 default:
                     std::cout << "Images of dimension " << dimensionType << " 
are not supported!" << std::endl;
                     break;
                     }

                 break;
                 }
...


The program when run reports:

Pixel Type is unsigned_short
numDimensions: 3
component size: 2
pixel type (string): scalar
pixel type: 1
max: identified as max
Max: 3.40282e+38

I would expect Max to be 65535 for unsigned_short. Am I missing something or 
using it in an inappropriate way? If I use NumericTraits in a function that is 
not templated (e.g. as in 
http://itk.org/ITKExamples/Examples/Core/Common/GetTypeBasicInformation/GetTypeBasicInformation.html), 
it works as expected.

Any help or hints are very much appreciated
Roman

-- 
Dr. Roman Grothausmann

Tomographie und Digitale Bildverarbeitung
Tomography and Digital Image Analysis

Institut für Funktionelle und Angewandte Anatomie, OE 4120
Medizinische Hochschule Hannover
Carl-Neuberg-Str. 1
D-30625 Hannover

Tel. +49 511 532-9574


More information about the Insight-users mailing list