Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkCollectionDilatation.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 ============================================================================*/
13 
14 #include <mitkITKImageImport.h>
15 #include <mitkImageCast.h>
16 
17 #include "itkBinaryBallStructuringElement.h"
18 #include "itkBinaryDilateImageFilter.h"
19 #include "itkBinaryErodeImageFilter.h"
20 #include "itkFlatStructuringElement.h"
21 #include "itkGrayscaleDilateImageFilter.h"
22 
24  mitk::DataCollection *dataCollection, std::string name, unsigned int xy, unsigned int z, std::string suffix)
25 {
26  for (size_t patient = 0; patient < dataCollection->Size(); ++patient)
27  {
28  DataCollection *dataPatient = dynamic_cast<DataCollection *>(dataCollection->GetData(patient).GetPointer());
29  if (dataPatient == nullptr)
30  MITK_ERROR << "DilateBinaryByName - Structure of DataCollection is invalid at patient level. Data inconsistent!";
31 
32  if (dataPatient->Size() == 0)
33  MITK_ERROR << "Empty Patient Collective. Probably Fatal.";
34 
35  for (size_t timeStep = 0; timeStep < dataPatient->Size(); ++timeStep)
36  {
37  DataCollection *dataTimeStep = dynamic_cast<DataCollection *>(dataPatient->GetData(timeStep).GetPointer());
38  if (dataTimeStep == nullptr)
40  << "DilateBinaryByName- Structure of DataCollection is invalid at time step level. Data inconsistent!";
41 
42  BinaryImage::Pointer itkTumorSeed = BinaryImage::New();
43  Image *tumorSeed = dynamic_cast<Image *>(dataTimeStep->GetMitkImage(name).GetPointer());
44  if (tumorSeed == nullptr)
45  MITK_ERROR << "Image " << name << " does not exits. Fatal.";
46 
47  CastToItkImage(tumorSeed, itkTumorSeed);
48 
49  typedef itk::FlatStructuringElement<3> StructuringElementType;
50  StructuringElementType::RadiusType elementRadius;
51  elementRadius.Fill(xy);
52  elementRadius[2] = z;
53 
54  StructuringElementType structuringElement = StructuringElementType::Box(elementRadius);
55  typedef itk::BinaryDilateImageFilter<BinaryImage, BinaryImage, StructuringElementType>
56  BinaryDilateImageFilterType;
57 
58  BinaryDilateImageFilterType::Pointer dilateFilter0 = BinaryDilateImageFilterType::New();
59  dilateFilter0->SetInput(itkTumorSeed);
60  dilateFilter0->SetKernel(structuringElement);
61  dilateFilter0->SetForegroundValue(1);
62  dilateFilter0->Update();
63 
64  Image::Pointer dil = GrabItkImageMemory(dilateFilter0->GetOutput());
65 
66  dil->SetGeometry(tumorSeed->GetGeometry());
67  dataTimeStep->AddData(dil.GetPointer(), name + suffix, "Dilated Binary");
68  }
69  }
70 }
71 
73  mitk::DataCollection *dataCollection, std::string name, unsigned int xy, unsigned int z, std::string suffix)
74 {
75  for (size_t patient = 0; patient < dataCollection->Size(); ++patient)
76  {
77  DataCollection *dataPatient = dynamic_cast<DataCollection *>(dataCollection->GetData(patient).GetPointer());
78  if (dataPatient == nullptr)
79  MITK_ERROR << "DilateBinaryByName - Structure of DataCollection is invalid at patient level. Data inconsistent!";
80 
81  if (dataPatient->Size() == 0)
82  MITK_ERROR << "Empty Patient Collective. Probably Fatal.";
83 
84  for (size_t timeStep = 0; timeStep < dataPatient->Size(); ++timeStep)
85  {
86  DataCollection *dataTimeStep = dynamic_cast<DataCollection *>(dataPatient->GetData(timeStep).GetPointer());
87  if (dataTimeStep == nullptr)
89  << "DilateBinaryByName- Structure of DataCollection is invalid at time step level. Data inconsistent!";
90 
91  BinaryImage::Pointer itkTumorSeed = BinaryImage::New();
92  Image *tumorSeed = dynamic_cast<Image *>(dataTimeStep->GetMitkImage(name).GetPointer());
93  if (tumorSeed == nullptr)
94  MITK_ERROR << "Image " << name << " does not exits. Fatal.";
95 
96  CastToItkImage(tumorSeed, itkTumorSeed);
97 
98  typedef itk::FlatStructuringElement<3> StructuringElementType;
99  StructuringElementType::RadiusType elementRadius;
100  elementRadius.Fill(xy);
101  elementRadius[2] = z;
102 
103  StructuringElementType structuringElement = StructuringElementType::Box(elementRadius);
104 
105  typedef itk::BinaryErodeImageFilter<BinaryImage, BinaryImage, StructuringElementType> BinaryErodeImageFilterType;
106 
107  BinaryErodeImageFilterType::Pointer dilateFilter0 = BinaryErodeImageFilterType::New();
108  dilateFilter0->SetInput(itkTumorSeed);
109  dilateFilter0->SetKernel(structuringElement);
110  dilateFilter0->SetForegroundValue(1);
111  dilateFilter0->Update();
112 
113  Image::Pointer dil = GrabItkImageMemory(dilateFilter0->GetOutput());
114 
115  dil->SetGeometry(tumorSeed->GetGeometry());
116  dataTimeStep->AddData(dil.GetPointer(), name + suffix, "Dilated Binary");
117  }
118  }
119 }
static void ErodeBinaryByName(DataCollection *dataCollection, std::string name, unsigned int xy=5, unsigned int z=0, std::string suffix="ERODE")
ErodeBinaryByName - Erode all occurances of a modality within a mitk::DataCollection.
vcl_size_t Size() const
Size - number of data items in collection.
#define MITK_ERROR
Definition: mitkLogMacros.h:20
vcl_size_t AddData(DataObject::Pointer data, std::string name, std::string filePath="")
AddData Add a data item.
mitk::Image::Pointer GetMitkImage(vcl_size_t index)
GetMitkImage - casts data to mitk::Image and returns it.
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grabs the memory of an itk::Image (with a specific type) and puts it into an mitk::Image.The memory is managed by the mitk::Image after calling this function. The itk::Image remains valid until the mitk::Image decides to free the memory.
Image class for storing images.
Definition: mitkImage.h:72
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
itk::DataObject::Pointer GetData(vcl_size_t index)
GetData Get original data by index.
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
Definition: mitkBaseData.h:138
static void DilateBinaryByName(DataCollection *dataCollection, std::string name, unsigned int xy=5, unsigned int z=0, std::string suffix="DILATE")
DilateBinaryByName - Dilate all occurances of a modality within a mitk::DataCollection.