1 #include <tclap/CmdLine.h> 3 #include <itkScalarImageKmeansImageFilter.h> 5 #include <itkImageRegionIterator.h> 7 int main(
int argc,
char **argv)
9 TCLAP::CmdLine cmd(
"INRIA / IRISA - VisAGeS/Empenn Team",
' ',ANIMA_VERSION);
11 TCLAP::ValueArg<std::string> inArg(
"i",
"inputfile",
"Input image",
true,
"",
"input image",cmd);
12 TCLAP::ValueArg<std::string> outArg(
"o",
"outputfile",
"output image",
true,
"",
"output image",cmd);
13 TCLAP::ValueArg<unsigned int> numClassArg(
"c",
"numclass",
"Number of classes",
false,4,
"number of classes",cmd);
14 TCLAP::ValueArg<int> keptClassArg(
"k",
"class",
"Class to keep",
false,-1,
"class to be kept",cmd);
15 TCLAP::ValueArg<unsigned int> nbpArg(
"n",
"ncores",
"Number of cores (default: all cores)",
false,itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads(),
"number of cores",cmd);
21 catch (TCLAP::ArgException& e)
23 std::cerr <<
"Error: " << e.error() <<
"for argument " << e.argId() << std::endl;
27 std::string refName, resName;
28 refName = inArg.getValue();
29 resName = outArg.getValue();
31 typedef itk::Image <double,3> doubleImageType;
32 typedef itk::ScalarImageKmeansImageFilter <doubleImageType> MainFilterType;
34 MainFilterType::Pointer kmeansFilter = MainFilterType::New();
35 kmeansFilter->SetInput(anima::readImage <doubleImageType> (refName));
36 kmeansFilter->SetUseNonContiguousLabels(
false);
38 for (
unsigned int i = 0;i < numClassArg.getValue();++i)
39 kmeansFilter->AddClassWithInitialMean(i * 100);
41 kmeansFilter->SetNumberOfWorkUnits(nbpArg.getValue());
42 kmeansFilter->Update();
44 MainFilterType::ParametersType estimatedMeans = kmeansFilter->GetFinalMeans();
45 typedef MainFilterType::OutputImageType OutputImageType;
47 if (keptClassArg.getValue() >= 0)
49 const unsigned int numberOfClasses = estimatedMeans.Size();
50 unsigned int numLabel = 0;
52 for(
unsigned int i = 0 ; i < numberOfClasses ; ++i)
54 unsigned int numInf = 0;
56 for (
unsigned int j = 0;j < numberOfClasses;++j)
58 if (estimatedMeans[j] < estimatedMeans[i])
62 if (numInf == keptClassArg.getValue())
69 typedef itk::ImageRegionIterator <OutputImageType> IteratorType;
70 IteratorType outIterator(kmeansFilter->GetOutput(),kmeansFilter->GetOutput()->GetLargestPossibleRegion());
72 while(!outIterator.IsAtEnd())
74 outIterator.Set((outIterator.Get() == numLabel));
79 anima::writeImage <OutputImageType> (resName,kmeansFilter->GetOutput());
int main(int argc, char **argv)