Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
CLPointSetToSegmentation.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 #ifndef mitkCLPolyToNrrd_cpp
13 #define mitkCLPolyToNrrd_cpp
14 
15 #include "time.h"
16 #include <sstream>
17 
18 #include <mitkIOUtil.h>
19 #include <mitkPointSet.h>
20 #include <mitkImageCast.h>
21 #include <itkImageRegionIteratorWithIndex.h>
22 #include <limits>
23 
24 #include "mitkCommandLineParser.h"
25 
26 
27 
28 typedef itk::Image< double, 3 > FloatImageType;
29 typedef itk::Image< unsigned short, 3 > MaskImageType;
30 
31 int main(int argc, char* argv[])
32 {
33  mitkCommandLineParser parser;
34  parser.setArgumentPrefix("--", "-");
35  // required params
36  parser.addArgument("pointset", "p", mitkCommandLineParser::Directory, "Input Polydata", "Path to the input VTK polydata", us::Any(), false, false, false, mitkCommandLineParser::Input);
37  parser.addArgument("image", "i", mitkCommandLineParser::Directory, "Input Image", "Image which defines the dimensions of the Segmentation", us::Any(), false, false, false, mitkCommandLineParser::Output);
38  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output file", "Output files. Two files are create, a .nrrd image and a 3d-vtk.", us::Any(), false, false, false, mitkCommandLineParser::Input);
39  // Miniapp Infos
40  parser.setCategory("Classification Tools");
41  parser.setTitle("2D-Polydata to Nrrd Segmentation");
42  parser.setDescription("Creates a Nrrd segmentation based on a 2d-vtk polydata.");
43  parser.setContributor("German Cancer Research Center (DKFZ)");
44 
45  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
46 
47  if (parsedArgs.size()==0)
48  {
49  return EXIT_FAILURE;
50  }
51  if ( parsedArgs.count("help") || parsedArgs.count("h"))
52  {
53  return EXIT_SUCCESS;
54  }
55 
56  mitk::BaseData::Pointer data = mitk::IOUtil::Load(parsedArgs["pointset"].ToString())[0];
57  mitk::Image::Pointer image = mitk::IOUtil::Load<mitk::Image>(parsedArgs["image"].ToString());
58 
59  //MITK_INFO << data;
60  mitk::PointSet::Pointer points = dynamic_cast<mitk::PointSet*>(data.GetPointer());
61  MaskImageType::Pointer mask = MaskImageType::New();
62  mitk::CastToItkImage(image, mask);
63 
64  double minX, minY, minZ;
65  double maxX, maxY, maxZ;
66  minX = minY = minZ = std::numeric_limits<double>::max();
67  maxX = maxY = maxZ = std::numeric_limits<double>::lowest();
68 
69  for (auto iter = points->Begin(); iter != points->End(); ++iter)
70  {
71  minX = std::min<double>(minX, iter.Value().GetElement(0));
72  minY = std::min<double>(minY, iter.Value().GetElement(1));
73  minZ = std::min<double>(minZ, iter.Value().GetElement(2));
74  maxX = std::max<double>(maxX, iter.Value().GetElement(0));
75  maxY = std::max<double>(maxY, iter.Value().GetElement(1));
76  maxZ = std::max<double>(maxZ, iter.Value().GetElement(2));
77  }
78  MaskImageType::PointType point;
79  MaskImageType::IndexType iMin;
80  MaskImageType::IndexType iMax;
81  point[0] = minX;
82  point[1] = minY;
83  point[2] = minZ;
84  mask->TransformPhysicalPointToIndex(point, iMin);
85  point[0] = maxX;
86  point[1] = maxY;
87  point[2] = maxZ;
88  mask->TransformPhysicalPointToIndex(point, iMax);
89 
90  itk::ImageRegionIteratorWithIndex<MaskImageType> iter(mask, mask->GetLargestPossibleRegion());
91  while (!iter.IsAtEnd())
92  {
93  MaskImageType::IndexType index = iter.GetIndex();
94  if ((index[0] >= iMin[0]) && (index[1] >= iMin[1]) && (index[2] >= iMin[2]) &&
95  (index[0] <= iMax[0]) && (index[1] <= iMax[1]) && (index[2] <= iMax[2]))
96  {
97  iter.Set(1);
98  }
99  else
100  {
101  iter.Set(0);
102  }
103  ++iter;
104  }
105 
107  mitk::CastToMitkImage(mask, ergImage);
108 
109  std::string saveAs = parsedArgs["output"].ToString();
110  MITK_INFO << "Save as: " << saveAs;
111  mitk::IOUtil::Save(ergImage, saveAs);
112 
113  return 0;
114 }
115 
116 #endif
#define MITK_INFO
Definition: mitkLogMacros.h:18
void setContributor(std::string contributor)
void addArgument(const std::string &longarg, const std::string &shortarg, Type type, const std::string &argLabel, const std::string &argHelp=std::string(), const us::Any &defaultValue=us::Any(), bool optional=true, bool ignoreRest=false, bool deprecated=false, mitkCommandLineParser::Channel channel=mitkCommandLineParser::Channel::None)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
itk::Image< unsigned short, 3 > MaskImageType
Data structure which stores a set of points. Superclass of mitk::Mesh.
Definition: mitkPointSet.h:75
Definition: usAny.h:163
static T max(T x, T y)
Definition: svm.cpp:56
void setCategory(std::string category)
mitk::Image::Pointer image
static Pointer New()
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
Definition: mitkImageCast.h:74
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
mitk::Image::Pointer mask
itk::Image< double, 3 > FloatImageType
void setTitle(std::string title)
void setDescription(std::string description)
int main(int argc, char *argv[])
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage, const ReaderOptionsFunctorBase *optionsCallback=nullptr)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:489