14 #include <itkBilateralImageFilter.h> 17 #include "itkUnaryFunctorImageFilter.h" 18 #include <itkImageRandomNonRepeatingIteratorWithIndex.h> 19 #include "itkImageDuplicator.h" 21 mitk::RandomImageSampler::RandomImageSampler()
27 mitk::RandomImageSampler::~RandomImageSampler()
31 template<
class TInput,
class TOutput>
32 class RandomlySampleFunctor
35 RandomlySampleFunctor() {};
36 ~RandomlySampleFunctor() {};
37 bool operator!=(
const RandomlySampleFunctor &)
const 41 bool operator==(
const RandomlySampleFunctor & other)
const 43 return !(*
this != other);
45 inline TOutput operator()(
const TInput & A)
const 47 if (rand() < RAND_MAX*m_AcceptanceRate)
53 double m_AcceptanceRate = 0.1;
56 template<
class TInput,
class TOutput>
57 class RandomlySampleClassDependedFunctor
60 RandomlySampleClassDependedFunctor() {};
61 ~RandomlySampleClassDependedFunctor() {};
62 bool operator!=(
const RandomlySampleClassDependedFunctor &)
const 66 bool operator==(
const RandomlySampleClassDependedFunctor & other)
const 68 return !(*
this != other);
70 inline TOutput operator()(
const TInput & A)
const 72 std::size_t index =
static_cast<std::size_t
>(A + 0.5);
73 double samplingRate = 0;
74 if (index >= 0 && index < m_SamplingRateVector.size())
76 samplingRate = m_SamplingRateVector[index];
79 if (rand() < RAND_MAX*samplingRate)
85 std::vector<double> m_SamplingRateVector;
88 void mitk::RandomImageSampler::GenerateData()
91 switch (m_SamplingMode)
94 AccessByItk(inputImage.GetPointer(), ItkImageProcessing);
97 AccessByItk(inputImage.GetPointer(), ItkImageProcessingClassDependendSampling);
100 AccessByItk(inputImage.GetPointer(), ItkImageProcessingFixedNumberSampling);
103 AccessByItk(inputImage.GetPointer(), ItkImageProcessingClassDependendNumberSampling);
106 AccessByItk(inputImage.GetPointer(), ItkImageProcessing);
111 template<
typename TPixel,
unsigned int VImageDimension>
112 void mitk::RandomImageSampler::ItkImageProcessing(
const itk::Image<TPixel,VImageDimension>* itkImage )
115 typedef itk::Image< TPixel, VImageDimension > ItkImageType;
117 typedef RandomlySampleFunctor<
typename ItkImageType::PixelType,
118 typename ItkImageType::PixelType> LocalSampleFunctorType;
119 typedef itk::UnaryFunctorImageFilter<ItkImageType, ItkImageType, LocalSampleFunctorType > RandomImageSamplerType;
120 typename RandomImageSamplerType::Pointer RandomImageSampler = RandomImageSamplerType::New();
121 RandomImageSampler->SetInput(itkImage);
123 LocalSampleFunctorType functor;
124 functor.m_AcceptanceRate = m_AcceptanceRate;
125 RandomImageSampler->SetFunctor(functor);
126 RandomImageSampler->GetFunctor().m_AcceptanceRate = m_AcceptanceRate;
127 RandomImageSampler->Update();
136 template<
typename TPixel,
unsigned int VImageDimension>
137 void mitk::RandomImageSampler::ItkImageProcessingClassDependendSampling(
const itk::Image<TPixel, VImageDimension>* itkImage)
140 typedef itk::Image< TPixel, VImageDimension > ItkImageType;
142 typedef RandomlySampleClassDependedFunctor<
typename ItkImageType::PixelType,
143 typename ItkImageType::PixelType> LocalSampleFunctorType;
144 typedef itk::UnaryFunctorImageFilter<ItkImageType, ItkImageType, LocalSampleFunctorType > RandomImageSamplerType;
145 typename RandomImageSamplerType::Pointer RandomImageSampler = RandomImageSamplerType::New();
146 RandomImageSampler->SetInput(itkImage);
148 LocalSampleFunctorType functor;
149 functor.m_SamplingRateVector = m_AcceptanceRateVector;
150 RandomImageSampler->SetFunctor(functor);
151 RandomImageSampler->GetFunctor().m_SamplingRateVector = m_AcceptanceRateVector;
152 RandomImageSampler->Update();
161 template<
typename TPixel,
unsigned int VImageDimension>
162 void mitk::RandomImageSampler::ItkImageProcessingFixedNumberSampling(
const itk::Image<TPixel, VImageDimension>* itkImage)
165 typedef itk::Image< TPixel, VImageDimension > ItkImageType;
166 typedef itk::ImageDuplicator< ItkImageType > DuplicatorType;
167 typedef itk::ImageRandomNonRepeatingIteratorWithIndex <ItkImageType> IteratorType;
169 typename DuplicatorType::Pointer duplicator = DuplicatorType::New();
170 duplicator->SetInputImage(itkImage);
171 duplicator->Update();
172 typename ItkImageType::Pointer clonedImage = duplicator->GetOutput();
175 std::vector<unsigned int> counts;
176 IteratorType iter(clonedImage, clonedImage->GetLargestPossibleRegion());
177 iter.SetNumberOfSamples(clonedImage->GetLargestPossibleRegion().GetNumberOfPixels());
179 while (!iter.IsAtEnd())
181 std::size_t index =
static_cast<std::size_t
>(iter.Value() + 0.5);
182 while (index >= counts.size())
186 if (counts[index] < m_NumberOfSamples)
206 template<
typename TPixel,
unsigned int VImageDimension>
207 void mitk::RandomImageSampler::ItkImageProcessingClassDependendNumberSampling(
const itk::Image<TPixel, VImageDimension>* itkImage)
210 typedef itk::Image< TPixel, VImageDimension > ItkImageType;
211 typedef itk::ImageDuplicator< ItkImageType > DuplicatorType;
212 typedef itk::ImageRandomNonRepeatingIteratorWithIndex<ItkImageType> IteratorType;
214 typename DuplicatorType::Pointer duplicator = DuplicatorType::New();
215 duplicator->SetInputImage(itkImage);
216 duplicator->Update();
217 typename ItkImageType::Pointer clonedImage = duplicator->GetOutput();
219 std::vector<unsigned int> counts;
220 IteratorType iter(clonedImage, clonedImage->GetLargestPossibleRegion());
221 iter.SetNumberOfSamples(clonedImage->GetLargestPossibleRegion().GetNumberOfPixels());
222 while (!iter.IsAtEnd())
224 std::size_t index =
static_cast<std::size_t
>(iter.Value() + 0.5);
225 if (index < m_NumberOfSamplesVector.size())
227 while (index >= counts.size())
232 if (counts[index] < m_NumberOfSamplesVector[index])
256 void mitk::RandomImageSampler::GenerateOutputInformation()
260 itkDebugMacro(<<
"GenerateOutputInformation()");
261 if(inputImage.IsNull())
return;
MITKCORE_EXPORT bool operator!=(const InteractionEvent &a, const InteractionEvent &b)
MITKCORE_EXPORT bool operator==(const InteractionEvent &a, const InteractionEvent &b)
const BaseData * GetInput() const override
Get the input data set via SetInput().
Image class for storing images.
#define AccessByItk(mitkImage, itkImageTypeFunction)
Access a MITK image by an ITK image.
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.