ProbabilisticAtlas.h
1 /*
2  * Developing brain Region Annotation With Expectation-Maximization (Draw-EM)
3  *
4  * Copyright 2013-2016 Imperial College London
5  * Copyright 2013-2016 Wenzhe Shi
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 _MIRTKPROBABILISTICATLAS_H
22 
23 #define _MIRTKPROBABILISTICATLAS_H
24 
25 #include "mirtk/Object.h"
26 
27 #include "mirtk/Image.h"
28 
29 #include <vector>
30 
31 
32 /**
33 
34 Atlas probability map class
35 
36  */
37 using namespace std;
38 namespace mirtk {
39 
40 
41 class ProbabilisticAtlas : public Object
42 {
43  mirtkObjectMacro(ProbabilisticAtlas);
44 
45  // Vector of probability maps
46  vector<RealImage> _images;
47 
48  // Vector of pointers to probability maps
49  vector<RealPixel *> _pointers;
50 
51  ///Number of voxels
52  int _number_of_voxels;
53 
54  ///Number of voxels
55  int _number_of_tissues;
56 
57  /// Hard segmentation
58  RealImage _segmentation;
59 
60 
61 public:
62 
63  /// Constructor
64  ProbabilisticAtlas();
65 
66  /// swap images within atlas
67  void SwapImages(int, int);
68 
69  /// Adds new image (channel)
70  void AddImage(RealImage image);
71 
72  /// Moves pointers in all images to the first voxel
73  void First();
74 
75  /// Moves pointers in all images to the next voxel
76  void Next();
77 
78  ///Returns intensity value at pointer
79  RealPixel GetValue(unsigned int channel);
80 
81  ///Returns intensity value at position
82  RealPixel GetValue(int x, int y, int z, unsigned int tissue);
83 
84  ///Sets intensity value at pointer
85  void SetValue(unsigned int tissue, RealPixel value);
86 
87  ///Sets intensity value at position
88  void SetValue(int x, int y, int z, unsigned int tissue, RealPixel value);
89 
90  /// Returns number of voxels
91  int GetNumberOfVoxels();
92 
93  /// Add probability maps
94  void AddProbabilityMaps(int, RealImage **atlas);
95 
96  /// Computes probability map for the last tissue type
97  void AddBackground();
98 
99  /// normalize atlas without adding background
100  void NormalizeAtlas();
101 
102  /// normalize atlas while adding background
103  void NormalizeAtlas(RealImage background);
104 
105  ///Returns number of tissues
106  int GetNumberOfTissues();
107 
108  /// Write
109  void Write(int, const char *);
110 
111  /// Computes hard segmentation
112  RealImage ComputeHardSegmentation();
113 
114  /// Extract a label from hard segmentation
115  void ExtractLabel(int label, RealImage& image);
116 
117  /// Writes hard segmentation into a file
118  void WriteHardSegmentation(const char *filename);
119 
120  RealImage GetImage(int);
121 
122 };
123 
124 inline void ProbabilisticAtlas::First()
125 {
126  unsigned int i;
127  for (i=0; i<_pointers.size(); i++) _pointers[i] = _images[i].GetPointerToVoxels();
128 }
129 
130 inline void ProbabilisticAtlas::Next()
131 {
132  unsigned int i;
133  for (i=0; i<_pointers.size(); i++) _pointers[i]++;
134 }
135 
136 inline RealPixel ProbabilisticAtlas::GetValue(unsigned int channel)
137 {
138  if (channel < _images.size()) return *_pointers[channel];
139  else {
140  std::cerr << "Channel identificator " << channel <<" out of range." <<std::endl;
141  exit(1);
142  }
143 }
144 
145 inline RealPixel ProbabilisticAtlas::GetValue(int x, int y, int z, unsigned int channel)
146 {
147  if (channel < _images.size()) return _images[channel].Get(x,y,z);
148  else {
149  std::cerr << "Channel identificator " << channel <<" out of range." <<std::endl;
150  exit(1);
151  }
152 }
153 
154 inline void ProbabilisticAtlas::SetValue(unsigned int channel, RealPixel value)
155 {
156  if (channel < _images.size()) *_pointers[channel] = value;
157  else {
158  std::cerr << "Channel identificator " << channel << " out of range." <<std::endl;
159  exit(1);
160  }
161 }
162 
163 inline void ProbabilisticAtlas::SetValue(int x, int y, int z, unsigned int channel, RealPixel value)
164 {
165  if (channel < _images.size()) _images[channel].Put( x, y, z, value);
166  else {
167  std::cerr << "Channel identificator " << channel <<" out of range." <<std::endl;
168  exit(1);
169  }
170 }
171 
172 inline int ProbabilisticAtlas::GetNumberOfVoxels()
173 {
174  return _number_of_voxels;
175 }
176 
177 inline int ProbabilisticAtlas::GetNumberOfTissues()
178 {
179  return _number_of_tissues;
180 }
181 
182 }
183 
184 #endif
string Get(const ParameterList &params, string name)
Get parameter value from parameters list.
Definition: Object.h:202
STL namespace.
Definition: IOConfig.h:41