Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkOtsuSegmentationFilter.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 
18 #include "itkOtsuMultipleThresholdsImageFilter.h"
19 #include "mitkImageAccessByItk.h"
20 #include "mitkImageCast.h"
21 
22 struct paramContainer
23 {
24  paramContainer(unsigned int numThresholds, bool useValley, unsigned int numBins, mitk::Image::Pointer image)
25  : m_NumberOfThresholds(numThresholds), m_ValleyEmphasis(useValley), m_NumberOfBins(numBins), m_Image(image)
26  {
27  }
28 
29  unsigned int m_NumberOfThresholds;
30  bool m_ValleyEmphasis;
31  unsigned int m_NumberOfBins;
32  mitk::Image::Pointer m_Image;
33 };
34 
35 template <typename TPixel, unsigned int VImageDimension>
36 void AccessItkOtsuFilter(const itk::Image<TPixel, VImageDimension> *itkImage, paramContainer params)
37 {
38  typedef itk::Image<TPixel, VImageDimension> itkInputImageType;
39  typedef itk::Image<mitk::OtsuSegmentationFilter::OutputPixelType, VImageDimension> itkOutputImageType;
40  typedef itk::OtsuMultipleThresholdsImageFilter<itkInputImageType, itkOutputImageType> OtsuFilterType;
41 
42  typename OtsuFilterType::Pointer filter = OtsuFilterType::New();
43  filter->SetNumberOfThresholds(params.m_NumberOfThresholds);
44  filter->SetInput(itkImage);
45  filter->SetValleyEmphasis(params.m_ValleyEmphasis);
46  filter->SetNumberOfHistogramBins(params.m_NumberOfBins);
47 
48  try
49  {
50  filter->Update();
51  }
52  catch (...)
53  {
54  mitkThrow() << "itkOtsuFilter error.";
55  }
56 
57  mitk::CastToMitkImage<itkOutputImageType>(filter->GetOutput(), params.m_Image);
58  return;
59 }
60 
61 namespace mitk
62 {
64  : m_NumberOfThresholds(2), m_ValleyEmphasis(false), m_NumberOfBins(128)
65  {
66  }
67 
70  {
71  mitk::Image::ConstPointer mitkImage = GetInput();
72  AccessByItk_n(mitkImage,
74  (paramContainer(m_NumberOfThresholds, m_ValleyEmphasis, m_NumberOfBins, this->GetOutput())));
75  }
76 }
itk::SmartPointer< Self > Pointer
DataCollection - Class to facilitate loading/accessing structured data.
#define AccessByItk_n(mitkImage, itkImageTypeFunction, va_tuple)
Access a MITK image by an ITK image with one or more parameters.
void AccessItkOtsuFilter(const itk::Image< TPixel, VImageDimension > *itkImage, paramContainer params)
#define mitkThrow()
virtual void GenerateData() override
A version of GenerateData() specific for image processing filters.
InputImageType * GetInput(void)
OutputType * GetOutput()
Get the output data of this image source object.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.