Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkImageToPointCloudFilter.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 <itkImageRegionIterator.h>
16 #include <itkLaplacianImageFilter.h>
17 
18 #include <vtkPolyVertex.h>
19 #include <vtkUnstructuredGrid.h>
20 #include <vtkSmartPointer.h>
21 
22 #include <mitkITKImageImport.h>
23 #include <mitkImageAccessByItk.h>
25 #include <mitkUnstructuredGrid.h>
27 
29 {
30  m_Method = DetectionMethod(0);
31 
32  this->SetNumberOfRequiredInputs(1);
33 
34  this->SetNumberOfIndexedOutputs(1);
35 }
36 
38 {
39 }
40 
42 {
44  m_Geometry = image->GetGeometry();
45 
46  if (image.IsNull())
47  {
48  MITK_ERROR << "mitk::ImageToContourFilter: No input available. "
49  "Please set the input!"
50  << std::endl;
51  return;
52  }
53 
54  mitk::Image::Pointer notConstImage = const_cast<mitk::Image *>(image.GetPointer());
55 
56  switch (m_Method)
57  {
58  case 0:
59  AccessByItk_1(notConstImage.GetPointer(), StdDeviations, 2) break;
60 
61  case 1:
62  AccessByItk_1(notConstImage.GetPointer(), StdDeviations, 3) break;
63 
64  case 2:
65  AccessByItk_1(notConstImage.GetPointer(), StdDeviations, 4) break;
66 
67  default:
68  AccessByItk_1(notConstImage.GetPointer(), StdDeviations, 2) break;
69  }
70 }
71 
72 template <typename TPixel, unsigned int VImageDimension>
73 void mitk::ImageToPointCloudFilter::StdDeviations(itk::Image<TPixel, VImageDimension> *image, int amount)
74 {
75  typedef itk::Image<TPixel, VImageDimension> InputImageType;
76  typedef itk::CastImageFilter<InputImageType, FloatImageType> ImagePTypeToFloatPTypeCasterType;
77  typedef itk::LaplacianImageFilter<FloatImageType, FloatImageType> LaplacianFilterType;
78  typename LaplacianFilterType::Pointer lapFilter = LaplacianFilterType::New();
79 
80  typename ImagePTypeToFloatPTypeCasterType::Pointer caster = ImagePTypeToFloatPTypeCasterType::New();
81  caster->SetInput(image);
82  caster->Update();
83  FloatImageType::Pointer fImage = caster->GetOutput();
84 
85  lapFilter->SetInput(fImage);
86  lapFilter->UpdateLargestPossibleRegion();
87  auto edgeImage = mitk::ImportItkImage(lapFilter->GetOutput());
88 
91  statCalc->SetInputImage(edgeImage.GetPointer());
92  auto stats = statCalc->GetStatistics()->GetStatisticsForTimeStep(0);
93  auto mean = stats.GetValueConverted<double>(mitk::ImageStatisticsConstants::MEAN());
94  auto stdDev = stats.GetValueConverted<double>(mitk::ImageStatisticsConstants::STANDARDDEVIATION());
95 
96  double upperThreshold = mean + stdDev * amount;
97  double lowerThreshold = mean - stdDev * amount;
98 
99  typename itk::ImageRegionIterator<FloatImageType> it(lapFilter->GetOutput(),
100  lapFilter->GetOutput()->GetRequestedRegion());
101 
102  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
103 
104  double greatX = 0, greatY = 0, greatZ = 0;
105 
106  it.GoToBegin();
107  while (!it.IsAtEnd())
108  {
109  if (it.Get() > lowerThreshold && it.Get() < upperThreshold)
110  {
111  it.Set(0);
112  }
113  else
114  {
115  it.Set(1);
116 
117  mitk::Point3D imagePoint;
118  mitk::Point3D worldPoint;
119 
120  imagePoint[0] = it.GetIndex()[0];
121  imagePoint[1] = it.GetIndex()[1];
122  imagePoint[2] = it.GetIndex()[2];
123 
124  m_Geometry->IndexToWorld(imagePoint, worldPoint);
125 
126  if (worldPoint[0] > greatX)
127  greatX = worldPoint[0];
128  if (worldPoint[1] > greatY)
129  greatY = worldPoint[1];
130  if (worldPoint[2] > greatZ)
131  greatZ = worldPoint[2];
132 
133  points->InsertNextPoint(worldPoint[0], worldPoint[1], worldPoint[2]);
135  }
136  ++it;
137  }
138 
139  /*need to build the UnstructuredGrid with at least one vertex otherwise its
140  not visible*/
141  vtkSmartPointer<vtkPolyVertex> verts = vtkSmartPointer<vtkPolyVertex>::New();
142 
143  verts->GetPointIds()->SetNumberOfIds(m_NumberOfExtractedPoints);
144  for (int i = 0; i < m_NumberOfExtractedPoints; i++)
145  {
146  verts->GetPointIds()->SetId(i, i);
147  }
148 
149  vtkSmartPointer<vtkUnstructuredGrid> uGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();
150  uGrid->Allocate(1);
151 
152  uGrid->InsertNextCell(verts->GetCellType(), verts->GetPointIds());
153  uGrid->SetPoints(points);
154 
156  outputGrid->SetVtkUnstructuredGrid(uGrid);
157  this->SetNthOutput(0, outputGrid);
158 }
159 
161 {
162  Superclass::GenerateOutputInformation();
163 }
void IndexToWorld(const mitk::Vector3D &vec_units, mitk::Vector3D &vec_mm) const
Convert (continuous or discrete) index coordinates of a vector vec_units to world coordinates (in mm)...
#define MITK_ERROR
Definition: mitkLogMacros.h:20
#define AccessByItk_1(mitkImage, itkImageTypeFunction, arg1)
Image::Pointer ImportItkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, const BaseGeometry *geometry=nullptr, bool update=true)
Imports an itk::Image (with a specific type) as an mitk::Image.Instantiates instance of ITKImageImpor...
static Pointer New()
DetectionMethod
The method which calculates and extracts the edge pixels/points. For the edge detection the laplacian...
Image class for storing images.
Definition: mitkImage.h:72
mitk::Image::Pointer image
static const std::string STANDARDDEVIATION()