Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkManualSegmentationToSurfaceFilter.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 
14 
15 #include <vtkImageShiftScale.h>
16 #include <vtkSmartPointer.h>
17 
18 #include "mitkProgressBar.h"
19 
21 {
22  m_MedianFilter3D = false;
28  m_Interpolation = false;
29  m_InterpolationX = 1.0f;
30  m_InterpolationY = 1.0f;
31  m_InterpolationZ = 1.0f;
32 };
33 
35 
37 {
38  mitk::Surface *surface = this->GetOutput();
39  auto *image = (mitk::Image *)GetInput();
40  mitk::Image::RegionType outputRegion = image->GetRequestedRegion();
41 
42  int tstart = outputRegion.GetIndex(3);
43  int tmax = tstart + outputRegion.GetSize(3); // GetSize()==1 - will aber 0 haben, wenn nicht zeitaufgeloest
44 
45  ScalarType thresholdExpanded = this->m_Threshold;
46 
47  if ((tmax - tstart) > 0)
48  {
49  ProgressBar::GetInstance()->AddStepsToDo(4 * (tmax - tstart));
50  }
51  else
52  {
54  }
55 
56  for (int t = tstart; t < tmax; ++t)
57  {
58  vtkSmartPointer<vtkImageData> vtkimage = image->GetVtkImageData(t);
59 
60  // Median -->smooth 3D
61  // MITK_INFO << (m_MedianFilter3D ? "Applying median..." : "No median filtering");
62  if (m_MedianFilter3D)
63  {
64  vtkImageMedian3D *median = vtkImageMedian3D::New();
65  median->SetInputData(vtkimage); // RC++ (VTK < 5.0)
66  median->SetKernelSize(m_MedianKernelSizeX, m_MedianKernelSizeY, m_MedianKernelSizeZ); // Std: 3x3x3
67  median->ReleaseDataFlagOn();
68  median->UpdateInformation();
69  median->Update();
70  vtkimage = median->GetOutput(); //->Out
71  median->Delete();
72  }
74 
75  // Interpolate image spacing
76  // MITK_INFO << (m_Interpolation ? "Resampling..." : "No resampling");
77  if (m_Interpolation)
78  {
79  vtkImageResample *imageresample = vtkImageResample::New();
80  imageresample->SetInputData(vtkimage);
81 
82  // Set Spacing Manual to 1mm in each direction (Original spacing is lost during image processing)
83  imageresample->SetAxisOutputSpacing(0, m_InterpolationX);
84  imageresample->SetAxisOutputSpacing(1, m_InterpolationY);
85  imageresample->SetAxisOutputSpacing(2, m_InterpolationZ);
86  imageresample->UpdateInformation();
87  imageresample->Update();
88  vtkimage = imageresample->GetOutput(); //->Output
89  imageresample->Delete();
90  }
92 
93  // MITK_INFO << (m_UseGaussianImageSmooth ? "Applying gaussian smoothing..." : "No gaussian smoothing");
94  if (m_UseGaussianImageSmooth) // gauss
95  {
96  vtkImageShiftScale *scalefilter = vtkImageShiftScale::New();
97  scalefilter->SetScale(100);
98  scalefilter->SetInputData(vtkimage);
99  scalefilter->Update();
100 
101  vtkImageGaussianSmooth *gaussian = vtkImageGaussianSmooth::New();
102  gaussian->SetInputConnection(scalefilter->GetOutputPort());
103  gaussian->SetDimensionality(3);
104  gaussian->SetRadiusFactor(0.49);
105  gaussian->SetStandardDeviation(m_GaussianStandardDeviation);
106  gaussian->ReleaseDataFlagOn();
107  gaussian->UpdateInformation();
108  gaussian->Update();
109 
110  vtkimage = scalefilter->GetOutput();
111 
112  double range[2];
113  vtkimage->GetScalarRange(range);
114 
115  if (range[1] != 0) // too little slices, image smoothing eliminates all segmentation pixels
116  {
117  vtkimage = gaussian->GetOutput(); //->Out
118  }
119  else
120  {
121  MITK_INFO << "Smoothing would remove all pixels of the segmentation. Use unsmoothed result instead.";
122  }
123  gaussian->Delete();
124  scalefilter->Delete();
125  }
127 
128  // Create surface for t-Slice
129  CreateSurface(t, vtkimage, surface, thresholdExpanded);
131  }
132 
133  // MITK_INFO << "Updating Time Geometry to ensure right timely displaying";
134  // Fixing wrong time geometry
135  TimeGeometry *surfaceTG = surface->GetTimeGeometry();
136  auto *surfacePTG = dynamic_cast<ProportionalTimeGeometry *>(surfaceTG);
137  TimeGeometry *imageTG = image->GetTimeGeometry();
138  auto *imagePTG = dynamic_cast<ProportionalTimeGeometry *>(imageTG);
139  // Requires ProportionalTimeGeometries to work. May not be available for all steps.
140  assert(surfacePTG != nullptr);
141  assert(imagePTG != nullptr);
142  if ((surfacePTG != nullptr) && (imagePTG != nullptr))
143  {
144  TimePointType firstTime = imagePTG->GetFirstTimePoint();
145  TimePointType duration = imagePTG->GetStepDuration();
146  surfacePTG->SetFirstTimePoint(firstTime);
147  surfacePTG->SetStepDuration(duration);
148  // MITK_INFO << "First Time Point: " << firstTime << " Duration: " << duration;
149  }
150 };
151 
153 {
157 }
158 
160 {
161  m_InterpolationX = x;
162  m_InterpolationY = y;
163  m_InterpolationZ = z;
164 }
void Progress(unsigned int steps=1)
Sets the current amount of progress to current progress + steps.
Class for storing surfaces (vtkPolyData).
Definition: mitkSurface.h:28
#define MITK_INFO
Definition: mitkLogMacros.h:18
double ScalarType
OutputType * GetOutput()
const mitk::Image * GetInput(void)
static ProgressBar * GetInstance()
static method to get the GUI dependent ProgressBar-instance so the methods for steps to do and progre...
virtual void SetInterpolation(bool _arg)
const mitk::TimeGeometry * GetTimeGeometry() const
Return the TimeGeometry of the data as const pointer.
Definition: mitkBaseData.h:61
itk::ImageRegion< RegionDimension > RegionType
Image class for storing images.
Definition: mitkImage.h:72
mitk::ScalarType TimePointType
void CreateSurface(int time, vtkImageData *vtkimage, mitk::Surface *surface, const ScalarType threshold)
mitk::Image::Pointer image
void AddStepsToDo(unsigned int steps)
Adds steps to totalSteps.