Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 #ifndef mitkCLPolyToNrrd_cpp
17 #define mitkCLPolyToNrrd_cpp
18 
19 #include "time.h"
20 #include <sstream>
21 #include <fstream>
22 
23 #include <mitkIOUtil.h>
24 #include "mitkCommandLineParser.h"
25 
26 #include "itkImageRegionIterator.h"
27 // MITK
28 #include <mitkITKImageImport.h>
29 #include <mitkImageCast.h>
30 #include <mitkImageAccessByItk.h>
31 // ITK
32 #include <itkLabelStatisticsImageFilter.h>
33 #include <itkMinimumMaximumImageCalculator.h>
34 
35 typedef itk::Image< double, 3 > FloatImageType;
36 typedef itk::Image< unsigned char, 3 > MaskImageType;
37 
38 template<typename TPixel, unsigned int VImageDimension>
39 void
40  Normalize(itk::Image<TPixel, VImageDimension>* itkImage, mitk::Image::Pointer im2, mitk::Image::Pointer mask1, std::string output)
41 {
42  typedef itk::Image<TPixel, VImageDimension> ImageType;
43  typedef itk::Image<unsigned char, VImageDimension> MaskType;
44 
45  typename ImageType::Pointer itkIm2 = ImageType::New();
46  typename MaskType::Pointer itkMask1 = MaskType::New();
47  mitk::CastToItkImage(im2, itkIm2);
48  mitk::CastToItkImage(mask1, itkMask1);
49 
50  itk::ImageRegionIterator<ImageType> iterI1(itkImage, itkImage->GetLargestPossibleRegion());
51  itk::ImageRegionIterator<ImageType> iterI2(itkIm2, itkImage->GetLargestPossibleRegion());
52  itk::ImageRegionIterator<MaskType> iter(itkMask1, itkImage->GetLargestPossibleRegion());
53  while (! iter.IsAtEnd())
54  {
55  unsigned char maskV = 0;
56  if (iterI1.Value() > 0.0001 && iterI2.Value() > 0.00001)
57  maskV = 1;
58  iter.Set(maskV);
59  ++iter;
60  ++iterI1;
61  ++iterI2;
62  }
63 
65  mitk::IOUtil::SaveImage(img, output);
66 }
67 
68 int main(int argc, char* argv[])
69 {
70  mitkCommandLineParser parser;
71  parser.setArgumentPrefix("--", "-");
72  // required params
73  parser.addArgument("image", "i", mitkCommandLineParser::InputImage, "Input Image", "Path to the input VTK polydata", us::Any(), false);
74  parser.addArgument("image2", "i2", mitkCommandLineParser::InputImage, "Input Mask", "The median of the area covered by this mask will be set to 0", us::Any(), false);
75  parser.addArgument("mask", "m", mitkCommandLineParser::InputImage, "Input Mask", "The median of the area covered by this mask will be set to 1", us::Any(), false);
76  parser.addArgument("output", "o", mitkCommandLineParser::OutputFile, "Output Image", "Target file. The output statistic is appended to this file.", us::Any(), false);
77 
78  // Miniapp Infos
79  parser.setCategory("Classification Tools");
80  parser.setTitle("MR Normalization Tool");
81  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.");
82  parser.setContributor("MBI");
83 
84  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
85 
86  if (parsedArgs.size()==0)
87  {
88  return EXIT_FAILURE;
89  }
90  if ( parsedArgs.count("help") || parsedArgs.count("h"))
91  {
92  return EXIT_SUCCESS;
93  }
94 
95  mitk::Image::Pointer image = mitk::IOUtil::LoadImage(parsedArgs["image"].ToString());
96  mitk::Image::Pointer im2= mitk::IOUtil::LoadImage(parsedArgs["image2"].ToString());
97  mitk::Image::Pointer mask = mitk::IOUtil::LoadImage(parsedArgs["mask"].ToString());
98 
99  AccessByItk_3(image, Normalize, im2, mask, parsedArgs["output"].ToString());
100 
101  return 0;
102 }
103 
104 #endif
#define AccessByItk_3(mitkImage, itkImageTypeFunction, arg1, arg2, arg3)
itk::SmartPointer< Self > Pointer
itk::Image< unsigned char, 3 > ImageType
void setContributor(std::string contributor)
itk::Image< double, 3 > FloatImageType
Definition: CLBrainMask.cpp:35
int main(int argc, char *argv[])
Definition: CLBrainMask.cpp:68
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...
static bool SaveImage(mitk::Image::Pointer image, const std::string &path)
SaveImage Convenience method to save an arbitrary mitkImage.
Definition: mitkIOUtil.cpp:870
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)
Definition: usAny.h:163
itk::Image< unsigned char, 3 > MaskImageType
Definition: CLBrainMask.cpp:36
void setCategory(std::string category)
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:40
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
void setTitle(std::string title)
void setDescription(std::string description)
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
Definition: mitkIOUtil.cpp:597
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.