Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkOtsuTool3D.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 // MITK
18 #include "mitkOtsuTool3D.h"
19 #include "mitkImageAccessByItk.h"
20 #include "mitkLabelSetImage.h"
22 #include "mitkRenderingManager.h"
23 #include "mitkToolManager.h"
24 #include <mitkITKImageImport.h>
25 #include <mitkImageCast.h>
30 
31 // ITK
32 #include <itkBinaryThresholdImageFilter.h>
33 #include <itkOrImageFilter.h>
34 #include <itkOtsuMultipleThresholdsImageFilter.h>
35 
36 // us
37 #include <usGetModuleContext.h>
38 #include <usModule.h>
39 #include <usModuleContext.h>
40 #include <usModuleResource.h>
41 
43 
44 namespace mitk
45 {
47 }
48 
50 {
51 }
52 
54 {
55 }
56 
58 {
59  Superclass::Activated();
60 
61  if (m_ToolManager)
62  {
63  m_OriginalImage = dynamic_cast<mitk::Image *>(m_ToolManager->GetReferenceData(0)->GetData());
64 
65  m_BinaryPreviewNode = mitk::DataNode::New();
66  m_BinaryPreviewNode->SetName("Binary_Preview");
67  m_BinaryPreviewNode->SetProperty("color", ColorProperty::New(0.0, 1.0, 0.0));
68  m_BinaryPreviewNode->SetProperty("opacity", FloatProperty::New(0.3));
69  // m_BinaryPreviewNode->SetBoolProperty("helper object", true);
70  // m_BinaryPreviewNode->SetProperty("binary", mitk::BoolProperty::New(true));
71  m_ToolManager->GetDataStorage()->Add(this->m_BinaryPreviewNode);
72 
73  m_MultiLabelResultNode = mitk::DataNode::New();
74  m_MultiLabelResultNode->SetName("Otsu_Preview");
75  // m_MultiLabelResultNode->SetBoolProperty("helper object", true);
76  m_MultiLabelResultNode->SetVisibility(true);
77 
78  m_MaskedImagePreviewNode = mitk::DataNode::New();
79  m_MaskedImagePreviewNode->SetName("Volume_Preview");
80  // m_MultiLabelResultNode->SetBoolProperty("helper object", true);
81  m_MaskedImagePreviewNode->SetVisibility(false);
82 
83  m_ToolManager->GetDataStorage()->Add(this->m_MultiLabelResultNode);
84  }
85 }
86 
88 {
89  m_ToolManager->GetDataStorage()->Remove(this->m_MultiLabelResultNode);
90  m_MultiLabelResultNode = NULL;
91  m_ToolManager->GetDataStorage()->Remove(this->m_BinaryPreviewNode);
92  m_BinaryPreviewNode = NULL;
93  m_ToolManager->GetDataStorage()->Remove(this->m_MaskedImagePreviewNode);
94  m_MaskedImagePreviewNode = NULL;
95 
96  Superclass::Deactivated();
97 }
98 
99 const char **mitk::OtsuTool3D::GetXPM() const
100 {
101  return NULL;
102 }
103 
105 {
107  us::ModuleResource resource = module->GetResource("Otsu_48x48.png");
108  return resource;
109 }
110 
111 void mitk::OtsuTool3D::RunSegmentation(int regions, bool useValley, int numberOfBins)
112 {
113  // this->m_OtsuSegmentationDialog->setCursor(Qt::WaitCursor);
114 
115  int numberOfThresholds = regions - 1;
116 
118 
119  mitk::Image::Pointer image3D = Get3DImage(m_OriginalImage, timestep);
120 
122  otsuFilter->SetNumberOfThresholds(numberOfThresholds);
123  otsuFilter->SetValleyEmphasis(useValley);
124  otsuFilter->SetNumberOfBins(numberOfBins);
125  otsuFilter->SetInput(image3D);
126 
127  try
128  {
129  otsuFilter->Update();
130  }
131  catch (...)
132  {
133  mitkThrow() << "itkOtsuFilter error (image dimension must be in {2, 3} and image must not be RGB)";
134  }
135 
136  m_ToolManager->GetDataStorage()->Remove(this->m_MultiLabelResultNode);
137  m_MultiLabelResultNode = NULL;
138  m_MultiLabelResultNode = mitk::DataNode::New();
139  m_MultiLabelResultNode->SetName("Otsu_Preview");
140  m_MultiLabelResultNode->SetVisibility(true);
141  m_ToolManager->GetDataStorage()->Add(this->m_MultiLabelResultNode);
142  m_MultiLabelResultNode->SetOpacity(1.0);
143 
145  resultImage->InitializeByLabeledImage(otsuFilter->GetOutput());
146  this->m_MultiLabelResultNode->SetData(resultImage);
147  m_MultiLabelResultNode->SetProperty("binary", mitk::BoolProperty::New(false));
150  m_MultiLabelResultNode->SetProperty("Image Rendering.Mode", renderingMode);
153  vtkSmartPointer<vtkLookupTable> lookupTable = vtkSmartPointer<vtkLookupTable>::New();
154  lookupTable->SetHueRange(1.0, 0.0);
155  lookupTable->SetSaturationRange(1.0, 1.0);
156  lookupTable->SetValueRange(1.0, 1.0);
157  lookupTable->SetTableRange(-1.0, 1.0);
158  lookupTable->Build();
159  lut->SetVtkLookupTable(lookupTable);
160  prop->SetLookupTable(lut);
161  m_MultiLabelResultNode->SetProperty("LookupTable", prop);
163  mitk::LevelWindow levelwindow;
164  levelwindow.SetRangeMinMax(0, numberOfThresholds + 1);
165  levWinProp->SetLevelWindow(levelwindow);
166  m_MultiLabelResultNode->SetProperty("levelwindow", levWinProp);
167 
168  // m_BinaryPreviewNode->SetVisibility(false);
169  // m_MultiLabelResultNode->SetVisibility(true);
170  // this->m_OtsuSegmentationDialog->setCursor(Qt::ArrowCursor);
172 }
173 
175 {
177  resultImage->InitializeByLabeledImage(dynamic_cast<mitk::Image *>(m_BinaryPreviewNode->GetData()));
178  GetTargetSegmentationNode()->SetData(resultImage);
179 
180  m_ToolManager->ActivateTool(-1);
181 }
182 
183 void mitk::OtsuTool3D::UpdateBinaryPreview(std::vector<int> regionIDs)
184 {
185  m_MultiLabelResultNode->SetVisibility(false);
186  mitk::Image::Pointer multiLabelSegmentation = dynamic_cast<mitk::Image *>(m_MultiLabelResultNode->GetData());
187  AccessByItk_1(multiLabelSegmentation, CalculatePreview, regionIDs);
188 }
189 
190 template <typename TPixel, unsigned int VImageDimension>
191 void mitk::OtsuTool3D::CalculatePreview(itk::Image<TPixel, VImageDimension> *itkImage, std::vector<int> regionIDs)
192 {
193  typedef itk::Image<TPixel, VImageDimension> InputImageType;
194  typedef itk::Image<mitk::Tool::DefaultSegmentationDataType, VImageDimension> OutputImageType;
195 
196  typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> FilterType;
197 
198  typename FilterType::Pointer filter = FilterType::New();
199 
200  // InputImageType::Pointer itkImage;
201  typename OutputImageType::Pointer itkBinaryTempImage1;
202  typename OutputImageType::Pointer itkBinaryTempImage2;
203  typename OutputImageType::Pointer itkBinaryResultImage;
204 
205  // mitk::Image::Pointer multiLabelSegmentation = dynamic_cast<mitk::Image*>(m_MultiLabelResultNode->GetData());
206  // mitk::CastToItkImage(multiLabelSegmentation, itkImage);
207 
208  filter->SetInput(itkImage);
209  filter->SetLowerThreshold(regionIDs[0]);
210  filter->SetUpperThreshold(regionIDs[0]);
211  filter->SetInsideValue(1);
212  filter->SetOutsideValue(0);
213  filter->Update();
214  itkBinaryTempImage2 = filter->GetOutput();
215 
218 
219  // if more than one region id is used compute the union of all given binary regions
220  for (std::vector<int>::iterator it = regionIDs.begin(); it != regionIDs.end(); ++it)
221  {
222  filter->SetLowerThreshold(*it);
223  filter->SetUpperThreshold(*it);
224  filter->SetInsideValue(1);
225  filter->SetOutsideValue(0);
226  filter->Update();
227  itkBinaryTempImage1 = filter->GetOutput();
228 
229  orFilter->SetInput1(itkBinaryTempImage1);
230  orFilter->SetInput2(itkBinaryTempImage2);
231 
232  orFilter->UpdateLargestPossibleRegion();
233  itkBinaryResultImage = orFilter->GetOutput();
234  itkBinaryTempImage2 = itkBinaryResultImage;
235  }
236  //----------------------------------------------------------------------------------------------------
237  mitk::Image::Pointer binarySegmentation;
238  mitk::CastToMitkImage(itkBinaryResultImage, binarySegmentation);
239  m_BinaryPreviewNode->SetData(binarySegmentation);
240  m_BinaryPreviewNode->SetVisibility(true);
241  m_BinaryPreviewNode->SetProperty("outline binary", mitk::BoolProperty::New(false));
242 
244 }
245 
246 // void mitk::OtsuTool3D::UpdateBinaryPreview(int regionID)
247 //{
248 // m_MultiLabelResultNode->SetVisibility(false);
249 // //pixel with regionID -> binary image
250 // const unsigned short dim = 3;
251 // typedef unsigned char PixelType;
252 //
253 // typedef itk::Image< PixelType, dim > InputImageType;
254 // typedef itk::Image< PixelType, dim > OutputImageType;
255 //
256 // typedef itk::BinaryThresholdImageFilter< InputImageType, OutputImageType > FilterType;
257 //
258 // FilterType::Pointer filter = FilterType::New();
259 //
260 // InputImageType::Pointer itkImage;
261 //
262 // mitk::Image::Pointer multiLabelSegmentation = dynamic_cast<mitk::Image*>(m_MultiLabelResultNode->GetData());
263 // mitk::CastToItkImage(multiLabelSegmentation, itkImage);
264 //
265 // filter->SetInput(itkImage);
266 // filter->SetLowerThreshold(regionID);
267 // filter->SetUpperThreshold(regionID);
268 // filter->SetInsideValue(1);
269 // filter->SetOutsideValue(0);
270 // filter->Update();
271 // mitk::Image::Pointer binarySegmentation;
272 // mitk::CastToMitkImage( filter->GetOutput(), binarySegmentation);
273 // m_BinaryPreviewNode->SetData(binarySegmentation);
274 // m_BinaryPreviewNode->SetVisibility(true);
275 // m_BinaryPreviewNode->SetProperty("outline binary", mitk::BoolProperty::New(false));
276 //
277 // mitk::RenderingManager::GetInstance()->RequestUpdateAll();
278 //}
279 
280 const char *mitk::OtsuTool3D::GetName() const
281 {
282  return "Otsu";
283 }
284 
285 void mitk::OtsuTool3D::UpdateVolumePreview(bool volumeRendering)
286 {
287  if (volumeRendering)
288  {
289  m_MaskedImagePreviewNode->SetBoolProperty("volumerendering", true);
290  m_MaskedImagePreviewNode->SetBoolProperty("volumerendering.uselod", true);
291  }
292  else
293  {
294  m_MaskedImagePreviewNode->SetBoolProperty("volumerendering", false);
295  }
297 }
298 
300 {
301  m_MultiLabelResultNode->SetVisibility(show);
302  m_BinaryPreviewNode->SetVisibility(!show);
304 }
305 
307 {
308  ScalarType min = m_OriginalImage.GetPointer()->GetStatistics()->GetScalarValueMin();
309  ScalarType max = m_OriginalImage.GetPointer()->GetStatistics()->GetScalarValueMaxNoRecompute();
310  return static_cast<int>(max - min) + 1;
311 }
void UpdateVolumePreview(bool volumeRendering)
const SliceNavigationController * GetTimeNavigationController() const
itk::SmartPointer< Self > Pointer
us::ModuleResource GetIconResource() const override
Returns the tool button icon of the tool wrapped by a usModuleResource.
static Pointer New()
double ScalarType
static Pointer New()
void ShowMultiLabelResultNode(bool)
#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)
virtual void Activated() override
Called when the tool gets activated.
void SetRangeMinMax(ScalarType min, ScalarType max)
The LevelWindow class Class to store level/window values.
static Pointer New()
void RunSegmentation(int regions, bool useValley, int numberOfBins)
itk::Image< double, 3 > InputImageType
virtual const char * GetName() const override
Returns the name of this tool. Make it short!
static Pointer New()
static Pointer New()
static RenderingManager * GetInstance()
Module * GetModule() const
#define mitkThrow()
Image class for storing images.
Definition: mitkImage.h:76
void CalculatePreview(itk::Image< TPixel, VImageDimension > *itkImage, std::vector< int > regionIDs)
void UpdateBinaryPreview(std::vector< int > regionIDs)
virtual unsigned int GetPos() const
static T max(T x, T y)
Definition: svm.cpp:70
static Pointer New()
static Pointer New()
static T min(T x, T y)
Definition: svm.cpp:67
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
Definition: mitkImageCast.h:78
mitk::Stepper * GetTime()
Get the Stepper through the time.
virtual const char ** GetXPM() const override
Returns an icon in the XPM format.
ModuleResource GetResource(const std::string &path) const
Definition: usModule.cpp:267
static Pointer New()
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
virtual void Deactivated() override
Called when the tool gets deactivated.
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.