[ITK-users] How to determine PixelType of itk::Image

Dženan Zukić dzenanz at gmail.com
Wed Mar 16 16:44:39 EDT 2016


Hi Lambert,

that is a limitation which has annoyed me for quite some time. I
frequently have:

template< typename TImage >
void WriteDebug(typename TImage::Pointer out, const char *filename)
{
  typedef ImageFileWriter<TImage> WriterType;
  typename WriterType::Pointer w = WriterType::New();
  w->SetInput(out);
  w->SetFileName(filename);
  try
    {
    w->Update();
    }
  catch (ExceptionObject & error)
    {
    std::cerr << "Error: " << error << std::endl;
    }
}

I have not figured out a better way to invoke this function than this:
typedef itk::Image<short, 3> ImageType; //defined somewhere, once
WriteDebug<ImageType>(step3, "C:\\step3.nrrd"); //ImageType used many times over

Regards,
Dženan

On Wed, Mar 16, 2016 at 4:32 PM, Lambert Zijp <ljzijp at gmail.com> wrote:
> Hi Dženan,
>
> Thank you, you have been very helpful. I opted for the 'bloated' version,
> and it works. It's just a pity that the caller of the conversion function
> needs to know what type of image is to be converted. In order to convert a
> 3D deformation vector field, the caller needs to say:
>    rc = ItkToAvs<itk::Vector<float, 3>, 3>(ppAvsVectorField,
> pItkVectorField);
> (The prototype of ItkToAvs is given in the first post of this thread)
> On other words, the caller needs to know that such an image is 3D and
> contains float triplet pixels. Is there a way that the compiler works this
> out?
>
> Greetings,
> Lambert
>
>
> On Tue, Mar 15, 2016 at 8:49 PM, Dženan Zukić <dzenanz at gmail.com> wrote:
>>
>> Hi Lambert,
>>
>> a 'general' interface cannot be made. You are not the first to face this
>> issue. One way is to do if/else for all the supported cases. Another way is
>> to define some template function or helper class, and then let compiler take
>> care of type matching. But indeed, the code is either bloated (version one)
>> or somewhat exotic (version two).
>>
>> RGB image can be stored either way. You can have itk::Image<char,3> with 3
>> dimensions x,y,color (consecutive color planes) or
>> itk::Image<itk::RGBPixel<char>, 2> (consecutive triplets). It depends on
>> your treatment. I believe that consecutive triplets is more common.
>>
>> Regards,
>> Dženan
>>
>> On Tue, Mar 15, 2016 at 3:39 PM, Lambert Zijp <ljzijp at gmail.com> wrote:
>>>
>>> Hi Dženan,
>>>
>>> Haha, true enough, ItkToAvs would have to return an errorcode
>>> 'unsupported'. Yes, pixelspacing and origin can be retrieved. Also the size
>>> of each dimension, though that took me a while: it was well hidden.
>>> Still, it is very frustrating that it cannot be determined from
>>> itk::image whether it is an ordinary, commonly occurring image like an BGR
>>> ARGB or a 3-vector float image.
>>> At least, that is what I gather from your answer: a 'general' interface
>>> to other image processing libraries cannot be made!
>>>
>>> Am I the first person having problems with this?
>>> How is an RGB image stored in itk, as consecutive triplets or as
>>> consecutive color-planes.
>>>
>>> Greetings,
>>> Lambert
>>>
>>> On Tue, Mar 15, 2016 at 7:57 PM, Dženan Zukić <dzenanz at gmail.com> wrote:
>>>>
>>>> Yes, it knows. Using something like this:
>>>>
>>>> template <typename ImageType>
>>>> void ItkToAvs(AVSfield** ppAvs, typename ImageType::Pointer image)
>>>> {
>>>> typename ImageType::PixelType *pixelPointer=image->GetBufferPointer();
>>>> unsigned int dimension=ImageType::ImageDimension;
>>>> //the rest of conversion code
>>>> }
>>>>
>>>> However, ImageType::PixelType can be arbitrary type. Consider this:
>>>> class myClass {int a; complex<double> c; string sRep;};
>>>> typedef itk::Image<myClass, 2> ImageType;
>>>> ItkToAvs(ppAvs, image);
>>>>
>>>> What do you expect ItkToAvs to do?
>>>>
>>>> Regards,
>>>> Dženan
>>>>
>>>> On Tue, Mar 15, 2016 at 2:53 PM, Lambert Zijp <ljzijp at gmail.com> wrote:
>>>>>
>>>>> This is all very confusing to me. In the FileWriter example, the caller
>>>>> already knows what the IOComponentType and what the IOPixelType of the
>>>>> itk::Image is (it is hard-coded; not retrieved from the itk::Image). The
>>>>> same holds for the reader: it gets that information from the file.
>>>>> I want to make a general function that takes ANY itk::Image as input,
>>>>> and converts it to an AVS datasructure (which supports all of them). So I
>>>>> need to get the information from itk::Image.
>>>>> Is there a way to get it from itk::Image? Or is it true that an
>>>>> itk::Image does not know what it actually is/represents?
>>>>>
>>>>> On Tue, Mar 15, 2016 at 7:07 PM, Dženan Zukić <dzenanz at gmail.com>
>>>>> wrote:
>>>>>>
>>>>>> If I remember correctly, using this approach you can also get number
>>>>>> of components with something like:
>>>>>> imageIO->GetNumberOfComponents();
>>>>>> You can take a look at class documentation for more details.
>>>>>>
>>>>>> On Tue, Mar 15, 2016 at 1:09 PM, Lambert Zijp <ljzijp at gmail.com>
>>>>>> wrote:
>>>>>>>
>>>>>>> I'm still not sure how to handle vector images. Dženan gave an
>>>>>>> example of an RGB image. That would be a 3-vector unsigned char pixeltype.
>>>>>>> Would I have to write out in those 'if  (typeid(ValueType) == ???)'
>>>>>>> statements all permutations of the number of vectors and the type? Or is
>>>>>>> there a way to separate the number of vectors and the pixeltype?
>>>>>>> How are vector images stored in memory in ITK. In an RGB image for
>>>>>>> example, are the pixels consecutive triplets, or is the image stored as
>>>>>>> three consecutive color planes?
>>>>>>> I'm particular interested in 3D displacement vector fields, that are
>>>>>>> the result of deformable registration.
>>>>>>>
>>>>>>>
>>>>>>> On Tue, Mar 15, 2016 at 5:22 PM, Bill Lorensen
>>>>>>> <bill.lorensen at gmail.com> wrote:
>>>>>>>>
>>>>>>>> This example may help
>>>>>>>> http://www.vtk.org/Wiki/ITK/Examples/IO/ReadUnknownImageType
>>>>>>>>
>>>>>>>> On Mar 15, 2016 8:44 AM, "Dženan Zukić" <dzenanz at gmail.com> wrote:
>>>>>>>>>
>>>>>>>>> Of course it complained, switch statement is for integral types (if
>>>>>>>>> I remember correctly).
>>>>>>>>>
>>>>>>>>> Regards
>>>>>>>>>
>>>>>>>>> On Tue, Mar 15, 2016 at 11:39 AM, Lambert Zijp <ljzijp at gmail.com>
>>>>>>>>> wrote:
>>>>>>>>>>
>>>>>>>>>> Hi Dženan,
>>>>>>>>>>
>>>>>>>>>> Thank you very much!
>>>>>>>>>> Visual Studio 9 does not recognize decltype(), but typeid() worked
>>>>>>>>>> fine. At least, when you make separate if statements, like you did in your
>>>>>>>>>> example. When tried to use it in a switch statement, the compiler
>>>>>>>>>> complained.
>>>>>>>>>>
>>>>>>>>>> Greetings,
>>>>>>>>>> Lambert
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> On Tue, Mar 15, 2016 at 2:47 PM, Dženan Zukić <dzenanz at gmail.com>
>>>>>>>>>> wrote:
>>>>>>>>>>>
>>>>>>>>>>> Hi Lambert,
>>>>>>>>>>>
>>>>>>>>>>> the classic way is using a bunch of if-else's.
>>>>>>>>>>>
>>>>>>>>>>> if (decltype(ValueType)==decltype(int))
>>>>>>>>>>> {
>>>>>>>>>>> //int specific processing
>>>>>>>>>>> }
>>>>>>>>>>> else if (decltype(ValueType)==decltype(itk::RGB<char>))
>>>>>>>>>>> {
>>>>>>>>>>> //RGB<char> specific processing
>>>>>>>>>>> }
>>>>>>>>>>> else if ...
>>>>>>>>>>>
>>>>>>>>>>> If you can't use C++11's decltype then use typeid from <typeinfo>
>>>>>>>>>>> header.
>>>>>>>>>>>
>>>>>>>>>>> Regards,
>>>>>>>>>>> Dženan
>>>>>>>>>>>
>>>>>>>>>>> On Tue, Mar 15, 2016 at 5:48 AM, Lambert Zijp <ljzijp at gmail.com>
>>>>>>>>>>> wrote:
>>>>>>>>>>>>
>>>>>>>>>>>> I want to write a function that converts an arbitrary ITK image
>>>>>>>>>>>> to another format (AVS in my case).
>>>>>>>>>>>> It has been suggested that following functionprototype could de
>>>>>>>>>>>> the job:
>>>>>>>>>>>> template<typename ValueType,unsigned int Dimension>
>>>>>>>>>>>> int ItkToAvs(AVSfield** ppAvs, typename
>>>>>>>>>>>> itk::Image<ValueType,Dimension>::Pointer pItkImage);
>>>>>>>>>>>>
>>>>>>>>>>>> Now I want to know whether ValueType equals unsigned char or
>>>>>>>>>>>> short or int etc. Also whether the pixels are single numbers or vectors. How
>>>>>>>>>>>> does one do that?
>>>>>>>>>>>>
>>>>>>>>>>>> Greetings,
>>>>>>>>>>>> Lambert
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> _____________________________________
>>>>>>>>>>>> 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://public.kitware.com/mailman/listinfo/insight-users
>>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> _____________________________________
>>>>>>>>> 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://public.kitware.com/mailman/listinfo/insight-users
>>>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> _____________________________________
>>>>> 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://public.kitware.com/mailman/listinfo/insight-users
>>>>>
>>>>
>>>
>>
>


More information about the Insight-users mailing list