Medical Imaging Interaction Toolkit  2018.4.99-07c45cb1
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 (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 #include "mitkToFImageGrabber.h"
13 #include <itkCommand.h>
14 #include <usModuleContext.h>
15 #include <usGetModuleContext.h>
16 
17 namespace mitk
18 {
20  m_ToFCameraDevice(nullptr),
21  m_CaptureWidth(-1),
22  m_CaptureHeight(-1),
23  m_PixelNumber(-1),
24  m_RGBImageWidth(0),
25  m_RGBImageHeight(0),
26  m_RGBPixelNumber(0),
27  m_ImageSequence(0),
28  m_SourceDataSize(0),
29  m_IntensityArray(nullptr),
30  m_DistanceArray(nullptr),
31  m_AmplitudeArray(nullptr),
32  m_SourceDataArray(nullptr),
33  m_RgbDataArray(nullptr),
34  m_DeviceObserverTag()
35 {
36  // Create the output. We use static_cast<> here because we know the default
37  // output must be of type TOutputImage
38  OutputImageType::Pointer output0 = static_cast<OutputImageType*>(this->MakeOutput(0).GetPointer());
39  OutputImageType::Pointer output1 = static_cast<OutputImageType*>(this->MakeOutput(1).GetPointer());
40  OutputImageType::Pointer output2 = static_cast<OutputImageType*>(this->MakeOutput(2).GetPointer());
41  OutputImageType::Pointer output3 = static_cast<OutputImageType*>(this->MakeOutput(3).GetPointer());
42  mitk::ImageSource::SetNumberOfRequiredOutputs(3);
43  mitk::ImageSource::SetNthOutput(0, output0.GetPointer());
44  mitk::ImageSource::SetNthOutput(1, output1.GetPointer());
45  mitk::ImageSource::SetNthOutput(2, output2.GetPointer());
46  mitk::ImageSource::SetNthOutput(3, output3.GetPointer());
47 }
48 
50 {
52  {
54  {
55  m_ToFCameraDevice->RemoveObserver(m_DeviceObserverTag);
56  }
57  this->DisconnectCamera();
58  this->CleanUpImageArrays();
59  }
60 }
61 
63 {
64  int requiredImageSequence = 0;
65  // acquire new image data
66  this->m_ToFCameraDevice->GetAllImages(this->m_DistanceArray, this->m_AmplitudeArray, this->m_IntensityArray, this->m_SourceDataArray,
67  requiredImageSequence, this->m_ImageSequence, this->m_RgbDataArray );
68 
69  mitk::Image::Pointer distanceImage = this->GetOutput(0);
70  if (m_DistanceArray)
71  {
72  distanceImage->SetSlice(this->m_DistanceArray, 0, 0, 0);
73  }
74 
75  bool hasAmplitudeImage = false;
76  m_ToFCameraDevice->GetBoolProperty("HasAmplitudeImage", hasAmplitudeImage);
77  if((hasAmplitudeImage) && (m_AmplitudeArray))
78  {
79  mitk::Image::Pointer amplitudeImage = this->GetOutput(1);
80  amplitudeImage->SetSlice(this->m_AmplitudeArray, 0, 0, 0);
81  }
82 
83  bool hasIntensityImage = false;
84  m_ToFCameraDevice->GetBoolProperty("HasIntensityImage", hasIntensityImage);
85  if((hasIntensityImage) && (m_IntensityArray))
86  {
87  mitk::Image::Pointer intensityImage = this->GetOutput(2);
88  intensityImage->SetSlice(this->m_IntensityArray, 0, 0, 0);
89  }
90 
91  bool hasRGBImage = false;
92  m_ToFCameraDevice->GetBoolProperty("HasRGBImage", hasRGBImage);
93  if( hasRGBImage )
94  {
95  mitk::Image::Pointer rgbImage = this->GetOutput(3);
96  if (m_RgbDataArray)
97  {
98  rgbImage->SetSlice(this->m_RgbDataArray, 0, 0, 0);
99  }
100  }
101 }
102 
104 {
105  bool ok = m_ToFCameraDevice->ConnectCamera();
106  if (ok)
107  {
108  this->m_CaptureWidth = this->m_ToFCameraDevice->GetCaptureWidth();
109  this->m_CaptureHeight = this->m_ToFCameraDevice->GetCaptureHeight();
110  this->m_PixelNumber = this->m_CaptureWidth * this->m_CaptureHeight;
111 
112  this->m_RGBImageWidth = this->m_ToFCameraDevice->GetRGBCaptureWidth();
113  this->m_RGBImageHeight = this->m_ToFCameraDevice->GetRGBCaptureHeight();
114  this->m_RGBPixelNumber = this->m_RGBImageWidth * this->m_RGBImageHeight;
115 
116  this->m_SourceDataSize = m_ToFCameraDevice->GetSourceDataSize();
117  this->AllocateImageArrays();
118  this->InitializeImages();
119  }
120  return ok;
121 }
122 
124 {
125  return m_ToFCameraDevice->DisconnectCamera();
126 }
127 
129 {
130  m_ToFCameraDevice->StartCamera();
131  us::ModuleContext* context = us::GetModuleContext();
132  us::ServiceProperties deviceProps;
133  deviceProps["ToFImageSourceName"] = std::string("Image Grabber");
134  m_ServiceRegistration = context->RegisterService<mitk::ToFImageSource>(this,deviceProps);
135 }
136 
138 {
139  m_ToFCameraDevice->StopCamera();
142 }
143 
145 {
146  return m_ToFCameraDevice->IsCameraActive();
147 }
149 {
150  return m_ToFCameraDevice->IsCameraConnected();
151 }
152 
154 {
155  m_ToFCameraDevice = aToFCameraDevice;
156  itk::SimpleMemberCommand<ToFImageGrabber>::Pointer modifiedCommand = itk::SimpleMemberCommand<ToFImageGrabber>::New();
157  modifiedCommand->SetCallbackFunction(this, &ToFImageGrabber::OnToFCameraDeviceModified);
158  m_DeviceObserverTag = m_ToFCameraDevice->AddObserver(itk::ModifiedEvent(), modifiedCommand);
159  this->Modified();
160 }
161 
163 {
164  return m_ToFCameraDevice;
165 }
166 
168 {
169  return m_CaptureWidth;
170 }
171 
173 {
174  return m_CaptureHeight;
175 }
176 
178 {
179  return m_PixelNumber;
180 }
181 
183 {
184  return m_RGBImageWidth;
185 }
186 
188 {
189  return m_RGBImageHeight;
190 }
191 
193 {
194  return m_RGBPixelNumber;
195 }
196 
197 int ToFImageGrabber::SetModulationFrequency(int modulationFrequency)
198 {
199  this->m_ToFCameraDevice->SetProperty("ModulationFrequency",mitk::IntProperty::New(modulationFrequency));
200  this->Modified();
201  modulationFrequency = this->GetModulationFrequency(); // return the new valid modulation frequency from the camera
202  return modulationFrequency;
203 }
204 
205 int ToFImageGrabber::SetIntegrationTime(int integrationTime)
206 {
207  this->m_ToFCameraDevice->SetProperty("IntegrationTime",mitk::IntProperty::New(integrationTime));
208  this->Modified();
209  integrationTime = this->GetIntegrationTime(); // return the new valid integration time from the camera
210  return integrationTime;
211 }
212 
214 {
215  int integrationTime = 0;
216  this->m_ToFCameraDevice->GetIntProperty("IntegrationTime",integrationTime);
217  return integrationTime;
218 }
219 
221 {
222  int modulationFrequency = 0;
223  this->m_ToFCameraDevice->GetIntProperty("ModulationFrequency",modulationFrequency);
224  return modulationFrequency;
225 }
226 
227 void ToFImageGrabber::SetBoolProperty( const char* propertyKey, bool boolValue )
228 {
229  SetProperty(propertyKey, mitk::BoolProperty::New(boolValue));
230 }
231 
232 void ToFImageGrabber::SetIntProperty( const char* propertyKey, int intValue )
233 {
234  SetProperty(propertyKey, mitk::IntProperty::New(intValue));
235 }
236 
237 void ToFImageGrabber::SetFloatProperty( const char* propertyKey, float floatValue )
238 {
239  SetProperty(propertyKey, mitk::FloatProperty::New(floatValue));
240 }
241 
242 void ToFImageGrabber::SetStringProperty( const char* propertyKey, const char* stringValue )
243 {
244  SetProperty(propertyKey, mitk::StringProperty::New(stringValue));
245 }
246 
247 void ToFImageGrabber::SetProperty( const char *propertyKey, BaseProperty* propertyValue )
248 {
249  this->m_ToFCameraDevice->SetProperty(propertyKey, propertyValue);
250 }
251 
252 bool ToFImageGrabber::GetBoolProperty( const char* propertyKey)
253 {
254  mitk::BoolProperty::Pointer boolProp = dynamic_cast<mitk::BoolProperty*>(GetProperty(propertyKey));
255  if(!boolProp) return false;
256  return boolProp->GetValue();
257 }
258 
259 int ToFImageGrabber::GetIntProperty( const char* propertyKey)
260 {
261  mitk::IntProperty::Pointer intProp = dynamic_cast<mitk::IntProperty*>(GetProperty(propertyKey));
262  return intProp->GetValue();
263 }
264 
265 float ToFImageGrabber::GetFloatProperty( const char* propertyKey)
266 {
267  mitk::FloatProperty::Pointer floatProp = dynamic_cast<mitk::FloatProperty*>(GetProperty(propertyKey));
268  return floatProp->GetValue();
269 }
270 
271 const char* ToFImageGrabber::GetStringProperty( const char* propertyKey)
272 {
273  mitk::StringProperty::Pointer stringProp = dynamic_cast<mitk::StringProperty*>(GetProperty(propertyKey));
274  return stringProp->GetValue();
275 }
276 
277 BaseProperty* ToFImageGrabber::GetProperty( const char *propertyKey)
278 {
279  return this->m_ToFCameraDevice->GetProperty(propertyKey);
280 }
281 
283 {
284  this->Modified();
285 }
286 
288 {
289  // free buffer
290  if (m_IntensityArray)
291  {
292  delete [] m_IntensityArray;
293  m_IntensityArray = nullptr;
294  }
295  if (m_DistanceArray)
296  {
297  delete [] m_DistanceArray;
298  m_DistanceArray = nullptr;
299  }
300  if (m_AmplitudeArray)
301  {
302  delete [] m_AmplitudeArray;
303  m_AmplitudeArray = nullptr;
304  }
305  if (m_SourceDataArray)
306  {
307  delete [] m_SourceDataArray;
308  m_SourceDataArray = nullptr;
309  }
310  if (m_RgbDataArray)
311  {
312  delete [] m_RgbDataArray;
313  m_RgbDataArray = nullptr;
314  }
315 }
316 
318 {
319  // cleanup memory if necessary
320  this->CleanUpImageArrays();
321  // allocate buffer
322  m_IntensityArray = new float[m_PixelNumber];
323  m_DistanceArray = new float[m_PixelNumber];
324  m_AmplitudeArray = new float[m_PixelNumber];
326  m_RgbDataArray = new unsigned char[m_RGBPixelNumber*3];
327 }
328 
330 {
331  unsigned int dimensions[3];
332  dimensions[0] = this->m_ToFCameraDevice->GetCaptureWidth();
333  dimensions[1] = this->m_ToFCameraDevice->GetCaptureHeight();
334  dimensions[2] = 1;
335  mitk::PixelType FloatType = MakeScalarPixelType<float>();
336  mitk::Image::Pointer distanceImage = this->GetOutput();
337  distanceImage->ReleaseData();
338  distanceImage->Initialize(FloatType, 3, dimensions, 1);
339 
340  bool hasAmplitudeImage = false;
341  m_ToFCameraDevice->GetBoolProperty("HasAmplitudeImage", hasAmplitudeImage);
342  if(hasAmplitudeImage)
343  {
344  mitk::Image::Pointer amplitudeImage = this->GetOutput(1);
345  amplitudeImage->ReleaseData();
346  amplitudeImage->Initialize(FloatType, 3, dimensions, 1);
347  }
348 
349  bool hasIntensityImage = false;
350  m_ToFCameraDevice->GetBoolProperty("HasIntensityImage", hasIntensityImage);
351  if(hasIntensityImage)
352  {
353  mitk::Image::Pointer intensityImage = this->GetOutput(2);
354  intensityImage->ReleaseData();
355  intensityImage->Initialize(FloatType, 3, dimensions, 1);
356  }
357 
358  bool hasRGBImage = false;
359  m_ToFCameraDevice->GetBoolProperty("HasRGBImage", hasRGBImage);
360  if(hasRGBImage)
361  {
362  unsigned int rgbDimension[3];
363  rgbDimension[0] = this->m_ToFCameraDevice->GetRGBCaptureWidth();
364  rgbDimension[1] = this->m_ToFCameraDevice->GetRGBCaptureHeight();
365  rgbDimension[2] = 1 ;
366  mitk::Image::Pointer rgbImage = this->GetOutput(3);
367  rgbImage->ReleaseData();
368  rgbImage->Initialize(mitk::PixelType(MakePixelType<unsigned char, itk::RGBPixel<unsigned char>, 3>()), 3, rgbDimension,1);
369  }
370 }
371 }
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.
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.
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:72
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:51
bool GetBoolProperty(const char *propertyKey)