ImageSequence.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
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 #ifndef MIRTK_ImageSequence_H
21 #define MIRTK_ImageSequence_H
22 
23 #include "mirtk/Object.h"
24 #include "mirtk/BaseImage.h"
25 #include "mirtk/Array.h"
26 
27 
28 namespace mirtk {
29 
30 
31 ////////////////////////////////////////////////////////////////////////////////
32 // Image channel
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 /**
36  * Single channel of a multi-channel image
37  *
38  * \note The image associated with the channel cannot have a fourth dimension!
39  */
40 template <class TImage = BaseImage>
41 class ImageChannel : public Object
42 {
43  mirtkObjectMacro(ImageChannel);
44 
45 public:
46 
47  // ---------------------------------------------------------------------------
48  // Types
49 
50  /// Type of image associated with this channel
51  typedef TImage ImageType;
52 
53  // ---------------------------------------------------------------------------
54  // Construction/Destruction
55 
56  /// Constructor
57  ///
58  /// \param[in] image Image associated with channel.
59  /// \param[in] manage Whether to manage memory of image.
60  /// \param[in] copy Whether to copy image if \p manage is \c false.
61  ImageChannel(ImageType *image = NULL, bool manage = false, bool copy = false);
62 
63  /// Copy constructor
64  ///
65  /// \attention If the given channel object owns the associated image and thus
66  /// would destruct it upon destruction of the channel itself, this
67  /// copy constructor takes over ownership of the channel image.
68  /// This is required for using ImageChannel as type for an STL
69  /// container which creates copies of the elements.
70  ImageChannel(const ImageChannel &);
71 
72  /// Copy constructor
73  ///
74  /// \attention If the given channel object owns the associated image and thus
75  /// would destruct it upon destruction of the channel itself, this
76  /// copy constructor takes over ownership of the channel image.
77  /// This is required for using ImageChannel as type for an STL
78  /// container which creates copies of the elements.
79  ///
80  /// \note Implemented only for TImage template argument BaseImage.
81  template <class TOtherImage>
83 
84  /// Assignment operator
85  ///
86  /// \attention If the given channel object owns the associated image and thus
87  /// would destruct it upon destruction of the channel itself, this
88  /// assignment transfer also the ownership of the channel image.
89  /// This is required for using ImageChannel as type for an STL
90  /// container which creates copies of the elements.
92 
93  /// Assignment operator
94  ///
95  /// \attention If the given channel object owns the associated image and thus
96  /// would destruct it upon destruction of the channel itself, this
97  /// assignment transfer also the ownership of the channel image.
98  /// This is required for using ImageChannel as type for an STL
99  /// container which creates copies of the elements.
100  ///
101  /// \note Implemented only for TImage template argument BaseImage.
102  template <class TOtherImage>
104 
105  /// Destructor
106  virtual ~ImageChannel();
107 
108  // ---------------------------------------------------------------------------
109  // Channel image
110 
111  /// Set image associated with this channel
112  ///
113  /// \param[in] image Image associated with this channel.
114  /// \param[in] manage Whether to manage memory of image.
115  /// \param[in] copy Whether to copy image if \p manage is \c false.
116  void Image(ImageType *image, bool manage = false, bool copy = false);
117 
118  /// Get image associated with this channel
119  ImageType *Image() const;
120 
121  // ---------------------------------------------------------------------------
122  // Data members
123 protected:
124 
125  ImageType *_Image; ///< Image associated with this channel
126  mutable bool _Manage; ///< Whether to manage the associated image
127 
128  // Copy constructor and assignment operator modify the _ManageMemory
129  // member of the const channel object which is being copied
130  template <class T> friend class ImageChannel;
131 };
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 // Multi-channel image
135 ////////////////////////////////////////////////////////////////////////////////
136 
137 /**
138  * Auxiliary type to store channels of a single frame of a temporal image sequence
139  *
140  * All channels of a given frame have the same spatial image attributes. Moreover,
141  * the temporal origin and voxel size must be identical for all channels as well.
142  * The size of the channel images in the time domain is always one.
143  *
144  * \note This data type can also be used for single multi-channel images which are
145  * not part of a temporal sequence of images.
146  */
147 template <class TChannel = ImageChannel<BaseImage> >
148 class ImageFrame : public Object
149 {
150  mirtkObjectMacro(ImageFrame);
151 
152 public:
153 
154  // ---------------------------------------------------------------------------
155  // Types
156 
157  typedef typename TChannel::ImageType ImageType; ///< Type of channel image
158  typedef TChannel ChannelType; ///< Type of a channel
159 
160  // ---------------------------------------------------------------------------
161  // Construction/Destruction
162 
163  /// Default constructor
164  ImageFrame();
165 
166  /// Copy constructor
167  ImageFrame(const ImageFrame &);
168 
169  /// Assignment operator
170  ImageFrame &operator =(const ImageFrame &);
171 
172  /// Destructor
173  virtual ~ImageFrame();
174 
175  // ---------------------------------------------------------------------------
176  // Frame
177 
178  /// Add image as channel to this frame
179  ///
180  /// \param[in] image Channel image. If the image has a fourth dimension, a
181  /// copy of each 2D/3D sub-image is added as separate channel.
182  /// \param[in] copy Whether to copy image even if it is 2D/3D only. Otherwise,
183  /// the given image is associated with the channel which also
184  /// takes over its ownership, i.e., destructing it when the
185  /// channel object itself is destructed.
186  /// \param[in] manage Whether to manage memory of input image if not copied.
187  void Add(ImageType *image, bool manage = false, bool copy = false);
188 
189  /// Clear frame
190  void Clear();
191 
192  // ---------------------------------------------------------------------------
193  // Channels
194 
195  /// Set number of image channels
196  ///
197  /// Newly added channels are initialized using the default constructor of the
198  /// ChannelType, i.e., the associated image pointers set to NULL. Existing
199  /// channels within the specified range of channels are not changed.
200  void NumberOfChannels(int);
201 
202  /// Get number of image channels
203  int NumberOfChannels() const;
204 
205  /// Get image channel
206  ChannelType &Channel(int = 0);
207 
208  /// Get image channel
209  const ChannelType &Channel(int = 0) const;
210 
211  /// Set channel image
212  ///
213  /// \param[in] idx Index of channel.
214  /// \param[in] image Image to associate with specified channel.
215  /// \param[in] manage Whether to manage memory of input image.
216  /// \param[in] copy Whether to copy image if \c manage is \c false.
217  void Image(int idx, ImageType *image, bool manage = false, bool copy = false);
218 
219  /// Get channel image
220  ImageType *Image(int = 0) const;
221 
222  // ---------------------------------------------------------------------------
223  // Attributes
224 
225  /// Get attributes of this frame
226  ///
227  /// The spatial attributes of the frame correspond to those of its channels
228  /// which are the same for all channels.
229  ///
230  /// The temporal attributes are:
231  /// - _t: Number of channels
232  /// - _dt: Temporal voxel size of frame (in ms)
233  /// - _torigin: Time coordinate of frame (in ms)
234  ImageAttributes Attributes() const;
235 
236  /// Get number of voxels per channel
237  int NumberOfVoxels() const;
238 
239  /// Get number of voxels in x dimension for each channel
240  int X() const;
241 
242  /// Get number of voxels in y dimension for each channel
243  int Y() const;
244 
245  /// Get number of voxels in z dimension for each channel
246  int Z() const;
247 
248  /// Get size of each voxel in x dimension
249  double XSize() const;
250 
251  /// Get size of each voxel in y dimension
252  double YSize() const;
253 
254  /// Get size of each voxel in z dimension
255  double ZSize() const;
256 
257  /// Get voxel size
258  void GetPixelSize(double *, double *, double *) const;
259 
260  /// Convert voxel to world coordinate
261  void ImageToWorld(double &, double &, double &) const;
262 
263  /// Convert world to voxel coordinate
264  void WorldToImage(double &, double &, double &) const;
265 
266  /// Get time of temporal frame
267  double Time() const;
268 
269  // ---------------------------------------------------------------------------
270  // Attributes
271 private:
272 
273  Array<ChannelType> _Channel; ///< Channels of image frame
274 
275 };
276 
277 ////////////////////////////////////////////////////////////////////////////////
278 // Temporal multi-channel image sequence
279 ////////////////////////////////////////////////////////////////////////////////
280 
281 /**
282  * Auxiliary type to store ordered sequence of temporal (multi-channel) image frames
283  */
284 template <class TFrame = ImageFrame<> >
285 class ImageSequence : public Object
286 {
287  mirtkObjectMacro(ImageSequence);
288 
289 public:
290 
291  // ---------------------------------------------------------------------------
292  // Types
293 
294  typedef typename TFrame::ImageType ImageType; ///< Type of images
295  typedef typename TFrame::ChannelType ChannelType; ///< Type of channels
296  typedef TFrame FrameType; ///< Type of frames
297 
298  // ---------------------------------------------------------------------------
299  // Construction/Destruction
300 
301  /// Constructor
302  ImageSequence();
303 
304  /// Copy constructor
305  ImageSequence(const ImageSequence &);
306 
307  /// Assignment operator
309 
310  /// Destructor
311  ~ImageSequence();
312 
313  // ---------------------------------------------------------------------------
314  // Sequence
315 
316  /// Add channels or frames to image sequence
317  ///
318  /// \param[in] image Image to add to this sequence. If the image has a fouth
319  /// dimension, a copy of each 2D/3D sub-image is added.
320  /// \param[in] manage Whether to manage memory of input image if not copied.
321  /// \param[in] copy Whether to copy image if \p manage is \c false even
322  /// though it has no fourth dimension.
323  void Add(ImageType *image, bool manage = false, bool copy = false);
324 
325  /// Clear image sequence
326  void Clear();
327 
328  // ---------------------------------------------------------------------------
329  // Frames
330 
331  /// Set number of frames
332  void NumberOfFrames(int) const;
333 
334  /// Get number of frames
335  int NumberOfFrames() const;
336 
337  /// Get frame of sequence
338  FrameType &Frame(int);
339 
340  /// Get frame of sequence
341  const FrameType &Frame(int) const;
342 
343  // ---------------------------------------------------------------------------
344  // Channels
345 
346  /// Set number of channels per frame
347  void NumberOfChannels(int) const;
348 
349  /// Get number of channels per frame
350  int NumberOfChannels() const;
351 
352  /// Get channel
353  ///
354  /// \param[in] idx Index of channel across all frames.
355  ChannelType &Channel(int idx);
356 
357  /// Get channel
358  ///
359  /// \param[in] idx Index of channel across all frames.
360  const ChannelType &Channel(int idx) const;
361 
362  /// Get channel
363  ///
364  /// \param[in] f Index of frame.
365  /// \param[in] c Index of channel.
366  ChannelType &Channel(int f, int c);
367 
368  /// Get channel
369  ///
370  /// \param[in] f Index of frame.
371  /// \param[in] c Index of channel.
372  const ChannelType &Channel(int f, int c) const;
373 
374  // ---------------------------------------------------------------------------
375  // Images
376 
377  /// Get number of frames times number of channels
378  int NumberOfImages() const;
379 
380  /// Set image associated with channel
381  ///
382  /// \param[in] f Index of frame.
383  /// \param[in] c Index of channel.
384  /// \param[in] image Image which is associated with the channel.
385  /// \param[in] manage Whether to manage the memory of the image.
386  /// \param[in] copy Whether to copy the image if \p manage is \c false.
387  void Image(int f, int c, ImageType *image, bool manage, bool copy);
388 
389  /// Set image associated with channel to copy of given image
390  ///
391  /// \param[in] f Index of frame.
392  /// \param[in] c Index of channel.
393  /// \param[in] image Image of which a copy is associated with the channel.
394  void Image(int f, int c, const ImageType *image);
395 
396  /// Get image associated with channel
397  ///
398  /// \param[in] idx Index of channel across all frames.
399  ImageType *Image(int idx) const;
400 
401  /// Get image associated with channel
402  ///
403  /// \param[in] f Index of frame.
404  /// \param[in] c Index of channel.
405  ImageType *Image(int f, int c) const;
406 
407  // ---------------------------------------------------------------------------
408  // Attributes
409 
410  /// Get attributes of this image sequence
411  ///
412  /// The spatial attributes of the sequence correspond to those of its frames
413  /// which are the same for all frames. The temporal attributes are:
414  /// - _t: Number of frames
415  /// - _dt: Temporal size of first frame (in ms)
416  /// - _torigin: Time coordinate of first frame (in ms)
417  ImageAttributes Attributes() const;
418 
419  /// Get number of voxels per channel
420  int NumberOfVoxels() const;
421 
422  /// Get number of voxels in x dimension for each channel
423  int X() const;
424 
425  /// Get number of voxels in y dimension for each channel
426  int Y() const;
427 
428  /// Get number of voxels in z dimension for each channel
429  int Z() const;
430 
431  /// Get number of frames
432  int T() const;
433 
434  /// Get size of each voxel in x dimension
435  double XSize() const;
436 
437  /// Get size of each voxel in y dimension
438  double YSize() const;
439 
440  /// Get size of each voxel in z dimension
441  double ZSize() const;
442 
443  /// Get voxel size
444  void GetPixelSize(double *, double *, double *) const;
445 
446  /// Convert voxel to world coordinate
447  void ImageToWorld(double &, double &, double &) const;
448 
449  /// Convert world to voxel coordinate
450  void WorldToImage(double &, double &, double &) const;
451 
452  /// Get time of temporal frame
453  double Time(int) const;
454 
455  // ---------------------------------------------------------------------------
456  // Attributes
457 private:
458 
459  Array<FrameType> _Frame; ///< Frames of image frame
460 
461 };
462 
463 
464 } // namespace mirtk
465 
466 
467 // Inline definitions
468 #include "mirtk/ImageSequence.hh"
469 
470 
471 #endif // MIRTK_ImageSequence_H
TImage ImageType
Type of image associated with this channel.
Definition: ImageSequence.h:51
TChannel::ImageType ImageType
Type of channel image.
ImageType * _Image
Image associated with this channel.
TFrame::ImageType ImageType
Type of images.
TFrame FrameType
Type of frames.
Definition: IOConfig.h:41
TChannel ChannelType
Type of a channel.
virtual ~ImageChannel()
Destructor.
bool _Manage
Whether to manage the associated image.
vtkSmartPointer< vtkPointSet > WorldToImage(vtkSmartPointer< vtkPointSet > pointset, const BaseImage *image)
Map point set points to voxel coordinates.
ImageChannel & operator=(const ImageChannel &)
ImageType * Image() const
Get image associated with this channel.
TFrame::ChannelType ChannelType
Type of channels.