Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
itkNeighborhoodFunctorImageFilter.cpp
Go to the documentation of this file.
1 #ifndef itkNeighborhoodFunctorImageFilter_cpp
2 #define itkNeighborhoodFunctorImageFilter_cpp
3 
5 #include "itkNeighborhoodAlgorithm.h"
6 #include <itkVectorImageToImageAdaptor.h>
7 
8 namespace itk
9 {
10 
11 template< typename TInputImage, typename TFeatureImageType , class FunctorType>
12 void
15 {
16  const TInputImage * input = this->GetInput(0);
17 
18  for(unsigned int i = 0 ; i < FunctorType::OutputCount; i ++)
19  {
21  output->SetRegions(input->GetLargestPossibleRegion());
22  output->SetSpacing(input->GetSpacing());
23  output->SetOrigin(input->GetOrigin());
24  output->SetDirection(input->GetDirection());
25  output->Allocate();
26  this->SetNthOutput( i, output.GetPointer() );
27  }
28 
29  if(m_MaskImage.IsNull())
30  {
31  m_MaskImage = MaskImageType::New();
32  m_MaskImage->SetRegions(input->GetLargestPossibleRegion());
33  m_MaskImage->Allocate();
34  m_MaskImage->FillBuffer(1);
35  }
36 }
37 
38 template< typename TInputImage, typename TFeatureImageType , class FunctorType>
39 void
42 throw ( InvalidRequestedRegionError )
43 {
44 
45  // call the superclass' implementation of this method. this should
46  // copy the output requested region to the input requested region
47  Superclass::GenerateInputRequestedRegion();
48 
49  // get pointers to the input and output
50  TInputImage * inputPtr = const_cast< TInputImage * >( this->GetInput() );
51 
52  if ( !inputPtr )
53  {
54  return;
55  }
56 
57  // get a copy of the input requested region (should equal the output
58  // requested region)
59  typename TInputImage::RegionType inputRequestedRegion;
60  inputRequestedRegion = inputPtr->GetRequestedRegion();
61 
62  // pad the input requested region by the operator radius
63  inputRequestedRegion.PadByRadius( m_Size );
64 
65  // crop the input requested region at the input's largest possible region
66  if ( inputRequestedRegion.Crop( inputPtr->GetLargestPossibleRegion() ) )
67  {
68  inputPtr->SetRequestedRegion(inputRequestedRegion);
69  return;
70  }
71  else
72  {
73  // Couldn't crop the region (requested region is outside the largest
74  // possible region). Throw an exception.
75 
76  // store what we tried to request (prior to trying to crop)
77  inputPtr->SetRequestedRegion(inputRequestedRegion);
78 
79  // build an exception
80  InvalidRequestedRegionError e(__FILE__, __LINE__);
81  e.SetLocation(ITK_LOCATION);
82  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
83  e.SetDataObject(inputPtr);
84  throw e;
85  }
86 }
87 
88 template< typename TInputImage, typename TFeatureImageType, class FunctorType >
89 void
91 ::ThreadedGenerateData(const OutputImageRegionType & outputRegionForThread,
92  ThreadIdType /*threadId*/)
93 {
94 
95 
96  typedef NeighborhoodAlgorithm::ImageBoundaryFacesCalculator< InputImageType > BFC;
97  typedef typename BFC::FaceListType FaceListType;
98  BFC faceCalculator;
99  FaceListType faceList;
100 
101  // Allocate output
102  const InputImageType *input = this->GetInput();
103 
104  // Break the input into a series of regions. The first region is free
105  // of boundary conditions, the rest with boundary conditions. Note,
106  // we pass in the input image and the OUTPUT requested region. We are
107  // only concerned with centering the neighborhood operator at the
108  // pixels that correspond to output pixels.
109  faceList = faceCalculator( input, outputRegionForThread, m_Size );
110 
111  typename FaceListType::iterator fit;
112  ImageRegionConstIterator< MaskImageType > mit;
113 
114  // Process non-boundary region and each of the boundary faces.
115  // These are N-d regions which border the edge of the buffer.
116  ConstNeighborhoodIterator< InputImageType > bit;
117 // for ( fit = faceList.begin(); fit != faceList.end(); ++fit )
118 // {
119  bit = ConstNeighborhoodIterator< InputImageType >(m_Size, input, outputRegionForThread);
120 
121  mit = ImageRegionConstIterator< MaskImageType >(m_MaskImage, outputRegionForThread);
122 
123  std::vector<ImageRegionIterator< FeatureImageType > > featureImageIterators;
124 
125  for(unsigned int i = 0; i < FunctorType::OutputCount; i++)
126  {
127  featureImageIterators.push_back(ImageRegionIterator< FeatureImageType >(this->GetOutput(i), outputRegionForThread));
128  featureImageIterators[i].GoToBegin();
129  }
130 
131  mit.GoToBegin();
132  bit.GoToBegin();
133 
134  while ( !bit.IsAtEnd() || !mit.IsAtEnd() )
135  {
136  if(mit.Value() != 0)
137  {
138  //bit.GetNeighborhood().Print(std::cout);
139  typename FunctorType::OutputVectorType features = ( m_Functor( bit ) );
140 
141  for(unsigned int i = 0 ; i < FunctorType::OutputCount; i++)
142  featureImageIterators[i].Set(features[i]);
143  }
144 
145  for(unsigned int i = 0 ; i < FunctorType::OutputCount; i++)
146  ++featureImageIterators[i];
147  ++bit;
148  ++mit;
149  }
150 // }
151 
152  std::cout << "Thread done!" << std::endl;
153 }
154 } // end namespace itk
155 
156 
157 #endif //itkNeighborhoodFunctorImageFilter_cpp
itk::SmartPointer< Self > Pointer
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, ThreadIdType threadId)
Superclass::OutputImageRegionType OutputImageRegionType
const char features[]
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.