ANIMA  4.0
animaMaskedImageToImageFilter.hxx
Go to the documentation of this file.
1 #pragma once
3 
4 #include <itkImageRegionConstIterator.h>
5 
6 namespace anima
7 {
8 template< typename TInputImage, typename TOutputImage >
9 void
10 MaskedImageToImageFilter < TInputImage, TOutputImage >
11 ::CheckComputationMask()
12 {
13  if (!m_ComputationMask)
14  {
15  m_ComputationMask = MaskImageType::New();
16  m_ComputationMask->Initialize();
17 
18  m_ComputationMask->SetRegions (this->GetOutput(0)->GetLargestPossibleRegion());
19  m_ComputationMask->SetSpacing (this->GetOutput(0)->GetSpacing());
20  m_ComputationMask->SetOrigin (this->GetOutput(0)->GetOrigin());
21  m_ComputationMask->SetDirection (this->GetOutput(0)->GetDirection());
22 
23  m_ComputationMask->Allocate();
24 
25  m_ComputationMask->FillBuffer(1);
26  }
27 }
28 
29 template< typename TInputImage, typename TOutputImage >
30 void
32 ::BeforeThreadedGenerateData()
33 {
34  if (this->GetComputationRegion().GetSize(0) == 0)
35  this->InitializeComputationRegionFromMask();
36 
37  Superclass::BeforeThreadedGenerateData();
38 }
39 
40 
41 template< typename TInputImage, typename TOutputImage >
42 void
44 ::InitializeComputationRegionFromMask()
45 {
46  this->CheckComputationMask();
47 
48  typedef itk::ImageRegionConstIterator< MaskImageType > MaskRegionIteratorType;
49 
50  MaskRegionIteratorType maskItr(m_ComputationMask,m_ComputationMask->GetLargestPossibleRegion());
51  maskItr.GoToBegin();
52 
53  MaskIndexType minPos, maxPos;
54 
55  for (unsigned int i = 0;i < m_ComputationMask->GetImageDimension();++i)
56  {
57  minPos[i] = m_ComputationMask->GetLargestPossibleRegion().GetIndex()[i] + m_ComputationMask->GetLargestPossibleRegion().GetSize()[i];
58  maxPos[i] = 0;
59  }
60 
61  unsigned int numPoints = 0;
62  while (!maskItr.IsAtEnd())
63  {
64  if (maskItr.Get() != 0)
65  {
66  ++numPoints;
67  MaskIndexType tmpInd = maskItr.GetIndex();
68 
69  for (unsigned int i = 0;i < m_ComputationMask->GetImageDimension();++i)
70  {
71  if (minPos[i] > tmpInd[i])
72  minPos[i] = tmpInd[i];
73 
74  if (maxPos[i] < tmpInd[i])
75  maxPos[i] = tmpInd[i];
76  }
77  }
78 
79  ++maskItr;
80  }
81 
82  this->SetNumberOfPointsToProcess(numPoints);
83 
84  OutputImageRegionType computationRegion;
85  computationRegion.SetIndex(minPos);
86 
87  MaskSizeType tmpSize;
88  for (unsigned int i = 0;i < m_ComputationMask->GetImageDimension();++i)
89  tmpSize[i] = maxPos[i] - minPos[i] + 1;
90 
91  computationRegion.SetSize(tmpSize);
92  this->SetComputationRegion(computationRegion);
93 }
94 
95 } // end namespace anima