Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkLabelSetImageConverter.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 ===================================================================*/
16 
17 #include <mitkITKImageImport.h>
18 #include <mitkImageAccessByItk.h>
19 #include <mitkImageCast.h>
21 
22 #include <itkComposeImageFilter.h>
23 #include <itkExtractImageFilter.h>
24 #include <itkImageDuplicator.h>
25 #include <itkVectorIndexSelectionCastImageFilter.h>
26 
27 template <typename TPixel, unsigned int VDimension>
28 static void ConvertLabelSetImageToImage(const itk::Image<TPixel, VDimension> *,
30  mitk::Image::Pointer &image)
31 {
32  typedef itk::Image<TPixel, VDimension> ImageType;
33  typedef itk::ComposeImageFilter<ImageType> ComposeFilterType;
34  typedef itk::ImageDuplicator<ImageType> DuplicatorType;
35 
36  auto numberOfLayers = labelSetImage->GetNumberOfLayers();
37 
38  if (numberOfLayers > 1)
39  {
40  auto vectorImageComposer = ComposeFilterType::New();
41  auto activeLayer = labelSetImage->GetActiveLayer();
42 
43  for (decltype(numberOfLayers) layer = 0; layer < numberOfLayers; ++layer)
44  {
45  auto layerImage = mitk::ImageToItkImage<TPixel, VDimension>(
46  layer != activeLayer ? labelSetImage->GetLayerImage(layer) : labelSetImage);
47 
48  vectorImageComposer->SetInput(layer, layerImage);
49  }
50 
51  vectorImageComposer->Update();
52  // mitk::GrabItkImageMemory does not support 4D, this will handle 4D correctly
53  // and create a memory managed copy
54  image = mitk::ImportItkImage(vectorImageComposer->GetOutput())->Clone();
55  }
56  else
57  {
58  auto layerImage = mitk::ImageToItkImage<TPixel, VDimension>(labelSetImage);
59 
60  auto duplicator = DuplicatorType::New();
61  duplicator->SetInputImage(layerImage);
62  duplicator->Update();
63 
64  // mitk::GrabItkImageMemory does not support 4D, this will handle 4D correctly
65  // and create a memory managed copy
66  image = mitk::ImportItkImage(duplicator->GetOutput())->Clone();
67  }
68 }
69 
71 {
72  Image::Pointer image;
73 
74  if (labelSetImage->GetNumberOfLayers() > 0)
75  {
76  if (labelSetImage->GetDimension() == 4)
77  {
78  AccessFixedDimensionByItk_n(labelSetImage, ::ConvertLabelSetImageToImage, 4, (labelSetImage, image));
79  }
80  else
81  {
82  AccessByItk_2(labelSetImage->GetLayerImage(0), ::ConvertLabelSetImageToImage, labelSetImage, image);
83  }
84  }
85 
86  return image;
87 }
88 
89 template <typename TPixel, unsigned int VDimensions>
90 static void ConvertImageToLabelSetImage(const itk::VectorImage<TPixel, VDimensions> *image,
91  mitk::LabelSetImage::Pointer &labelSetImage)
92 {
93  typedef itk::VectorImage<TPixel, VDimensions> VectorImageType;
94  typedef itk::Image<TPixel, VDimensions> ImageType;
95  typedef itk::VectorIndexSelectionCastImageFilter<VectorImageType, ImageType> VectorIndexSelectorType;
96 
97  labelSetImage = mitk::LabelSetImage::New();
98 
99  auto numberOfLayers = image->GetVectorLength();
100  for (decltype(numberOfLayers) layer = 0; layer < numberOfLayers; ++layer)
101  {
102  auto layerSelector = VectorIndexSelectorType::New();
103  layerSelector->SetInput(image);
104  layerSelector->SetIndex(layer);
105  layerSelector->Update();
106 
107  mitk::Image::Pointer layerImage;
108  mitk::CastToMitkImage(layerSelector->GetOutput(), layerImage);
109 
110  if (layer == 0)
111  {
112  labelSetImage->InitializeByLabeledImage(layerImage);
113  }
114  else
115  {
116  labelSetImage->AddLayer(layerImage);
117  }
118  }
119 }
120 
122 {
123  LabelSetImage::Pointer labelSetImage;
124 
125  if (image.IsNotNull())
126  {
127  if (image->GetChannelDescriptor().GetPixelType().GetPixelType() == itk::ImageIOBase::VECTOR)
128  {
129  if (4 == image->GetDimension())
130  {
132  }
133  else
134  {
136  }
137  }
138  else
139  {
140  labelSetImage = mitk::LabelSetImage::New();
141  labelSetImage->InitializeByLabeledImage(image);
142  }
143  }
144 
145  return labelSetImage;
146 }
MITKMULTILABEL_EXPORT Image::Pointer ConvertLabelSetImageToImage(LabelSetImage::ConstPointer labelSetImage)
Convert mitk::LabelSetImage to mitk::Image (itk::VectorImage)
itk::Image< unsigned char, 3 > ImageType
#define AccessFixedDimensionByItk_n(mitkImage, itkImageTypeFunction, dimension, va_tuple)
Access a mitk-image with known dimension by an itk-image with one or more parameters.
static void ConvertLabelSetImageToImage(const itk::Image< TPixel, VDimension > *, mitk::LabelSetImage::ConstPointer labelSetImage, mitk::Image::Pointer &image)
MITKMULTILABEL_EXPORT LabelSetImage::Pointer ConvertImageToLabelSetImage(Image::Pointer image)
Convert mitk::Image to mitk::LabelSetImage, templating and differentation between itk::Image and itk:...
Image::Pointer ImportItkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, const BaseGeometry *geometry=nullptr, bool update=true)
Imports an itk::Image (with a specific type) as an mitk::Image.Instantiates instance of ITKImageImpor...
itk::VectorImage< float, 3 > VectorImageType
#define AccessVectorPixelTypeByItk_n(mitkImage, itkImageTypeFunction, va_tuple)
Access a vector mitk::Image by an ITK vector image with one or more parameters.
static Pointer New()
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
#define AccessByItk_2(mitkImage, itkImageTypeFunction, arg1, arg2)
static mitk::PlanarFigure::Pointer Clone(mitk::PlanarFigure::Pointer original)
static void ConvertImageToLabelSetImage(const itk::VectorImage< TPixel, VDimensions > *image, mitk::LabelSetImage::Pointer &labelSetImage)
#define AccessVectorFixedDimensionByItk_n(mitkImage, itkImageTypeFunction, dimension, va_tuple)
Access a vector mitk-image with known dimension by a ITK vector image with one or more parameters...
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.