Medical Imaging Interaction Toolkit  2018.4.99-eed36131
Medical Imaging Interaction Toolkit
mitkBoundingObjectToSegmentationFilter.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 #include "mitkImageCast.h"
14 
15 #include <itkImageRegionIteratorWithIndex.h>
16 
18 {
19  this->SetNumberOfRequiredInputs(1);
20 }
21 
23 {
24 }
25 
27 {
28  auto *testgroup = dynamic_cast<mitk::BoundingObjectGroup *>(boundingObject.GetPointer());
29  if (testgroup)
30  m_boundingObjectGroup = testgroup;
31  else
32  {
34  m_boundingObjectGroup->AddBoundingObject(boundingObject);
35  }
36 }
37 
39 {
40  typedef itk::Image<unsigned char, 3> itkImageType;
41  mitk::Image::Pointer outputImage = this->GetOutput();
42  mitk::Image::ConstPointer inputImage = this->GetInput();
43  outputImage->Initialize(inputImage);
44 
45  itkImageType::Pointer itkImage;
46  CastToItkImage(outputImage, itkImage);
47  itkImage->FillBuffer(0);
48 
49  for (unsigned int i = 0; i < m_boundingObjectGroup->GetCount(); i++)
50  {
51  // create region for boundingobject
52  mitk::BoundingObject::Pointer boundingObject = m_boundingObjectGroup->GetBoundingObjects().at(i);
53  mitk::BaseGeometry::Pointer boGeometry = boundingObject->GetGeometry();
54  mitk::BaseGeometry::Pointer inputImageGeometry = inputImage->GetSlicedGeometry();
55  mitk::BoundingBox::Pointer boToIm =
56  boGeometry->CalculateBoundingBoxRelativeToTransform(inputImageGeometry->GetIndexToWorldTransform());
57 
58  mitk::BoundingBox::ConstPointer imgBB = inputImageGeometry->GetBoundingBox();
59  mitk::BoundingBox::PointType minImg = imgBB->GetMinimum();
60  mitk::BoundingBox::PointType maxImg = imgBB->GetMaximum();
61 
62  itkImageType::IndexType boIndex;
63  itkImageType::SizeType boSize;
64 
65  mitk::BoundingBox::PointType min = boToIm->GetMinimum();
66  mitk::BoundingBox::PointType max = boToIm->GetMaximum();
67 
68  // check if boundingbox is inside imageregion
69  for (int i = 0; i < 3; i++)
70  {
71  if (min[i] < minImg[i])
72  min[i] = minImg[i];
73  if (max[i] < minImg[i])
74  max[i] = minImg[i];
75  if (max[i] > maxImg[i])
76  max[i] = maxImg[i];
77  if (min[i] > maxImg[i])
78  min[i] = maxImg[i] - 1;
79  }
80 
81  // add 0.5 (boGeometry is no image geometry)
82  boIndex[0] = (mitk::SlicedData::IndexType::IndexValueType)(min[0] + 0.5);
83  boIndex[1] = (mitk::SlicedData::IndexType::IndexValueType)(min[1] + 0.5);
84  boIndex[2] = (mitk::SlicedData::IndexType::IndexValueType)(min[2] + 0.5);
85 
86  // add 1 because we need 0.5 for each index
87  boSize[0] = (mitk::SlicedData::IndexType::IndexValueType)(max[0] - min[0]);
88  boSize[1] = (mitk::SlicedData::IndexType::IndexValueType)(max[1] - min[1]);
89  boSize[2] = (mitk::SlicedData::IndexType::IndexValueType)(max[2] - min[2]);
90 
91  itkImageType::RegionType region(boIndex, boSize);
92 
93  // create region iterator
94  itk::ImageRegionIteratorWithIndex<itkImageType> itBoundingObject =
95  itk::ImageRegionIteratorWithIndex<itkImageType>(itkImage, region);
96  itBoundingObject.GoToBegin();
97 
98  while (!itBoundingObject.IsAtEnd())
99  {
100  itkImageType::IndexType index = itBoundingObject.GetIndex();
101  mitk::Point3D p;
102  p[0] = index[0];
103  p[1] = index[1];
104  p[2] = index[2];
105  inputImageGeometry->IndexToWorld(p, p);
106 
107  if (boundingObject->IsInside(p) && boundingObject->GetPositive())
108  itBoundingObject.Set(1);
109  else if (boundingObject->IsInside(p) && !boundingObject->GetPositive())
110  itBoundingObject.Set(0);
111 
112  ++itBoundingObject;
113  }
114  }
115  CastToMitkImage(itkImage, outputImage);
116 }
static Pointer New()
group object, that contains several mitk::BoundingObjects
static T max(T x, T y)
Definition: svm.cpp:56
static T min(T x, T y)
Definition: svm.cpp:53
InputImageType * GetInput(void)
void SetBoundingObject(mitk::BoundingObject::Pointer boundingObject)
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.
OutputType * GetOutput()
Get the output data of this image source object.