MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkToFImageGrabber.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12#include "mitkToFImageGrabber.h"
13#include <itkCommand.h>
14#include <usModuleContext.h>
15#include <usGetModuleContext.h>
16
17namespace 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
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);
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);
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();
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();
140 if (m_ServiceRegistration != nullptr) m_ServiceRegistration.Unregister();
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
166
171
176
181
186
191
196
197int 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
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
227void ToFImageGrabber::SetBoolProperty( const char* propertyKey, bool boolValue )
228{
229 SetProperty(propertyKey, mitk::BoolProperty::New(boolValue));
230}
231
232void ToFImageGrabber::SetIntProperty( const char* propertyKey, int intValue )
233{
234 SetProperty(propertyKey, mitk::IntProperty::New(intValue));
235}
236
237void ToFImageGrabber::SetFloatProperty( const char* propertyKey, float floatValue )
238{
239 SetProperty(propertyKey, mitk::FloatProperty::New(floatValue));
240}
241
242void ToFImageGrabber::SetStringProperty( const char* propertyKey, const char* stringValue )
243{
244 SetProperty(propertyKey, mitk::StringProperty::New(stringValue));
245}
246
247void ToFImageGrabber::SetProperty( const char *propertyKey, BaseProperty* propertyValue )
248{
249 this->m_ToFCameraDevice->SetProperty(propertyKey, propertyValue);
250}
251
252bool 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
259int ToFImageGrabber::GetIntProperty( const char* propertyKey)
260{
261 mitk::IntProperty::Pointer intProp = dynamic_cast<mitk::IntProperty*>(GetProperty(propertyKey));
262 return intProp->GetValue();
263}
264
265float ToFImageGrabber::GetFloatProperty( const char* propertyKey)
266{
267 mitk::FloatProperty::Pointer floatProp = dynamic_cast<mitk::FloatProperty*>(GetProperty(propertyKey));
268 return floatProp->GetValue();
269}
270
271const char* ToFImageGrabber::GetStringProperty( const char* propertyKey)
272{
273 mitk::StringProperty::Pointer stringProp = dynamic_cast<mitk::StringProperty*>(GetProperty(propertyKey));
274 return stringProp->GetValue();
275}
276
277BaseProperty* 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
291 {
292 delete [] m_IntensityArray;
293 m_IntensityArray = nullptr;
294 }
295 if (m_DistanceArray)
296 {
297 delete [] m_DistanceArray;
298 m_DistanceArray = nullptr;
299 }
301 {
302 delete [] m_AmplitudeArray;
303 m_AmplitudeArray = nullptr;
304 }
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}
Virtual interface and base class for all Time-of-Flight devices.
virtual void SetProperty(const char *propertyKey, BaseProperty *propertyValue)
set a BaseProperty property in the property list
int GetCaptureWidth()
Get the dimension in x direction of the ToF image.
float * m_AmplitudeArray
member holding the current amplitude array
void SetProperty(const char *propertyKey, BaseProperty *propertyValue)
unsigned char * m_RgbDataArray
member holding the current rgb data array
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 bool DisconnectCamera()
Disconnects the ToF camera.
int GetIntProperty(const char *propertyKey)
virtual void StopCamera()
Stops the continuous updating of the camera.
void InitializeImages()
InitializeImages Initialze the geometries of the images according to the device properties.
int GetRGBImageWidth()
Get the dimension in x direction of the ToF image.
int m_SourceDataSize
size of the source data in bytes
int SetModulationFrequency(int modulationFrequency)
Set the modulation frequency used by the ToF camera. For default values see the corresponding device ...
unsigned long m_DeviceObserverTag
tag of the observer for the ToFCameraDevice
int GetCaptureHeight()
Get the dimension in y direction of the ToF image.
int GetIntegrationTime()
Get the integration time in used by the ToF camera.
void SetFloatProperty(const char *propertyKey, float floatValue)
virtual bool ConnectCamera()
Establish a connection to the ToF camera.
int GetRGBPixelNumber()
Get the number of pixel in the ToF image.
virtual void AllocateImageArrays()
Allocate memory for the image arrays m_IntensityArray, m_DistanceArray, m_AmplitudeArray and m_Source...
int GetModulationFrequency()
Get the modulation frequency used by the ToF camera.
float GetFloatProperty(const char *propertyKey)
float * m_DistanceArray
member holding the current distance array
void GenerateData() override
Method generating the outputs of this filter. Called in the updated process of the pipeline....
void SetIntProperty(const char *propertyKey, int intValue)
void SetBoolProperty(const char *propertyKey, bool boolValue)
float * m_IntensityArray
member holding the current intensity array
int m_CaptureHeight
Height of the captured ToF image.
int m_CaptureWidth
Width of the captured ToF image.
int SetIntegrationTime(int integrationTime)
Set the integration time used by the ToF camera. For default values see the corresponding device clas...
virtual void StartCamera()
Starts the continuous updating of the camera. A separate thread updates the source data,...
int m_RGBImageHeight
Height of the captured RGB image.
ToFCameraDevice::Pointer m_ToFCameraDevice
Device allowing access to ToF image data.
virtual void CleanUpImageArrays()
clean up memory allocated for the image arrays m_IntensityArray, m_DistanceArray, m_AmplitudeArray an...
bool GetBoolProperty(const char *propertyKey)
ToFCameraDevice * GetCameraDevice()
Get the currently set ToF camera device.
int m_RGBImageWidth
Width of the captured RGB image.
const char * GetStringProperty(const char *propertyKey)
BaseProperty * GetProperty(const char *propertyKey)
void SetCameraDevice(ToFCameraDevice *aToFCameraDevice)
Sets the ToF device, the image grabber is grabbing the images from.
char * m_SourceDataArray
member holding the current source data array
void SetStringProperty(const char *propertyKey, const char *stringValue)
virtual bool IsCameraActive()
Returns true if the camera is connected and started.
int GetRGBImageHeight()
Get the dimension in y direction of the ToF image.
int GetPixelNumber()
Get the number of pixel in the ToF image.
int m_PixelNumber
Number of pixels in the image.
Image source providing ToF images. Interface for filters provided in ToFProcessing module.
us::ServiceRegistration< Self > m_ServiceRegistration
IGT Exceptions.