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