Medical Imaging Interaction Toolkit  2018.4.99-36d69b77
Medical Imaging Interaction Toolkit
CLPlanarFigureToNrrd.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 
20 #include "mitkCommandLineParser.h"
22 #include <mitkImageAccessByItk.h>
23 #include <mitkImageCast.h>
24 #include <itkImageRegionIteratorWithIndex.h>
25 #include <mitkImageAccessByItk.h>
26 
27 
28 typedef itk::Image< double, 3 > FloatImageType;
29 typedef itk::Image< unsigned char, 3 > MaskImageType;
30 
31 struct MaskParameter
32 {
34  unsigned int axis;
35  unsigned int slice;
36 };
37 
38 template < typename TPixel, unsigned int VImageDimension >
39 void CreateNewMask(const itk::Image< TPixel, VImageDimension > *image, MaskParameter param, mitk::Image::Pointer &output)
40 {
41  int transform[3][2];
42  transform[0][0] = 1; transform[0][1] = 2;
43  transform[1][0] = 0; transform[1][1] = 2;
44  transform[2][0] = 0; transform[2][1] = 1;
45 
46  typedef itk::Image<unsigned short, VImageDimension> MaskType;
47  typedef itk::Image<unsigned short, 2> Mask2DType;
48  typename Mask2DType::Pointer mask = Mask2DType::New();
49  mitk::CastToItkImage(param.mask, mask);
50 
51 
52  typename MaskType::Pointer mask3D = MaskType::New();
53  mask3D->SetRegions(image->GetLargestPossibleRegion());
54  mask3D->SetSpacing(image->GetSpacing());
55  mask3D->SetOrigin(image->GetOrigin());
56  mask3D->Allocate();
57 
58  itk::ImageRegionIteratorWithIndex<MaskType> iter(mask3D, mask3D->GetLargestPossibleRegion());
59  while (!iter.IsAtEnd())
60  {
61  auto index = iter.GetIndex();
62  iter.Set(0);
63  if (index[param.axis] == param.slice)
64  {
65  Mask2DType::IndexType index2D;
66  index2D[0] = index[transform[param.axis][0]];
67  index2D[1] = index[transform[param.axis][1]];
68  auto pixel = mask->GetPixel(index2D);
69  iter.Set(pixel);
70  }
71  ++iter;
72  }
73 
74  mitk::CastToMitkImage(mask3D, output);
75 
76 }
77 
78 int main(int argc, char* argv[])
79 {
80  mitkCommandLineParser parser;
81  parser.setArgumentPrefix("--", "-");
82  // required params
83  parser.addArgument("planar", "p", mitkCommandLineParser::Directory, "Input Polydata", "Path to the input VTK polydata", us::Any(), false, false, false, mitkCommandLineParser::Input);
84  parser.addArgument("image", "i", mitkCommandLineParser::Directory, "Input Image", "Image which defines the dimensions of the Segmentation", us::Any(), false, false, false, mitkCommandLineParser::Output);
85  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output file", "Output file. ", us::Any(), false, false, false, mitkCommandLineParser::Input);
86  // Miniapp Infos
87  parser.setCategory("Classification Tools");
88  parser.setTitle("Planar Data to Nrrd Segmentation");
89  parser.setDescription("Creates a Nrrd segmentation based on a 2d-vtk polydata.");
90  parser.setContributor("German Cancer Research Center (DKFZ)");
91 
92  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
93 
94  if (parsedArgs.size()==0)
95  {
96  return EXIT_FAILURE;
97  }
98  if ( parsedArgs.count("help") || parsedArgs.count("h"))
99  {
100  return EXIT_SUCCESS;
101  }
102 
103  try
104  {
105  mitk::BaseData::Pointer data = mitk::IOUtil::Load(parsedArgs["planar"].ToString())[0];
106  mitk::PlanarFigure::Pointer planar = dynamic_cast<mitk::PlanarFigure*>(data.GetPointer());
107 
108  mitk::Image::Pointer image = mitk::IOUtil::Load<mitk::Image>(parsedArgs["image"].ToString());
109 
111  pfMaskGen->SetPlanarFigure(planar);
112  pfMaskGen->SetTimeStep(0);
113  pfMaskGen->SetInputImage(image.GetPointer());
114 
115  mitk::Image::Pointer mask = pfMaskGen->GetMask();
116  unsigned int axis = pfMaskGen->GetPlanarFigureAxis();
117  unsigned int slice = pfMaskGen->GetPlanarFigureSlice();
118 
119  //itk::Image<unsigned short, 3>::IndexType index;
120  mitk::Image::Pointer fullMask;
121  MaskParameter param;
122  param.slice = slice;
123  param.axis = axis;
124  param.mask = mask;
125  AccessByItk_2(image, CreateNewMask, param, fullMask);
126 
127  std::string saveAs = parsedArgs["output"].ToString();
128  MITK_INFO << "Save as: " << saveAs;
129  mitk::IOUtil::Save(pfMaskGen->GetMask(), saveAs);
130  mitk::IOUtil::Save(fullMask, saveAs);
131 
132  return 0;
133  }
134  catch (...)
135  {
136  return EXIT_FAILURE;
137  }
138 }
139 
140 #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)
itk::Image< double, 3 > FloatImageType
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
void CreateNewMask(const itk::Image< TPixel, VImageDimension > *image, MaskParameter param, mitk::Image::Pointer &output)
int main(int argc, char *argv[])
Definition: usAny.h:163
void setCategory(std::string category)
mitk::Image::Pointer image
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
Base-class for geometric planar (2D) figures, such as lines, circles, rectangles, polygons...
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
#define AccessByItk_2(mitkImage, itkImageTypeFunction, arg1, arg2)
void setTitle(std::string title)
itk::Image< unsigned char, 3 > MaskImageType
void setDescription(std::string description)
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