HashProbabilisticAtlas.h
1 /*
2  * Developing brain Region Annotation With Expectation-Maximization (Draw-EM)
3  *
4  * Copyright 2013-2016 Imperial College London
5  * Copyright 2013-2016 Antonios Makropoulos
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 
21 #ifndef _HashProbabilisticAtlas_H
22 
23 #define _HashProbabilisticAtlas_H
24 
25 #include "mirtk/Object.h"
26 
27 #include "mirtk/Image.h"
28 #include "mirtk/HashImage.h"
29 
30 #include <vector>
31 
32 
33 /**
34 
35 Atlas probability mapnr class
36 
37  */
38 using namespace std;
39 namespace mirtk {
40 
41 
42 class HashProbabilisticAtlas : public Object
43 {
44  typedef HashImage<int> HashIntegerImage;
45 
46  mirtkObjectMacro(HashProbabilisticAtlas);
47 
48  // Vector of probability maps
49  vector<HashRealImage*> _images;
50 
51  // Number of voxels
52  int _number_of_voxels;
53 
54  // Number of maps
55  int _number_of_maps;
56 
57  // Hard segmentation
58  HashIntegerImage *_segmentation;
59 
60  // _position of iterator
61  int _position;
62 
63  // whether we have background
64  bool _has_background;
65 
66 public:
67 
68  // Constructor
69  HashProbabilisticAtlas();
70 
71  // Destructor
72  ~HashProbabilisticAtlas();
73 
74  // Copy operator
75  HashProbabilisticAtlas& operator=(const HashProbabilisticAtlas &atlas);
76 
77  // swap images within atlas
78  void SwapImages(int, int);
79 
80  // Adds new image
81  template <class ImageType>
82  void AddImage(ImageType image);
83 
84  // Moves pointers in all images to the first voxel
85  void First();
86 
87  // Moves pointers in all images to the next voxel
88  void Next();
89 
90  // Returns intensity value at pointer
91  RealPixel GetValue(unsigned int mapnr);
92 
93  // Returns intensity value at _position
94  RealPixel GetValue(int x, int y, int z, unsigned int mapnr);
95 
96  // Sets intensity value at pointer
97  void SetValue(unsigned int mapnr, RealPixel value);
98 
99  // Sets intensity value at _position
100  void SetValue(int x, int y, int z, unsigned int mapnr, RealPixel value);
101 
102  // Returns number of voxels
103  int GetNumberOfVoxels() const;
104 
105  // Add probability maps
106  template <class ImageType>
107  void AddProbabilityMaps(int, ImageType **atlas);
108 
109  // Add background map as (1-rest maps)
110  void AddBackground();
111 
112  // Add background map
113  template <class ImageType>
114  void AddBackground(ImageType image);
115 
116  // Whether it has background
117  bool HasBackground() const;
118 
119  // normalize atlas
120  void NormalizeAtlas();
121 
122  //Returns number of maps
123  int GetNumberOfMaps() const;
124 
125  // Write
126  void Write(int, const char *);
127 
128  // Computes hard segmentation
129  HashIntegerImage ComputeHardSegmentation();
130 
131  // Extract a label from hard segmentation
132  void ExtractLabel(int label, HashByteImage &image);
133 
134  // Writes hard segmentation into a file
135  void WriteHardSegmentation(const char *filename);
136 
137  // Get image
138  HashRealImage GetImage(unsigned int mapnr) const;
139 
140  // Get data
141  HashRealImage::DataIterator Begin(unsigned int mapnr) const;
142  HashRealImage::DataIterator End(unsigned int mapnr) const;
143 
144 };
145 
146 inline void HashProbabilisticAtlas::First(){
147  _position=0;
148 }
149 
150 inline void HashProbabilisticAtlas::Next(){
151  _position++;
152 }
153 
154 inline RealPixel HashProbabilisticAtlas::GetValue(unsigned int mapnr){
155  if (mapnr < _images.size()) return _images[mapnr]->Get(_position);
156  else {
157  std::cerr << "map identificator " << mapnr <<" out of range." <<std::endl;
158  exit(1);
159  }
160 }
161 
162 inline RealPixel HashProbabilisticAtlas::GetValue(int x, int y, int z, unsigned int mapnr){
163  if (mapnr < _images.size()) return _images[mapnr]->Get(x,y,z);
164  else {
165  std::cerr << "map identificator " << mapnr <<" out of range." <<std::endl;
166  exit(1);
167  }
168 }
169 
170 inline void HashProbabilisticAtlas::SetValue(unsigned int mapnr, RealPixel value){
171  if (mapnr < _images.size()) _images[mapnr]->Put(_position, value);
172  else {
173  std::cerr << "map identificator " << mapnr << " out of range." <<std::endl;
174  exit(1);
175  }
176 }
177 
178 inline void HashProbabilisticAtlas::SetValue(int x, int y, int z, unsigned int mapnr, RealPixel value){
179  if (mapnr < _images.size()) _images[mapnr]->Put(x,y,z, value);
180  else {
181  std::cerr << "map identificator " << mapnr <<" out of range." <<std::endl;
182  exit(1);
183  }
184 }
185 
186 inline int HashProbabilisticAtlas::GetNumberOfVoxels() const{
187  return _number_of_voxels;
188 }
189 
190 inline int HashProbabilisticAtlas::GetNumberOfMaps() const{
191  return _number_of_maps;
192 }
193 
194 inline bool HashProbabilisticAtlas::HasBackground() const{
195  return _has_background;
196 }
197 
198 template <class ImageType>
199 inline void HashProbabilisticAtlas::AddBackground(ImageType image){
200  AddImage(image);
201  _has_background=true;
202 }
203 
204 inline HashRealImage HashProbabilisticAtlas::GetImage(unsigned int mapnr) const{
205  if (mapnr < _images.size()) return *_images[mapnr];
206  else {
207  std::cerr << "map identificator " << mapnr <<" out of range." <<std::endl;
208  exit(1);
209  }
210 }
211 
212 inline HashRealImage::DataIterator HashProbabilisticAtlas::Begin(unsigned int mapnr) const
213 {
214  if (mapnr < _images.size()) return _images[mapnr]->Begin();
215  else {
216  std::cerr << "map identificator " << mapnr <<" out of range." <<std::endl;
217  exit(1);
218  }
219 }
220 inline HashRealImage::DataIterator HashProbabilisticAtlas::End(unsigned int mapnr) const
221 {
222  if (mapnr < _images.size()) return _images[mapnr]->End();
223  else {
224  std::cerr << "map identificator " << mapnr <<" out of range." <<std::endl;
225  exit(1);
226  }
227 }
228 
229 }
230 
231 #endif
string Get(const ParameterList &params, string name)
Get parameter value from parameters list.
Definition: Object.h:202
STL namespace.
Definition: IOConfig.h:41