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
mitkImageToContourModelFilter.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 #include "mitkImageAccessByItk.h"
19 
20 #include <itkConstantPadImageFilter.h>
22 
23 mitk::ImageToContourModelFilter::ImageToContourModelFilter() : m_SliceGeometry(nullptr), m_ContourValue(0.5)
24 {
25 }
26 
28 {
29 }
30 
32 {
33  this->SetInput(0, input);
34 }
35 
38 {
39  if (idx + 1 > this->GetNumberOfInputs())
40  {
41  this->SetNumberOfRequiredInputs(idx + 1);
42  }
43  if (input != static_cast<InputType *>(this->ProcessObject::GetInput(idx)))
44  {
45  this->ProcessObject::SetNthInput(idx, const_cast<InputType *>(input));
46  this->Modified();
47  }
48 }
49 
51 {
52  if (this->GetNumberOfInputs() < 1)
53  return nullptr;
54  return static_cast<const mitk::ImageToContourModelFilter::InputType *>(this->ProcessObject::GetInput(0));
55 }
56 
58 {
59  if (this->GetNumberOfInputs() < 1)
60  return nullptr;
61  return static_cast<const mitk::ImageToContourModelFilter::InputType *>(this->ProcessObject::GetInput(idx));
62 }
63 
65 {
66  this->m_ContourValue = contourValue;
67  this->Modified();
68 }
69 
71 {
72  return this->m_ContourValue;
73 }
74 
76 {
77  mitk::Image::ConstPointer sliceImage = this->GetInput();
78 
79  if (!sliceImage)
80  {
81  MITK_ERROR << "mitk::ImageToContourModelFilter: No input available. Please set the input!" << std::endl;
82  itkExceptionMacro("mitk::ImageToContourModelFilter: No input available. Please set the input!");
83  return;
84  }
85 
86  if (sliceImage->GetDimension() > 2 || sliceImage->GetDimension() < 2)
87  {
88  MITK_ERROR << "mitk::ImageToContourModelFilter::GenerateData() works only with 2D images. Please assure that your "
89  "input image is 2D!"
90  << std::endl;
91  itkExceptionMacro("mitk::ImageToContourModelFilter::GenerateData() works only with 2D images. Please assure that "
92  "your input image is 2D!");
93  return;
94  }
95 
96  m_SliceGeometry = sliceImage->GetGeometry();
97 
98  AccessFixedDimensionByItk(sliceImage, Itk2DContourExtraction, 2);
99 }
100 
101 template <typename TPixel, unsigned int VImageDimension>
102 void mitk::ImageToContourModelFilter::Itk2DContourExtraction(const itk::Image<TPixel, VImageDimension> *sliceImage)
103 {
104  typedef itk::Image<TPixel, VImageDimension> ImageType;
105  typedef itk::ContourExtractor2DImageFilter<ImageType> ContourExtractor;
106 
107  typedef itk::PolyLineParametricPath<2> PolyLineParametricPath2D;
108  typedef PolyLineParametricPath2D::VertexListType ContourPath;
109 
110  typedef itk::ConstantPadImageFilter<ImageType, ImageType> PadFilterType;
111  typename PadFilterType::Pointer padFilter = PadFilterType::New();
112  typename ImageType::SizeType lowerExtendRegion;
113  lowerExtendRegion[0] = 1;
114  lowerExtendRegion[1] = 1;
115 
116  typename ImageType::SizeType upperExtendRegion;
117  upperExtendRegion[0] = 1;
118  upperExtendRegion[1] = 1;
119 
120  /*
121  * We need to pad here, since the ITK contour extractor fails if the
122  * segmentation touches more than one image edge.
123  * By padding the image for one row at each edge we overcome this issue
124  */
125  padFilter->SetInput(sliceImage);
126  padFilter->SetConstant(0);
127  padFilter->SetPadLowerBound(lowerExtendRegion);
128  padFilter->SetPadUpperBound(upperExtendRegion);
129 
130  typename ContourExtractor::Pointer contourExtractor = ContourExtractor::New();
131  contourExtractor->SetInput(padFilter->GetOutput());
132  contourExtractor->SetContourValue(m_ContourValue);
133 
134  contourExtractor->Update();
135 
136  unsigned int foundPaths = contourExtractor->GetNumberOfOutputs();
137  this->SetNumberOfIndexedOutputs(foundPaths);
138 
139  for (unsigned int i = 0; i < foundPaths; i++)
140  {
141  const ContourPath *currentPath = contourExtractor->GetOutput(i)->GetVertexList();
142 
143  mitk::Point3D currentPoint;
144  mitk::Point3D currentWorldPoint;
145 
146  mitk::ContourModel::Pointer contour = this->GetOutput(i);
147  if (contour.IsNull())
148  {
149  contour = mitk::ContourModel::New();
150  }
151 
152  if (contour.IsNull())
153  contour = mitk::ContourModel::New();
154 
155  for (unsigned int j = 0; j < currentPath->Size(); j++)
156  {
157  currentPoint[0] = currentPath->ElementAt(j)[0];
158  currentPoint[1] = currentPath->ElementAt(j)[1];
159  currentPoint[2] = 0;
160 
161  m_SliceGeometry->IndexToWorld(currentPoint, currentWorldPoint);
162 
163  contour->AddVertex(currentWorldPoint);
164  } // for2
165 
166  contour->Close();
167 
168  } // for1
169 }
itk::SmartPointer< Self > Pointer
#define AccessFixedDimensionByItk(mitkImage, itkImageTypeFunction, dimension)
Access a mitk-image with known dimension by an itk-image.
#define MITK_ERROR
Definition: mitkLogMacros.h:24
void Itk2DContourExtraction(const itk::Image< TPixel, VImageDimension > *sliceImage)
map::core::discrete::Elements< 3 >::InternalImageType ImageType
Image class for storing images.
Definition: mitkImage.h:76
static Pointer New()
virtual void SetInput(const InputType *input)
Computes a list of PolyLineParametricPath objects from the contours in a 2D image.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.