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