ANIMA  4.0
animaImageSmoother.cxx
Go to the documentation of this file.
1 #include <tclap/CmdLine.h>
2 
4 #include <itkImage.h>
5 #include <itkVectorImage.h>
6 
8 
9 template <class ImageType>
10 void performSmoothing (std::string &inStr, std::string &outStr, double sigma, unsigned int nThreads)
11 {
13 
14  typename ImageType::Pointer currentImage = anima::readImage <ImageType> (inStr);
15  currentImage->DisconnectPipeline();
16 
17  double meanSpacing = currentImage->GetSpacing()[0];
18  for (unsigned int i = 1;i < ImageType::ImageDimension;++i)
19  meanSpacing += currentImage->GetSpacing()[i];
20  meanSpacing /= ImageType::ImageDimension;
21 
22  typename SmoothingFilterType::Pointer smoothFilter = SmoothingFilterType::New();
23 
24  smoothFilter->SetInput(currentImage);
25  smoothFilter->SetNumberOfWorkUnits(nThreads);
26  smoothFilter->SetSigma(sigma * meanSpacing);
27 
28  smoothFilter->Update();
29 
30  // Finally write the result
31  anima::writeImage <ImageType> (outStr,smoothFilter->GetOutput());
32 }
33 
34 int main(int argc, char **argv)
35 {
36  std::string descriptionMessage = "Performs Gaussian smoothing of an image\n";
37  descriptionMessage += "INRIA / IRISA - VisAGeS/Empenn Team";
38 
39  TCLAP::CmdLine cmd(descriptionMessage, ' ',ANIMA_VERSION);
40 
41  TCLAP::ValueArg<std::string> inArg("i","input","Input image",true,"","input image",cmd);
42  TCLAP::ValueArg<std::string> outArg("o","outputfile","output image",true,"","output image",cmd);
43 
44  TCLAP::ValueArg<double> sigmaArg("s","sigma","sigma value of Gaussian kernel",false,1.0,"sigma value",cmd);
45 
46  TCLAP::ValueArg<unsigned int> nbpArg("T","numberofthreads","Number of threads to run on (default : all cores)",false,itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads(),"number of threads",cmd);
47 
48  try
49  {
50  cmd.parse(argc,argv);
51  }
52  catch (TCLAP::ArgException& e)
53  {
54  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
55  return EXIT_FAILURE;
56  }
57 
58  typedef itk::Image <double,3> ImageType;
59  typedef itk::VectorImage <double,3> VectorImageType;
60 
61  itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(inArg.getValue().c_str(),
62  itk::ImageIOFactory::ReadMode);
63 
64  if(!imageIO)
65  {
66  std::cerr << "Itk could not find suitable IO factory for the input" << std::endl;
67  return EXIT_FAILURE;
68  }
69 
70  // Now that we found the appropriate ImageIO class, ask it to read the meta data from the image file.
71  imageIO->SetFileName(inArg.getValue());
72  imageIO->ReadImageInformation();
73 
74  bool vectorImage = (imageIO->GetNumberOfComponents() > 1);
75 
76  if (vectorImage)
77  performSmoothing<VectorImageType>(inArg.getValue(),outArg.getValue(),sigmaArg.getValue(),nbpArg.getValue());
78  else
79  performSmoothing<ImageType>(inArg.getValue(),outArg.getValue(),sigmaArg.getValue(),nbpArg.getValue());
80 
81  return EXIT_SUCCESS;
82 }
int main(int argc, char **argv)
void performSmoothing(std::string &inStr, std::string &outStr, double sigma, unsigned int nThreads)