Medical Imaging Interaction Toolkit  2023.04.00
Medical Imaging Interaction Toolkit
mitkImage.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkImage_h
14 #define mitkImage_h
15 
16 #include "mitkBaseData.h"
17 #include "mitkImageAccessorBase.h"
18 #include "mitkImageDataItem.h"
19 #include "mitkImageDescriptor.h"
20 #include "mitkImageVtkAccessor.h"
21 #include "mitkLevelWindow.h"
22 #include "mitkPlaneGeometry.h"
23 #include "mitkSlicedData.h"
24 #include <MitkCoreExports.h>
26 
27 #ifndef __itkHistogram_h
28 #include <itkHistogram.h>
29 #endif
30 
31 class vtkImageData;
32 
33 namespace itk
34 {
35  template <class T>
37 }
38 
39 namespace mitk
40 {
41  class SubImageSelector;
42  class ImageTimeSelector;
43 
44  class ImageStatisticsHolder;
45 
70  {
71  friend class SubImageSelector;
72 
73  friend class ImageAccessorBase;
74  friend class ImageVtkAccessor;
75  friend class ImageVtkReadAccessor;
76  friend class ImageVtkWriteAccessor;
77  friend class ImageReadAccessor;
78  friend class ImageWriteAccessor;
79 
80  public:
82 
83  itkFactorylessNewMacro(Self);
84 
85  itkCloneMacro(Self);
86 
89  typedef itk::Statistics::Histogram<double> HistogramType;
91 
95  {
99  DontManageMemory = ReferenceMemory
100  };
101 
107  typedef std::vector<ImageDataItemPointer> ImageDataItemPointerArray;
108 
109  public:
113  const mitk::PixelType GetPixelType(int n = 0) const;
114 
118  unsigned int GetDimension() const;
119 
125  unsigned int GetDimension(int i) const;
126 
127  public:
131  virtual vtkImageData *GetVtkImageData(int t = 0, int n = 0);
132  virtual const vtkImageData *GetVtkImageData(int t = 0, int n = 0) const;
133 
137  bool IsSliceSet(int s = 0, int t = 0, int n = 0) const override;
138 
142  bool IsVolumeSet(int t = 0, int n = 0) const override;
143 
147  bool IsChannelSet(int n = 0) const override;
148 
161  virtual bool SetSlice(const void *data, int s = 0, int t = 0, int n = 0);
162 
175  virtual bool SetVolume(const void *data, int t = 0, int n = 0);
176 
189  virtual bool SetChannel(const void *data, int n = 0);
190 
200  virtual bool SetImportSlice(
201  void *data, int s = 0, int t = 0, int n = 0, ImportMemoryManagementType importMemoryManagement = CopyMemory);
202 
212  virtual bool SetImportVolume(void *data,
213  int t = 0,
214  int n = 0,
215  ImportMemoryManagementType importMemoryManagement = CopyMemory);
216 
217  virtual bool SetImportVolume(const void *const_data, int t = 0, int n = 0);
218 
228  virtual bool SetImportChannel(void *data,
229  int n = 0,
230  ImportMemoryManagementType importMemoryManagement = CopyMemory);
231 
236  virtual void Initialize(const mitk::PixelType &type,
237  unsigned int dimension,
238  const unsigned int *dimensions,
239  unsigned int channels = 1);
240 
249  virtual void Initialize(const mitk::PixelType &type,
250  const mitk::BaseGeometry &geometry,
251  unsigned int channels = 1,
252  int tDim = 1);
253 
262  virtual void Initialize(const mitk::PixelType &type,
263  const mitk::TimeGeometry &geometry,
264  unsigned int channels = 1,
265  int tDim = -1);
266 
275  virtual void Initialize(const mitk::PixelType &type,
276  int sDim,
277  const mitk::PlaneGeometry &geometry2d,
278  unsigned int channels = 1,
279  int tDim = 1);
280 
286  virtual void Initialize(const mitk::Image *image);
287 
288  virtual void Initialize(const mitk::ImageDescriptor::Pointer inDesc);
289 
302  virtual void Initialize(vtkImageData *vtkimagedata, int channels = 1, int tDim = -1, int sDim = -1, int pDim = -1);
303 
315  template <typename itkImageType>
316  void InitializeByItk(const itkImageType *itkimage, int channels = 1, int tDim = -1, int sDim = -1)
317  {
318  if (itkimage == nullptr)
319  return;
320 
321  MITK_DEBUG << "Initializing MITK image from ITK image.";
322  // build array with dimensions in each direction with at least 4 entries
323  m_Dimension = itkimage->GetImageDimension();
324  unsigned int i, *tmpDimensions = new unsigned int[m_Dimension > 4 ? m_Dimension : 4];
325  for (i = 0; i < m_Dimension; ++i)
326  tmpDimensions[i] = itkimage->GetLargestPossibleRegion().GetSize().GetSize()[i];
327  if (m_Dimension < 4)
328  {
329  unsigned int *p;
330  for (i = 0, p = tmpDimensions + m_Dimension; i < 4 - m_Dimension; ++i, ++p)
331  *p = 1;
332  }
333 
334  // overwrite number of slices if sDim is set
335  if ((m_Dimension > 2) && (sDim >= 0))
336  tmpDimensions[2] = sDim;
337  // overwrite number of time points if tDim is set
338  if ((m_Dimension > 3) && (tDim >= 0))
339  tmpDimensions[3] = tDim;
340 
341  // rough initialization of Image
342  // mitk::PixelType importType = ImportItkPixelType( itkimage::PixelType );
343 
344  Initialize(
345  MakePixelType<itkImageType>(itkimage->GetNumberOfComponentsPerPixel()), m_Dimension, tmpDimensions, channels);
346  const typename itkImageType::SpacingType &itkspacing = itkimage->GetSpacing();
347 
348  MITK_DEBUG << "ITK spacing " << itkspacing;
349  // access spacing of itk::Image
350  Vector3D spacing;
351  FillVector3D(spacing, itkspacing[0], 1.0, 1.0);
352  if (m_Dimension >= 2)
353  spacing[1] = itkspacing[1];
354  if (m_Dimension >= 3)
355  spacing[2] = itkspacing[2];
356 
357  // access origin of itk::Image
358  Point3D origin;
359  const typename itkImageType::PointType &itkorigin = itkimage->GetOrigin();
360  MITK_DEBUG << "ITK origin " << itkorigin;
361  FillVector3D(origin, itkorigin[0], 0.0, 0.0);
362  if (m_Dimension >= 2)
363  origin[1] = itkorigin[1];
364  if (m_Dimension >= 3)
365  origin[2] = itkorigin[2];
366 
367  // access direction of itk::Imagm_PixelType = new mitk::PixelType(type);e and include spacing
368  const typename itkImageType::DirectionType &itkdirection = itkimage->GetDirection();
369  MITK_DEBUG << "ITK direction " << itkdirection;
370  mitk::Matrix3D matrix;
371  matrix.SetIdentity();
372  unsigned int j, itkDimMax3 = (m_Dimension >= 3 ? 3 : m_Dimension);
373  // check if spacing has no zero entry and itkdirection has no zero columns
374  bool itkdirectionOk = true;
375  mitk::ScalarType columnSum;
376  for (j = 0; j < itkDimMax3; ++j)
377  {
378  columnSum = 0.0;
379  for (i = 0; i < itkDimMax3; ++i)
380  {
381  columnSum += fabs(itkdirection[i][j]);
382  }
383  if (columnSum < mitk::eps)
384  {
385  itkdirectionOk = false;
386  }
387  if ((spacing[j] < -mitk::eps) // (normally sized) negative value
388  &&
389  (j == 2) && (m_Dimensions[2] == 1))
390  {
391  // Negative spacings can occur when reading single DICOM slices with ITK via GDCMIO
392  // In these cases spacing is not determined by ITK correctly (because it distinguishes correctly
393  // between slice thickness and inter slice distance -- slice distance is meaningless for
394  // single slices).
395  // I experienced that ITK produced something meaningful nonetheless because it is
396  // evaluating the tag "(0018,0088) Spacing between slices" as a fallback. This tag is not
397  // reliable (http://www.itk.org/pipermail/insight-users/2005-September/014711.html)
398  // but gives at least a hint.
399  // In real world cases I experienced that this tag contained the correct inter slice distance
400  // with a negative sign, so we just invert such negative spacings.
401  MITK_WARN << "Illegal value of itk::Image::GetSpacing()[" << j << "]=" << spacing[j]
402  << ". Using inverted value " << -spacing[j];
403  spacing[j] = -spacing[j];
404  }
405  else if (spacing[j] < mitk::eps) // value near zero
406  {
407  MITK_ERROR << "Illegal value of itk::Image::GetSpacing()[" << j << "]=" << spacing[j]
408  << ". Using 1.0 instead.";
409  spacing[j] = 1.0;
410  }
411  }
412  if (itkdirectionOk == false)
413  {
414  MITK_ERROR << "Illegal matrix returned by itk::Image::GetDirection():" << itkdirection
415  << " Using identity instead.";
416  for (i = 0; i < itkDimMax3; ++i)
417  for (j = 0; j < itkDimMax3; ++j)
418  if (i == j)
419  matrix[i][j] = spacing[j];
420  else
421  matrix[i][j] = 0.0;
422  }
423  else
424  {
425  for (i = 0; i < itkDimMax3; ++i)
426  for (j = 0; j < itkDimMax3; ++j)
427  matrix[i][j] = itkdirection[i][j] * spacing[j];
428  }
429 
430  // re-initialize PlaneGeometry with origin and direction
431  PlaneGeometry *planeGeometry = static_cast<PlaneGeometry *>(GetSlicedGeometry(0)->GetPlaneGeometry(0));
432  planeGeometry->SetOrigin(origin);
433  planeGeometry->GetIndexToWorldTransform()->SetMatrix(matrix);
434 
435  // re-initialize SlicedGeometry3D
436  SlicedGeometry3D *slicedGeometry = GetSlicedGeometry(0);
437  slicedGeometry->InitializeEvenlySpaced(planeGeometry, m_Dimensions[2]);
438  slicedGeometry->SetSpacing(spacing);
439 
440  // re-initialize TimeGeometry
442  timeGeometry->Initialize(slicedGeometry, m_Dimensions[3]);
443  SetTimeGeometry(timeGeometry);
444 
445  // clean-up
446  delete[] tmpDimensions;
447 
448  this->Initialize();
449  }
450 
455  virtual bool IsValidSlice(int s = 0, int t = 0, int n = 0) const;
456 
461  virtual bool IsValidVolume(int t = 0, int n = 0) const;
462 
467  virtual bool IsValidChannel(int n = 0) const;
468 
474  bool IsRotated() const;
475 
481  unsigned int *GetDimensions() const;
482 
483  ImageDescriptor::Pointer GetImageDescriptor() const { return m_ImageDescriptor; }
484  ChannelDescriptor GetChannelDescriptor(int id = 0) const { return m_ImageDescriptor->GetChannelDescriptor(id); }
487  void SetGeometry(BaseGeometry *aGeometry3D) override;
488 
492  virtual ImageDataItemPointer GetSliceData(int s = 0,
493  int t = 0,
494  int n = 0,
495  void *data = nullptr,
496  ImportMemoryManagementType importMemoryManagement = CopyMemory) const;
497 
501  virtual ImageDataItemPointer GetVolumeData(int t = 0,
502  int n = 0,
503  void *data = nullptr,
504  ImportMemoryManagementType importMemoryManagement = CopyMemory) const;
505 
509  virtual ImageDataItemPointer GetChannelData(int n = 0,
510  void *data = nullptr,
511  ImportMemoryManagementType importMemoryManagement = CopyMemory) const;
512 
519  StatisticsHolderPointer GetStatistics() const { return m_ImageStatistics; }
520 
521  protected:
523 
524  typedef std::lock_guard<std::mutex> MutexHolder;
525 
526  int GetSliceIndex(int s = 0, int t = 0, int n = 0) const;
527 
528  int GetVolumeIndex(int t = 0, int n = 0) const;
529 
530  void ComputeOffsetTable();
531 
532  virtual bool IsValidTimeStep(int t) const;
533 
534  void Expand(unsigned int timeSteps) override;
535 
536  virtual ImageDataItemPointer AllocateSliceData(
537  int s = 0,
538  int t = 0,
539  int n = 0,
540  void *data = nullptr,
541  ImportMemoryManagementType importMemoryManagement = CopyMemory) const;
542 
543  virtual ImageDataItemPointer AllocateVolumeData(
544  int t = 0, int n = 0, void *data = nullptr, ImportMemoryManagementType importMemoryManagement = CopyMemory) const;
545 
546  virtual ImageDataItemPointer AllocateChannelData(
547  int n = 0, void *data = nullptr, ImportMemoryManagementType importMemoryManagement = CopyMemory) const;
548 
549  Image();
550 
551  Image(const Image &other);
552 
553  ~Image() override;
554 
555  void Clear() override;
556 
558  void Initialize() override;
559 
560  void PrintSelf(std::ostream &os, itk::Indent indent) const override;
561 
565  mutable std::mutex m_ImageDataArraysLock;
566 
567  unsigned int m_Dimension;
568 
569  unsigned int *m_Dimensions;
570 
572 
573  size_t *m_OffsetTable;
575 
576  // Image statistics Holder replaces the former implementation directly inside this class
577  friend class ImageStatisticsHolder;
579 
580  private:
581  ImageDataItemPointer GetSliceData_unlocked(
582  int s, int t, int n, void *data, ImportMemoryManagementType importMemoryManagement) const;
583  ImageDataItemPointer GetVolumeData_unlocked(int t,
584  int n,
585  void *data,
586  ImportMemoryManagementType importMemoryManagement) const;
587  ImageDataItemPointer GetChannelData_unlocked(int n,
588  void *data,
589  ImportMemoryManagementType importMemoryManagement) const;
590 
591  ImageDataItemPointer AllocateSliceData_unlocked(
592  int s, int t, int n, void *data, ImportMemoryManagementType importMemoryManagement) const;
593  ImageDataItemPointer AllocateVolumeData_unlocked(int t,
594  int n,
595  void *data,
596  ImportMemoryManagementType importMemoryManagement) const;
597  ImageDataItemPointer AllocateChannelData_unlocked(int n,
598  void *data,
599  ImportMemoryManagementType importMemoryManagement) const;
600 
601  bool IsSliceSet_unlocked(int s, int t, int n) const;
602  bool IsVolumeSet_unlocked(int t, int n) const;
603  bool IsChannelSet_unlocked(int n) const;
604 
606  mutable std::vector<ImageAccessorBase *> m_Readers;
608  mutable std::vector<ImageAccessorBase *> m_Writers;
610  mutable std::vector<ImageAccessorBase *> m_VtkReaders;
611 
613  mutable std::mutex m_ReadWriteLock;
615  mutable std::mutex m_VtkReadersLock;
616  };
617 
636  MITKCORE_EXPORT bool Equal(const mitk::Image &leftHandSide,
637  const mitk::Image &rightHandSide,
638  ScalarType eps,
639  bool verbose);
640 
641 } // namespace mitk
642 
643 #endif
mitk::ImageVtkWriteAccessor
ImageVtkWriteAccessor class provides any image write access which is required by Vtk methods.
Definition: mitkImageVtkWriteAccessor.h:29
mitk::ChannelDescriptor
An object which holds all essential information about a single channel of an Image.
Definition: mitkChannelDescriptor.h:27
mitk::eps
const MITKCORE_EXPORT ScalarType eps
mitkImageVtkAccessor.h
mitk::ImageStatisticsHolder
Class holding the statistics information about a single mitk::Image.
Definition: mitkImageStatisticsHolder.h:36
mitk::Image::HistogramType
itk::Statistics::Histogram< double > HistogramType
Definition: mitkImage.h:89
mitkImageAccessorBase.h
mitk::TimeGeometry
Definition: mitkTimeGeometry.h:43
mitk::SubImageSelector
Base class of all classes providing access to parts of an image.
Definition: mitkSubImageSelector.h:31
mitk::Image::StatisticsHolderPointer
mitk::ImageStatisticsHolder * StatisticsHolderPointer
Definition: mitkImage.h:90
MITK_ERROR
#define MITK_ERROR
Definition: mitkLogMacros.h:20
mitk::SlicedData
Super class of data objects consisting of slices.
Definition: mitkSlicedData.h:37
mitk::Image::m_Dimensions
unsigned int * m_Dimensions
Definition: mitkImage.h:569
mitk::PlaneGeometry
Describes a two-dimensional, rectangular plane.
Definition: mitkPlaneGeometry.h:78
mitk::ImageWriteAccessor
ImageWriteAccessor class to get locked write-access for a particular image part.
Definition: mitkImageWriteAccessor.h:25
MITK_DEBUG
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
mitk::SlicedGeometry3D::InitializeEvenlySpaced
virtual void InitializeEvenlySpaced(mitk::PlaneGeometry *geometry2D, unsigned int slices)
Completely initialize this instance as evenly-spaced with slices parallel to the provided PlaneGeomet...
mitk::Image::ImportMemoryManagementType
ImportMemoryManagementType
Definition: mitkImage.h:94
mitk::BaseGeometry::SetOrigin
void SetOrigin(const Point3D &origin)
Set the origin, i.e. the upper-left corner of the plane.
mitk::Image::m_Slices
ImageDataItemPointerArray m_Slices
Definition: mitkImage.h:564
mitk::ImageAccessorBase
Definition: mitkImageAccessorBase.h:48
mitk::Image
Image class for storing images.
Definition: mitkImage.h:69
mitk::Equal
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
itk::SmartPointer< ImageDataItem >
mitk::Image::m_ImageStatistics
StatisticsHolderPointer m_ImageStatistics
Definition: mitkImage.h:578
mitk::Image::MutexHolder
std::lock_guard< std::mutex > MutexHolder
Definition: mitkImage.h:522
mitk::ProportionalTimeGeometry::New
static Pointer New()
MITK_WARN
#define MITK_WARN
Definition: mitkLogMacros.h:19
mitk
DataCollection - Class to facilitate loading/accessing structured data.
Definition: RenderingTests.dox:1
mitk::ImageReadAccessor
ImageReadAccessor class to get locked read access for a particular image part.
Definition: mitkImageReadAccessor.h:27
mitkProportionalTimeGeometry.h
mitk::Image::ManageMemory
@ ManageMemory
Definition: mitkImage.h:97
mitk::ImageVtkAccessor
ImageVtkAccessor class provides any image read access which is required by Vtk methods.
Definition: mitkImageVtkAccessor.h:25
mitk::Image::ImageDataItemPointer
itk::SmartPointer< ImageDataItem > ImageDataItemPointer
Definition: mitkImage.h:85
mitk::GetPixelType
Definition: mitkPixelTypeList.h:89
mitk::Image::m_Dimension
unsigned int m_Dimension
Definition: mitkImage.h:567
mitk::Vector< ScalarType, 3 >
mitkSlicedData.h
MitkCoreExports.h
mitk::BaseGeometry
BaseGeometry Describes the geometry of a data object.
Definition: mitkBaseGeometry.h:94
mitk::Point< ScalarType, 3 >
mitk::Matrix
Definition: mitkMatrix.h:25
mitk::Image::m_Channels
ImageDataItemPointerArray m_Channels
Definition: mitkImage.h:562
mitkImageDataItem.h
mitk::FillVector3D
void FillVector3D(Tout &out, mitk::ScalarType x, mitk::ScalarType y, mitk::ScalarType z)
Definition: mitkArray.h:105
mitk::Image::InitializeByItk
void InitializeByItk(const itkImageType *itkimage, int channels=1, int tDim=-1, int sDim=-1)
Definition: mitkImage.h:316
mitk::Image::m_CompleteData
ImageDataItemPointer m_CompleteData
Definition: mitkImage.h:574
mitk::PixelType
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51
mitk::Image::GetStatistics
StatisticsHolderPointer GetStatistics() const
Returns a pointer to the ImageStatisticsHolder object that holds all statistics information for the i...
Definition: mitkImage.h:519
mitk::BaseData
Base of all data objects.
Definition: mitkBaseData.h:42
mitk::Image::m_ImageDescriptor
ImageDescriptor::Pointer m_ImageDescriptor
Definition: mitkImage.h:571
mitk::Image::CopyMemory
@ CopyMemory
Definition: mitkImage.h:96
mitkImageDescriptor.h
mitk::SlicedGeometry3D
Describes the geometry of a data object consisting of slices.
Definition: mitkSlicedGeometry3D.h:62
mitk::ImageVtkReadAccessor
ImageVtkReadAccessor class provides any image read access which is required by Vtk methods.
Definition: mitkImageVtkReadAccessor.h:29
itk
SET FUNCTIONS.
Definition: itkIntelligentBinaryClosingFilter.h:30
mitkCloneMacro
#define mitkCloneMacro(classname)
Definition: mitkCommon.h:154
mitk::Image::ReferenceMemory
@ ReferenceMemory
Definition: mitkImage.h:98
mitkBaseData.h
mitkLevelWindow.h
mitk::Image::m_ImageDataArraysLock
std::mutex m_ImageDataArraysLock
Definition: mitkImage.h:565
mitk::Image::m_OffsetTable
size_t * m_OffsetTable
Definition: mitkImage.h:573
mitkClassMacro
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:36
itk::Image
class ITK_EXPORT Image
Definition: mitkGeometryClipImageFilter.h:25
itk::MutexLockHolder
Definition: mitkImage.h:36
mitk::Image::m_Volumes
ImageDataItemPointerArray m_Volumes
Definition: mitkImage.h:563
mitk::Image::ImageDataItemPointerArray
std::vector< ImageDataItemPointer > ImageDataItemPointerArray
Vector container of SmartPointers to ImageDataItems; Class is only for internal usage to allow conven...
Definition: mitkImage.h:107
mitk::Image::GetImageDescriptor
ImageDescriptor::Pointer GetImageDescriptor() const
Definition: mitkImage.h:483
mitk::Image::GetChannelDescriptor
ChannelDescriptor GetChannelDescriptor(int id=0) const
Definition: mitkImage.h:484
MITKCORE_EXPORT
#define MITKCORE_EXPORT
Definition: MitkCoreExports.h:15
mitk::BaseGeometry::GetIndexToWorldTransform
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the transformation used to convert from index to world coordinates.
mitk::ScalarType
double ScalarType
Definition: mitkNumericConstants.h:20
mitkPlaneGeometry.h
mitk::BaseGeometry::SetSpacing
void SetSpacing(const mitk::Vector3D &aSpacing, bool enforceSetSpacing=false)
Set the spacing (m_Spacing).