1 #include <tclap/CmdLine.h> 5 #include <itkConnectedComponentImageFilter.h> 8 int main(
int argc,
char **argv)
10 TCLAP::CmdLine cmd(
"INRIA / IRISA - VisAGeS/Empenn Team",
' ',ANIMA_VERSION);
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);
15 TCLAP::ValueArg<unsigned int> numThreadsArg(
"T",
"threads",
"Number of execution threads (default: all cores)",
false,itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads(),
"number of threads",cmd);
21 catch (TCLAP::ArgException& e)
23 std::cerr <<
"Error: " << e.error() <<
"for argument " << e.argId() << std::endl;
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;
33 ImageTypeUS::Pointer inputImage = anima::readImage <ImageTypeUS> (inArg.getValue());
35 ImageTypeUS::Pointer tmpImageUS = ImageTypeUS::New();
36 tmpImageUS->SetRegions(inputImage->GetLargestPossibleRegion());
37 tmpImageUS->CopyInformation(inputImage);
38 tmpImageUS->Allocate();
39 tmpImageUS->FillBuffer(0);
41 ImageTypeUS::Pointer outputImage = ImageTypeUS::New();
42 outputImage->SetRegions(inputImage->GetLargestPossibleRegion());
43 outputImage->CopyInformation(inputImage);
44 outputImage->Allocate();
45 outputImage->FillBuffer(0);
47 IteratorTypeUS inputImageIt(inputImage, inputImage->GetLargestPossibleRegion());
48 IteratorTypeUS tmpImageUSIt(tmpImageUS, tmpImageUS->GetLargestPossibleRegion());
49 IteratorTypeUS outputImageIt(outputImage, outputImage->GetLargestPossibleRegion());
51 while(!inputImageIt.IsAtEnd())
53 tmpImageUSIt.Set(1-inputImageIt.Get());
54 outputImageIt.Set(inputImageIt.Get());
61 bool connectivity =
false;
62 ConnectedComponentType::Pointer ccFilter = ConnectedComponentType::New();
63 ccFilter->SetInput( tmpImageUS );
64 ccFilter->SetFullyConnected( connectivity );
65 ccFilter->SetNumberOfWorkUnits( numThreadsArg.getValue() );
68 IteratorTypeInt tmpImageIntIt (ccFilter->GetOutput(), ccFilter->GetOutput()->GetLargestPossibleRegion() );
70 outputImageIt.GoToBegin();
72 while(!tmpImageIntIt.IsAtEnd())
74 if(tmpImageIntIt.Get() > 1)
81 anima::writeImage <ImageTypeUS> (outArg.getValue(),outputImage);
int main(int argc, char **argv)