Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkToFImageGrabber.cpp
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 #include "mitkToFImageGrabber.h"
17 #include <itkCommand.h>
18 #include <usModuleContext.h>
19 #include <usGetModuleContext.h>
20 
21 namespace mitk
22 {
24  m_ToFCameraDevice(NULL),
25  m_CaptureWidth(-1),
26  m_CaptureHeight(-1),
27  m_PixelNumber(-1),
28  m_RGBImageWidth(0),
29  m_RGBImageHeight(0),
30  m_RGBPixelNumber(0),
31  m_ImageSequence(0),
32  m_SourceDataSize(0),
33  m_IntensityArray(NULL),
34  m_DistanceArray(NULL),
35  m_AmplitudeArray(NULL),
36  m_SourceDataArray(NULL),
37  m_RgbDataArray(NULL),
38  m_DeviceObserverTag()
39 {
40  // Create the output. We use static_cast<> here because we know the default
41  // output must be of type TOutputImage
42  OutputImageType::Pointer output0 = static_cast<OutputImageType*>(this->MakeOutput(0).GetPointer());
43  OutputImageType::Pointer output1 = static_cast<OutputImageType*>(this->MakeOutput(1).GetPointer());
44  OutputImageType::Pointer output2 = static_cast<OutputImageType*>(this->MakeOutput(2).GetPointer());
45  OutputImageType::Pointer output3 = static_cast<OutputImageType*>(this->MakeOutput(3).GetPointer());
46  mitk::ImageSource::SetNumberOfRequiredOutputs(3);
47  mitk::ImageSource::SetNthOutput(0, output0.GetPointer());
48  mitk::ImageSource::SetNthOutput(1, output1.GetPointer());
49  mitk::ImageSource::SetNthOutput(2, output2.GetPointer());
50  mitk::ImageSource::SetNthOutput(3, output3.GetPointer());
51 }
52 
54 {
56  {
58  {
59  m_ToFCameraDevice->RemoveObserver(m_DeviceObserverTag);
60  }
61  this->DisconnectCamera();
62  this->CleanUpImageArrays();
63  }
64 }
65 
67 {
68  int requiredImageSequence = 0;
69  // acquire new image data
70  this->m_ToFCameraDevice->GetAllImages(this->m_DistanceArray, this->m_AmplitudeArray, this->m_IntensityArray, this->m_SourceDataArray,
71  requiredImageSequence, this->m_ImageSequence, this->m_RgbDataArray );
72 
73  mitk::Image::Pointer distanceImage = this->GetOutput(0);
74  if (m_DistanceArray)
75  {
76  distanceImage->SetSlice(this->m_DistanceArray, 0, 0, 0);
77  }
78 
79  bool hasAmplitudeImage = false;
80  m_ToFCameraDevice->GetBoolProperty("HasAmplitudeImage", hasAmplitudeImage);
81  if((hasAmplitudeImage) && (m_AmplitudeArray))
82  {
83  mitk::Image::Pointer amplitudeImage = this->GetOutput(1);
84  amplitudeImage->SetSlice(this->m_AmplitudeArray, 0, 0, 0);
85  }
86 
87  bool hasIntensityImage = false;
88  m_ToFCameraDevice->GetBoolProperty("HasIntensityImage", hasIntensityImage);
89  if((hasIntensityImage) && (m_IntensityArray))
90  {
91  mitk::Image::Pointer intensityImage = this->GetOutput(2);
92  intensityImage->SetSlice(this->m_IntensityArray, 0, 0, 0);
93  }
94 
95  bool hasRGBImage = false;
96  m_ToFCameraDevice->GetBoolProperty("HasRGBImage", hasRGBImage);
97  if( hasRGBImage )
98  {
99  mitk::Image::Pointer rgbImage = this->GetOutput(3);
100  if (m_RgbDataArray)
101  {
102  rgbImage->SetSlice(this->m_RgbDataArray, 0, 0, 0);
103  }
104  }
105 }
106 
108 {
109  bool ok = m_ToFCameraDevice->ConnectCamera();
110  if (ok)
111  {
112  this->m_CaptureWidth = this->m_ToFCameraDevice->GetCaptureWidth();
113  this->m_CaptureHeight = this->m_ToFCameraDevice->GetCaptureHeight();
114  this->m_PixelNumber = this->m_CaptureWidth * this->m_CaptureHeight;
115 
116  this->m_RGBImageWidth = this->m_ToFCameraDevice->GetRGBCaptureWidth();
117  this->m_RGBImageHeight = this->m_ToFCameraDevice->GetRGBCaptureHeight();
118  this->m_RGBPixelNumber = this->m_RGBImageWidth * this->m_RGBImageHeight;
119 
120  this->m_SourceDataSize = m_ToFCameraDevice->GetSourceDataSize();
121  this->AllocateImageArrays();
122  this->InitializeImages();
123  }
124  return ok;
125 }
126 
128 {
129  return m_ToFCameraDevice->DisconnectCamera();
130 }
131 
133 {
134  m_ToFCameraDevice->StartCamera();
135  us::ModuleContext* context = us::GetModuleContext();
136  us::ServiceProperties deviceProps;
137  deviceProps["ToFImageSourceName"] = std::string("Image Grabber");
138  m_ServiceRegistration = context->RegisterService<mitk::ToFImageSource>(this,deviceProps);
139 }
140 
142 {
143  m_ToFCameraDevice->StopCamera();
146 }
147 
149 {
150  return m_ToFCameraDevice->IsCameraActive();
151 }
153 {
154  return m_ToFCameraDevice->IsCameraConnected();
155 }
156 
158 {
159  m_ToFCameraDevice = aToFCameraDevice;
161  modifiedCommand->SetCallbackFunction(this, &ToFImageGrabber::OnToFCameraDeviceModified);
162  m_DeviceObserverTag = m_ToFCameraDevice->AddObserver(itk::ModifiedEvent(), modifiedCommand);
163  this->Modified();
164 }
165 
167 {
168  return m_ToFCameraDevice;
169 }
170 
172 {
173  return m_CaptureWidth;
174 }
175 
177 {
178  return m_CaptureHeight;
179 }
180 
182 {
183  return m_PixelNumber;
184 }
185 
187 {
188  return m_RGBImageWidth;
189 }
190 
192 {
193  return m_RGBImageHeight;
194 }
195 
197 {
198  return m_RGBPixelNumber;
199 }
200 
201 int ToFImageGrabber::SetModulationFrequency(int modulationFrequency)
202 {
203  this->m_ToFCameraDevice->SetProperty("ModulationFrequency",mitk::IntProperty::New(modulationFrequency));
204  this->Modified();
205  modulationFrequency = this->GetModulationFrequency(); // return the new valid modulation frequency from the camera
206  return modulationFrequency;
207 }
208 
209 int ToFImageGrabber::SetIntegrationTime(int integrationTime)
210 {
211  this->m_ToFCameraDevice->SetProperty("IntegrationTime",mitk::IntProperty::New(integrationTime));
212  this->Modified();
213  integrationTime = this->GetIntegrationTime(); // return the new valid integration time from the camera
214  return integrationTime;
215 }
216 
218 {
219  int integrationTime = 0;
220  this->m_ToFCameraDevice->GetIntProperty("IntegrationTime",integrationTime);
221  return integrationTime;
222 }
223 
225 {
226  int modulationFrequency = 0;
227  this->m_ToFCameraDevice->GetIntProperty("ModulationFrequency",modulationFrequency);
228  return modulationFrequency;
229 }
230 
231 void ToFImageGrabber::SetBoolProperty( const char* propertyKey, bool boolValue )
232 {
233  SetProperty(propertyKey, mitk::BoolProperty::New(boolValue));
234 }
235 
236 void ToFImageGrabber::SetIntProperty( const char* propertyKey, int intValue )
237 {
238  SetProperty(propertyKey, mitk::IntProperty::New(intValue));
239 }
240 
241 void ToFImageGrabber::SetFloatProperty( const char* propertyKey, float floatValue )
242 {
243  SetProperty(propertyKey, mitk::FloatProperty::New(floatValue));
244 }
245 
246 void ToFImageGrabber::SetStringProperty( const char* propertyKey, const char* stringValue )
247 {
248  SetProperty(propertyKey, mitk::StringProperty::New(stringValue));
249 }
250 
251 void ToFImageGrabber::SetProperty( const char *propertyKey, BaseProperty* propertyValue )
252 {
253  this->m_ToFCameraDevice->SetProperty(propertyKey, propertyValue);
254 }
255 
256 bool ToFImageGrabber::GetBoolProperty( const char* propertyKey)
257 {
258  mitk::BoolProperty::Pointer boolProp = dynamic_cast<mitk::BoolProperty*>(GetProperty(propertyKey));
259  if(!boolProp) return false;
260  return boolProp->GetValue();
261 }
262 
263 int ToFImageGrabber::GetIntProperty( const char* propertyKey)
264 {
265  mitk::IntProperty::Pointer intProp = dynamic_cast<mitk::IntProperty*>(GetProperty(propertyKey));
266  return intProp->GetValue();
267 }
268 
269 float ToFImageGrabber::GetFloatProperty( const char* propertyKey)
270 {
271  mitk::FloatProperty::Pointer floatProp = dynamic_cast<mitk::FloatProperty*>(GetProperty(propertyKey));
272  return floatProp->GetValue();
273 }
274 
275 const char* ToFImageGrabber::GetStringProperty( const char* propertyKey)
276 {
277  mitk::StringProperty::Pointer stringProp = dynamic_cast<mitk::StringProperty*>(GetProperty(propertyKey));
278  return stringProp->GetValue();
279 }
280 
281 BaseProperty* ToFImageGrabber::GetProperty( const char *propertyKey)
282 {
283  return this->m_ToFCameraDevice->GetProperty(propertyKey);
284 }
285 
287 {
288  this->Modified();
289 }
290 
292 {
293  // free buffer
294  if (m_IntensityArray)
295  {
296  delete [] m_IntensityArray;
297  m_IntensityArray = NULL;
298  }
299  if (m_DistanceArray)
300  {
301  delete [] m_DistanceArray;
302  m_DistanceArray = NULL;
303  }
304  if (m_AmplitudeArray)
305  {
306  delete [] m_AmplitudeArray;
307  m_AmplitudeArray = NULL;
308  }
309  if (m_SourceDataArray)
310  {
311  delete [] m_SourceDataArray;
312  m_SourceDataArray = NULL;
313  }
314  if (m_RgbDataArray)
315  {
316  delete [] m_RgbDataArray;
317  m_RgbDataArray = NULL;
318  }
319 }
320 
322 {
323  // cleanup memory if necessary
324  this->CleanUpImageArrays();
325  // allocate buffer
326  m_IntensityArray = new float[m_PixelNumber];
327  m_DistanceArray = new float[m_PixelNumber];
328  m_AmplitudeArray = new float[m_PixelNumber];
330  m_RgbDataArray = new unsigned char[m_RGBPixelNumber*3];
331 }
332 
334 {
335  unsigned int dimensions[3];
336  dimensions[0] = this->m_ToFCameraDevice->GetCaptureWidth();
337  dimensions[1] = this->m_ToFCameraDevice->GetCaptureHeight();
338  dimensions[2] = 1;
339  mitk::PixelType FloatType = MakeScalarPixelType<float>();
340  mitk::Image::Pointer distanceImage = this->GetOutput();
341  distanceImage->ReleaseData();
342  distanceImage->Initialize(FloatType, 3, dimensions, 1);
343 
344  bool hasAmplitudeImage = false;
345  m_ToFCameraDevice->GetBoolProperty("HasAmplitudeImage", hasAmplitudeImage);
346  if(hasAmplitudeImage)
347  {
348  mitk::Image::Pointer amplitudeImage = this->GetOutput(1);
349  amplitudeImage->ReleaseData();
350  amplitudeImage->Initialize(FloatType, 3, dimensions, 1);
351  }
352 
353  bool hasIntensityImage = false;
354  m_ToFCameraDevice->GetBoolProperty("HasIntensityImage", hasIntensityImage);
355  if(hasIntensityImage)
356  {
357  mitk::Image::Pointer intensityImage = this->GetOutput(2);
358  intensityImage->ReleaseData();
359  intensityImage->Initialize(FloatType, 3, dimensions, 1);
360  }
361 
362  bool hasRGBImage = false;
363  m_ToFCameraDevice->GetBoolProperty("HasRGBImage", hasRGBImage);
364  if(hasRGBImage)
365  {
366  unsigned int rgbDimension[3];
367  rgbDimension[0] = this->m_ToFCameraDevice->GetRGBCaptureWidth();
368  rgbDimension[1] = this->m_ToFCameraDevice->GetRGBCaptureHeight();
369  rgbDimension[2] = 1 ;
370  mitk::Image::Pointer rgbImage = this->GetOutput(3);
371  rgbImage->ReleaseData();
372  rgbImage->Initialize(mitk::PixelType(MakePixelType<unsigned char, itk::RGBPixel<unsigned char>, 3>()), 3, rgbDimension,1);
373  }
374 }
375 }
void SetProperty(const char *propertyKey, BaseProperty *propertyValue)
virtual void StopCamera()
Stops the continuous updating of the camera.
void SetIntProperty(const char *propertyKey, int intValue)
float * m_DistanceArray
member holding the current distance array
int m_RGBImageWidth
Width of the captured RGB image.
itk::SmartPointer< Self > Pointer
virtual bool IsCameraConnected()
Returns true if the camera is connected.
int m_ImageSequence
counter for currently acquired images
int m_RGBPixelNumber
Number of pixels in the RGB image.
virtual itk::DataObject::Pointer MakeOutput(DataObjectPointerArraySizeType idx) override
Make a DataObject of the correct type to used as the specified output.
int SetIntegrationTime(int integrationTime)
Set the integration time used by the ToF camera. For default values see the corresponding device clas...
float GetFloatProperty(const char *propertyKey)
signed integer value
Definition: jsoncpp.h:348
int GetCaptureHeight()
Get the dimension in y direction of the ToF image.
void SetFloatProperty(const char *propertyKey, float floatValue)
int m_RGBImageHeight
Height of the captured RGB image.
Virtual interface and base class for all Time-of-Flight devices.
virtual void AllocateImageArrays()
Allocate memory for the image arrays m_IntensityArray, m_DistanceArray, m_AmplitudeArray and m_Source...
virtual bool IsCameraActive()
Returns true if the camera is connected and started.
us::ServiceRegistration< Self > m_ServiceRegistration
BaseProperty * GetProperty(const char *propertyKey)
DataCollection - Class to facilitate loading/accessing structured data.
ToFCameraDevice * GetCameraDevice()
Get the currently set ToF camera device.
int m_CaptureWidth
Width of the captured ToF image.
float * m_IntensityArray
member holding the current intensity array
virtual bool DisconnectCamera()
Disconnects the ToF camera.
void SetCameraDevice(ToFCameraDevice *aToFCameraDevice)
Sets the ToF device, the image grabber is grabbing the images from.
int GetPixelNumber()
Get the number of pixel in the ToF image.
static Pointer New()
char * m_SourceDataArray
member holding the current source data array
int GetRGBPixelNumber()
Get the number of pixel in the ToF image.
virtual void StartCamera()
Starts the continuous updating of the camera. A separate thread updates the source data...
int GetRGBImageHeight()
Get the dimension in y direction of the ToF image.
MITKCORE_EXPORT mitk::PixelType MakePixelType(vtkImageData *vtkimagedata)
deduct the PixelType for a given vtk image
void SetStringProperty(const char *propertyKey, const char *stringValue)
Abstract base class for properties.
int GetCaptureWidth()
Get the dimension in x direction of the ToF image.
ToFCameraDevice::Pointer m_ToFCameraDevice
Device allowing access to ToF image data.
void SetBoolProperty(const char *propertyKey, bool boolValue)
Image class for storing images.
Definition: mitkImage.h:76
int GetIntProperty(const char *propertyKey)
int SetModulationFrequency(int modulationFrequency)
Set the modulation frequency used by the ToF camera. For default values see the corresponding device ...
void InitializeImages()
InitializeImages Initialze the geometries of the images according to the device properties.
int GetIntegrationTime()
Get the integration time in used by the ToF camera.
int GetRGBImageWidth()
Get the dimension in x direction of the ToF image.
US_UNORDERED_MAP_TYPE< std::string, Any > ServiceProperties
static Pointer New()
Image source providing ToF images. Interface for filters provided in ToFProcessing module...
int m_SourceDataSize
size of the source data in bytes
static Pointer New()
UTF-8 string value.
Definition: jsoncpp.h:351
unsigned long m_DeviceObserverTag
tag of the observer for the ToFCameraDevice
unsigned char * m_RgbDataArray
member holding the current rgb data array
virtual void CleanUpImageArrays()
clean up memory allocated for the image arrays m_IntensityArray, m_DistanceArray, m_AmplitudeArray an...
virtual bool ConnectCamera()
Establish a connection to the ToF camera.
int m_PixelNumber
Number of pixels in the image.
OutputType * GetOutput()
Get the output data of this image source object.
int GetModulationFrequency()
Get the modulation frequency used by the ToF camera.
Property for strings.
const char * GetStringProperty(const char *propertyKey)
float * m_AmplitudeArray
member holding the current amplitude array
void GenerateData() override
Method generating the outputs of this filter. Called in the updated process of the pipeline...
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
static Pointer New()
int m_CaptureHeight
Height of the captured ToF image.
Class for defining the data type of pixels.
Definition: mitkPixelType.h:55
bool GetBoolProperty(const char *propertyKey)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.