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
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,
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 
18 
19 #include <itkImageRegionIterator.h>
20 #include <itkLaplacianImageFilter.h>
21 
22 #include <vtkPolyVertex.h>
23 #include <vtkUnstructuredGrid.h>
24 #include <vtkSmartPointer.h>
25 
26 #include <mitkITKImageImport.h>
27 #include <mitkImageAccessByItk.h>
28 #include <mitkImageStatisticsCalculator.h>
29 #include <mitkUnstructuredGrid.h>
30 
32 {
33  m_Method = DetectionMethod(0);
34 
35  this->SetNumberOfRequiredInputs(1);
36 
37  this->SetNumberOfIndexedOutputs(1);
38 }
39 
41 {
42 }
43 
45 {
47  m_Geometry = image->GetGeometry();
48 
49  if (image.IsNull())
50  {
51  MITK_ERROR << "mitk::ImageToContourFilter: No input available. "
52  "Please set the input!"
53  << std::endl;
54  return;
55  }
56 
57  mitk::Image::Pointer notConstImage = const_cast<mitk::Image *>(image.GetPointer());
58 
59  switch (m_Method)
60  {
61  case 0:
62  AccessByItk_1(notConstImage.GetPointer(), StdDeviations, 2) break;
63 
64  case 1:
65  AccessByItk_1(notConstImage.GetPointer(), StdDeviations, 3) break;
66 
67  case 2:
68  AccessByItk_1(notConstImage.GetPointer(), StdDeviations, 4) break;
69 
70  default:
71  AccessByItk_1(notConstImage.GetPointer(), StdDeviations, 2) break;
72  }
73 }
74 
75 template <typename TPixel, unsigned int VImageDimension>
76 void mitk::ImageToPointCloudFilter::StdDeviations(itk::Image<TPixel, VImageDimension> *image, int amount)
77 {
78  typedef itk::Image<TPixel, VImageDimension> InputImageType;
79  typedef itk::CastImageFilter<InputImageType, FloatImageType> ImagePTypeToFloatPTypeCasterType;
80  typedef itk::LaplacianImageFilter<FloatImageType, FloatImageType> LaplacianFilterType;
82 
84  caster->SetInput(image);
85  caster->Update();
86  FloatImageType::Pointer fImage = caster->GetOutput();
87 
88  lapFilter->SetInput(fImage);
89  lapFilter->UpdateLargestPossibleRegion();
90  mitk::Image::Pointer edgeImage = mitk::ImportItkImage(lapFilter->GetOutput());
91 
94  statCalc->SetInputImage(edgeImage);
95  mitk::ImageStatisticsCalculator::StatisticsContainer::Pointer stats = statCalc->GetStatistics();
96  double mean = stats->GetMean();
97  double stdDev = stats->GetStd();
98 
99  double upperThreshold = mean + stdDev * amount;
100  double lowerThreshold = mean - stdDev * amount;
101 
102  typename itk::ImageRegionIterator<FloatImageType> it(lapFilter->GetOutput(),
103  lapFilter->GetOutput()->GetRequestedRegion());
104 
105  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
106 
107  double greatX = 0, greatY = 0, greatZ = 0;
108 
109  it.GoToBegin();
110  while (!it.IsAtEnd())
111  {
112  if (it.Get() > lowerThreshold && it.Get() < upperThreshold)
113  {
114  it.Set(0);
115  }
116  else
117  {
118  it.Set(1);
119 
120  mitk::Point3D imagePoint;
121  mitk::Point3D worldPoint;
122 
123  imagePoint[0] = it.GetIndex()[0];
124  imagePoint[1] = it.GetIndex()[1];
125  imagePoint[2] = it.GetIndex()[2];
126 
127  m_Geometry->IndexToWorld(imagePoint, worldPoint);
128 
129  if (worldPoint[0] > greatX)
130  greatX = worldPoint[0];
131  if (worldPoint[1] > greatY)
132  greatY = worldPoint[1];
133  if (worldPoint[2] > greatZ)
134  greatZ = worldPoint[2];
135 
136  points->InsertNextPoint(worldPoint[0], worldPoint[1], worldPoint[2]);
137  m_NumberOfExtractedPoints++;
138  }
139  ++it;
140  }
141 
142  /*need to build the UnstructuredGrid with at least one vertex otherwise its
143  not visible*/
144  vtkSmartPointer<vtkPolyVertex> verts = vtkSmartPointer<vtkPolyVertex>::New();
145 
146  verts->GetPointIds()->SetNumberOfIds(m_NumberOfExtractedPoints);
147  for (int i = 0; i < m_NumberOfExtractedPoints; i++)
148  {
149  verts->GetPointIds()->SetId(i, i);
150  }
151 
152  vtkSmartPointer<vtkUnstructuredGrid> uGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();
153  uGrid->Allocate(1);
154 
155  uGrid->InsertNextCell(verts->GetCellType(), verts->GetPointIds());
156  uGrid->SetPoints(points);
157 
159  outputGrid->SetVtkUnstructuredGrid(uGrid);
160  this->SetNthOutput(0, outputGrid);
161 }
162 
164 {
165  Superclass::GenerateOutputInformation();
166 }
itk::SmartPointer< Self > Pointer
#define MITK_ERROR
Definition: mitkLogMacros.h:24
#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...
itk::Image< double, 3 > InputImageType
Image class for storing images.
Definition: mitkImage.h:76
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.