ANIMA  4.0
animaFillHoleImage.cxx
Go to the documentation of this file.
1 #include <tclap/CmdLine.h>
2 #include <iostream>
3 #include <string>
4 
5 #include <itkConnectedComponentImageFilter.h>
7 
8 int main(int argc, char **argv)
9 {
10  TCLAP::CmdLine cmd("INRIA / IRISA - VisAGeS/Empenn Team", ' ',ANIMA_VERSION);
11 
12  TCLAP::ValueArg<std::string> inArg("i","input","Input image",true,"","input image",cmd);
13  TCLAP::ValueArg<std::string> outArg("o","output","Output image",true,"","output image",cmd);
14 
15  TCLAP::ValueArg<unsigned int> numThreadsArg("T","threads","Number of execution threads (default: all cores)",false,itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads(),"number of threads",cmd);
16 
17  try
18  {
19  cmd.parse(argc,argv);
20  }
21  catch (TCLAP::ArgException& e)
22  {
23  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
24  return EXIT_FAILURE;
25  }
26 
27  typedef itk::Image <unsigned short,3> ImageTypeUS;
28  typedef itk::Image <unsigned int,3> ImageTypeInt;
29  typedef itk::ImageRegionIterator <ImageTypeUS> IteratorTypeUS;
30  typedef itk::ImageRegionIterator <ImageTypeInt> IteratorTypeInt;
31  typedef itk::ConnectedComponentImageFilter <ImageTypeUS,ImageTypeInt> ConnectedComponentType;
32 
33  ImageTypeUS::Pointer inputImage = anima::readImage <ImageTypeUS> (inArg.getValue());
34 
35  ImageTypeUS::Pointer tmpImageUS = ImageTypeUS::New();
36  tmpImageUS->SetRegions(inputImage->GetLargestPossibleRegion());
37  tmpImageUS->CopyInformation(inputImage);
38  tmpImageUS->Allocate();
39  tmpImageUS->FillBuffer(0);
40 
41  ImageTypeUS::Pointer outputImage = ImageTypeUS::New();
42  outputImage->SetRegions(inputImage->GetLargestPossibleRegion());
43  outputImage->CopyInformation(inputImage);
44  outputImage->Allocate();
45  outputImage->FillBuffer(0);
46 
47  IteratorTypeUS inputImageIt(inputImage, inputImage->GetLargestPossibleRegion());
48  IteratorTypeUS tmpImageUSIt(tmpImageUS, tmpImageUS->GetLargestPossibleRegion());
49  IteratorTypeUS outputImageIt(outputImage, outputImage->GetLargestPossibleRegion());
50 
51  while(!inputImageIt.IsAtEnd())
52  {
53  tmpImageUSIt.Set(1-inputImageIt.Get());
54  outputImageIt.Set(inputImageIt.Get());
55 
56  ++inputImageIt;
57  ++tmpImageUSIt;
58  ++outputImageIt;
59  }
60 
61  bool connectivity = false;
62  ConnectedComponentType::Pointer ccFilter = ConnectedComponentType::New();
63  ccFilter->SetInput( tmpImageUS );
64  ccFilter->SetFullyConnected( connectivity );
65  ccFilter->SetNumberOfWorkUnits( numThreadsArg.getValue() );
66  ccFilter->Update();
67 
68  IteratorTypeInt tmpImageIntIt (ccFilter->GetOutput(), ccFilter->GetOutput()->GetLargestPossibleRegion() );
69 
70  outputImageIt.GoToBegin();
71 
72  while(!tmpImageIntIt.IsAtEnd())
73  {
74  if(tmpImageIntIt.Get() > 1)
75  outputImageIt.Set(1);
76 
77  ++tmpImageIntIt;
78  ++outputImageIt;
79  }
80 
81  anima::writeImage <ImageTypeUS> (outArg.getValue(),outputImage);
82 
83  return EXIT_SUCCESS;
84 }
int main(int argc, char **argv)