Medical Imaging Interaction Toolkit  2021.02.99-9ccbc4d1
Medical Imaging Interaction Toolkit
mitkImageGenerator.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 ImageGenerator_H_HEADER_INCLUDED
14 #define ImageGenerator_H_HEADER_INCLUDED
15 
16 #include "mitkImageWriteAccessor.h"
17 #include <MitkCoreExports.h>
18 #include <itkImageRegionIterator.h>
19 #include <itkMersenneTwisterRandomVariateGenerator.h>
20 #include <mitkImage.h>
21 
22 namespace mitk
23 {
31  {
32  public:
36  template <typename TPixelType>
37  static mitk::Image::Pointer GenerateGradientImage(unsigned int dimX,
38  unsigned int dimY,
39  unsigned int dimZ,
40  float spacingX = 1,
41  float spacingY = 1,
42  float spacingZ = 1)
43  {
44  typedef itk::Image<TPixelType, 3> ImageType;
45  typename ImageType::RegionType imageRegion;
46  imageRegion.SetSize(0, dimX);
47  imageRegion.SetSize(1, dimY);
48  imageRegion.SetSize(2, dimZ);
49  typename ImageType::SpacingType spacing;
50  spacing[0] = spacingX;
51  spacing[1] = spacingY;
52  spacing[2] = spacingZ;
53 
54  mitk::Point3D origin;
55  origin.Fill(0.0);
56  itk::Matrix<double, 3, 3> directionMatrix;
57  directionMatrix.SetIdentity();
58 
59  typename ImageType::Pointer image = ImageType::New();
60  image->SetSpacing(spacing);
61  image->SetOrigin(origin);
62  image->SetDirection(directionMatrix);
63  image->SetLargestPossibleRegion(imageRegion);
64  image->SetBufferedRegion(imageRegion);
65  image->SetRequestedRegion(imageRegion);
66  image->Allocate();
67  image->FillBuffer(0.0);
68 
69  typedef itk::ImageRegionIterator<ImageType> IteratorOutputType;
70  IteratorOutputType it(image, imageRegion);
71  it.GoToBegin();
72 
73  TPixelType val = 0;
74  while (!it.IsAtEnd())
75  {
76  it.Set(val);
77  val++;
78  ++it;
79  }
80 
82  mitkImage->InitializeByItk(image.GetPointer());
83  mitkImage->SetVolume(image->GetBufferPointer());
84  return mitkImage;
85  }
86 
92  template <typename TPixelType>
93  static mitk::Image::Pointer GenerateImageFromReference(mitk::Image::Pointer reference, TPixelType fill_value)
94  {
96  mitk::PixelType output_type = MakeScalarPixelType<TPixelType>();
97 
98  // all metadata (except type) come from reference image
99  output->SetGeometry(reference->GetGeometry());
100  output->Initialize(output_type, reference->GetDimension(), reference->GetDimensions());
101 
102  // get a pointer to the image buffer to write into
103  TPixelType *imageBuffer = nullptr;
104  try
105  {
106  mitk::ImageWriteAccessor writeAccess(output);
107  imageBuffer = static_cast<TPixelType *>(writeAccess.GetData());
108  }
109  catch (...)
110  {
111  MITK_ERROR << "Write access not granted on mitk::Image.";
112  }
113 
114  // fill the buffer with the specifed value
115  for (unsigned int i = 0; i < output->GetVolumeData(0)->GetSize(); i++)
116  {
117  imageBuffer[i] = fill_value;
118  }
119 
120  return output;
121  }
122 
126  template <typename TPixelType>
127  static mitk::Image::Pointer GenerateRandomImage(unsigned int dimX,
128  unsigned int dimY,
129  unsigned int dimZ = 1,
130  unsigned int dimT = 1,
131  mitk::ScalarType spacingX = 1,
132  mitk::ScalarType spacingY = 1,
133  mitk::ScalarType spacingZ = 1,
134  const double randomMax = 1000.0f,
135  const double randMin = 0.0f)
136  {
137  // set the data type according to the template
138  mitk::PixelType type = MakeScalarPixelType<TPixelType>();
139  // type.Initialize(typeid(TPixelType));
140 
141  // initialize the MITK image with given dimenion and data type
143  auto dimensions = new unsigned int[4];
144  unsigned int numberOfDimensions = 0;
145  unsigned int bufferSize = 0;
146 
147  // check which dimension is needed
148  if (dimT <= 1)
149  {
150  if (dimZ <= 1)
151  { // 2D
152  numberOfDimensions = 2;
153  dimensions[0] = dimX;
154  dimensions[1] = dimY;
155  bufferSize = dimX * dimY;
156  }
157  else
158  { // 3D
159  numberOfDimensions = 3;
160  dimensions[0] = dimX;
161  dimensions[1] = dimY;
162  dimensions[2] = dimZ;
163  bufferSize = dimX * dimY * dimZ;
164  }
165  }
166  else
167  { // 4D
168  numberOfDimensions = 4;
169  dimensions[0] = dimX;
170  dimensions[1] = dimY;
171  dimensions[2] = dimZ;
172  dimensions[3] = dimT;
173  bufferSize = dimX * dimY * dimZ * dimT;
174  }
175  output->Initialize(type, numberOfDimensions, dimensions);
176  mitk::Vector3D spacing;
177  spacing[0] = spacingX;
178  spacing[1] = spacingY;
179  spacing[2] = spacingZ;
180  output->SetSpacing(spacing);
181 
182  // get a pointer to the image buffer to write into
183  TPixelType *imageBuffer = nullptr;
184  try
185  {
186  mitk::ImageWriteAccessor writeAccess(output);
187  imageBuffer = static_cast<TPixelType *>(writeAccess.GetData());
188  }
189  catch (...)
190  {
191  MITK_ERROR << "Write access not granted on mitk::Image.";
192  }
193 
194  // initialize the random generator
195  itk::Statistics::MersenneTwisterRandomVariateGenerator::Pointer randomGenerator =
196  itk::Statistics::MersenneTwisterRandomVariateGenerator::New();
197  randomGenerator->Initialize();
198 
199  // fill the buffer for each pixel/voxel
200  for (unsigned int i = 0; i < bufferSize; i++)
201  {
202  // the comparison of the component type is sufficient enough since the mitk::PixelType type object is
203  // created as SCALAR and hence does not need the comparison against type.GetPixelTypeId() ==
204  // itk::ImageIOBase::SCALAR
205  if (type.GetComponentType() == itk::ImageIOBase::INT) // call integer function
206  {
207  imageBuffer[i] = (TPixelType)randomGenerator->GetIntegerVariate((int)randomMax);
208  // TODO random generator does not support integer values in a given range (e.g. from 5-10)
209  // range is always [0, (int)randomMax]
210  }
211  else if ((type.GetComponentType() == itk::ImageIOBase::DOUBLE) ||
212  (type.GetComponentType() == itk::ImageIOBase::FLOAT)) // call floating point function
213  {
214  imageBuffer[i] = (TPixelType)randomGenerator->GetUniformVariate(randMin, randomMax);
215  }
216  else if (type.GetComponentType() == itk::ImageIOBase::UCHAR)
217  {
218  // use the integer randomGenerator with mod 256 to generate unsigned char values
219  imageBuffer[i] = (unsigned char)((int)randomGenerator->GetIntegerVariate((int)randomMax)) % 256;
220  }
221  else if (type.GetComponentType() == itk::ImageIOBase::USHORT)
222  {
223  imageBuffer[i] = (unsigned short)((int)randomGenerator->GetIntegerVariate((int)randomMax)) % 65536;
224  }
225  else
226  {
227  MITK_ERROR << "Datatype not supported yet.";
228  // TODO call different methods for other datatypes
229  }
230  }
231  return output;
232  }
233  };
234 } // namespace mitk
235 
236 #endif /* ImageGenerator_H_HEADER_INCLUDED */
#define MITKCORE_EXPORT
#define MITK_ERROR
Definition: mitkLogMacros.h:20
double ScalarType
DataCollection - Class to facilitate loading/accessing structured data.
void * GetData()
Gives full data access.
int GetComponentType() const
Get the component type (the scalar (!) type). Each element may contain m_NumberOfComponents (more tha...
static mitk::Image::Pointer GenerateGradientImage(unsigned int dimX, unsigned int dimY, unsigned int dimZ, float spacingX=1, float spacingY=1, float spacingZ=1)
Generates gradient image with the defined size and spacing.
generator for synthetic MITK images This is a helper class to generate synthetic MITK images (random ...
static mitk::Image::Pointer GenerateRandomImage(unsigned int dimX, unsigned int dimY, unsigned int dimZ=1, unsigned int dimT=1, mitk::ScalarType spacingX=1, mitk::ScalarType spacingY=1, mitk::ScalarType spacingZ=1, const double randomMax=1000.0f, const double randMin=0.0f)
Generates random image with the defined size and spacing.
static mitk::Image::Pointer GenerateImageFromReference(mitk::Image::Pointer reference, TPixelType fill_value)
Generates an image of a same geometry as the one given as reference.
static Pointer New()
ImageWriteAccessor class to get locked write-access for a particular image part.
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51