Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkBinaryImageToLabelSetImageFilter.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 
14 
15 #include <mitkImage.h>
16 #include <mitkLabelSetImage.h>
17 #include <mitkImageAccessByItk.h>
18 #include <mitkITKImageImport.h>
19 
20 //itk
21 #include <itkBinaryImageToLabelMapFilter.h>
22 #include <itkLabelMapToLabelImageFilter.h>
23 
24 
25  template <typename TPixel, unsigned int VImageDimension>
26  void mitk::BinaryImageToLabelSetImageFilter::ApplyBinaryImageToLabelMapFilter(const itk::Image<TPixel, VImageDimension>* inputImage)
27  {
28  using ImageType = itk::Image<TPixel, VImageDimension>;
29  using BinaryImageToLabelMapFilterType = itk::BinaryImageToLabelMapFilter<ImageType>;
30  typename BinaryImageToLabelMapFilterType::Pointer binaryImageToLabelMapFilter = BinaryImageToLabelMapFilterType::New();
31  binaryImageToLabelMapFilter->SetInput(inputImage);
32  binaryImageToLabelMapFilter->SetInputForegroundValue(m_ForegroundValue);
33  binaryImageToLabelMapFilter->SetFullyConnected(m_FullyConnected);
34 
35  using LabelMap2ImageType = itk::LabelMapToLabelImageFilter< typename BinaryImageToLabelMapFilterType::OutputImageType, ImageType>;
36 
37  typename LabelMap2ImageType::Pointer label2image = LabelMap2ImageType::New();
38  label2image->SetInput(binaryImageToLabelMapFilter->GetOutput());
39  label2image->Update();
40  auto labeledImage = mitk::ImportItkImage(label2image->GetOutput());
41 
42  if (m_OutputIsLabelSetImage) {
43  auto labeledBinaryImage = mitk::LabelSetImage::New();
44  labeledBinaryImage->InitializeByLabeledImage(labeledImage);
45  this->SetOutput(MakeNameFromOutputIndex(0), labeledBinaryImage.GetPointer());
46  }
47  else {
48  this->SetOutput(MakeNameFromOutputIndex(0), labeledImage.GetPointer());
49  }
50  }
51 
52  void mitk::BinaryImageToLabelSetImageFilter::VerifyInputImage(const mitk::Image* inputImage)
53  {
54  if (!inputImage->IsInitialized())
55  mitkThrow() << "Input image is not initialized.";
56 
57  if (!inputImage->IsVolumeSet())
58  mitkThrow() << "Input image volume is not set.";
59 
60  auto geometry = inputImage->GetGeometry();
61 
62  if (nullptr == geometry || !geometry->IsValid())
63  mitkThrow() << "Input image has invalid geometry.";
64 
65  if (!geometry->GetImageGeometry())
66  mitkThrow() << "Geometry of input image is not an image geometry.";
67  }
68 
69 void mitk::BinaryImageToLabelSetImageFilter::GenerateData()
70 {
71  const auto* inputImage = this->GetInput();
72  AccessByItk(inputImage, ApplyBinaryImageToLabelMapFilter);
73 }
74 
76 {
77  if (this->GetInput() == image)
78  return;
79 
80  Superclass::SetInput(image);
81 }
82 
84 {
85  if (0 != index)
86  mitkThrow() << "Input index " << index << " is invalid.";
87 
88  this->SetInput(image);
89 }
90 
91 void mitk::BinaryImageToLabelSetImageFilter::VerifyInputInformation()
92 {
93  Superclass::VerifyInputInformation();
94 
95  VerifyInputImage(this->GetInput());
96 }
itk::Image< unsigned char, 3 > ImageType
bool IsVolumeSet(int t=0, int n=0) const override
Check whether volume at time t in channel n is set.
Definition: mitkImage.cpp:602
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 mitkThrow()
Image class for storing images.
Definition: mitkImage.h:72
#define AccessByItk(mitkImage, itkImageTypeFunction)
Access a MITK image by an ITK image.
mitk::Image::Pointer image
static Pointer New()
InputImageType * GetInput(void)
virtual bool IsInitialized() const
Check whether the data has been initialized, i.e., at least the Geometry and other header data has be...
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
Definition: mitkBaseData.h:138
void SetInput(const InputImageType *image) override