Medical Imaging Interaction Toolkit  2018.4.99-c4b6bb11
Medical Imaging Interaction Toolkit
mitkCropImageFilter.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 "mitkCropImageFilter.h"
14 #include <mitkImageReadAccessor.h>
15 #include <mitkImageWriteAccessor.h>
16 
18  m_XPixelsCropStart(0),
19  m_YPixelsCropStart(0),
20  m_ZPixelsCropStart(0),
21  m_XPixelsCropEnd(0),
22  m_YPixelsCropEnd(0),
23  m_ZPixelsCropEnd(0)
24 {
25  MITK_INFO << "Instantiating CropImageFilter...";
26  SetNumberOfIndexedInputs(1);
27  SetNumberOfIndexedOutputs(1);
28  MITK_INFO << "Instantiating CropImageFilter...[Done]";
29 }
30 
32 {
33  MITK_INFO << "Destructed CastToFloatImageFilter.";
34 }
35 
37 {
38  mitk::Image::Pointer inputImage = GetInput();
39 
40  std::string type = inputImage->GetPixelType().GetTypeAsString();
41  if (!(type == "scalar (float)" || type == " (float)"))
42  {
43  MITK_ERROR << "This filter can currently only handle float type images.";
44  mitkThrow() << "This filter can currently only handle float type images.";
45  }
46 
47  if (m_XPixelsCropStart + m_XPixelsCropEnd >= inputImage->GetDimension(0))
48  {
49  MITK_ERROR << "X Crop area too large for selected input image";
50  mitkThrow() << "X Crop area too large for selected input image";
51  }
52  if (m_YPixelsCropStart + m_YPixelsCropEnd >= inputImage->GetDimension(1))
53  {
54  MITK_ERROR << "Y Crop area too large for selected input image";
55  mitkThrow() << "Y Crop area too large for selected input image";
56  }
57 
58  if (inputImage->GetDimension() == 3)
59  {
60  if (m_ZPixelsCropStart + m_ZPixelsCropEnd >= inputImage->GetDimension(2))
61  {
62  MITK_INFO << m_ZPixelsCropStart + m_ZPixelsCropEnd << "vs" << inputImage->GetDimension(2);
63  MITK_ERROR << "Z Crop area too large for selected input image";
64  mitkThrow() << "Z Crop area too large for selected input image";
65  }
66  }
67  else
68  {
70  {
71  mitkThrow() << "Z Crop area too large for selected input image";
72  }
73  }
74 }
75 
77 {
78  mitk::Image::Pointer inputImage = GetInput();
79  mitk::Image::Pointer outputImage = GetOutput();
80 
82  unsigned int* outputDim;
83  unsigned int* inputDim = inputImage->GetDimensions();
84  if (inputImage->GetDimension() == 2)
85  outputDim = new unsigned int[2]{ inputImage->GetDimension(0) - m_XPixelsCropStart - m_XPixelsCropEnd,
86  inputImage->GetDimension(1) - m_YPixelsCropStart - m_YPixelsCropEnd };
87  else
88  outputDim = new unsigned int[3]{ inputImage->GetDimension(0) - m_XPixelsCropStart - m_XPixelsCropEnd,
89  inputImage->GetDimension(1) - m_YPixelsCropStart - m_YPixelsCropEnd,
90  inputImage->GetDimension(2) - m_ZPixelsCropStart - m_ZPixelsCropEnd };
91 
92  outputImage->Initialize(mitk::MakeScalarPixelType<float>(), inputImage->GetDimension(), outputDim);
93  outputImage->SetSpacing(inputImage->GetGeometry()->GetSpacing());
94 
95  ImageReadAccessor accR(inputImage);
96  const float* inputData = (const float*)(accR.GetData());
97 
98  ImageWriteAccessor accW(outputImage);
99  float* outputData = (float*)(accW.GetData());
100 
101  unsigned int zLength = inputImage->GetDimension() == 3 ? outputDim[2] : 1;
102 
103  for (unsigned int sl = 0; sl < zLength; ++sl)
104  {
105  for (unsigned int l = 0; l < outputDim[0]; ++l)
106  {
107  for (unsigned int s = 0; s < outputDim[1]; ++s)
108  {
109  outputData[l + s*outputDim[0] + sl*outputDim[0] * outputDim[1]] =
110  inputData[(l + m_XPixelsCropStart) + (s + m_YPixelsCropStart)*inputDim[0] +
111  (sl + m_ZPixelsCropStart)*inputDim[0] * inputDim[1]];
112  }
113  }
114  }
115  delete[] outputDim;
116 }
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_ERROR
Definition: mitkLogMacros.h:20
void * GetData()
Gives full data access.
#define mitkThrow()
InputImageType * GetInput(void)
OutputType * GetOutput()
Get the output data of this image source object.
ImageWriteAccessor class to get locked write-access for a particular image part.
ImageReadAccessor class to get locked read access for a particular image part.
const void * GetData() const
Gives const access to the data.