ANIMA  4.0
animaBlockMatchInitializer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <itkImageRegion.h>
4 #include <itkObject.h>
5 #include <itkMultiThreaderBase.h>
6 
7 #include <itkVectorImage.h>
8 #include <itkImage.h>
9 
10 namespace anima
11 {
12 
13 template <class PixelType, unsigned int NDimensions=3>
15  : public itk::Object
16 {
17 public:
18  typedef itk::Object Superclass;
20  typedef itk::SmartPointer<Self> Pointer;
21  typedef itk::SmartPointer<const Self> ConstPointer;
22 
23  typedef itk::Image <PixelType, NDimensions> ScalarImageType;
24  typedef itk::Image <double, NDimensions> WeightImageType;
25  typedef typename WeightImageType::Pointer WeightImagePointer;
26  typedef typename ScalarImageType::IndexType IndexType;
27  typedef typename ScalarImageType::PointType PointType;
28  typedef itk::VectorImage <PixelType, NDimensions> VectorImageType;
29  typedef itk::Image <unsigned char, NDimensions> MaskImageType;
30 
32  itkNewMacro(Self)
33 
34 
35  itkTypeMacro(BlockMatchingInitializer, itk::Object)
36 
37  typedef typename ScalarImageType::RegionType ImageRegionType;
38 
39  typedef typename ScalarImageType::Pointer ScalarImagePointer;
40  typedef typename VectorImageType::Pointer VectorImagePointer;
41  typedef typename MaskImageType::Pointer MaskImagePointer;
42 
43  itkSetMacro(NumberOfThreads, unsigned int)
44  itkGetMacro(NumberOfThreads, unsigned int)
45 
46  void SetPercentageKept(double val);
47  itkGetMacro(PercentageKept, double)
48 
49  void SetScalarVarianceThreshold(double val);
50  itkGetMacro(ScalarVarianceThreshold, double)
51 
52  void SetOrientedModelVarianceThreshold(double val);
53  itkGetMacro(OrientedModelVarianceThreshold, double)
54 
55  void SetRequestedRegion(const ImageRegionType &val);
56  itkGetMacro(RequestedRegion, ImageRegionType)
57 
58  void SetBlockSpacing(unsigned int val);
59  void SetBlockSize(unsigned int val);
60 
61  itkGetMacro(BlockSpacing, unsigned int)
62  itkGetMacro(BlockSize, unsigned int)
63 
64  void clearGenerationMasks() {m_GenerationMasks.clear();}
65  void AddGenerationMask(MaskImageType *mask);
66 
67  virtual void AddReferenceImage(itk::ImageBase <NDimensions> *refImage);
68  itk::ImageBase <NDimensions> *GetFirstReferenceImage();
69 
70  ScalarImageType *GetReferenceScalarImage(unsigned int i) {return m_ReferenceScalarImages[i];}
71  VectorImageType *GetReferenceVectorImage(unsigned int i) {return m_ReferenceVectorImages[i];}
72 
73  void Update();
74 
75  // Does the splitting and calls RegionBlockGenerator on a sub region
76  static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION ThreadBlockGenerator(void *arg);
77 
78  std::vector <ImageRegionType> &GetOutput();
79  std::vector <unsigned int> &GetMaskStartingIndexes();
80 
81  std::vector <PointType> &GetOutputPositions();
82 
84  {
85  Pointer Filter;
86  std::vector < std::vector <ImageRegionType> > tmpOutput;
87  std::vector <unsigned int> blockStartOffsets;
88  std::vector < std::vector <unsigned int> > startBlocks, nb_blocks;
89  std::vector <unsigned int> totalNumberOfBlocks;
90  std::vector < std::vector <double> > blocks_variances;
91  unsigned int maskIndex;
92  std::vector < std::vector <PointType> > blocks_positions;
93 
96  };
97 
98  void RegionBlockGenerator(BlockGeneratorThreadStruct *workStr, unsigned int threadId);
99 
100 protected:
101  BlockMatchingInitializer() : Superclass()
102  {
103  m_BlockSize = 5;
104  m_BlockSpacing = 3;
105  m_NumberOfThreads = itk::MultiThreaderBase::GetGlobalDefaultNumberOfThreads();
106 
107  m_ScalarVarianceThreshold = 5.0;
108  m_OrientedModelVarianceThreshold = 0.0;
109 
110  m_PercentageKept = 0.8;
111  m_GenerationMasks.clear();
112 
113  m_UpToDate = false;
114  }
115 
117 
118  void ComputeBlocksOnGenerationMask(unsigned int maskIndex);
119 
120  virtual void InitializeThreading(unsigned int maskIndex, BlockGeneratorThreadStruct *&workStr);
121 
122  bool CheckBlockConditions(ImageRegionType &region, double &blockVariance, BlockGeneratorThreadStruct *workStr,
123  unsigned int threadId);
124 
125  bool CheckScalarVariance(ScalarImageType *refImage, ImageRegionType &region, double &blockVariance);
126  virtual bool CheckOrientedModelVariance(unsigned int imageIndex, ImageRegionType &region, double &blockVariance,
127  BlockGeneratorThreadStruct *workStr, unsigned int threadId);
128 
129  bool ProgressCounter(std::vector <unsigned int> &counter, std::vector <unsigned int> &bounds);
130 
132  {
133  bool operator() (const std::pair<double, std::pair <PointType, ImageRegionType> > & f,
134  const std::pair<double, std::pair <PointType, ImageRegionType> > & s)
135  { return (f.first < s.first); }
136  };
137 
138 private:
139  BlockMatchingInitializer(const Self&); //purposely not implemented
140  void operator=(const Self&); //purposely not implemented
141 
142  unsigned int m_BlockSize;
143  unsigned int m_BlockSpacing;
144  unsigned int m_NumberOfThreads;
145 
146  double m_ScalarVarianceThreshold;
147  double m_OrientedModelVarianceThreshold;
148  double m_PercentageKept;
149 
150  ImageRegionType m_RequestedRegion;
151 
152  std::vector <ScalarImagePointer> m_ReferenceScalarImages;
153  std::vector <VectorImagePointer> m_ReferenceVectorImages;
154 
155  std::vector <MaskImagePointer> m_GenerationMasks;
156 
157  std::vector <ImageRegionType> m_Output;
158  std::vector <unsigned int> m_MaskStartingIndexes;
159  std::vector <PointType> m_OutputPositions;
160 
161  bool m_UpToDate;
162 };
163 
164 } // end of namespace anima
165 
itk::Image< unsigned char, NDimensions > MaskImageType
ScalarImageType * GetReferenceScalarImage(unsigned int i)
void RegionBlockGenerator(BlockGeneratorThreadStruct *workStr, unsigned int threadId)
virtual bool CheckOrientedModelVariance(unsigned int imageIndex, ImageRegionType &region, double &blockVariance, BlockGeneratorThreadStruct *workStr, unsigned int threadId)
virtual void AddReferenceImage(itk::ImageBase< NDimensions > *refImage)
VectorImageType * GetReferenceVectorImage(unsigned int i)
std::vector< PointType > & GetOutputPositions()
void SetRequestedRegion(const ImageRegionType &val)
virtual void InitializeThreading(unsigned int maskIndex, BlockGeneratorThreadStruct *&workStr)
virtual ~BlockGeneratorThreadStruct()
To be able to inherit from it.
itk::SmartPointer< const Self > ConstPointer
bool CheckScalarVariance(ScalarImageType *refImage, ImageRegionType &region, double &blockVariance)
void ComputeBlocksOnGenerationMask(unsigned int maskIndex)
static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION ThreadBlockGenerator(void *arg)
ScalarImageType::RegionType ImageRegionType
itk::VectorImage< PixelType, NDimensions > VectorImageType
itk::Image< PixelType, NDimensions > ScalarImageType
bool ProgressCounter(std::vector< unsigned int > &counter, std::vector< unsigned int > &bounds)
ScalarImageType::Pointer ScalarImagePointer
VectorImageType::Pointer VectorImagePointer
WeightImageType::Pointer WeightImagePointer
std::vector< unsigned int > & GetMaskStartingIndexes()
bool CheckBlockConditions(ImageRegionType &region, double &blockVariance, BlockGeneratorThreadStruct *workStr, unsigned int threadId)
BlockMatchingInitializer< PixelType, NDimensions > Self
itk::Image< double, NDimensions > WeightImageType
itk::ImageBase< NDimensions > * GetFirstReferenceImage()
std::vector< ImageRegionType > & GetOutput()