SIRF  3.5.0
NiftiImageData.h
Go to the documentation of this file.
1 /*
2 SyneRBI Synergistic Image Reconstruction Framework (SIRF)
3 Copyright 2017 - 2020 University College London
4 
5 This is software developed for the Collaborative Computational
6 Project in Synergistic Reconstruction for Biomedical Imaging (formerly CCP PETMR)
7 (http://www.ccpsynerbi.ac.uk/).
8 
9 Licensed under the Apache License, Version 2.0 (the "License");
10 you may not use this file except in compliance with the License.
11 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
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 
30 #pragma once
31 
32 #include <nifti1_io.h>
33 #include <vector>
34 #include <string>
35 #include <memory>
36 #include <iostream>
37 #include <sstream>
38 #include "sirf/common/ANumRef.h"
39 #include "sirf/common/ImageData.h"
40 #include <_reg_tools.h>
41 
42 namespace sirf {
43 
76 template<class dataType>
77 class NiftiImageData : public ImageData
78 {
79 public:
80 
83  class Iterator : public BaseIter {
84  public:
85  Iterator(dataType *iter) : _iter(iter)
86  {}
87  Iterator& operator=(const Iterator& iter)
88  {
89  _iter = iter._iter;
90  _ref.copy(iter._ref);
91  return *this;
92  }
93  virtual Iterator& operator++()
94  {
95  ++_iter;
96  return *this;
97  }
98  virtual bool operator==(const BaseIter& an_iter) const
99  {
100  const Iterator& iter = (const Iterator&)an_iter;
101  return _iter == iter._iter;
102  }
103  virtual bool operator!=(const BaseIter& an_iter) const
104  {
105  const Iterator& iter = (const Iterator&)an_iter;
106  return _iter != iter._iter;
107  }
108  virtual FloatRef& operator*()
109  {
110  dataType& v = *_iter;
111  _ref.set_ptr(&v);
112  return _ref;
113  }
114  private:
115  dataType *_iter;
116  FloatRef _ref;
117  };
119  public:
120  Iterator_const(const dataType *iter) : _iter(iter)
121  {}
122  Iterator_const& operator=(const Iterator_const& iter)
123  {
124  _iter = iter._iter;
125  _ref.copy(iter._ref);
126  return *this;
127  }
128  virtual Iterator_const& operator++()
129  {
130  ++_iter;
131  return *this;
132  }
133  virtual bool operator==(const BaseIter_const& an_iter) const
134  {
135  const Iterator_const& iter = (const Iterator_const&)an_iter;
136  return _iter == iter._iter;
137  }
138  virtual bool operator!=(const BaseIter_const& an_iter) const
139  {
140  const Iterator_const& iter = (const Iterator_const&)an_iter;
141  return _iter != iter._iter;
142  }
143  virtual const FloatRef& operator*() const
144  {
145  const dataType& v = *_iter;
146  _ref.set_ptr((void*)&v);
147  return _ref;
148  }
149  private:
150  const dataType *_iter;
151  mutable FloatRef _ref;
152  };
153 
156 
158  virtual ~NiftiImageData() {}
159 
161  NiftiImageData(const NiftiImageData& to_copy);
162 
164  NiftiImageData& operator=(const NiftiImageData& to_copy);
165 
167  NiftiImageData(const ImageData& to_copy);
168 
170  NiftiImageData& operator=(const ImageData& to_copy);
171 
173  NiftiImageData(const std::string &filename);
174 
176  NiftiImageData(const nifti_image &image_nifti);
177 
179  template<class inputType>
180  NiftiImageData(const inputType * const data, const VoxelisedGeometricalInfo3D &geom, const bool is_tensor = false)
181  {
182  this->_nifti_image = create_from_geom_info(geom, is_tensor);
183 
184  // Set the datatype
185  if (typeid(inputType) == typeid(bool)) this->set_up_data(DT_BINARY);
186  else if (typeid(inputType) == typeid(signed char)) this->set_up_data(DT_INT8);
187  else if (typeid(inputType) == typeid(signed short)) this->set_up_data(DT_INT16);
188  else if (typeid(inputType) == typeid(signed int)) this->set_up_data(DT_INT32);
189  else if (typeid(inputType) == typeid(float)) this->set_up_data(DT_FLOAT32);
190  else if (typeid(inputType) == typeid(double)) this->set_up_data(DT_FLOAT64);
191  else if (typeid(inputType) == typeid(unsigned char)) this->set_up_data(DT_UINT8);
192  else if (typeid(inputType) == typeid(unsigned short)) this->set_up_data(DT_UINT16);
193  else if (typeid(inputType) == typeid(unsigned int)) this->set_up_data(DT_UINT32);
194  else if (typeid(inputType) == typeid(signed long long)) this->set_up_data(DT_INT64);
195  else if (typeid(inputType) == typeid(unsigned long long)) this->set_up_data(DT_UINT64);
196  else if (typeid(inputType) == typeid(long double)) this->set_up_data(DT_FLOAT128);
197  else {
198  std::stringstream ss;
199  ss << "NiftiImageData constructor from raw array: ";
200  ss << typeid (inputType).name();
201  ss << " (bytes per voxel: ";
202  ss << sizeof(inputType) << ").";
203  throw std::runtime_error(ss.str());
204  }
205 
206  for (unsigned i=0; i<_nifti_image->nvox; ++i)
207  this->_data[i] = dataType(data[i]);
208  }
209 
211  static std::shared_ptr<nifti_image> create_from_geom_info(const VoxelisedGeometricalInfo3D &geom, const bool is_tensor=false, const NREG_TRANS_TYPE tensor_type=NREG_TRANS_TYPE::DEF_FIELD);
212 
214  static void construct_NiftiImageData_from_complex_im_real_component(std::shared_ptr<NiftiImageData> &out_sptr, const std::shared_ptr<const ImageData> in_sptr);
215 
217  static void construct_NiftiImageData_from_complex_im_imag_component(std::shared_ptr<NiftiImageData> &out_sptr, const std::shared_ptr<const ImageData> in_sptr);
218 
220  static void construct_NiftiImageData_from_complex_im(std::shared_ptr<NiftiImageData> &out_real_sptr, std::shared_ptr<NiftiImageData> &out_imag_sptr, const std::shared_ptr<const ImageData> in_sptr);
221 
223  bool operator==(const NiftiImageData &other) const;
224 
226  bool operator!=(const NiftiImageData &other) const;
227 
230 
233  {
234  lhs += rhs;
235  return lhs;
236  }
237 
240 
243  {
244  lhs -= rhs;
245  return lhs;
246  }
247 
249  NiftiImageData& operator+=(const float);
250 
252  friend NiftiImageData operator+(NiftiImageData lhs, const float val)
253  {
254  lhs += val;
255  return lhs;
256  }
257 
259  NiftiImageData& operator-=(const float);
260 
262  friend NiftiImageData operator-(NiftiImageData lhs, const float val)
263  {
264  lhs -= val;
265  return lhs;
266  }
267 
269  NiftiImageData& operator*=(const float);
272 
274  friend NiftiImageData operator*(NiftiImageData lhs, const float val)
275  {
276  lhs *= val;
277  return lhs;
278  }
279 
282  {
283  lhs *= rhs;
284  return lhs;
285  }
286 
288  NiftiImageData& operator/=(const float);
289  // /// Division operator
291 
292  // /// Division operator
293  friend NiftiImageData operator/(NiftiImageData lhs, const float val)
294  {
295  lhs /= val;
296  return lhs;
297  }
298 
301  {
302  lhs /= rhs;
303  return lhs;
304  }
305 
307  float operator()(const int index) const;
308 
310  float &operator()(const int index);
311 
313  float operator()(const int index[7]) const;
314 
316  float &operator()(const int index[7]);
317 
319  float operator()(const int x, const int y, const int z, const int t=0, const int u=0, const int v=0, const int w=0) const;
320 
322  float &operator()(const int x, const int y, const int z, const int t=0, const int u=0, const int v=0, const int w=0);
323 
325  bool is_initialised() const { return (_nifti_image && _data && _nifti_image->datatype == DT_FLOAT32 ? true : false); }
326 
328  std::shared_ptr<const nifti_image> get_raw_nifti_sptr() const;
329 
331  std::shared_ptr<nifti_image> get_raw_nifti_sptr();
332 
335  virtual void write(const std::string &filename, const int datatype) const;
336 
338  virtual void write(const std::string &filename) const { this->write(filename,-1); }
339 
341  float get_max() const;
342 
344  float get_min() const;
345 
347  float get_mean() const;
348 
350  float get_variance() const;
351 
353  float get_standard_deviation() const;
354 
356  float get_sum() const;
357 
359  unsigned get_nan_count() const;
360 
362  void fill(const float v);
363 
365  void fill(const dataType *v);
366 
368  void fill(const NiftiImageData &im);
369 
371  float get_norm(const NiftiImageData&) const;
372 
374  const int* get_dimensions() const;
375 
377  size_t get_num_voxels() const;
378 
380  void print_header() const;
381 
383  static void print_headers(const std::vector<const NiftiImageData*> &ims);
384 
386  void crop(const int min_index[7], const int max_index[7]);
387 
389  void pad(const int min_index[7], const int max_index[7], const dataType val = 0);
390 
392  int get_1D_index(const int idx[7]) const;
393 
396 
398  static bool are_equal_to_given_accuracy(const NiftiImageData &im1, const NiftiImageData &im2, const float required_accuracy_compared_to_max);
399 
401  bool is_in_bounds(const int index[7]) const;
402 
404  bool is_in_bounds(const int index) const;
405 
407  bool is_same_size(const NiftiImageData &im) const;
408 
410  static bool do_nifti_image_metadata_match(const NiftiImageData &im1, const NiftiImageData &im2, bool verbose);
411 
413  static void dump_headers(const std::vector<const NiftiImageData*> &ims);
414 
416  template<typename T>
417  static void dump_nifti_element(const std::vector<const NiftiImageData*> &ims, const std::string &name, const T &call_back);
418 
420  template<typename T>
421  static void dump_nifti_element(const std::vector<const NiftiImageData*> &ims, const std::string &name, const T &call_back, const unsigned num_elems);
422 
423  static std::string get_headers(const std::vector<const NiftiImageData<dataType>*> &ims);
424  template<typename T>
425  static std::string get_nifti_element(const std::vector<const NiftiImageData*> &ims, const std::string &name, const T &call_back);
426  template<typename T>
427  static std::string get_nifti_element(const std::vector<const NiftiImageData*> &ims, const std::string &name, const T &call_back, const unsigned num_elems);
428 
431  void set_voxel_spacing(const float factors[3], const int interpolation_order);
432 
434  void kernel_convolution(const float sigma, NREG_CONV_KERNEL_TYPE conv_type = GAUSSIAN_KERNEL);
435 
437  bool get_contains_nans() const { return (this->get_nan_count() > 0); }
438 
440  void flip_along_axis(const unsigned axis);
441 
443  void mirror_along_axis(const unsigned axis);
444 
446  dataType get_inner_product(const NiftiImageData &other) const;
447 
449  void normalise_zero_and_one();
450 
452  void standardise();
453 
454 protected:
455 
456  enum NiftiImageDataType { _general, _3D, _3DTensor, _3DDisp, _3DDef};
457 
458  enum MathsType { ADD, sub, mul, div};
459 
461  std::shared_ptr<nifti_image> _nifti_image;
462 
464  float *_data = NULL;
465 
468 
470  void check_dimensions(const enum NiftiImageDataType image_type = _general);
471 
473  void set_up_data(const int original_datatype);
474 
476  void maths(const NiftiImageData& c, const MathsType type);
477 
479  void maths(const float val, const MathsType type);
480 
482  static void open_nifti_image(std::shared_ptr<nifti_image> &image, const std::string &filename);
483 
485  static void copy_nifti_image(std::shared_ptr<nifti_image> &output_image_sptr, const std::shared_ptr<nifti_image> &image_to_copy_sptr);
486 
487 private:
488 
490  void change_datatype(const int datatype);
491 
493  template<typename newType>
494  static void change_datatype(NiftiImageData<dataType> &im)
495  {
496  if (im.get_raw_nifti_sptr()->datatype == DT_BINARY) return change_datatype<newType,bool> (im);
497  if (im.get_raw_nifti_sptr()->datatype == DT_INT8) return change_datatype<newType,signed char> (im);
498  if (im.get_raw_nifti_sptr()->datatype == DT_INT16) return change_datatype<newType,signed short> (im);
499  if (im.get_raw_nifti_sptr()->datatype == DT_INT32) return change_datatype<newType,signed int> (im);
500  if (im.get_raw_nifti_sptr()->datatype == DT_FLOAT32) return change_datatype<newType,float> (im);
501  if (im.get_raw_nifti_sptr()->datatype == DT_FLOAT64) return change_datatype<newType,double> (im);
502  if (im.get_raw_nifti_sptr()->datatype == DT_UINT8) return change_datatype<newType,unsigned char> (im);
503  if (im.get_raw_nifti_sptr()->datatype == DT_UINT16) return change_datatype<newType,unsigned short> (im);
504  if (im.get_raw_nifti_sptr()->datatype == DT_UINT32) return change_datatype<newType,unsigned int> (im);
505  if (im.get_raw_nifti_sptr()->datatype == DT_INT64) return change_datatype<newType,signed long long> (im);
506  if (im.get_raw_nifti_sptr()->datatype == DT_UINT64) return change_datatype<newType,unsigned long long>(im);
507  if (im.get_raw_nifti_sptr()->datatype == DT_FLOAT128) return change_datatype<newType,long double> (im);
508 
509  std::stringstream ss;
510  ss << "change_datatype not implemented for your data type: ";
511  ss << nifti_datatype_string(im.get_raw_nifti_sptr()->datatype);
512  ss << " (bytes per voxel: ";
513  ss << im.get_raw_nifti_sptr()->nbyper << ").";
514  throw std::runtime_error(ss.str());
515  }
516 
518  template<typename newType, typename oldType>
519  static void change_datatype(NiftiImageData &image)
520  {
521  // If the two types are equal, nothing to be done.
522  if (typeid (newType) == typeid(oldType))
523  return;
524 
525  nifti_image *im = image.get_raw_nifti_sptr().get();
526 
527  // Copy the original array
528  oldType *originalArray = static_cast<oldType*>(malloc(im->nvox*im->nbyper));
529  memcpy(originalArray, im->data, im->nvox*im->nbyper);
530  // Reset image array
531  free(im->data);
532 
533  // Set the datatype
534  if (typeid(newType) == typeid(bool)) im->datatype = DT_BINARY;
535  else if (typeid(newType) == typeid(signed char)) im->datatype = DT_INT8;
536  else if (typeid(newType) == typeid(signed short)) im->datatype = DT_INT16;
537  else if (typeid(newType) == typeid(signed int)) im->datatype = DT_INT32;
538  else if (typeid(newType) == typeid(float)) im->datatype = DT_FLOAT32;
539  else if (typeid(newType) == typeid(double)) im->datatype = DT_FLOAT64;
540  else if (typeid(newType) == typeid(unsigned char)) im->datatype = DT_UINT8;
541  else if (typeid(newType) == typeid(unsigned short)) im->datatype = DT_UINT16;
542  else if (typeid(newType) == typeid(unsigned int)) im->datatype = DT_UINT32;
543  else if (typeid(newType) == typeid(signed long long)) im->datatype = DT_INT64;
544  else if (typeid(newType) == typeid(unsigned long long)) im->datatype = DT_UINT64;
545  else if (typeid(newType) == typeid(long double)) im->datatype = DT_FLOAT128;
546  else {
547  std::stringstream ss;
548  ss << "change_datatype not implemented for your new data type: ";
549  ss << typeid (newType).name();
550  ss << " (bytes per voxel: ";
551  ss << sizeof(newType) << ").";
552  throw std::runtime_error(ss.str());
553  }
554 
555  // Set the nbyper and swap size from the datatype
556  nifti_datatype_sizes(im->datatype, &im->nbyper, &im->swapsize);
557 
558  // Copy data
559  im->data = static_cast<void*>(calloc(im->nvox,sizeof(newType)));
560  newType *dataPtr = static_cast<newType*>(im->data);
561  for (size_t i = 0; i < im->nvox; i++)
562  dataPtr[i] = newType(originalArray[i]);
563 
564  free(originalArray);
565  return;
566  }
567 
568  // ------------------------------------------------------------------------------ //
569  // Pure virtual methods from ImageData
570  // ------------------------------------------------------------------------------ //
571 public:
573  std::unique_ptr<NiftiImageData> clone() const
574  {
575  return std::unique_ptr<NiftiImageData>(this->clone_impl());
576  }
577  virtual Iterator& begin()
578  {
579  _begin.reset(new Iterator(_data));
580  return *_begin;
581  }
582  virtual Iterator_const& begin() const
583  {
584  _begin_const.reset(new Iterator_const(_data));
585  return *_begin_const;
586  }
587  virtual Iterator& end()
588  {
589  _end.reset(new Iterator(_data+_nifti_image->nvox));
590  return *_end;
591  }
592  virtual Iterator_const& end() const
593  {
594  _end_const.reset(new Iterator_const(_data+_nifti_image->nvox));
595  return *_end_const;
596  }
597  /*
598  unsigned int items() const { return 1; }
599  virtual void dot (const DataContainer& a_x, void* ptr) const;
600  virtual void axpby (const void* ptr_a, const DataContainer& a_x, const void* ptr_b, const DataContainer& a_y);
601  virtual void xapyb (const DataContainer& a_x, const void* ptr_a, const DataContainer& a_y, const void* ptr_b);
602  virtual void xapyb (const DataContainer& a_x, const DataContainer& a_a, const DataContainer& a_y, const DataContainer& a_b);
603  virtual float norm() const;
604  virtual void scale(float s);
605  virtual void multiply (const DataContainer& a_x, const DataContainer& a_y);
606  virtual void divide (const DataContainer& a_x, const DataContainer& a_y);
607  virtual void maximum(const DataContainer& x, const DataContainer& y);
608  virtual void minimum(const DataContainer& x, const DataContainer& y);
609 */
610 protected:
612  virtual NiftiImageData* clone_impl() const
613  {
614  return new NiftiImageData(*this);
615  }
616  virtual ObjectHandle<DataContainer>* new_data_container_handle() const
617  {
618  return new ObjectHandle<DataContainer>
619  (std::shared_ptr<DataContainer>(new NiftiImageData));
620  }
621 
622 public:
623  unsigned int items() const { return 1; }
625  virtual void sum (void* ptr) const;
626  virtual void max (void* ptr) const;
627  virtual void dot (const DataContainer& a_x, void* ptr) const;
628  virtual void axpby (const void* ptr_a, const DataContainer& a_x, const void* ptr_b, const DataContainer& a_y);
629  virtual void xapyb (const DataContainer& a_x, const void* ptr_a, const DataContainer& a_y, const void* ptr_b);
630  virtual void xapyb (const DataContainer& a_x, const DataContainer& a_a, const DataContainer& a_y, const DataContainer& a_b);
631  virtual void xapyb(
632  const DataContainer& a_x, const void* ptr_a,
633  const DataContainer& a_y, const DataContainer& a_b);
634  virtual float norm() const;
635  virtual void multiply (const DataContainer& a_x, const DataContainer& a_y);
636  virtual void divide (const DataContainer& a_x, const DataContainer& a_y);
637  virtual void maximum(const DataContainer& x, const DataContainer& y);
638  virtual void minimum(const DataContainer& x, const DataContainer& y);
639  virtual void power(const DataContainer& x, const DataContainer& y);
640  virtual void multiply(const DataContainer& a_x, const void* a_y);
641  virtual void add(const DataContainer& a_x, const void* a_y);
642  virtual void maximum(const DataContainer& x, const void* a_y);
643  virtual void minimum(const DataContainer& x, const void* a_y);
644  virtual void power(const DataContainer& x, const void* a_y);
645  virtual void exp(const DataContainer& x);
646  virtual void log(const DataContainer& x);
647  virtual void sqrt(const DataContainer& x);
648  virtual void sign(const DataContainer& x);
649  virtual void abs(const DataContainer& x);
650 
651  virtual Dimensions dimensions() const
652  {
653  Dimensions dim;
654  int *d = _nifti_image->dim;
655  dim["x"] = d[1];
656  dim["y"] = d[2];
657  dim["z"] = d[3];
658  dim["t"] = d[4];
659  dim["u"] = d[5];
660  dim["v"] = d[6];
661  dim["w"] = d[7];
662  return dim;
663  }
664  void unary_op(const DataContainer& a_x, dataType(*f)(dataType));
665  void semibinary_op(const DataContainer& a_x, const void* a_y, dataType(*f)(dataType, dataType));
666  void binary_op(const DataContainer& a_x, const DataContainer& a_y, dataType(*f)(dataType, dataType));
668  virtual void set_up_geom_info();
669 protected:
670  mutable std::shared_ptr<Iterator> _begin;
671  mutable std::shared_ptr<Iterator> _end;
672  mutable std::shared_ptr<Iterator_const> _begin_const;
673  mutable std::shared_ptr<Iterator_const> _end_const;
674 };
675 }
Definition: DataHandle.h:159
Definition: ANumRef.h:68
Definition: ImageData.h:52
Definition: ImageData.h:44
Definition: ImageData.h:38
Definition: NiftiImageData.h:118
Definition: NiftiImageData.h:83
Definition: NiftiImageData.h:78
static bool are_equal_to_given_accuracy(const NiftiImageData &im1, const NiftiImageData &im2, const float required_accuracy_compared_to_max)
Check if the norms of two images are equal to a given accuracy.
Definition: NiftiImageData.cpp:1680
float get_mean() const
Get mean.
Definition: NiftiImageData.cpp:437
static void dump_nifti_element(const std::vector< const NiftiImageData * > &ims, const std::string &name, const T &call_back)
Dump nifti element.
Definition: NiftiImageData.cpp:1264
NiftiImageData & operator*=(const float)
Multiplication operator.
Definition: NiftiImageData.cpp:292
virtual void exp(const DataContainer &x)
*this = the elementwise exp(x)
Definition: NiftiImageData.cpp:1996
static void construct_NiftiImageData_from_complex_im_imag_component(std::shared_ptr< NiftiImageData > &out_sptr, const std::shared_ptr< const ImageData > in_sptr)
Construct NiftiImageData from the imaginary component of a complex SIRF ImageData.
Definition: NiftiImageData.cpp:228
unsigned get_nan_count() const
Get nan count.
Definition: NiftiImageData.cpp:477
virtual void axpby(const void *ptr_a, const DataContainer &a_x, const void *ptr_b, const DataContainer &a_y)
*this = the linear combination of x and y
Definition: NiftiImageData.cpp:1776
virtual float norm() const
returns the norm of this container viewed as a vector
Definition: NiftiImageData.cpp:1856
virtual void write(const std::string &filename, const int datatype) const
Definition: NiftiImageData.cpp:383
float get_min() const
Get min.
Definition: NiftiImageData.cpp:427
friend NiftiImageData operator+(NiftiImageData lhs, const NiftiImageData &rhs)
Addition operator.
Definition: NiftiImageData.h:232
virtual void add(const DataContainer &a_x, const void *a_y)
*this = the sum x + y with scalar y
Definition: NiftiImageData.cpp:1941
float get_norm(const NiftiImageData &) const
Get norm.
Definition: NiftiImageData.cpp:521
int get_original_datatype() const
Get original datatype.
Definition: NiftiImageData.h:395
float * _data
Data.
Definition: NiftiImageData.h:464
friend NiftiImageData operator/(NiftiImageData lhs, const NiftiImageData &rhs)
Division operator.
Definition: NiftiImageData.h:300
friend NiftiImageData operator*(NiftiImageData lhs, const float val)
Multiplication operator.
Definition: NiftiImageData.h:274
virtual void multiply(const DataContainer &a_x, const DataContainer &a_y)
*this = the elementwise product x*y
Definition: NiftiImageData.cpp:1927
void fill(const float v)
Fill.
Definition: NiftiImageData.cpp:491
static void open_nifti_image(std::shared_ptr< nifti_image > &image, const std::string &filename)
Open nifti image.
Definition: NiftiImageData.cpp:646
const int * get_dimensions() const
Get data size in each dimension.
Definition: NiftiImageData.cpp:543
bool operator!=(const NiftiImageData &other) const
Equality operator.
Definition: NiftiImageData.cpp:258
NiftiImageData & operator=(const NiftiImageData &to_copy)
Assignment.
Definition: NiftiImageData.cpp:71
virtual void minimum(const DataContainer &x, const DataContainer &y)
*this = the elementwise min(x, y)
Definition: NiftiImageData.cpp:1969
virtual void abs(const DataContainer &x)
*this = the elementwise abs(x)
Definition: NiftiImageData.cpp:2020
virtual void maximum(const DataContainer &x, const DataContainer &y)
*this = the elementwise max(x, y)
Definition: NiftiImageData.cpp:1955
virtual void xapyb(const DataContainer &a_x, const void *ptr_a, const DataContainer &a_y, const void *ptr_b)
alternative interface to the above
Definition: NiftiImageData.cpp:1819
void set_up_data(const int original_datatype)
Set up datatype. Set to float if not already, store the original type.
Definition: NiftiImageData.cpp:997
virtual void divide(const DataContainer &a_x, const DataContainer &a_y)
*this = the elementwise ratio x / y
Definition: NiftiImageData.cpp:1948
static void dump_headers(const std::vector< const NiftiImageData * > &ims)
Dump info of multiple nifti images.
Definition: NiftiImageData.cpp:1152
friend NiftiImageData operator+(NiftiImageData lhs, const float val)
Addition operator.
Definition: NiftiImageData.h:252
float get_max() const
Get max.
Definition: NiftiImageData.cpp:417
virtual void write(const std::string &filename) const
Write.
Definition: NiftiImageData.h:338
void check_dimensions(const enum NiftiImageDataType image_type=_general)
Check dimensions. Don't require anything for this class.
Definition: NiftiImageData.cpp:555
virtual void sum(void *ptr) const
below all void* are actually float*
Definition: NiftiImageData.cpp:1753
void pad(const int min_index[7], const int max_index[7], const dataType val=0)
Pad image with value. Give number of voxels to increase in min and max directions....
Definition: NiftiImageData.cpp:867
void maths(const NiftiImageData &c, const MathsType type)
Add, subract image from another.
Definition: NiftiImageData.cpp:604
int get_1D_index(const int idx[7]) const
get 1D index from ND index
Definition: NiftiImageData.cpp:965
float operator()(const int index) const
Access data element via 1D index (const)
Definition: NiftiImageData.cpp:319
friend NiftiImageData operator-(NiftiImageData lhs, const float val)
Subtraction operator.
Definition: NiftiImageData.h:262
bool operator==(const NiftiImageData &other) const
Equality operator.
Definition: NiftiImageData.cpp:250
NiftiImageData & operator+=(const NiftiImageData &rhs)
Addition operator.
Definition: NiftiImageData.cpp:264
float get_sum() const
Get sum.
Definition: NiftiImageData.cpp:465
std::unique_ptr< NiftiImageData > clone() const
Clone and return as unique pointer.
Definition: NiftiImageData.h:573
NiftiImageData & operator/=(const float)
Division operator.
Definition: NiftiImageData.cpp:305
static void copy_nifti_image(std::shared_ptr< nifti_image > &output_image_sptr, const std::shared_ptr< nifti_image > &image_to_copy_sptr)
Copy nifti image.
Definition: NiftiImageData.cpp:664
bool is_initialised() const
Is the image initialised? (Should be unless default constructor was used.)
Definition: NiftiImageData.h:325
virtual void set_up_geom_info()
Set up the geometrical info. Use qform preferentially over sform.
Definition: NiftiImageData.cpp:2026
virtual void sign(const DataContainer &x)
*this = the elementwise sign(x)
Definition: NiftiImageData.cpp:2014
std::shared_ptr< nifti_image > _nifti_image
Image data as a nifti object.
Definition: NiftiImageData.h:461
virtual void dot(const DataContainer &a_x, void *ptr) const
calculates the dot product of this container with another one
Definition: NiftiImageData.cpp:1741
friend NiftiImageData operator*(NiftiImageData lhs, const NiftiImageData &rhs)
Multiplication operator.
Definition: NiftiImageData.h:281
bool is_in_bounds(const int index[7]) const
Point is in bounds?
Definition: NiftiImageData.cpp:1028
friend NiftiImageData operator-(NiftiImageData lhs, const NiftiImageData &rhs)
Subtraction operator.
Definition: NiftiImageData.h:242
NiftiImageData(const inputType *const data, const VoxelisedGeometricalInfo3D &geom, const bool is_tensor=false)
Construct from array.
Definition: NiftiImageData.h:180
virtual void sqrt(const DataContainer &x)
*this = the elementwise sqrt(x)
Definition: NiftiImageData.cpp:2008
virtual NiftiImageData * clone_impl() const
Clone helper function. Don't use.
Definition: NiftiImageData.h:612
virtual ~NiftiImageData()
Destructor.
Definition: NiftiImageData.h:158
static bool do_nifti_image_metadata_match(const NiftiImageData &im1, const NiftiImageData &im2, bool verbose)
Do nifti image metadatas match?
Definition: NiftiImageData.cpp:1076
void set_voxel_spacing(const float factors[3], const int interpolation_order)
Definition: NiftiImageData.cpp:1429
bool is_same_size(const NiftiImageData &im) const
Images are same size.
Definition: NiftiImageData.cpp:1043
NiftiImageData()
Constructor.
Definition: NiftiImageData.h:155
float get_variance() const
Get variance.
Definition: NiftiImageData.cpp:448
void kernel_convolution(const float sigma, NREG_CONV_KERNEL_TYPE conv_type=GAUSSIAN_KERNEL)
Kernel convolution.
Definition: NiftiImageData.cpp:1578
dataType get_inner_product(const NiftiImageData &other) const
Inner product of two images.
Definition: NiftiImageData.cpp:1671
void mirror_along_axis(const unsigned axis)
Mirror the image along a given axis (This will change handedness of image)
Definition: NiftiImageData.cpp:1660
float get_standard_deviation() const
Get standard deviation.
Definition: NiftiImageData.cpp:459
int _original_datatype
Original datatype.
Definition: NiftiImageData.h:467
static std::shared_ptr< nifti_image > create_from_geom_info(const VoxelisedGeometricalInfo3D &geom, const bool is_tensor=false, const NREG_TRANS_TYPE tensor_type=NREG_TRANS_TYPE::DEF_FIELD)
Create NiftiImageData from geometrical info.
Definition: NiftiImageData.cpp:127
void normalise_zero_and_one()
Normalise image between 0 and 1.
Definition: NiftiImageData.cpp:694
std::shared_ptr< const nifti_image > get_raw_nifti_sptr() const
Get image as nifti as const.
Definition: NiftiImageData.cpp:367
void flip_along_axis(const unsigned axis)
Flip the image along a given axis (Rotation of 180 degrees about axis)
Definition: NiftiImageData.cpp:1649
virtual void power(const DataContainer &x, const DataContainer &y)
*this = the elementwise pow(x, y)
Definition: NiftiImageData.cpp:1983
NiftiImageData & operator-=(const NiftiImageData &rhs)
Subtraction operator.
Definition: NiftiImageData.cpp:271
void standardise()
Standardise (subtract mean and divide by standard deviation).
Definition: NiftiImageData.cpp:704
static void print_headers(const std::vector< const NiftiImageData * > &ims)
Print multiple header info.
Definition: NiftiImageData.cpp:740
static void construct_NiftiImageData_from_complex_im_real_component(std::shared_ptr< NiftiImageData > &out_sptr, const std::shared_ptr< const ImageData > in_sptr)
Construct NiftiImageData from the real component of a complex SIRF ImageData.
Definition: NiftiImageData.cpp:216
static void construct_NiftiImageData_from_complex_im(std::shared_ptr< NiftiImageData > &out_real_sptr, std::shared_ptr< NiftiImageData > &out_imag_sptr, const std::shared_ptr< const ImageData > in_sptr)
Construct two NiftiImageData from a complex SIRF ImageData.
Definition: NiftiImageData.cpp:243
size_t get_num_voxels() const
Get total number of voxels.
Definition: NiftiImageData.cpp:549
virtual void log(const DataContainer &x)
*this = the elementwise log(x)
Definition: NiftiImageData.cpp:2002
void print_header() const
Print header info.
Definition: NiftiImageData.cpp:733
virtual void max(void *ptr) const
calculates the value of this container's element with the largest real part
Definition: NiftiImageData.cpp:1763
bool get_contains_nans() const
Does the image contain any NaNs?
Definition: NiftiImageData.h:437
void crop(const int min_index[7], const int max_index[7])
Crop. Set to -1 to leave unchanged.
Definition: NiftiImageData.cpp:747
Definition: GeometricalInfo.h:50
Abstract data container.
Definition: GeometricalInfo.cpp:141