Medical Imaging Interaction Toolkit  2018.4.99-e1268d66
Medical Imaging Interaction Toolkit
mitkCollectionGrayOpening.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 <mitkITKImageImport.h>
15 #include <mitkImageCast.h>
16 
17 #include "itkBinaryBallStructuringElement.h"
18 #include "itkBinaryDilateImageFilter.h"
19 #include "itkFlatStructuringElement.h"
20 #include "itkGrayscaleMorphologicalOpeningImageFilter.h"
21 
23  std::string name,
24  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 << "PerformGrayOpening - 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 itkImage = BinaryImage::New();
43  ImageType::Pointer itkImage = ImageType::New();
44  Image::Pointer tmp = dataTimeStep->GetMitkImage(name).GetPointer();
45  if (tmp.IsNull())
46  MITK_ERROR << "null";
47  CastToItkImage(tmp, itkImage);
48  if (itkImage.IsNull())
49  MITK_ERROR << "Image " << name << " does not exist. Fatal.";
50 
51  typedef itk::FlatStructuringElement<3> StructuringElementType;
52  StructuringElementType::RadiusType elementRadius;
53  elementRadius.Fill(1);
54  elementRadius[2] = 0;
55  StructuringElementType structuringElement = StructuringElementType::Box(elementRadius);
56 
57  typedef itk::GrayscaleMorphologicalOpeningImageFilter<ImageType, ImageType, StructuringElementType>
58  DilateImageFilterType;
59 
60  DilateImageFilterType::Pointer dilateFilter0 = DilateImageFilterType::New();
61  dilateFilter0->SetInput(itkImage);
62  dilateFilter0->SetKernel(structuringElement);
63  dilateFilter0->Update();
64 
65  DilateImageFilterType::Pointer dilateFilter1 = DilateImageFilterType::New();
66  dilateFilter1->SetInput(dilateFilter0->GetOutput());
67  dilateFilter1->SetKernel(structuringElement);
68  dilateFilter1->Update();
69 
70  Image::Pointer dil = GrabItkImageMemory(dilateFilter1->GetOutput());
71  dataTimeStep->AddData(dil.GetPointer(), name + suffix, "");
72  }
73  }
74 }
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.
static void PerformGrayOpening(mitk::DataCollection *dataCollection, std::string name, std::string suffix)
PerformGrayOpening - Opening operation on a specific modality type wihtin the DataCollection. Creates a new item.
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.