Medical Imaging Interaction Toolkit  2018.4.99-c7ee88da
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
13 
15 #include "mitkImage.h"
16 #include "mitkImageAccessByItk.h"
17 #include "mitkImageCast.h"
18 #include "mitkProperties.h"
19 #include "mitkToolManager.h"
20 #include <itkImageRegionIterator.h>
21 
22 #include "mitkPixelManipulationTool.xpm"
23 
24 namespace mitk
25 {
27 }
28 
29 mitk::PixelManipulationTool::PixelManipulationTool() : Tool("dummy"), m_Value(0), m_FixedValue(false)
30 {
31 }
32 
34 {
35 }
36 
38 {
39  Superclass::Activated();
40 
44 
45  if (m_OriginalImageNode.IsNotNull())
46  {
47  mitk::Image::Pointer image = dynamic_cast<mitk::Image *>(m_OriginalImageNode->GetData());
48  if (image.IsNotNull())
49  {
50  // mitk::ScalarType scalar = image->GetScalarValueMax();
51  }
52  }
53  else
55 }
56 
58 {
61 
62  Superclass::Deactivated();
63 }
64 
66 {
67  return "pixelmanipulation";
68 }
69 
71 {
72  return mitkPixelManipulationTool_xpm;
73 }
74 
76 {
77 }
78 
80 {
81  if (m_OriginalImageNode.IsNotNull())
82  {
83  mitk::Image::Pointer image = dynamic_cast<mitk::Image *>(m_OriginalImageNode->GetData());
84  mitk::DataNode *maskNode = m_ToolManager->GetRoiData(0);
86 
87  if (maskNode)
88  {
89  auto *boundingObject = dynamic_cast<mitk::BoundingObject *>(maskNode->GetData());
90 
91  if (boundingObject)
92  {
94  filter->SetBoundingObject(boundingObject);
95  filter->SetInput(image);
96  filter->Update();
97  roi = filter->GetOutput();
98  }
99  else
100  roi = dynamic_cast<mitk::Image *>(maskNode->GetData());
101 
103  newImage->Initialize(image);
104 
105  if (image)
106  {
107  AccessByItk_3(image, ITKPixelManipulation, roi, newImage, m_Value);
108  this->AddImageToDataStorage(newImage);
109  }
110  }
111  }
112 }
113 
114 template <typename TPixel, unsigned int VImageDimension>
115 void mitk::PixelManipulationTool::ITKPixelManipulation(itk::Image<TPixel, VImageDimension> *originalImage,
116  Image *maskImage,
117  Image *newImage,
118  int newValue)
119 {
120  typedef itk::Image<TPixel, VImageDimension> itkImageType;
121  typedef itk::Image<unsigned char, 3> itkMaskType;
122  typename itkImageType::Pointer itkImage;
123  typename itkMaskType::Pointer itkMask;
124  CastToItkImage(newImage, itkImage);
125  CastToItkImage(maskImage, itkMask);
126 
127  typedef itk::ImageRegionConstIterator<itkImageType> InputIteratorType;
128  typedef itk::ImageRegionIterator<itkImageType> OutputIteratorType;
129  typedef itk::ImageRegionConstIterator<itkMaskType> MaskIteratorType;
130 
131  MaskIteratorType maskIterator(itkMask, itkMask->GetLargestPossibleRegion());
132  InputIteratorType inputIterator(originalImage, originalImage->GetLargestPossibleRegion());
133  OutputIteratorType outputIterator(itkImage, itkImage->GetLargestPossibleRegion());
134 
135  inputIterator.GoToBegin();
136  outputIterator.GoToBegin();
137  maskIterator.GoToBegin();
138 
139  while (!outputIterator.IsAtEnd())
140  {
141  if (maskIterator.Get())
142  {
143  if (m_FixedValue)
144  outputIterator.Set(newValue);
145  else
146  outputIterator.Set(inputIterator.Get() + newValue);
147  }
148  else
149  outputIterator.Set(inputIterator.Get());
150 
151  ++inputIterator;
152  ++outputIterator;
153  ++maskIterator;
154  }
155 }
156 
158 {
159  if (image.IsNotNull())
160  {
162  std::string name = m_OriginalImageNode->GetName();
163  name.append("_modified");
164  node->SetName(name);
165  node->SetProperty("binary", mitk::BoolProperty::New(false));
166  node->SetData(image);
167 
168  if (m_ToolManager)
170  }
171 }
172 
174 {
175  m_Value = value;
176 }
177 
179 {
180  return m_Value;
181 }
182 
184 {
185  m_FixedValue = value;
186 }
187 
189 {
190  return m_FixedValue;
191 }
#define AccessByItk_3(mitkImage, itkImageTypeFunction, arg1, arg2, arg3)
Base class of all tools used by mitk::ToolManager.
Definition: mitkTool.h:86
const char * GetName() const override
Returns the name of this tool. Make it short!
DataStorage * GetDataStorage()
#define MITKSEGMENTATION_EXPORT
DataCollection - Class to facilitate loading/accessing structured data.
mitk::DataNode::Pointer m_OriginalImageNode
DataVectorType GetRoiData()
virtual void Add(DataNode *node, const DataStorage::SetOfObjects *parents=nullptr)=0
Adds a DataNode containing a data object to its internal storage.
bool ActivateTool(int id)
void ITKPixelManipulation(itk::Image< TPixel, VImageDimension > *originalImage, Image *maskImage, Image *newImage, int newValue)
static Pointer New()
superclass of all bounding objects (cylinder, cuboid,...)
void Activated() override
Called when the tool gets activated.
MITK_TOOL_MACRO(MITKSEGMENTATION_EXPORT, LiveWireTool2D, "LiveWire tool")
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
static Pointer New()
Image class for storing images.
Definition: mitkImage.h:72
mitk::Image::Pointer image
void Deactivated() override
Called when the tool gets deactivated.
static Pointer New()
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.
DataVectorType GetReferenceData()
ToolManager * m_ToolManager
Definition: mitkTool.h:228
void AddImageToDataStorage(itk::SmartPointer< mitk::Image > image)
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57