8 template<
typename TInput,
typename TMask,
typename TOutput>
11 this->SetNthInput(0, const_cast<TInput*>(image));
14 template<
typename TInput,
typename TMask,
typename TOutput>
17 this->SetNthInput(1, const_cast<TMask*>(mask));
21 template<
typename TInput,
typename TMask,
typename TOutput>
24 return static_cast< const TInput *
> 25 ( this->itk::ProcessObject::GetInput(0) );
27 template<
typename TInput,
typename TMask,
typename TOutput>
30 return static_cast< const TMask *
> 31 ( this->itk::ProcessObject::GetInput(1) );
35 template<
typename TInput,
typename TMask,
typename TOutput>
38 itk::DataObject::Pointer output;
43 output = ( TOutput::New() ).GetPointer();
46 output = ( TOutput::New() ).GetPointer();
49 std::cerr <<
"No output " << idx << std::endl;
53 return output.GetPointer();
56 template<
typename TInput,
typename TMask,
typename TOutput>
59 return dynamic_cast< TOutput*
>( this->itk::ProcessObject::GetOutput(0) );
62 template<
typename TInput,
typename TMask,
typename TOutput>
65 return dynamic_cast< TOutput*
>( this->itk::ProcessObject::GetOutput(1) );
68 template<
typename TInput,
typename TMask,
typename TOutput>
73 if( m_OutputNonTouchingBorderFilename !=
"" )
75 std::cout <<
"Writing output non touching border image to: " << m_OutputNonTouchingBorderFilename << std::endl;
76 anima::writeImage<TOutput>(m_OutputNonTouchingBorderFilename, this->GetOutputNonTouchingBorder());
79 if( m_OutputTouchingBorderFilename !=
"" )
81 std::cout <<
"Writing output touching border image to: " << m_OutputTouchingBorderFilename << std::endl;
82 anima::writeImage<TOutput>(m_OutputTouchingBorderFilename, this->GetOutputTouchingBorder());
86 template<
typename TInput,
typename TMask,
typename TOutput>
92 InputImageSeg->SetRegions(this->GetInputImageSeg()->GetLargestPossibleRegion());
93 InputImageSeg->CopyInformation(this->GetInputImageSeg());
94 InputImageSeg->Allocate();
95 InputImageSeg->FillBuffer(0);
98 InputIteratorType InputImageSegIt(InputImageSeg, InputImageSeg->GetLargestPossibleRegion() );
99 InputConstIteratorType maskSegIt(this->GetInputImageSeg(), this->GetInputImageSeg()->GetLargestPossibleRegion() );
101 while(!maskSegIt.IsAtEnd())
103 InputImageSegIt.Set(maskSegIt.Get());
110 typename ConnectedComponentImageFilterType::Pointer connectedComponentImageFilter = ConnectedComponentImageFilterType::New();
111 typename LabelImageToLabelMapFilterType::Pointer labelImageToLabelMapFilter = LabelImageToLabelMapFilterType::New();
112 typename LabelImageToLabelMapFilterType2::Pointer labelImageToLabelMapFilter2 = LabelImageToLabelMapFilterType2::New();
113 typename LabelMapToLabelImageFilterType::Pointer labelMapToLabelImageFilter = LabelMapToLabelImageFilterType::New();
114 typename TOutputMap::Pointer labelMap = ITK_NULLPTR;
115 int originalNumberOfObject = 0;
116 bool connectivity =
false;
118 if( !m_LabeledImage )
121 connectedComponentImageFilter->SetInput( InputImageSeg );
122 connectedComponentImageFilter->SetFullyConnected( connectivity );
123 connectedComponentImageFilter->SetNumberOfWorkUnits( this->GetNumberOfWorkUnits() );
124 connectedComponentImageFilter->Update();
126 labelImageToLabelMapFilter->SetInput( connectedComponentImageFilter->GetOutput() );
127 labelImageToLabelMapFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
128 labelImageToLabelMapFilter->Update();
129 originalNumberOfObject = labelImageToLabelMapFilter->GetOutput()->GetNumberOfLabelObjects() ;
131 labelMap = labelImageToLabelMapFilter->GetOutput();
132 labelMap->DisconnectPipeline();
136 labelImageToLabelMapFilter2->SetInput( InputImageSeg );
137 labelImageToLabelMapFilter2->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
138 labelImageToLabelMapFilter2->Update();
139 originalNumberOfObject = labelImageToLabelMapFilter2->GetOutput()->GetNumberOfLabelObjects() ;
141 labelMap = labelImageToLabelMapFilter2->GetOutput();
142 labelMap->DisconnectPipeline();
146 labelMapToLabelImageFilter->SetInput( labelMap );
147 labelMapToLabelImageFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
148 labelMapToLabelImageFilter->Update();
152 ImageIteratorTypeInt segLabelIt (labelMapToLabelImageFilter->GetOutput(), labelMapToLabelImageFilter->GetOutput()->GetLargestPossibleRegion() );
154 std::vector<int> labelsToRemove;
160 typename ConnectedComponentImageFilterType2::Pointer connectedComponentImageFilter2 = ConnectedComponentImageFilterType2::New();
161 connectedComponentImageFilter2->SetInput( this->GetMask() );
162 connectedComponentImageFilter2->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
165 itk::LabelContourImageFilter<ImageTypeInt, ImageTypeInt>::Pointer labelContourImageFilter = itk::LabelContourImageFilter<ImageTypeInt, ImageTypeInt>::New();
166 labelContourImageFilter->SetInput( connectedComponentImageFilter2->GetOutput() );
167 labelContourImageFilter->SetFullyConnected(
true);
168 labelContourImageFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
169 labelContourImageFilter->SetBackgroundValue(0);
170 labelContourImageFilter->Update();
174 ImageIteratorTypeInt maskContourIt (labelContourImageFilter->GetOutput(), labelContourImageFilter->GetOutput()->GetLargestPossibleRegion() );
176 std::vector<int>::iterator it;
177 while(!maskContourIt.IsAtEnd())
179 if( maskContourIt.Get()!=0 && segLabelIt.Get()!=0 )
181 it = find (labelsToRemove.begin(), labelsToRemove.end(), segLabelIt.Get());
182 if (it == labelsToRemove.end())
184 labelsToRemove.push_back( segLabelIt.Get() );
185 m_labelsToRemove.push_back( segLabelIt.Get() );
197 std::vector<int>::iterator it;
198 while(!maskContourIt.IsAtEnd())
200 if( maskContourIt.Get()!=0 && segLabelIt.Get()!=0)
202 it = find (labelsToRemove.begin(), labelsToRemove.end(), segLabelIt.Get());
203 if (it == labelsToRemove.end())
205 labelsToRemove.push_back( segLabelIt.Get() );
206 m_labelsToRemove.push_back( segLabelIt.Get() );
216 for(
unsigned int i = 0; i < labelsToRemove.size(); ++i)
218 labelMap->RemoveLabel(labelsToRemove[i]);
222 typename LabelMapToLabelImageFilterType::Pointer labelMapToLabelImageFilter2 = LabelMapToLabelImageFilterType::New();
223 labelMapToLabelImageFilter2->SetInput( labelMap );
224 labelMapToLabelImageFilter2->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
225 labelMapToLabelImageFilter2->Update();
228 ImageIteratorTypeInt segIt (labelMapToLabelImageFilter2->GetOutput(), labelMapToLabelImageFilter2->GetOutput()->GetLargestPossibleRegion() );
229 maskSegIt.GoToBegin();
232 output->SetRegions(this->GetInputImageSeg()->GetLargestPossibleRegion());
233 output->CopyInformation(this->GetInputImageSeg());
235 output->FillBuffer(0);
238 output2->SetRegions(this->GetInputImageSeg()->GetLargestPossibleRegion());
239 output2->CopyInformation(this->GetInputImageSeg());
241 output2->FillBuffer(0);
246 while(!maskIt.IsAtEnd())
252 if( (segIt.Get()==0) && (maskSegIt.Get()!=0))
265 std::cout <<
" -- Rule to erase objects touching mask border: " << std::endl;
266 std::cout <<
" * Initial number of objects: " << originalNumberOfObject << std::endl;
267 std::cout <<
" * Number of rejected objects: " << labelsToRemove.size() << std::endl;
268 std::cout <<
" * Number of objects after clean: " << labelMap->GetNumberOfLabelObjects() << std::endl;
269 std::cout << std::endl;
void SetInputImageSeg(const TInput *image)
itk::ImageRegionIterator< OutputImageType > OutputIteratorType
TOutput * GetOutputNonTouchingBorder()
TInput::ConstPointer GetInputImageSeg()
TMask::ConstPointer GetMask()
virtual void GenerateData() ITK_OVERRIDE
itk::ImageRegionConstIterator< InputImageType > InputConstIteratorType
itk::ImageRegionIterator< ImageTypeInt > ImageIteratorTypeInt
TOutput * GetOutputTouchingBorder()
itk::ImageRegionIterator< InputImageType > InputIteratorType
OutputImageType::Pointer OutputImagePointer
itk::DataObject::Pointer MakeOutput(unsigned int idx)
itk::ImageRegionConstIterator< MaskImageType > MaskConstIteratorType
InputImageType::Pointer InputImagePointer
void SetMask(const TMask *mask)