Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkWatershedTool.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 ============================================================================*/
12 
13 #include "mitkWatershedTool.h"
14 
15 #include "mitkIOUtil.h"
16 #include "mitkITKImageImport.h"
17 #include "mitkImage.h"
18 #include "mitkLabelSetImage.h"
19 #include "mitkImageAccessByItk.h"
20 #include "mitkImageCast.h"
22 #include "mitkLevelWindowManager.h"
23 #include "mitkLookupTable.h"
25 #include "mitkProgressBar.h"
26 #include "mitkRenderingManager.h"
28 #include "mitkToolCommand.h"
29 #include "mitkToolManager.h"
31 
32 #include <usGetModuleContext.h>
33 #include <usModule.h>
34 #include <usModuleContext.h>
35 #include <usModuleResource.h>
36 
37 #include <vtkLookupTable.h>
38 
39 #include <itkExceptionObject.h>
40 #include <itkGradientMagnitudeRecursiveGaussianImageFilter.h>
41 #include <itkWatershedImageFilter.h>
42 
43 namespace mitk
44 {
46 }
47 
48 mitk::WatershedTool::WatershedTool() : m_Threshold(0.0), m_Level(0.0)
49 {
50 }
51 
53 {
54 }
55 
57 {
58  Superclass::Activated();
59 }
60 
62 {
63  Superclass::Deactivated();
64 }
65 
67 {
69  us::ModuleResource resource = module->GetResource("Watershed_48x48.png");
70  return resource;
71 }
72 
73 const char **mitk::WatershedTool::GetXPM() const
74 {
75  return nullptr;
76 }
77 
78 const char *mitk::WatershedTool::GetName() const
79 {
80  return "Watershed";
81 }
82 
84 {
85  // get image from tool manager
87  mitk::Image::Pointer input = dynamic_cast<mitk::Image *>(referenceData->GetData());
88  if (input.IsNull())
89  return;
90 
92  input = Get3DImage(input, timestep);
93 
94  mitk::Image::Pointer output;
95 
96  try
97  {
98  // create and run itk filter pipeline
99  AccessByItk_1(input.GetPointer(), ITKWatershed, output);
100 
102  labelSetOutput->InitializeByLabeledImage(output);
103 
104  // create a new datanode for output
106  dataNode->SetData(labelSetOutput);
107 
108  // set name of data node
109  std::string name = referenceData->GetName() + "_Watershed";
110  dataNode->SetName(name);
111 
112  // look, if there is already a node with this name
113  mitk::DataStorage::SetOfObjects::ConstPointer children =
114  m_ToolManager->GetDataStorage()->GetDerivations(referenceData);
115  mitk::DataStorage::SetOfObjects::ConstIterator currentNode = children->Begin();
116  mitk::DataNode::Pointer removeNode;
117  while (currentNode != children->End())
118  {
119  if (dataNode->GetName().compare(currentNode->Value()->GetName()) == 0)
120  {
121  removeNode = currentNode->Value();
122  }
123  currentNode++;
124  }
125  // remove node with same name
126  if (removeNode.IsNotNull())
127  m_ToolManager->GetDataStorage()->Remove(removeNode);
128 
129  // add output to the data storage
130  m_ToolManager->GetDataStorage()->Add(dataNode, referenceData);
131  }
132  catch (itk::ExceptionObject &e)
133  {
134  MITK_ERROR << "Watershed Filter Error: " << e.GetDescription();
135  }
136 
138 }
139 
140 template <typename TPixel, unsigned int VImageDimension>
141 void mitk::WatershedTool::ITKWatershed(itk::Image<TPixel, VImageDimension> *originalImage,
142  mitk::Image::Pointer &segmentation)
143 {
144  typedef itk::WatershedImageFilter<itk::Image<float, VImageDimension>> WatershedFilter;
145  typedef itk::GradientMagnitudeRecursiveGaussianImageFilter<itk::Image<TPixel, VImageDimension>,
146  itk::Image<float, VImageDimension>>
147  MagnitudeFilter;
148 
149  // at first add a gradient magnitude filter
150  typename MagnitudeFilter::Pointer magnitude = MagnitudeFilter::New();
151  magnitude->SetInput(originalImage);
152  magnitude->SetSigma(1.0);
153 
154  // use the progress bar
156  command->AddStepsToDo(60);
157 
158  // then add the watershed filter to the pipeline
159  typename WatershedFilter::Pointer watershed = WatershedFilter::New();
160  watershed->SetInput(magnitude->GetOutput());
161  watershed->SetThreshold(m_Threshold);
162  watershed->SetLevel(m_Level);
163  watershed->AddObserver(itk::ProgressEvent(), command);
164  watershed->Update();
165 
166  // then make sure, that the output has the desired pixel type
167  typedef itk::CastImageFilter<typename WatershedFilter::OutputImageType,
168  itk::Image<Tool::DefaultSegmentationDataType, VImageDimension>>
169  CastFilter;
170  typename CastFilter::Pointer cast = CastFilter::New();
171  cast->SetInput(watershed->GetOutput());
172 
173  // start the whole pipeline
174  cast->Update();
175 
176  // reset the progress bar by setting progress
177  command->SetProgress(10);
178 
179  // since we obtain a new image from our pipeline, we have to make sure, that our mitk::Image::Pointer
180  // is responsible for the memory management of the output image
181  segmentation = mitk::GrabItkImageMemory(cast->GetOutput());
182 }
double m_Level
Threshold parameter of the ITK Watershed Image Filter. See ITK Documentation for more information...
Simple watershed segmentation tool.
void DoIt()
Grabs the tool reference data and creates an ITK pipeline consisting of a GradientMagnitude image fil...
virtual SetOfObjects::ConstPointer GetDerivations(const DataNode *node, const NodePredicateBase *condition=nullptr, bool onlyDirectDerivations=true) const =0
returns a set of derived objects for a given node.
DataStorage * GetDataStorage()
#define MITK_ERROR
Definition: mitkLogMacros.h:20
#define MITKSEGMENTATION_EXPORT
DataCollection - Class to facilitate loading/accessing structured data.
const SliceNavigationController * GetTimeNavigationController() const
virtual void Add(DataNode *node, const DataStorage::SetOfObjects *parents=nullptr)=0
Adds a DataNode containing a data object to its internal storage.
#define AccessByItk_1(mitkImage, itkImageTypeFunction, arg1)
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.
static Pointer New()
MITK_TOOL_MACRO(MITKSEGMENTATION_EXPORT, LiveWireTool2D, "LiveWire tool")
const char ** GetXPM() const override
Returns an icon in the XPM format.
void Activated() override
Called when the tool gets activated.
static Pointer New()
static RenderingManager * GetInstance()
Module * GetModule() const
Image class for storing images.
Definition: mitkImage.h:72
ModuleResource GetResource(const std::string &path) const
Definition: usModule.cpp:267
void ITKWatershed(itk::Image< TPixel, VImageDimension > *originalImage, itk::SmartPointer< mitk::Image > &segmentation)
Creates and runs an ITK filter pipeline consisting of the filters: GradientMagnitude-, Watershed- and CastImageFilter.
virtual void Remove(const DataNode *node)=0
Removes node from the DataStorage.
static Pointer New()
DataVectorType GetReferenceData()
ToolManager * m_ToolManager
Definition: mitkTool.h:228
mitk::Stepper * GetTime()
Get the Stepper through the time.
virtual itk::SmartPointer< Image > Get3DImage(itk::SmartPointer< Image > image, unsigned int timestep)
virtual unsigned int GetPos() const
void Deactivated() override
Called when the tool gets deactivated.
us::ModuleResource GetIconResource() const override
Returns the tool button icon of the tool wrapped by a usModuleResource.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)
const char * GetName() const override
Returns the name of this tool. Make it short!
double m_Threshold
Threshold parameter of the ITK Watershed Image Filter. See ITK Documentation for more information...