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