Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
itkLabelSampler.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 #ifndef itkLabelSampler_cpp
14 #define itkLabelSampler_cpp
15 
16 #include "itkLabelSampler.h"
17 #include "itkObjectFactory.h"
18 #include "itkImageRegionIterator.h"
19 #include "itkImageRegionConstIterator.h"
20 #include "itkImageIterator.h"
21 #include "itkImageConstIterator.h"
22 
23 #include <cstdlib> /* srand, rand */
24 #include <ctime> /* time */
25 
26 namespace itk
27 {
28 
29 template< class TImage>
32 {
33 
34 
35  srand (time(nullptr));
36  this->AllocateOutputs();
37 
38  typename TImage::ConstPointer input = this->GetInput();
39  TImage * output = this->GetOutput();
40 
41  m_NumberOfSampledVoxels = 0;
42 
43  ImageRegionConstIterator<TImage> inputIter(input, input->GetLargestPossibleRegion());
44  ImageRegionIterator<TImage> outputIter(output, output->GetLargestPossibleRegion());
45  while (!inputIter.IsAtEnd())
46  {
47  if (inputIter.Get() > 0)
48  {
49 
50  if(m_LabelVoxelCountMap.find(inputIter.Get()) == m_LabelVoxelCountMap.end())
51  m_LabelVoxelCountMap[inputIter.Get()] = 0;
52  m_LabelVoxelCountMap[inputIter.Get()]++;
53 
54  double r;
55  if(inputIter.Get() == m_Label || m_Label == -1 ){
56  r = (double)(rand()) / RAND_MAX;
57  if (r < m_AcceptRate)
58  {
59  outputIter.Set(inputIter.Get());
60  m_NumberOfSampledVoxels++;
61  }
62  }else
63  outputIter.Set(0);
64 
65  }
66  else
67  {
68  outputIter.Set(0);
69  }
70  ++inputIter;
71  ++outputIter;
72  }
73 
74 }
75 
76 }// end namespace
77 
78 #endif // itkLabelSampler_cpp
virtual void GenerateData()