Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef ImageGenerator_H_HEADER_INCLUDED
18 #define ImageGenerator_H_HEADER_INCLUDED
19 
20 #include "mitkImageWriteAccessor.h"
21 #include <MitkCoreExports.h>
22 #include <itkImageRegionIterator.h>
23 #include <itkMersenneTwisterRandomVariateGenerator.h>
24 #include <mitkImage.h>
25 
26 namespace mitk
27 {
35  {
36  public:
40  template <typename TPixelType>
41  static mitk::Image::Pointer GenerateGradientImage(unsigned int dimX,
42  unsigned int dimY,
43  unsigned int dimZ,
44  float spacingX = 1,
45  float spacingY = 1,
46  float spacingZ = 1)
47  {
48  typedef itk::Image<TPixelType, 3> ImageType;
49  typename ImageType::RegionType imageRegion;
50  imageRegion.SetSize(0, dimX);
51  imageRegion.SetSize(1, dimY);
52  imageRegion.SetSize(2, dimZ);
53  typename ImageType::SpacingType spacing;
54  spacing[0] = spacingX;
55  spacing[1] = spacingY;
56  spacing[2] = spacingZ;
57 
58  mitk::Point3D origin;
59  origin.Fill(0.0);
60  itk::Matrix<double, 3, 3> directionMatrix;
61  directionMatrix.SetIdentity();
62 
63  typename ImageType::Pointer image = ImageType::New();
64  image->SetSpacing(spacing);
65  image->SetOrigin(origin);
66  image->SetDirection(directionMatrix);
67  image->SetLargestPossibleRegion(imageRegion);
68  image->SetBufferedRegion(imageRegion);
69  image->SetRequestedRegion(imageRegion);
70  image->Allocate();
71  image->FillBuffer(0.0);
72 
73  typedef itk::ImageRegionIterator<ImageType> IteratorOutputType;
74  IteratorOutputType it(image, imageRegion);
75  it.GoToBegin();
76 
77  TPixelType val = 0;
78  while (!it.IsAtEnd())
79  {
80  it.Set(val);
81  val++;
82  ++it;
83  }
84 
86  mitkImage->InitializeByItk(image.GetPointer());
87  mitkImage->SetVolume(image->GetBufferPointer());
88  return mitkImage;
89  }
90 
96  template <typename TPixelType>
97  static mitk::Image::Pointer GenerateImageFromReference(mitk::Image::Pointer reference, TPixelType fill_value)
98  {
100  mitk::PixelType output_type = MakeScalarPixelType<TPixelType>();
101 
102  // all metadata (except type) come from reference image
103  output->SetGeometry(reference->GetGeometry());
104  output->Initialize(output_type, reference->GetDimension(), reference->GetDimensions());
105 
106  // get a pointer to the image buffer to write into
107  TPixelType *imageBuffer = NULL;
108  try
109  {
110  mitk::ImageWriteAccessor writeAccess(output);
111  imageBuffer = static_cast<TPixelType *>(writeAccess.GetData());
112  }
113  catch (...)
114  {
115  MITK_ERROR << "Write access not granted on mitk::Image.";
116  }
117 
118  // fill the buffer with the specifed value
119  for (unsigned int i = 0; i < output->GetVolumeData(0)->GetSize(); i++)
120  {
121  imageBuffer[i] = fill_value;
122  }
123 
124  return output;
125  }
126 
130  template <typename TPixelType>
131  static mitk::Image::Pointer GenerateRandomImage(unsigned int dimX,
132  unsigned int dimY,
133  unsigned int dimZ = 1,
134  unsigned int dimT = 1,
135  mitk::ScalarType spacingX = 1,
136  mitk::ScalarType spacingY = 1,
137  mitk::ScalarType spacingZ = 1,
138  const double randomMax = 1000.0f,
139  const double randMin = 0.0f)
140  {
141  // set the data type according to the template
142  mitk::PixelType type = MakeScalarPixelType<TPixelType>();
143  // type.Initialize(typeid(TPixelType));
144 
145  // initialize the MITK image with given dimenion and data type
147  auto dimensions = new unsigned int[4];
148  unsigned int numberOfDimensions = 0;
149  unsigned int bufferSize = 0;
150 
151  // check which dimension is needed
152  if (dimT <= 1)
153  {
154  if (dimZ <= 1)
155  { // 2D
156  numberOfDimensions = 2;
157  dimensions[0] = dimX;
158  dimensions[1] = dimY;
159  bufferSize = dimX * dimY;
160  }
161  else
162  { // 3D
163  numberOfDimensions = 3;
164  dimensions[0] = dimX;
165  dimensions[1] = dimY;
166  dimensions[2] = dimZ;
167  bufferSize = dimX * dimY * dimZ;
168  }
169  }
170  else
171  { // 4D
172  numberOfDimensions = 4;
173  dimensions[0] = dimX;
174  dimensions[1] = dimY;
175  dimensions[2] = dimZ;
176  dimensions[3] = dimT;
177  bufferSize = dimX * dimY * dimZ * dimT;
178  }
179  output->Initialize(type, numberOfDimensions, dimensions);
180  mitk::Vector3D spacing;
181  spacing[0] = spacingX;
182  spacing[1] = spacingY;
183  spacing[2] = spacingZ;
184  output->SetSpacing(spacing);
185 
186  // get a pointer to the image buffer to write into
187  TPixelType *imageBuffer = nullptr;
188  try
189  {
190  mitk::ImageWriteAccessor writeAccess(output);
191  imageBuffer = static_cast<TPixelType *>(writeAccess.GetData());
192  }
193  catch (...)
194  {
195  MITK_ERROR << "Write access not granted on mitk::Image.";
196  }
197 
198  // initialize the random generator
201  randomGenerator->Initialize();
202 
203  // fill the buffer for each pixel/voxel
204  for (unsigned int i = 0; i < bufferSize; i++)
205  {
206  // the comparison of the component type is sufficient enough since the mitk::PixelType type object is
207  // created as SCALAR and hence does not need the comparison against type.GetPixelTypeId() ==
208  // itk::ImageIOBase::SCALAR
209  if (type.GetComponentType() == itk::ImageIOBase::INT) // call integer function
210  {
211  imageBuffer[i] = (TPixelType)randomGenerator->GetIntegerVariate((int)randomMax);
212  // TODO random generator does not support integer values in a given range (e.g. from 5-10)
213  // range is always [0, (int)randomMax]
214  }
215  else if ((type.GetComponentType() == itk::ImageIOBase::DOUBLE) ||
216  (type.GetComponentType() == itk::ImageIOBase::FLOAT)) // call floating point function
217  {
218  imageBuffer[i] = (TPixelType)randomGenerator->GetUniformVariate(randMin, randomMax);
219  }
220  else if (type.GetComponentType() == itk::ImageIOBase::UCHAR)
221  {
222  // use the integer randomGenerator with mod 256 to generate unsigned char values
223  imageBuffer[i] = (unsigned char)((int)randomGenerator->GetIntegerVariate((int)randomMax)) % 256;
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 */
itk::SmartPointer< Self > Pointer
#define MITKCORE_EXPORT
#define MITK_ERROR
Definition: mitkLogMacros.h:24
double ScalarType
DataCollection - Class to facilitate loading/accessing structured data.
void * GetData()
Gives full data access.
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 ...
map::core::discrete::Elements< 3 >::InternalImageType ImageType
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.
int GetComponentType() const
Get the component type (the scalar (!) type). Each element may contain m_NumberOfComponents (more tha...
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:55
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.