ANIMA  4.0
animaMajorityLabelVotingImageFilter.hxx
Go to the documentation of this file.
1 #pragma once
3 
4 #include <itkImageRegionConstIterator.h>
5 #include <itkImageRegionIterator.h>
6 #include <map>
7 
8 namespace anima
9 {
10 
11 template <class TPixelType>
12 void
13 MajorityLabelVotingImageFilter <TPixelType>
14 ::DynamicThreadedGenerateData(const InputRegionType &region)
15 {
16  unsigned int numInputs = this->GetNumberOfIndexedInputs();
17 
18  typedef itk::ImageRegionConstIterator <InputImageType> InputIteratorType;
19  typedef itk::ImageRegionIterator <OutputImageType> OutputIteratorType;
20 
21  std::vector<InputIteratorType> inputIterators (numInputs);
22 
23  for (unsigned int i = 0; i < numInputs; ++i)
24  inputIterators[i] = InputIteratorType (this->GetInput(i), region);
25 
26  OutputIteratorType outputIterator (this->GetOutput(), region);
27 
28  std::map <TPixelType,unsigned int> vecCount;
29  using MapIterator = typename std::map <TPixelType,unsigned int>::iterator;
30  while (!inputIterators[0].IsAtEnd())
31  {
32  vecCount.clear();
33 
34  for (unsigned int i = 0;i < numInputs;++i)
35  {
36  TPixelType value = inputIterators[i].Get();
37  if (vecCount.count(value) == 0)
38  vecCount[value] = 1;
39  else
40  ++vecCount[value];
41  }
42 
43  TPixelType outValue = 0;
44  unsigned int maxCount = 0;
45  for (MapIterator it = vecCount.begin();it != vecCount.end();++it)
46  {
47  if (it->second > maxCount)
48  {
49  outValue = it->first;
50  maxCount = it->second;
51  }
52  }
53 
54  //Set output value then move iterators
55  outputIterator.Set(outValue);
56  for (unsigned int i = 0;i < numInputs;++i)
57  ++inputIterators[i];
58  ++outputIterator;
59  }
60 }
61 
62 } // end namespace anima
typename InputImageType::RegionType InputRegionType