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