Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkIgnorePixelMaskGenerator.cpp
Go to the documentation of this file.
3 #include <mitkImageAccessByItk.h>
4 #include <itkImageIterator.h>
5 #include <itkImageConstIterator.h>
6 #include <mitkITKImageImport.h>
7 
8 namespace mitk
9 {
11 {
12  if (pixelValue != m_IgnoredPixelValue)
13  {
14  m_IgnoredPixelValue = pixelValue;
15  this->Modified();
16  }
17 }
18 
19 void IgnorePixelMaskGenerator::SetTimeStep(unsigned int timeStep)
20 {
21  if (m_TimeStep != timeStep)
22  {
23  m_TimeStep = timeStep;
24  }
25 }
26 
28 {
29  if (IsUpdateRequired())
30  {
31  if (m_inputImage.IsNull())
32  {
33  MITK_ERROR << "Image not set!";
34  }
35 
36  if (m_IgnoredPixelValue == std::numeric_limits<RealType>::min())
37  {
38  MITK_ERROR << "IgnotePixelValue not set!";
39  }
40 
41  if (m_TimeStep > (m_inputImage->GetTimeSteps() - 1))
42  {
43  MITK_ERROR << "Invalid time step: " << m_TimeStep << ". The image has " << m_inputImage->GetTimeSteps() << " timeSteps!";
44  }
45 
46  // extractimage time slice
48  imgTimeSel->SetInput(m_inputImage);
49  imgTimeSel->SetTimeNr(m_TimeStep);
50  imgTimeSel->UpdateLargestPossibleRegion();
51 
52  mitk::Image::Pointer timeSliceImage = imgTimeSel->GetOutput();
53 
54  // update m_InternalMask
55  AccessByItk(timeSliceImage, InternalCalculateMask);
56  m_InternalMask->SetGeometry(timeSliceImage->GetGeometry());
57 
58  this->Modified();
59  }
60  m_InternalMaskUpdateTime = m_InternalMask->GetMTime();
61  return m_InternalMask;
62 }
63 
64 template <typename TPixel, unsigned int VImageDimension>
65 void IgnorePixelMaskGenerator::InternalCalculateMask(typename itk::Image<TPixel, VImageDimension>* image)
66 {
67  typedef itk::Image<TPixel, VImageDimension> ImageType;
68  typedef itk::Image<unsigned short, VImageDimension> MaskType;
69 
70  typename MaskType::Pointer mask = MaskType::New();
71  mask->SetOrigin(image->GetOrigin());
72  mask->SetSpacing(image->GetSpacing());
73  mask->SetLargestPossibleRegion(image->GetLargestPossibleRegion());
74  mask->SetBufferedRegion(image->GetBufferedRegion());
75  mask->SetDirection(image->GetDirection());
76  mask->SetNumberOfComponentsPerPixel(image->GetNumberOfComponentsPerPixel());
77  mask->Allocate();
78  mask->FillBuffer(1);
79 
80  // iterate over image and mask and set mask=1 if image=m_IgnorePixelValue
81  itk::ImageRegionConstIterator<ImageType> imageIterator(image, image->GetLargestPossibleRegion());
82  itk::ImageRegionIterator<MaskType> maskIterator(mask, mask->GetLargestPossibleRegion());
83 
84 
85  for (imageIterator.GoToBegin(); !imageIterator.IsAtEnd(); ++imageIterator, ++maskIterator)
86  {
87  if (imageIterator.Value() == static_cast<TPixel>(m_IgnoredPixelValue))
88  {
89  maskIterator.Set(0);
90  }
91  }
92 
94 }
95 
96 bool IgnorePixelMaskGenerator::IsUpdateRequired() const
97 {
98  unsigned long thisClassTimeStamp = this->GetMTime();
99  unsigned long internalMaskTimeStamp = m_InternalMask->GetMTime();
100  unsigned long inputImageTimeStamp = m_inputImage->GetMTime();
101 
102  if (thisClassTimeStamp > m_InternalMaskUpdateTime) // inputs have changed
103  {
104  return true;
105  }
106 
107  if (m_InternalMaskUpdateTime < inputImageTimeStamp) // mask image has changed outside of this class
108  {
109  return true;
110  }
111 
112  if (internalMaskTimeStamp > m_InternalMaskUpdateTime) // internal mask has been changed outside of this class
113  {
114  return true;
115  }
116 
117  return false;
118 }
119 
120 } // end namespace
mitk::Image::Pointer m_inputImage
void SetTimeStep(unsigned int timeStep)
SetTimeStep is used to set the time step for which the mask is to be generated.
itk::SmartPointer< Self > Pointer
#define MITK_ERROR
Definition: mitkLogMacros.h:24
mitk::Image::Pointer GetMask()
Computes and returns the mask.
DataCollection - Class to facilitate loading/accessing structured data.
void InternalCalculateMask(typename itk::Image< TPixel, VImageDimension > *image)
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grabs the memory of an itk::Image (with a specific type) and puts it into an mitk::Image.The memory is managed by the mitk::Image after calling this function. The itk::Image remains valid until the mitk::Image decides to free the memory.
map::core::discrete::Elements< 3 >::InternalImageType ImageType
#define AccessByItk(mitkImage, itkImageTypeFunction)
Access a MITK image by an ITK image.
mitk::Image::Pointer m_InternalMask
static T min(T x, T y)
Definition: svm.cpp:67
void SetIgnoredPixelValue(RealType pixelValue)
The mask will be 0 there inputImage==pixelValue and 1 otherwise.
static Pointer New()
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.