Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
MriNormalization.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 
17 #include "itkImageRegionIterator.h"
18 #include "mitkCommandLineParser.h"
19 #include "mitkIOUtil.h"
20 #include "mitkImageCast.h"
21 #include <mitkITKImageImport.h>
22 
23 using namespace std;
24 
25 typedef itk::Image<unsigned char, 3> SeedImage;
26 typedef itk::Image<mitk::ScalarType, 3> FeatureImage;
27 
28 typedef itk::ImageRegionIterator<SeedImage> SeedIteratorType;
29 typedef itk::ImageRegionIterator<FeatureImage> FeatureIteratorType;
30 
31 int main(int argc, char *argv[])
32 {
33  mitkCommandLineParser parser;
34  parser.setTitle("Mri Normalization");
35  parser.setCategory("Preprocessing Tools");
36  parser.setDescription(
37  "Normalizes an MRI volume between medians of two given masks (e.g. ventricles and brain matter)");
38  parser.setContributor("MBI");
39 
40  parser.setArgumentPrefix("--", "-");
41 
42  // Add command line argument names
43  parser.addArgument("input", "i", mitkCommandLineParser::InputImage, "input image");
44  parser.addArgument("output", "o", mitkCommandLineParser::OutputFile, "output image");
45  parser.addArgument(
46  "maxMask", "m", mitkCommandLineParser::InputImage, "mask of which median is set as maximal value (1)");
47  parser.addArgument(
48  "minMask", "l", mitkCommandLineParser::InputImage, "mask of which median is set as minimal value (0)");
49  parser.addArgument(
50  "excludeMask", "e", mitkCommandLineParser::InputImage, "region which is exluded from the other two");
51 
52  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
53 
54  // Show a help message
55  if (parsedArgs.size() == 0 || parsedArgs.count("help") || parsedArgs.count("h"))
56  {
57  std::cout << parser.helpText();
58  return EXIT_SUCCESS;
59  }
60 
61  std::string outFile = us::any_cast<string>(parsedArgs["output"]);
62  mitk::Image::Pointer inputFile = mitk::IOUtil::LoadImage(us::any_cast<string>(parsedArgs["input"]));
63  mitk::Image::Pointer maxMask = mitk::IOUtil::LoadImage(us::any_cast<string>(parsedArgs["maxMask"]));
64  mitk::Image::Pointer minMask = mitk::IOUtil::LoadImage(us::any_cast<string>(parsedArgs["minMask"]));
65  mitk::Image::Pointer excludeMask = mitk::IOUtil::LoadImage(us::any_cast<string>(parsedArgs["excludeMask"]));
66 
67  SeedImage::Pointer itkMaxImage = SeedImage::New();
68  SeedImage::Pointer itkMinImage = SeedImage::New();
69  SeedImage::Pointer itkExcludeImage = SeedImage::New();
70 
71  FeatureImage::Pointer itkInputImage = FeatureImage::New();
72 
73  mitk::CastToItkImage(inputFile, itkInputImage);
74  mitk::CastToItkImage(maxMask, itkMaxImage);
75  mitk::CastToItkImage(minMask, itkMinImage);
76  mitk::CastToItkImage(excludeMask, itkExcludeImage);
77 
78  std::vector<mitk::ScalarType> medianMin;
79  std::vector<mitk::ScalarType> medianMax;
80 
81  itk::ImageRegionIterator<FeatureImage> inputIt(itkInputImage, itkInputImage->GetLargestPossibleRegion());
82  itk::ImageRegionIterator<SeedImage> excludeIt(itkExcludeImage, itkExcludeImage->GetLargestPossibleRegion());
83  itk::ImageRegionIterator<SeedImage> minIt(itkMinImage, itkMinImage->GetLargestPossibleRegion());
84  itk::ImageRegionIterator<SeedImage> maxIt(itkMaxImage, itkMaxImage->GetLargestPossibleRegion());
85 
86  while (!inputIt.IsAtEnd())
87  {
88  if (excludeIt.Get() == 0)
89  {
90  if (minIt.Get() != 0)
91  medianMin.push_back(inputIt.Get());
92 
93  if (maxIt.Get() != 0)
94  medianMax.push_back(inputIt.Get());
95  }
96  ++inputIt;
97  ++excludeIt;
98  ++minIt;
99  ++maxIt;
100  }
101 
102  std::sort(medianMax.begin(), medianMax.end());
103  std::sort(medianMin.begin(), medianMin.end());
104 
105  mitk::ScalarType minVal = medianMin.at(medianMin.size() / 2);
106  mitk::ScalarType maxVal = medianMax.at(medianMax.size() / 2);
107 
108  inputIt.GoToBegin();
109  // Create mapping
110  while (!inputIt.IsAtEnd())
111  {
112  inputIt.Set(1.0 * (inputIt.Get() - minVal) / (maxVal - minVal));
113  ++inputIt;
114  }
115 
116  mitk::Image::Pointer mitkResult = mitk::Image::New();
117  mitkResult = mitk::GrabItkImageMemory(itkInputImage);
118 
119  mitk::IOUtil::Save(mitkResult, outFile);
120 
121  return EXIT_SUCCESS;
122 }
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:824
itk::SmartPointer< Self > Pointer
itk::Image< mitk::ScalarType, 3 > FeatureImage
double ScalarType
void setContributor(std::string contributor)
STL namespace.
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
int main(int argc, char *argv[])
itk::Image< unsigned char, 3 > SeedImage
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grabs the memory of an itk::Image (with a specific type) and puts it into an mitk::Image.The memory is managed by the mitk::Image after calling this function. The itk::Image remains valid until the mitk::Image decides to free the memory.
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)
void setCategory(std::string category)
static Pointer New()
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.
std::string helpText() const
void setTitle(std::string title)
itk::ImageRegionIterator< SeedImage > SeedIteratorType
itk::ImageRegionIterator< FeatureImage > FeatureIteratorType
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.