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