Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
HistogramAdaption.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 "mitkIOUtil.h"
19 #include "mitkImageCast.h"
20 #include <mitkCommandLineParser.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("Contrast Adaption");
35  parser.setCategory("Preprocessing Tools");
36  parser.setDescription("Stretches or pushes image intensities above a given threshold");
37  parser.setContributor("MBI");
38 
39  parser.setArgumentPrefix("--", "-");
40 
41  // Add command line argument names
42  parser.addArgument("input", "i", mitkCommandLineParser::InputImage, "input image");
43  parser.addArgument("output", "o", mitkCommandLineParser::OutputFile, "output image");
44  parser.addArgument("cutOff", "c", mitkCommandLineParser::Float, "value at which different slope is to be applied");
45  parser.addArgument(
46  "slope", "s", mitkCommandLineParser::Float, "slope to be applied (total delta to max value starting from cutOff)");
47 
48  map<string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
49 
50  if (parsedArgs.size() == 0)
51  return EXIT_FAILURE;
52 
53  // Show a help message
54  if (parsedArgs.count("help") || parsedArgs.count("h"))
55  {
56  std::cout << parser.helpText();
57  return EXIT_SUCCESS;
58  }
59 
60  std::string outFile = us::any_cast<string>(parsedArgs["output"]);
61  mitk::Image::Pointer inputFile = mitk::IOUtil::LoadImage(us::any_cast<string>(parsedArgs["input"]));
62  float cutOff = us::any_cast<float>(parsedArgs["cutOff"]);
63  float slope = us::any_cast<float>(parsedArgs["slope"]);
64 
65  FeatureImage::Pointer itkInputImage = FeatureImage::New();
66 
67  mitk::CastToItkImage(inputFile, itkInputImage);
68  double max = -99999;
69 
70  itk::ImageRegionIterator<FeatureImage> inputIt(itkInputImage, itkInputImage->GetLargestPossibleRegion());
71 
72  while (!inputIt.IsAtEnd())
73  {
74  if (inputIt.Get() > max)
75  max = inputIt.Get();
76 
77  ++inputIt;
78  }
79 
80  inputIt.GoToBegin();
81  // Mapping
82  while (!inputIt.IsAtEnd())
83  {
84  if (inputIt.Get() > cutOff)
85  {
86  inputIt.Set(cutOff + slope * (inputIt.Get() - cutOff) / (max - cutOff));
87  }
88  ++inputIt;
89  }
90 
92  // !! CastToItk behaves very differently depending on the original data type
93  // if the target type is the same as the original, only a pointer to the data is set
94  // and an additional GrabItkImageMemory will cause a segfault when the image is destroyed
95  // GrabItkImageMemory - is not necessary in this case since we worked on the original data
96  // See Bug 17538.
97  if (inputFile->GetPixelType().GetComponentTypeAsString() != "double")
98  mitkResult = mitk::GrabItkImageMemory(itkInputImage);
99  else
100  mitkResult = inputFile;
101 
102  mitk::IOUtil::Save(mitkResult, outFile);
103  return EXIT_SUCCESS;
104 }
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:824
itk::Image< mitk::ScalarType, 3 > FeatureImage
itk::SmartPointer< Self > Pointer
itk::ImageRegionIterator< FeatureImage > FeatureIteratorType
itk::Image< unsigned char, 3 > SeedImage
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)
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)
itk::ImageRegionIterator< SeedImage > SeedIteratorType
int main(int argc, char *argv[])
static T max(T x, T y)
Definition: svm.cpp:70
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)
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.