Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
CLSkullMask.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 #include <fstream>
18 
19 #include <mitkIOUtil.h>
20 #include "mitkCommandLineParser.h"
21 
22 #include "itkImageRegionIterator.h"
23 // MITK
24 #include <mitkITKImageImport.h>
25 #include <mitkImageCast.h>
26 #include <mitkImageAccessByItk.h>
27 // ITK
28 #include <itkLabelStatisticsImageFilter.h>
29 #include <itkMinimumMaximumImageCalculator.h>
30 
31 typedef itk::Image< double, 3 > FloatImageType;
32 typedef itk::Image< unsigned char, 3 > MaskImageType;
33 
34 template<typename TPixel, unsigned int VImageDimension>
35 static void
36  DetectSkull(itk::Image<TPixel, VImageDimension>* itkImage, mitk::Image::Pointer im2, mitk::Image::Pointer mask1, std::string output)
37 {
38  typedef itk::Image<TPixel, VImageDimension> ImageType;
39  typedef itk::Image<unsigned char, VImageDimension> MaskType;
40 
41  typename ImageType::Pointer itkIm2 = ImageType::New();
42  typename MaskType::Pointer itkMask1 = MaskType::New();
43  mitk::CastToItkImage(im2, itkIm2);
44  mitk::CastToItkImage(mask1, itkMask1);
45 
46  itk::ImageRegionIterator<ImageType> iterI1(itkImage, itkImage->GetLargestPossibleRegion());
47  itk::ImageRegionIterator<ImageType> iterI2(itkIm2, itkImage->GetLargestPossibleRegion());
48  itk::ImageRegionIterator<MaskType> iter(itkMask1, itkImage->GetLargestPossibleRegion());
49  while (! iter.IsAtEnd())
50  {
51  unsigned char maskV = 0;
52  if (iterI1.Value() > 0.0001 && iterI2.Value() > 0.00001)
53  maskV = 1;
54  iter.Set(maskV);
55  ++iter;
56  ++iterI1;
57  ++iterI2;
58  }
59 
61  mitk::IOUtil::Save(img, output);
62 }
63 
64 int main(int argc, char* argv[])
65 {
66  typedef itk::Image<double, 3> ImageType;
67  typedef itk::Image<unsigned short, 3> MaskType;
68 
69 
70  mitkCommandLineParser parser;
71  parser.setArgumentPrefix("--", "-");
72  // required params
73  parser.addArgument("image", "i", mitkCommandLineParser::StringList, "Input Image", "Path to the input images. Mask covers area of all images", us::Any(), false);
74  parser.addArgument("mask", "m", mitkCommandLineParser::Image, "Input Mask", "The median of the area covered by this mask will be set to 1", us::Any(), false, false, false, mitkCommandLineParser::Input);
75 
76  // Miniapp Infos
77  parser.setCategory("Classification Tools");
78  parser.setTitle("MR Normalization Tool");
79  parser.setDescription("Normalizes a MR image. Sets the Median of the tissue covered by mask 0 to 0 and the median of the area covered by mask 1 to 1.");
80  parser.setContributor("German Cancer Research Center (DKFZ)");
81 
82  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
83 
84  if (parsedArgs.size()==0)
85  {
86  return EXIT_FAILURE;
87  }
88  if ( parsedArgs.count("help") || parsedArgs.count("h"))
89  {
90  return EXIT_SUCCESS;
91  }
92  us::Any listAny = parsedArgs["image"];
93  auto inputImageList = us::any_cast<mitkCommandLineParser::StringContainerType>(listAny);
94 
95  std::vector<ImageType::Pointer> imageList;
96  for (std::size_t i = 0; i < inputImageList.size(); ++i)
97  {
98  mitk::Image::Pointer image = mitk::IOUtil::Load<mitk::Image>(inputImageList[i]);
99  ImageType::Pointer itkImage = ImageType::New();
100  mitk::CastToItkImage(image, itkImage);
101  imageList.push_back(itkImage);
102  }
103  mitk::Image::Pointer mitkMask = mitk::IOUtil::Load<mitk::Image>(inputImageList[0]);
104  MaskType::Pointer mask = MaskType::New();
105  mitk::CastToItkImage(mitkMask, mask);
106 
107  itk::ImageRegionIterator<MaskType> maskIter(mask, mask->GetLargestPossibleRegion());
108  while (!maskIter.IsAtEnd())
109  {
110  maskIter.Set(0);
111  ++maskIter;
112  }
113 
114  std::vector<ImageType::IndexType> listOfIndexes;
115  listOfIndexes.reserve(1000);
116 
117  // Find Start Location for the case that one corner is "blocked" by content. Works only on the first image!
118  ImageType::IndexType tmpIndex;
119  ImageType::IndexType startIndex;
120  startIndex.Fill(0);
121  for (unsigned char i = 0; i < 8; ++i)
122  {
123  tmpIndex.Fill(0);
124  if ((i & 1) > 0) tmpIndex[0] = mask->GetLargestPossibleRegion().GetSize(0)-1;
125  if ((i & 2) > 0) tmpIndex[1] = mask->GetLargestPossibleRegion().GetSize(1)-1;
126  if ((i & 4) > 0) tmpIndex[2] = mask->GetLargestPossibleRegion().GetSize(2)-1;
127 
128  MITK_INFO << tmpIndex;
129  if (imageList[0]->GetPixel(tmpIndex) < imageList[0]->GetPixel(startIndex))
130  {
131  startIndex = tmpIndex;
132  }
133  }
134  listOfIndexes.push_back(tmpIndex);
135 
136  while (listOfIndexes.size() > 0)
137  {
138  ImageType::IndexType currentIndex = listOfIndexes.back();
139  listOfIndexes.pop_back();
140  if (!(mask->GetLargestPossibleRegion().IsInside(currentIndex)))
141  {
142  continue;
143  }
144  if (mask->GetPixel(currentIndex) == 0)
145  {
146  mask->SetPixel(currentIndex, 1);
147  double minimum = std::numeric_limits<double>::max();
148  for (std::size_t i = 0; i < imageList.size(); ++i)
149  {
150  minimum = std::min<double>(minimum, imageList[i]->GetPixel(currentIndex));
151  }
152  if (minimum < 35)
153  {
154  mask->SetPixel(currentIndex, 2);
155  tmpIndex = currentIndex;
156  tmpIndex[0] += 1;
157  listOfIndexes.push_back(tmpIndex);
158  tmpIndex[0] -= 2;
159  listOfIndexes.push_back(tmpIndex);
160  tmpIndex[0] += 1;
161  tmpIndex[1] += 1;
162  listOfIndexes.push_back(tmpIndex);
163  tmpIndex[1] -= 2;
164  listOfIndexes.push_back(tmpIndex);
165  tmpIndex[1] += 1;
166  tmpIndex[2] += 1;
167  listOfIndexes.push_back(tmpIndex);
168  tmpIndex[2] -= 2;
169  listOfIndexes.push_back(tmpIndex);
170  }
171  }
172  }
173  MITK_INFO << "Im here";
174  maskIter.GoToBegin();
175  while (!maskIter.IsAtEnd())
176  {
177  if (maskIter.Get() == 2)
178  maskIter.Set(0);
179  else
180  maskIter.Set(1);
181  ++maskIter;
182  }
183 
185 
186  std::string maskPath = parsedArgs["mask"].ToString();
187  mitk::IOUtil::Save(ergMask, maskPath);
188 
189  //AccessByItk_3(image, Normalize, im2, mask, parsedArgs["output"].ToString());
190 
191  return 0;
192 }
193 
194 #endif
static void DetectSkull(itk::Image< TPixel, VImageDimension > *itkImage, mitk::Image::Pointer im2, mitk::Image::Pointer mask1, std::string output)
Definition: CLSkullMask.cpp:36
#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
itk::Image< double, 3 > FloatImageType
Definition: CLSkullMask.cpp:31
static T max(T x, T y)
Definition: svm.cpp:56
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
std::vector< std::string > StringContainerType
mitk::Image::Pointer mask
void setTitle(std::string title)
itk::Image< unsigned char, 3 > MaskImageType
Definition: CLSkullMask.cpp:32
int main(int argc, char *argv[])
Definition: CLSkullMask.cpp:64
void setDescription(std::string description)