Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
MriOtsuNormalization.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 // ITK
24 #include <itkOtsuMultipleThresholdsImageFilter.h>
25 
26 using namespace std;
27 
28 typedef itk::Image<unsigned char, 3> SeedImage;
29 typedef itk::Image<mitk::ScalarType, 3> FeatureImage;
30 
31 typedef itk::ImageRegionIterator<SeedImage> SeedIteratorType;
32 typedef itk::ImageRegionIterator<FeatureImage> FeatureIteratorType;
33 
34 int main(int argc, char *argv[])
35 {
36  mitkCommandLineParser parser;
37  parser.setTitle("Mri Normalization");
38  parser.setCategory("Preprocessing Tools");
39  parser.setDescription("Normalizes an MRI volume based on regions determined by Otsu.");
40  parser.setContributor("MBI");
41 
42  parser.setArgumentPrefix("--", "-");
43 
44  // Add command line argument names
45  parser.addArgument("input", "i", mitkCommandLineParser::InputImage, "input image", "input image");
46  parser.addArgument("output", "o", mitkCommandLineParser::OutputFile, "output image", "output image");
47  parser.addArgument("bins", "b", mitkCommandLineParser::Int, "number of regions (bins)", "number of regions (bins)");
48 
49  parser.addArgument(
50  "minBin", "l", mitkCommandLineParser::Int, "bin of which median is set to 0", "bin of which median is set to 0");
51  parser.addArgument(
52  "maxBin", "h", mitkCommandLineParser::Int, "bin of which median is set to 1", "bin of which median is set to 1");
53 
54  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
55 
56  // Show a help message
57  if (parsedArgs.size() == 0 || parsedArgs.count("help") || parsedArgs.count("h"))
58  {
59  std::cout << parser.helpText();
60  return EXIT_SUCCESS;
61  }
62 
63  std::string outFile = us::any_cast<string>(parsedArgs["output"]);
64  mitk::Image::Pointer inputFile = mitk::IOUtil::LoadImage(us::any_cast<string>(parsedArgs["input"]));
65  int numberOfThresholds = us::any_cast<int>(parsedArgs["bins"]);
66  int minBin = us::any_cast<int>(parsedArgs["minBin"]);
67  int maxBin = us::any_cast<int>(parsedArgs["maxBin"]);
68 
69  FeatureImage::Pointer itkInputImage = FeatureImage::New();
70  mitk::CastToItkImage(inputFile, itkInputImage);
71 
72  typedef itk::OtsuMultipleThresholdsImageFilter<FeatureImage, FeatureImage> FilterType;
73  FilterType::Pointer otsuFilter = FilterType::New();
74  otsuFilter->SetInput(itkInputImage);
75  otsuFilter->SetNumberOfThresholds(numberOfThresholds - 1);
76  otsuFilter->Update();
77 
78  FeatureImage::Pointer itkLabelImage = otsuFilter->GetOutput();
79 
80  std::vector<mitk::ScalarType> medianMin;
81  std::vector<mitk::ScalarType> medianMax;
82 
83  itk::ImageRegionIterator<FeatureImage> inputIt(itkInputImage, itkInputImage->GetLargestPossibleRegion());
84  itk::ImageRegionIterator<FeatureImage> labelIt(itkLabelImage, itkLabelImage->GetLargestPossibleRegion());
85 
86  while (!inputIt.IsAtEnd())
87  {
88  if (labelIt.Get() == minBin)
89  {
90  medianMin.push_back(inputIt.Get());
91  }
92  else if (labelIt.Get() == maxBin)
93  {
94  medianMax.push_back(inputIt.Get());
95  }
96  ++inputIt;
97  ++labelIt;
98  }
99 
100  std::sort(medianMax.begin(), medianMax.end());
101  std::sort(medianMin.begin(), medianMin.end());
102 
103  mitk::ScalarType minVal = medianMin.at(medianMin.size() / 2);
104  mitk::ScalarType maxVal = medianMax.at(medianMax.size() / 2);
105 
106  inputIt.GoToBegin();
107  // labelIt.GoToBegin();
108  // Create mapping
109  while (!inputIt.IsAtEnd())
110  {
111  inputIt.Set(1.0 * (inputIt.Get() - minVal) / (maxVal - minVal));
112  // inputIt.Set(labelIt.Get());
113  ++inputIt;
114 
115  //++labelIt;
116  }
117 
118  mitk::Image::Pointer mitkResult = mitk::Image::New();
119  mitkResult = mitk::GrabItkImageMemory(itkInputImage);
120 
121  mitk::IOUtil::Save(mitkResult, outFile);
122 
123  return EXIT_SUCCESS;
124 }
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:824
itk::SmartPointer< Self > Pointer
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)
itk::ImageRegionIterator< FeatureImage > FeatureIteratorType
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()
itk::Image< unsigned char, 3 > SeedImage
itk::Image< mitk::ScalarType, 3 > FeatureImage
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)
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
itk::ImageRegionIterator< SeedImage > SeedIteratorType
int main(int argc, char *argv[])
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.