ANIMA  4.0
animaCheckStructureNeighborFilter.hxx
Go to the documentation of this file.
1 #pragma once
2 
4 
5 namespace anima
6 {
7 
8 template<typename TInput, typename TMask, typename TOutput>
10 {
11  this->SetNthInput(0, const_cast<TInput *>(image));
12 }
13 
14 template<typename TInput, typename TMask, typename TOutput>
16 {
17  this->SetNthInput(1, const_cast<TMask *>(image));
18 }
19 
20 template<typename TInput, typename TMask, typename TOutput>
22 {
23  return static_cast< const TInput * >
24  ( this->itk::ProcessObject::GetInput(0) );
25 }
26 template<typename TInput, typename TMask, typename TOutput>
28 {
29  return static_cast< const TMask * >
30  ( this->itk::ProcessObject::GetInput(1) );
31 }
32 
33 template<typename TInput, typename TMask, typename TOutput>
34 void
37 {
38  if( m_OutputFilename != "" )
39  {
40  std::cout << "Writing lesions output image to: " << m_OutputFilename << std::endl;
41  anima::writeImage<TOutput>(m_OutputFilename, this->GetOutput());
42  }
43 }
44 
45 template<typename TInput, typename TMask, typename TOutput >
46 void
49 {
50  bool fullyConnected = false;
51 
52  // Create labeled map
53  typename ConnectedComponentFilterType::Pointer ConnectedComponentFilter = ConnectedComponentFilterType::New();
54  ConnectedComponentFilter->SetInput( this->GetInputClassification() );
55  ConnectedComponentFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
56  ConnectedComponentFilter->SetCoordinateTolerance(m_Tol);
57  ConnectedComponentFilter->SetDirectionTolerance(m_Tol);
58  ConnectedComponentFilter->SetFullyConnected( fullyConnected );
59 
60  unsigned int radius = 1;
61 
62  StructuringElementType structuringElement;
63  structuringElement.SetRadius(radius);
64  structuringElement.CreateStructuringElement();
65 
66  DilateFilterType::Pointer dilateFilter = DilateFilterType::New();
67  dilateFilter->SetInput(ConnectedComponentFilter->GetOutput());
68  dilateFilter->SetKernel(structuringElement);
69  dilateFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
70  dilateFilter->SetCoordinateTolerance(m_Tol);
71  dilateFilter->SetDirectionTolerance(m_Tol);
72  dilateFilter->Update();
73 
74  // Find contours
75  typename LabelContourFilterType::Pointer filterLabelContour = LabelContourFilterType::New();
76  filterLabelContour->SetInput(dilateFilter->GetOutput());
77  filterLabelContour->SetFullyConnected( fullyConnected );
78  filterLabelContour->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
79  filterLabelContour->SetCoordinateTolerance(m_Tol);
80  filterLabelContour->SetDirectionTolerance(m_Tol);
81  filterLabelContour->SetBackgroundValue(0);
82  filterLabelContour->Update();
83 
84  unsigned int objectCount = ConnectedComponentFilter->GetObjectCount();
85 
86  std::vector<unsigned int> nbVoxelContour(objectCount+1,0);
87  std::vector<unsigned int> nbVoxelIntersec(objectCount+1,0);
88  std::vector<bool> enoughContour(objectCount+1,true);
89 
90  ImageIteratorTypeInt labelContourIt(filterLabelContour->GetOutput(), filterLabelContour->GetOutput()->GetLargestPossibleRegion());
91  MaskConstIteratorType mapIt(this->GetInputMap(), this->GetInputMap()->GetLargestPossibleRegion());
92  while(!labelContourIt.IsAtEnd())
93  {
94  if(labelContourIt.Get()!=0)
95  {
96  nbVoxelContour[labelContourIt.Get()]++;
97  if(mapIt.Get()!=0)
98  {
99  nbVoxelIntersec[labelContourIt.Get()]++;
100  }
101  }
102  ++labelContourIt;
103  ++mapIt;
104  }
105 
106  // calculer les ratios
107  for(unsigned int i = 1; i < objectCount+1; i++)
108  {
109  if(static_cast<double>(nbVoxelIntersec[i])/static_cast<double>(nbVoxelContour[i]) < m_Ratio)
110  {
111  enoughContour[i] = false;
112  }
113  }
114 
115 
116  // Calculer output
117  OutputImagePointer output = this->GetOutput();
118  output->SetRegions( this->GetInputClassification()->GetLargestPossibleRegion() );
119  output->CopyInformation( this->GetInputClassification() );
120  output->Allocate();
121  output->FillBuffer(0);
122 
123  ImageIteratorTypeInt classifIt(ConnectedComponentFilter->GetOutput(), ConnectedComponentFilter->GetOutput()->GetLargestPossibleRegion());
124  OutputIteratorType outputIt(output, output->GetLargestPossibleRegion());
125  while(!classifIt.IsAtEnd())
126  {
127  outputIt.Set(0);
128  if(classifIt.Get()!=0)
129  {
130  if(enoughContour[classifIt.Get()]==true)
131  {
132  outputIt.Set(1);
133  }
134  }
135  ++outputIt;
136  ++classifIt;
137  }
138 }
139 
140 } //end of namespace anima
itk::ImageRegionIterator< ImageTypeInt > ImageIteratorTypeInt
itk::ImageRegionIterator< OutputImageType > OutputIteratorType
itk::ImageRegionConstIterator< MaskImageType > MaskConstIteratorType
itk::BinaryBallStructuringElement< PixelTypeInt, 3 > StructuringElementType