Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPadImageFilter.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 
13 #include "mitkPadImageFilter.h"
14 #include "mitkImageCast.h"
15 
16 #include "itkBinaryThresholdImageFilter.h"
17 #include "itkConstantPadImageFilter.h"
18 
20 {
21  this->SetNumberOfIndexedInputs(2);
22  this->SetNumberOfRequiredInputs(2);
23  m_BinaryFilter = false;
24 
25  m_PadConstant = -32766;
26  m_LowerThreshold = -32766;
27  m_UpperThreshold = -32765;
28 }
29 
31 {
32 }
33 
35 {
37  mitk::Image::ConstPointer referenceImage = this->GetInput(1);
38 
39  typedef itk::Image<short, 3> ImageType;
40  ImageType::Pointer itkImage = ImageType::New();
41  mitk::CastToItkImage(image, itkImage);
42 
43  mitk::BaseGeometry *imageGeometry = image->GetGeometry();
44  mitk::Point3D origin = imageGeometry->GetOrigin();
45  mitk::Vector3D spacing = imageGeometry->GetSpacing();
46 
47  mitk::BaseGeometry *referenceImageGeometry = referenceImage->GetGeometry();
48  mitk::Point3D referenceOrigin = referenceImageGeometry->GetOrigin();
49 
50  double outputOrigin[3];
51  itk::SizeValueType padLowerBound[3];
52  itk::SizeValueType padUpperBound[3];
53 
54  int i;
55  for (i = 0; i < 3; ++i)
56  {
57  outputOrigin[i] = referenceOrigin[i];
58 
59  padLowerBound[i] = static_cast<unsigned long>((origin[i] - referenceOrigin[i]) / spacing[i] + 0.5);
60 
61  padUpperBound[i] = referenceImage->GetDimension(i) - image->GetDimension(i) - padLowerBound[i];
62  }
63 
64  // The origin of the input image is passed through the filter and used as
65  // output origin as well. Hence, it needs to be overwritten accordingly.
66  itkImage->SetOrigin(outputOrigin);
67 
68  typedef itk::ConstantPadImageFilter<ImageType, ImageType> PadFilterType;
69  PadFilterType::Pointer padFilter = PadFilterType::New();
70  padFilter->SetInput(itkImage);
71  padFilter->SetConstant(m_PadConstant);
72  padFilter->SetPadLowerBound(padLowerBound);
73  padFilter->SetPadUpperBound(padUpperBound);
74 
75  mitk::Image::Pointer outputImage = this->GetOutput();
76 
77  // If the Binary flag is set, use an additional binary threshold filter after
78  // padding.
79  if (m_BinaryFilter)
80  {
81  typedef itk::Image<unsigned char, 3> BinaryImageType;
82  typedef itk::BinaryThresholdImageFilter<ImageType, BinaryImageType> BinaryFilterType;
83  BinaryFilterType::Pointer binaryFilter = BinaryFilterType::New();
84 
85  binaryFilter->SetInput(padFilter->GetOutput());
86  binaryFilter->SetLowerThreshold(m_LowerThreshold);
87  binaryFilter->SetUpperThreshold(m_UpperThreshold);
88  binaryFilter->SetInsideValue(1);
89  binaryFilter->SetOutsideValue(0);
90  binaryFilter->Update();
91 
92  mitk::CastToMitkImage(binaryFilter->GetOutput(), outputImage);
93  }
94  else
95  {
96  padFilter->Update();
97  mitk::CastToMitkImage(padFilter->GetOutput(), outputImage);
98  }
99 
100  outputImage->SetRequestedRegionToLargestPossibleRegion();
101 }
void GenerateData() override
itk::Image< unsigned char, 3 > ImageType
mitk::Image::Pointer image
const Point3D GetOrigin() const
Get the origin, e.g. the upper-left corner of the plane.
InputImageType * GetInput(void)
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
Definition: mitkImageCast.h:74
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
const mitk::Vector3D GetSpacing() const
Get the spacing (size of a pixel).
OutputType * GetOutput()
Get the output data of this image source object.
BaseGeometry Describes the geometry of a data object.