Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkPixelManipulationTool.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 ===================================================================*/
17 
19 #include "mitkImage.h"
20 #include "mitkImageAccessByItk.h"
21 #include "mitkImageCast.h"
22 #include "mitkProperties.h"
23 #include "mitkToolManager.h"
24 #include <itkImageRegionIterator.h>
25 
26 #include "mitkPixelManipulationTool.xpm"
27 
28 namespace mitk
29 {
31 }
32 
33 mitk::PixelManipulationTool::PixelManipulationTool() : Tool("dummy"), m_Value(0), m_FixedValue(false)
34 {
35 }
36 
38 {
39 }
40 
42 {
43  Superclass::Activated();
44 
45  m_ToolManager->RoiDataChanged +=
47  m_OriginalImageNode = m_ToolManager->GetReferenceData(0);
48 
49  if (m_OriginalImageNode.IsNotNull())
50  {
51  mitk::Image::Pointer image = dynamic_cast<mitk::Image *>(m_OriginalImageNode->GetData());
52  if (image.IsNotNull())
53  {
54  // mitk::ScalarType scalar = image->GetScalarValueMax();
55  }
56  }
57  else
58  m_ToolManager->ActivateTool(-1);
59 }
60 
62 {
63  m_ToolManager->RoiDataChanged -=
65 
66  Superclass::Deactivated();
67 }
68 
70 {
71  return "pixelmanipulation";
72 }
73 
75 {
76  return mitkPixelManipulationTool_xpm;
77 }
78 
80 {
81 }
82 
84 {
85  if (m_OriginalImageNode.IsNotNull())
86  {
87  mitk::Image::Pointer image = dynamic_cast<mitk::Image *>(m_OriginalImageNode->GetData());
88  mitk::DataNode *maskNode = m_ToolManager->GetRoiData(0);
90 
91  if (maskNode)
92  {
93  mitk::BoundingObject *boundingObject = dynamic_cast<mitk::BoundingObject *>(maskNode->GetData());
94 
95  if (boundingObject)
96  {
98  filter->SetBoundingObject(boundingObject);
99  filter->SetInput(image);
100  filter->Update();
101  roi = filter->GetOutput();
102  }
103  else
104  roi = dynamic_cast<mitk::Image *>(maskNode->GetData());
105 
107  newImage->Initialize(image);
108 
109  if (image)
110  {
111  AccessByItk_3(image, ITKPixelManipulation, roi, newImage, m_Value);
112  this->AddImageToDataStorage(newImage);
113  }
114  }
115  }
116 }
117 
118 template <typename TPixel, unsigned int VImageDimension>
119 void mitk::PixelManipulationTool::ITKPixelManipulation(itk::Image<TPixel, VImageDimension> *originalImage,
120  Image *maskImage,
121  Image *newImage,
122  int newValue)
123 {
124  typedef itk::Image<TPixel, VImageDimension> itkImageType;
125  typedef itk::Image<unsigned char, 3> itkMaskType;
126  typename itkImageType::Pointer itkImage;
127  typename itkMaskType::Pointer itkMask;
128  CastToItkImage(newImage, itkImage);
129  CastToItkImage(maskImage, itkMask);
130 
131  typedef itk::ImageRegionConstIterator<itkImageType> InputIteratorType;
132  typedef itk::ImageRegionIterator<itkImageType> OutputIteratorType;
133  typedef itk::ImageRegionConstIterator<itkMaskType> MaskIteratorType;
134 
135  MaskIteratorType maskIterator(itkMask, itkMask->GetLargestPossibleRegion());
136  InputIteratorType inputIterator(originalImage, originalImage->GetLargestPossibleRegion());
137  OutputIteratorType outputIterator(itkImage, itkImage->GetLargestPossibleRegion());
138 
139  inputIterator.GoToBegin();
140  outputIterator.GoToBegin();
141  maskIterator.GoToBegin();
142 
143  while (!outputIterator.IsAtEnd())
144  {
145  if (maskIterator.Get())
146  {
147  if (m_FixedValue)
148  outputIterator.Set(newValue);
149  else
150  outputIterator.Set(inputIterator.Get() + newValue);
151  }
152  else
153  outputIterator.Set(inputIterator.Get());
154 
155  ++inputIterator;
156  ++outputIterator;
157  ++maskIterator;
158  }
159 }
160 
162 {
163  if (image.IsNotNull())
164  {
166  std::string name = m_OriginalImageNode->GetName();
167  name.append("_modified");
168  node->SetName(name);
169  node->SetProperty("binary", mitk::BoolProperty::New(false));
170  node->SetData(image);
171 
172  if (m_ToolManager)
173  m_ToolManager->GetDataStorage()->Add(node, m_OriginalImageNode);
174  }
175 }
176 
178 {
179  m_Value = value;
180 }
181 
183 {
184  return m_Value;
185 }
186 
188 {
189  m_FixedValue = value;
190 }
191 
193 {
194  return m_FixedValue;
195 }
#define AccessByItk_3(mitkImage, itkImageTypeFunction, arg1, arg2, arg3)
Base class of all tools used by mitk::ToolManager.
Definition: mitkTool.h:92
itk::SmartPointer< Self > Pointer
virtual const char * GetName() const override
Returns the name of this tool. Make it short!
#define MITKSEGMENTATION_EXPORT
DataCollection - Class to facilitate loading/accessing structured data.
#define MITK_TOOL_MACRO(EXPORT_SPEC, CLASS_NAME, DESCRIPTION)
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
void ITKPixelManipulation(itk::Image< TPixel, VImageDimension > *originalImage, Image *maskImage, Image *newImage, int newValue)
static Pointer New()
superclass of all bounding objects (cylinder, cuboid,...)
virtual void Activated() override
Called when the tool gets activated.
static Pointer New()
Image class for storing images.
Definition: mitkImage.h:76
virtual void Deactivated() override
Called when the tool gets deactivated.
static Pointer New()
virtual const char ** GetXPM() const override
Returns an icon in the XPM format.
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 AddImageToDataStorage(itk::SmartPointer< mitk::Image > image)
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66