Medical Imaging Interaction Toolkit  2018.4.99-b7f3afaa
Medical Imaging Interaction Toolkit
CLBrainMask.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 void
36  Normalize(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  mitkCommandLineParser parser;
67  parser.setArgumentPrefix("--", "-");
68  // required params
69  parser.addArgument("image", "i", mitkCommandLineParser::Image, "Input Image", "Path to the input VTK polydata", us::Any(), false, false, false, mitkCommandLineParser::Input);
70  parser.addArgument("image2", "i2", mitkCommandLineParser::Image, "Input Mask", "The median of the area covered by this mask will be set to 0", us::Any(), false, false, false, mitkCommandLineParser::Input);
71  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);
72  parser.addArgument("output", "o", mitkCommandLineParser::File, "Output Image", "Target file. The output statistic is appended to this file.", us::Any(), false, false, false, mitkCommandLineParser::Output);
73 
74  // Miniapp Infos
75  parser.setCategory("Classification Tools");
76  parser.setTitle("MR Normalization Tool");
77  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.");
78  parser.setContributor("German Cancer Research Center (DKFZ)");
79 
80  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
81 
82  if (parsedArgs.size()==0)
83  {
84  return EXIT_FAILURE;
85  }
86  if ( parsedArgs.count("help") || parsedArgs.count("h"))
87  {
88  return EXIT_SUCCESS;
89  }
90 
91  mitk::Image::Pointer image = mitk::IOUtil::Load<mitk::Image>(parsedArgs["image"].ToString());
92  mitk::Image::Pointer im2= mitk::IOUtil::Load<mitk::Image>(parsedArgs["image2"].ToString());
93  mitk::Image::Pointer mask = mitk::IOUtil::Load<mitk::Image>(parsedArgs["mask"].ToString());
94 
95  AccessByItk_3(image, Normalize, im2, mask, parsedArgs["output"].ToString());
96 
97  return 0;
98 }
99 
100 #endif
#define AccessByItk_3(mitkImage, itkImageTypeFunction, arg1, arg2, arg3)
itk::Image< unsigned char, 3 > ImageType
void setContributor(std::string contributor)
itk::Image< double, 3 > FloatImageType
Definition: CLBrainMask.cpp:31
int main(int argc, char *argv[])
Definition: CLBrainMask.cpp:64
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< unsigned char, 3 > MaskImageType
Definition: CLBrainMask.cpp:32
void setCategory(std::string category)
mitk::Image::Pointer image
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
void Normalize(itk::Image< TPixel, VImageDimension > *itkImage, mitk::Image::Pointer im2, mitk::Image::Pointer mask1, std::string output)
Definition: CLBrainMask.cpp:36
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 setDescription(std::string description)