[ITK Community] [Insight-users] MultipleImageIterator

Joël Schaerer joel.schaerer at gmail.com
Thu Feb 20 04:49:13 EST 2014


I made a (tiny) Insight Journal article: 
http://www.insight-journal.org/browse/publication/915

Regarding IsAtEnd(), I decided to handle it differently depending on 
whether or not the code is compiled in debug mode:
- If compiled in release mode, only the first iterator is checked since 
the multiple iterator is only makes sense if all the iterators have the 
same lenght
- In debug mode, all iterators are checked and assertions are in place 
to make sure their IsAtEnd() outputs all match.

joel

Le 06/02/2014 18:03, Matt McCormick a écrit :
> Hi Joel,
>
> Sounds like an interesting topic for an Insight Journal article [1].
>
> An challenge is to handle IsAtEnd().  In your example !it[0].IsAtEnd()
> is applied, which may be appropriate for some cases.  In other cases,
> the iterators may complete at different points. Something to think
> about ...
>
> Thanks,
> Matt
>
> [1] http://insight-journal.org/
>
> On Thu, Feb 6, 2014 at 5:54 AM, Joël Schaerer <joel.schaerer at gmail.com> wrote:
>> Hi all,
>>
>> Recently I've had to iterate over many images at the same time. Instead of
>> mixing the tedious iterator housekeeping code with the algorithm, I thought
>> it could be a good idea to have a "multiple image iterator" for that. Since
>> I didn't find one in ITK, I started writing a simple one myself. Its main
>> limitation is that all iterators and thus all images must be of the same
>> type:
>>
>> namespace itk {
>> template<typename TIterator>
>> class MultipleImageIterator {
>> public:
>>    typedef MultipleImageIterator Self;
>>    typedef TIterator IteratorType;
>>    typedef typename IteratorType::ImageType ImageType;
>>    IteratorType& operator[](const int i) {return m_iterators[i];}
>>    void AddIterator(const IteratorType& it) {m_iterators.push_back(it);}
>>    Self& operator++() {
>>      for (typename std::vector<IteratorType>::iterator it =
>> m_iterators.begin();
>>        it != m_iterators.end(); ++it) {
>>        ++(*it);
>>      }
>>    }
>>    void GoToBegin() {
>>      for (typename std::vector<IteratorType>::iterator it =
>> m_iterators.begin();
>>        it != m_iterators.end(); ++it) {
>>        it->GoToBegin();
>>      }
>>    }
>>    unsigned int Size () const { return m_iterators.size(); }
>> protected:
>>    std::vector<IteratorType> m_iterators;
>>
>> };
>> }
>>
>> Here is a pretty straightforward usage example, which prints the values of 4
>> images side by side:
>>
>> int main()
>> {
>>    typedef itk::Image<float,3> ImageType;
>>    typedef itk::ImageFileReader<ImageType> ReaderType;
>>
>>    typedef itk::ImageRegionIterator<ImageType> IteratorType;
>>    itk::MultipleImageIterator<IteratorType> it;
>>
>>    std::string filenames[] =
>> {"originalT1.mha","csf_reg.nii.gz","grey_reg.nii.gz","white_reg.nii.gz"};
>>    std::vector<ImageType::Pointer> images; // Need to keep a reference as
>> iterators only have weak references
>>    ReaderType::Pointer r = ReaderType::New();
>>    for (unsigned int i=0; i<4;++i) {
>>      r->SetFileName(filenames[i]);
>>      r->Update();
>>      ImageType::Pointer im = r->GetOutput();
>>      im->DisconnectPipeline();
>>      images.push_back(im);
>> it.AddIterator(itk::ImageRegionIterator<ImageType>(im,im->GetLargestPossibleRegion()));
>>    }
>>
>>    for (it.GoToBegin(); !it[0].IsAtEnd(); ++it) {
>>      if ((it[1].Get() != 0) && ((float)std::rand()) / RAND_MAX < 0.01) {
>>        for (unsigned int i=0; i<it.Size(); ++i) {
>>          std::cout << it[i].Get() << ";";
>>        }
>>        std::cout << std::endl;
>>      }
>>    }
>> }
>>
>>
>> Any comments? What is the usual way of doing this with ITK? If there is any
>> interest, this could be improved and potentially included in the library.
>>
>> joel
>> _____________________________________
>> 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

_____________________________________
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



More information about the Community mailing list