Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
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 "mitkWatershedTool.h"
18 
19 #include "mitkIOUtil.h"
20 #include "mitkITKImageImport.h"
21 #include "mitkImage.h"
22 #include "mitkLabelSetImage.h"
23 #include "mitkImageAccessByItk.h"
24 #include "mitkImageCast.h"
26 #include "mitkLevelWindowManager.h"
27 #include "mitkLookupTable.h"
29 #include "mitkProgressBar.h"
30 #include "mitkRenderingManager.h"
32 #include "mitkToolCommand.h"
33 #include "mitkToolManager.h"
35 
36 #include <usGetModuleContext.h>
37 #include <usModule.h>
38 #include <usModuleContext.h>
39 #include <usModuleResource.h>
40 
41 #include <vtkLookupTable.h>
42 
43 #include <itkExceptionObject.h>
44 #include <itkGradientMagnitudeRecursiveGaussianImageFilter.h>
45 #include <itkWatershedImageFilter.h>
46 
47 namespace mitk
48 {
50 }
51 
52 mitk::WatershedTool::WatershedTool() : m_Threshold(0.0), m_Level(0.0)
53 {
54 }
55 
57 {
58 }
59 
61 {
62  Superclass::Activated();
63 }
64 
66 {
67  Superclass::Deactivated();
68 }
69 
71 {
73  us::ModuleResource resource = module->GetResource("Watershed_48x48.png");
74  return resource;
75 }
76 
77 const char **mitk::WatershedTool::GetXPM() const
78 {
79  return nullptr;
80 }
81 
82 const char *mitk::WatershedTool::GetName() const
83 {
84  return "Watershed";
85 }
86 
88 {
89  // get image from tool manager
90  mitk::DataNode::Pointer referenceData = m_ToolManager->GetReferenceData(0);
91  mitk::Image::Pointer input = dynamic_cast<mitk::Image *>(referenceData->GetData());
92  if (input.IsNull())
93  return;
94 
96  input = Get3DImage(input, timestep);
97 
98  mitk::Image::Pointer output;
99 
100  try
101  {
102  // create and run itk filter pipeline
103  AccessByItk_1(input.GetPointer(), ITKWatershed, output);
104 
106  labelSetOutput->InitializeByLabeledImage(output);
107 
108  // create a new datanode for output
110  dataNode->SetData(labelSetOutput);
111 
112  // set name of data node
113  std::string name = referenceData->GetName() + "_Watershed";
114  dataNode->SetName(name);
115 
116  // look, if there is already a node with this name
118  m_ToolManager->GetDataStorage()->GetDerivations(referenceData);
119  mitk::DataStorage::SetOfObjects::ConstIterator currentNode = children->Begin();
120  mitk::DataNode::Pointer removeNode;
121  while (currentNode != children->End())
122  {
123  if (dataNode->GetName().compare(currentNode->Value()->GetName()) == 0)
124  {
125  removeNode = currentNode->Value();
126  }
127  currentNode++;
128  }
129  // remove node with same name
130  if (removeNode.IsNotNull())
131  m_ToolManager->GetDataStorage()->Remove(removeNode);
132 
133  // add output to the data storage
134  m_ToolManager->GetDataStorage()->Add(dataNode, referenceData);
135  }
136  catch (itk::ExceptionObject &e)
137  {
138  MITK_ERROR << "Watershed Filter Error: " << e.GetDescription();
139  }
140 
142 }
143 
144 template <typename TPixel, unsigned int VImageDimension>
145 void mitk::WatershedTool::ITKWatershed(itk::Image<TPixel, VImageDimension> *originalImage,
146  mitk::Image::Pointer &segmentation)
147 {
148  typedef itk::WatershedImageFilter<itk::Image<float, VImageDimension>> WatershedFilter;
149  typedef itk::GradientMagnitudeRecursiveGaussianImageFilter<itk::Image<TPixel, VImageDimension>,
150  itk::Image<float, VImageDimension>>
151  MagnitudeFilter;
152 
153  // at first add a gradient magnitude filter
154  typename MagnitudeFilter::Pointer magnitude = MagnitudeFilter::New();
155  magnitude->SetInput(originalImage);
156  magnitude->SetSigma(1.0);
157 
158  // use the progress bar
160  command->AddStepsToDo(60);
161 
162  // then add the watershed filter to the pipeline
163  typename WatershedFilter::Pointer watershed = WatershedFilter::New();
164  watershed->SetInput(magnitude->GetOutput());
165  watershed->SetThreshold(m_Threshold);
166  watershed->SetLevel(m_Level);
167  watershed->AddObserver(itk::ProgressEvent(), command);
168  watershed->Update();
169 
170  // then make sure, that the output has the desired pixel type
171  typedef itk::CastImageFilter<typename WatershedFilter::OutputImageType,
172  itk::Image<Tool::DefaultSegmentationDataType, VImageDimension>>
173  CastFilter;
174  typename CastFilter::Pointer cast = CastFilter::New();
175  cast->SetInput(watershed->GetOutput());
176 
177  // start the whole pipeline
178  cast->Update();
179 
180  // reset the progress bar by setting progress
181  command->SetProgress(10);
182 
183  // since we obtain a new image from our pipeline, we have to make sure, that our mitk::Image::Pointer
184  // is responsible for the memory management of the output image
185  segmentation = mitk::GrabItkImageMemory(cast->GetOutput());
186 }
Simple watershed segmentation tool.
const SliceNavigationController * GetTimeNavigationController() const
itk::SmartPointer< Self > Pointer
void DoIt()
Grabs the tool reference data and creates an ITK pipeline consisting of a GradientMagnitude image fil...
#define MITK_ERROR
Definition: mitkLogMacros.h:24
#define MITKSEGMENTATION_EXPORT
DataCollection - Class to facilitate loading/accessing structured data.
#define MITK_TOOL_MACRO(EXPORT_SPEC, CLASS_NAME, DESCRIPTION)
#define AccessByItk_1(mitkImage, itkImageTypeFunction, arg1)
itk::SmartPointer< const Self > ConstPointer
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()
const char ** GetXPM() const override
Returns an icon in the XPM format.
virtual 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:76
virtual unsigned int GetPos() const
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.
static Pointer New()
mitk::Stepper * GetTime()
Get the Stepper through the time.
virtual void Deactivated() override
Called when the tool gets deactivated.
ModuleResource GetResource(const std::string &path) const
Definition: usModule.cpp:267
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!
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.