Medical Imaging Interaction Toolkit  2018.4.99-a3d2e8fb
Medical Imaging Interaction Toolkit
XRaxSimulationFromCT.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 
13 #include "mitkProperties.h"
14 
15 #include "mitkCommandLineParser.h"
16 #include "mitkIOUtil.h"
17 
18 #include "itkImageRegionIterator.h"
19 // MITK
20 #include <mitkITKImageImport.h>
21 #include <mitkImageCast.h>
22 #include <mitkImageAccessByItk.h>
23 
24 struct Params {
25  bool invert;
26  float zeroValue;
27 };
28 template<typename TPixel, unsigned int VImageDimension>
29 void
30 CreateXRay(itk::Image<TPixel, VImageDimension>* itkImage, mitk::Image::Pointer mask1, std::string output, Params param)
31 {
32  typedef itk::Image<TPixel, VImageDimension> ImageType;
33  typedef itk::Image<unsigned char, VImageDimension> MaskType;
34  typedef itk::Image<double, 2 > NewImageType;
35 
36  typename MaskType::Pointer itkMask = MaskType::New();
37  mitk::CastToItkImage(mask1, itkMask);
38 
39  NewImageType::SpacingType newSpacing;
40  typename ImageType::SpacingType spacing;
41  spacing[0] = 0;
42  spacing[1] = 0;
43  spacing[2] = 0;
44  spacing = itkImage->GetSpacing();
45 
46  NewImageType::RegionType region1,region2,region3,region1m,region2m,region3m;
47  NewImageType::IndexType start;
48  start[0] = 0; start[1] = 0;
49 
50  NewImageType::SizeType size1, size2, size3;
51  size1[0] = mask1->GetDimensions()[0];
52  size2[0] = mask1->GetDimensions()[0];
53  size3[0] = mask1->GetDimensions()[1];
54  size1[1] = mask1->GetDimensions()[1];
55  size2[1] = mask1->GetDimensions()[2];
56  size3[1] = mask1->GetDimensions()[2];
57 
58  region1.SetSize(size1);
59  region1m.SetSize(size1);
60  region2.SetSize(size2);
61  region2m.SetSize(size2);
62  region3.SetSize(size3);
63  region3m.SetSize(size3);
64  region1.SetIndex(start);
65  region1m.SetIndex(start);
66  region2.SetIndex(start);
67  region2m.SetIndex(start);
68  region3.SetIndex(start);
69  region3m.SetIndex(start);
70 
71  NewImageType::Pointer image1 = NewImageType::New();
72  image1->SetRegions(region1);
73  image1->Allocate();
74  image1->FillBuffer(0);
75  newSpacing[0] = spacing[0]; newSpacing[1] = spacing[1];
76  image1->SetSpacing(newSpacing);
77  NewImageType::Pointer image2 = NewImageType::New();
78  image2->SetRegions(region2);
79  image2->Allocate();
80  image2->FillBuffer(0);
81  newSpacing[0] = spacing[0]; newSpacing[1] = spacing[2];
82  image2->SetSpacing(newSpacing);
83  NewImageType::Pointer image3 = NewImageType::New();
84  image3->SetRegions(region3);
85  image3->Allocate();
86  image3->FillBuffer(0);
87  newSpacing[0] = spacing[1]; newSpacing[1] = spacing[2];
88  image3->SetSpacing(newSpacing);
89  NewImageType::Pointer image1m = NewImageType::New();
90  image1m->SetRegions(region1m);
91  image1m->Allocate();
92  image1m->FillBuffer(0);
93  newSpacing[0] = spacing[0]; newSpacing[1] = spacing[1];
94  image1m->SetSpacing(newSpacing);
95  NewImageType::Pointer image2m = NewImageType::New();
96  image2m->SetRegions(region2m);
97  image2m->Allocate();
98  image2m->FillBuffer(0);
99  newSpacing[0] = spacing[0]; newSpacing[1] = spacing[2];
100  image2m->SetSpacing(newSpacing);
101  NewImageType::Pointer image3m = NewImageType::New();
102  image3m->SetRegions(region3m);
103  image3m->Allocate();
104  image3m->FillBuffer(0);
105  newSpacing[0] = spacing[1]; newSpacing[1] = spacing[2];
106  image3m->SetSpacing(newSpacing);
107 
108  for (unsigned int x = 0; x < mask1->GetDimensions()[0]; ++x)
109  {
110  for (unsigned int y = 0; y < mask1->GetDimensions()[1]; ++y)
111  {
112  for (unsigned int z = 0; z < mask1->GetDimensions()[2]; ++z)
113  {
114  NewImageType::IndexType newIndex;
115  typename ImageType::IndexType index;
116  index[0] = x; index[1] = y; index[2] = z;
117  double pixel = itkImage->GetPixel(index)+1024;
118  pixel = pixel / 1000.0;
119  pixel = (pixel < 0)? 0 : pixel;
120  newIndex[0] = x; newIndex[1] = y;
121  image1->SetPixel(newIndex, image1->GetPixel(newIndex) + pixel);
122  newIndex[0] = x; newIndex[1] = z;
123  image2->SetPixel(newIndex, image2->GetPixel(newIndex) + pixel);
124  newIndex[0] = y; newIndex[1] = z;
125  image3->SetPixel(newIndex, image3->GetPixel(newIndex) + pixel);
126  if (itkMask->GetPixel(index) > 0 && !param.invert)
127  {
128  pixel = param.zeroValue + 1024;
129  pixel = pixel / 1000.0;
130  }
131  if (itkMask->GetPixel(index) < 1 && param.invert)
132  {
133  pixel = param.zeroValue + 1024;
134  pixel = pixel / 1000.0;
135  }
136  pixel = (pixel < 0)? 0 : pixel;
137  newIndex[0] = x; newIndex[1] = y;
138  image1m->SetPixel(newIndex, image1m->GetPixel(newIndex) + pixel);
139  newIndex[0] = x; newIndex[1] = z;
140  image2m->SetPixel(newIndex, image2m->GetPixel(newIndex) + pixel);
141  newIndex[0] = y; newIndex[1] = z;
142  image3m->SetPixel(newIndex, image3m->GetPixel(newIndex) + pixel);
143  }
144  }
145  }
146 
147 
149  mitk::IOUtil::Save(img, output + "1.nrrd");
150  img = mitk::ImportItkImage(image2);
151  mitk::IOUtil::Save(img, output + "2.nrrd");
152  img = mitk::ImportItkImage(image3);
153  mitk::IOUtil::Save(img, output + "3.nrrd");
154  img = mitk::ImportItkImage(image1m);
155  mitk::IOUtil::Save(img, output + "1m.nrrd");
156  img = mitk::ImportItkImage(image2m);
157  mitk::IOUtil::Save(img, output + "2m.nrrd");
158  img = mitk::ImportItkImage(image3m);
159  mitk::IOUtil::Save(img, output + "3m.nrrd");
160 }
161 
162 int main(int argc, char* argv[])
163 {
164  mitkCommandLineParser parser;
165 
166  parser.setTitle("Dicom Loader");
167  parser.setCategory("Preprocessing Tools");
168  parser.setDescription("");
169  parser.setContributor("German Cancer Research Center (DKFZ)");
170 
171  parser.setArgumentPrefix("-","-");
172  // Add command line argument names
173  parser.addArgument("help", "h",mitkCommandLineParser::Bool, "Help:", "Show this help text");
174  parser.addArgument("input", "i", mitkCommandLineParser::Directory, "Input image:", "Input folder", us::Any(), false, false, false, mitkCommandLineParser::Input);
175  parser.addArgument("mask", "m", mitkCommandLineParser::Directory, "Input mask:", "Input folder", us::Any(), false, false, false, mitkCommandLineParser::Input);
176  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output file:", "Output file", us::Any(), false, false, false, mitkCommandLineParser::Output);
177  parser.addArgument("invert", "invert", mitkCommandLineParser::Bool, "Input mask:", "Input folder", us::Any());
178  parser.addArgument("zero_value", "zero", mitkCommandLineParser::Float, "Output file:", "Output file", us::Any());
179 
180  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
181 
182  if (parsedArgs.size()==0)
183  return EXIT_FAILURE;
184 
185  // Show a help message
186  if ( parsedArgs.count("help") || parsedArgs.count("h"))
187  {
188  std::cout << parser.helpText();
189  return EXIT_SUCCESS;
190  }
191 
192  std::string inputImage = us::any_cast<std::string>(parsedArgs["input"]);
193  MITK_INFO << inputImage;
194  std::string inputMask = us::any_cast<std::string>(parsedArgs["mask"]);
195  MITK_INFO << inputMask;
196 
197  Params param;
198  param.invert = false;
199  param.zeroValue = 0;
200  if (parsedArgs.count("invert"))
201  {
202  param.invert = true;
203  }
204  if (parsedArgs.count("zero_value"))
205  {
206  param.zeroValue = us::any_cast<float>(parsedArgs["zero_value"]);
207  }
208 
209 
210  mitk::Image::Pointer image = mitk::IOUtil::Load<mitk::Image>(inputImage);
211  mitk::Image::Pointer mask = mitk::IOUtil::Load<mitk::Image>(inputMask);
212 
213  AccessByItk_3(image, CreateXRay, mask, parsedArgs["output"].ToString(),param);
214 
215  //const mitk::Image::Pointer image = *imageIter;
216  //mitk::IOUtil::SaveImage(image,outFileName);
217 
218 
219 
220  return EXIT_SUCCESS;
221 }
#define AccessByItk_3(mitkImage, itkImageTypeFunction, arg1, arg2, arg3)
int main(int argc, char *argv[])
#define MITK_INFO
Definition: mitkLogMacros.h:18
itk::Image< unsigned char, 3 > ImageType
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
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)
Image::Pointer ImportItkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, const BaseGeometry *geometry=nullptr, bool update=true)
Imports an itk::Image (with a specific type) as an mitk::Image.Instantiates instance of ITKImageImpor...
Definition: usAny.h:163
std::string helpText() const
void setCategory(std::string category)
mitk::Image::Pointer image
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
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
void setTitle(std::string title)
void CreateXRay(itk::Image< TPixel, VImageDimension > *itkImage, mitk::Image::Pointer mask1, std::string output, Params param)
void setDescription(std::string description)