Medical Imaging Interaction Toolkit  2018.4.99-4c24e3cb
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 
82  return image;
83 }
84 
85 template <typename TPixel, unsigned int VDimensions>
86 static void ConvertImageToLabelSetImage(const itk::VectorImage<TPixel, VDimensions> *image,
87  mitk::LabelSetImage::Pointer &labelSetImage)
88 {
89  typedef itk::VectorImage<TPixel, VDimensions> VectorImageType;
90  typedef itk::Image<TPixel, VDimensions> ImageType;
91  typedef itk::VectorIndexSelectionCastImageFilter<VectorImageType, ImageType> VectorIndexSelectorType;
92 
93  labelSetImage = mitk::LabelSetImage::New();
94 
95  auto numberOfLayers = image->GetVectorLength();
96  for (decltype(numberOfLayers) layer = 0; layer < numberOfLayers; ++layer)
97  {
98  auto layerSelector = VectorIndexSelectorType::New();
99  layerSelector->SetInput(image);
100  layerSelector->SetIndex(layer);
101  layerSelector->Update();
102 
103  mitk::Image::Pointer layerImage;
104  mitk::CastToMitkImage(layerSelector->GetOutput(), layerImage);
105 
106  if (layer == 0)
107  {
108  labelSetImage->InitializeByLabeledImage(layerImage);
109  }
110  else
111  {
112  labelSetImage->AddLayer(layerImage);
113  }
114  }
115 }
116 
118 {
119  LabelSetImage::Pointer labelSetImage;
120 
121  if (image.IsNotNull())
122  {
123  if (image->GetChannelDescriptor().GetPixelType().GetPixelType() == itk::ImageIOBase::VECTOR)
124  {
125  if (4 == image->GetDimension())
126  {
128  }
129  else
130  {
132  }
133  }
134  else
135  {
136  labelSetImage = mitk::LabelSetImage::New();
137  labelSetImage->InitializeByLabeledImage(image);
138  }
139  }
140 
141  return labelSetImage;
142 }
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 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...