Medical Imaging Interaction Toolkit  2018.4.99-9a29ffc6
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 (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 
13 #include <mitkITKImageImport.h>
14 #include <mitkImageAccessByItk.h>
15 #include <mitkImageCast.h>
17 
18 #include <itkComposeImageFilter.h>
19 #include <itkExtractImageFilter.h>
20 #include <itkImageDuplicator.h>
21 #include <itkVectorIndexSelectionCastImageFilter.h>
22 
23 template <typename TPixel, unsigned int VDimension>
24 static void ConvertLabelSetImageToImage(const itk::Image<TPixel, VDimension> *,
27 {
28  typedef itk::Image<TPixel, VDimension> ImageType;
29  typedef itk::ComposeImageFilter<ImageType> ComposeFilterType;
30  typedef itk::ImageDuplicator<ImageType> DuplicatorType;
31 
32  auto numberOfLayers = labelSetImage->GetNumberOfLayers();
33 
34  if (numberOfLayers > 1)
35  {
36  auto vectorImageComposer = ComposeFilterType::New();
37  auto activeLayer = labelSetImage->GetActiveLayer();
38 
39  for (decltype(numberOfLayers) layer = 0; layer < numberOfLayers; ++layer)
40  {
41  auto layerImage = mitk::ImageToItkImage<TPixel, VDimension>(
42  layer != activeLayer ? labelSetImage->GetLayerImage(layer) : labelSetImage);
43 
44  vectorImageComposer->SetInput(layer, layerImage);
45  }
46 
47  vectorImageComposer->Update();
48  // mitk::GrabItkImageMemory does not support 4D, this will handle 4D correctly
49  // and create a memory managed copy
50  image = mitk::ImportItkImage(vectorImageComposer->GetOutput())->Clone();
51  }
52  else
53  {
54  auto layerImage = mitk::ImageToItkImage<TPixel, VDimension>(labelSetImage);
55 
56  auto duplicator = DuplicatorType::New();
57  duplicator->SetInputImage(layerImage);
58  duplicator->Update();
59 
60  // mitk::GrabItkImageMemory does not support 4D, this will handle 4D correctly
61  // and create a memory managed copy
62  image = mitk::ImportItkImage(duplicator->GetOutput())->Clone();
63  }
64 }
65 
67 {
69 
70  if (labelSetImage->GetNumberOfLayers() > 0)
71  {
72  if (labelSetImage->GetDimension() == 4)
73  {
74  AccessFixedDimensionByItk_n(labelSetImage, ::ConvertLabelSetImageToImage, 4, (labelSetImage, image));
75  }
76  else
77  {
78  AccessByItk_2(labelSetImage->GetLayerImage(0), ::ConvertLabelSetImageToImage, labelSetImage, image);
79  }
80 
81  image->SetTimeGeometry(labelSetImage->GetTimeGeometry()->Clone());
82  }
83 
84  return image;
85 }
86 
87 template <typename TPixel, unsigned int VDimensions>
88 static void ConvertImageToLabelSetImage(const itk::VectorImage<TPixel, VDimensions> *image,
89  mitk::LabelSetImage::Pointer &labelSetImage)
90 {
91  typedef itk::VectorImage<TPixel, VDimensions> VectorImageType;
92  typedef itk::Image<TPixel, VDimensions> ImageType;
93  typedef itk::VectorIndexSelectionCastImageFilter<VectorImageType, ImageType> VectorIndexSelectorType;
94 
95  labelSetImage = mitk::LabelSetImage::New();
96 
97  auto numberOfLayers = image->GetVectorLength();
98  for (decltype(numberOfLayers) layer = 0; layer < numberOfLayers; ++layer)
99  {
100  auto layerSelector = VectorIndexSelectorType::New();
101  layerSelector->SetInput(image);
102  layerSelector->SetIndex(layer);
103  layerSelector->Update();
104 
105  mitk::Image::Pointer layerImage;
106  mitk::CastToMitkImage(layerSelector->GetOutput(), layerImage);
107 
108  if (layer == 0)
109  {
110  labelSetImage->InitializeByLabeledImage(layerImage);
111  }
112  else
113  {
114  labelSetImage->AddLayer(layerImage);
115  }
116  }
117 }
118 
120 {
121  LabelSetImage::Pointer labelSetImage;
122 
123  if (image.IsNotNull())
124  {
125  if (image->GetChannelDescriptor().GetPixelType().GetPixelType() == itk::ImageIOBase::VECTOR)
126  {
127  if (4 == image->GetDimension())
128  {
130  }
131  else
132  {
134  }
135  }
136  else
137  {
138  labelSetImage = mitk::LabelSetImage::New();
139  labelSetImage->InitializeByLabeledImage(image);
140  }
141  labelSetImage->SetTimeGeometry(image->GetTimeGeometry()->Clone());
142  }
143 
144  return labelSetImage;
145 }
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...
#define AccessVectorPixelTypeByItk_n(mitkImage, itkImageTypeFunction, va_tuple)
Access a vector mitk::Image by an ITK vector image with one or more parameters.
mitk::Image::Pointer image
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:74
#define AccessByItk_2(mitkImage, itkImageTypeFunction, arg1, arg2)
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...