Medical Imaging Interaction Toolkit  2018.4.99-6aa36ba9
Medical Imaging Interaction Toolkit
mitkPixelBasedDescriptionParameterImageGenerator.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 "itkCommand.h"
15 
17 #include "mitkImageTimeSelector.h"
18 #include "mitkImageAccessByItk.h"
19 #include "mitkImageCast.h"
21 #include "mitkModelBase.h"
22 
23 void
25  onFitProgressEvent(::itk::Object* caller, const ::itk::EventObject& /*eventObject*/)
26 {
27  this->InvokeEvent(::itk::ProgressEvent());
28 
29  itk::ProcessObject* process = dynamic_cast<itk::ProcessObject*>(caller);
30  if (process)
31  {
32  this->m_Progress = process->GetProgress();
33  }
34 };
35 
36 template <typename TPixel, unsigned int VDim>
37 void
39 {
40  m_InternalMask = dynamic_cast<InternalMaskType*>(image);
41 
42  if (m_InternalMask.IsNull())
43  {
44  MITK_INFO << "Parameter Fit Generator. Need to cast mask for parameter fit.";
45  typedef itk::Image<TPixel, VDim> InputImageType;
46  typedef itk::CastImageFilter< InputImageType, InternalMaskType > CastFilterType;
47  typename CastFilterType::Pointer spImageCaster = CastFilterType::New();
48 
49  spImageCaster->SetInput(image);
50 
51  m_InternalMask = spImageCaster->GetOutput();
52  spImageCaster->Update();
53  }
54 }
55 
56 template<typename TImage>
58 {
59  if (source->GetNumberOfOutputs() != paramNames.size())
60  {
61  mitkThrow() << "Error while generating fitted parameter images. Number of sources does not match expected parameter number. Output size: " << source->GetNumberOfOutputs() << "; number of param names: " << paramNames.size();
62  }
63 
65  for (mitk::CurveParameterFunctor::ParameterNamesType::size_type j = 0; j < paramNames.size(); ++j)
66  {
68  typename TImage::ConstPointer outputImg = source->GetOutput(j);
69  mitk::CastToMitkImage(outputImg, paramImage);
70 
71  result.insert(std::make_pair(paramNames[j],paramImage));
72  }
73 
74  return result;
75 }
76 
77 template <typename TPixel, unsigned int VDim>
78 void
80 {
81  typedef itk::Image<TPixel, VDim-1> InputFrameImageType;
82  typedef itk::Image<ScalarType, VDim-1> ParameterImageType;
83 
85 
86  typename DescriptorFilterType::Pointer descFilter = DescriptorFilterType::New();
87 
88  typename ::itk::MemberCommand<Self>::Pointer spProgressCommand = ::itk::MemberCommand<Self>::New();
89  spProgressCommand->SetCallbackFunction(this, &Self::onFitProgressEvent);
90  descFilter->AddObserver(::itk::ProgressEvent(), spProgressCommand);
91 
92  //add the time frames to the descriptor filter
93  std::vector<Image::Pointer> frameCache;
94  for (unsigned int i = 0; i < this->m_DynamicImage->GetTimeSteps(); ++i)
95  {
96  typename InputFrameImageType::Pointer frameImage;
98  imageTimeSelector->SetInput(this->m_DynamicImage);
99  imageTimeSelector->SetTimeNr(i);
100  imageTimeSelector->UpdateLargestPossibleRegion();
101 
102  Image::Pointer frameMITKImage = imageTimeSelector->GetOutput();
103  frameCache.push_back(frameMITKImage);
104  mitk::CastToItkImage(frameMITKImage, frameImage);
105  descFilter->SetInput(i,frameImage);
106  }
107 
108 
109  SimpleFunctorPolicy functor;
110 
111  functor.SetFunctor(this->m_Functor);
112  descFilter->SetFunctor(functor);
113  if (this->m_InternalMask.IsNotNull())
114  {
115  descFilter->SetMask(this->m_InternalMask);
116  }
117 
118  //generate the fits
119  descFilter->Update();
120 
121  //convert the outputs into mitk images and fill the parameter image map
122  CurveParameterFunctor::ParameterNamesType paramNames = this->m_Functor->GetDescriptionParameterNames();
123 
124  if (descFilter->GetNumberOfOutputs() != (paramNames.size()))
125  {
126  mitkThrow() << "Error while generating fitted parameter images. Fit filter output size does not match expected parameter number. Output size: "<< descFilter->GetNumberOfOutputs();
127  }
128 
129  this->m_TempResultMap = StoreResultImages<ParameterImageType>(paramNames,descFilter);
130 }
131 
132 bool
134 {
135  bool result = Superclass::HasOutdatedResult();
136 
137  if (m_DynamicImage.IsNotNull())
138  {
139  if (m_DynamicImage->GetMTime() > this->m_GenerationTimeStamp)
140  {
141  result = true;
142  }
143  }
144 
145  if (m_Mask.IsNotNull())
146  {
147  if (m_Mask->GetMTime() > this->m_GenerationTimeStamp)
148  {
149  result = true;
150  }
151  }
152 
153  return result;
154 
155 };
156 
157 void
159 {
160  Superclass::CheckValidInputs();
161 
162  if (m_DynamicImage.IsNull())
163  {
164  mitkThrow() << "Cannot generate fitted parameter images. Input dynamic image is not set.";
165  }
166 
167 };
168 
170 {
171  this->m_Progress = 0;
172 
173  if(this->m_Mask.IsNotNull())
174  {
176  }
177  else
178  {
179  this->m_InternalMask = nullptr;
180  }
181 
183 
184  parameterImages = this->m_TempResultMap;
185  };
186 
187 double
189 {
190  return m_Progress;
191 };
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define AccessFixedDimensionByItk(mitkImage, itkImageTypeFunction, dimension)
Access a mitk-image with known dimension by an itk-image.
double ScalarType
void onFitProgressEvent(::itk::Object *caller, const ::itk::EventObject &eventObject)
Perform a generic pixel-wise operation on N images and produces m output images.
void SetFunctor(const mitk::SimpleFunctorBase *functor)
void DoParameterCalculationAndGetResults(ParameterImageMapType &parameterImages) override
DescriptionParameterImageGeneratorBase::ParameterImageMapType ParameterImageMapType
class ITK_EXPORT Image
#define mitkThrow()
mitk::Image::Pointer image
static Pointer New()
std::map< ParameterNameType, Image::Pointer > ParameterImageMapType
CurveDescriptionParameterBase::DescriptionParameterNamesType ParameterNamesType
mitk::PixelBasedDescriptionParameterImageGenerator::ParameterImageMapType StoreResultImages(const mitk::CurveParameterFunctor::ParameterNamesType &paramNames, itk::ImageSource< TImage > *source)
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
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
static Pointer New()