MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkToFCameraMITKPlayerDevice.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============================================================================*/
14#include "mitkRealTimeClock.h"
15
16#include <iostream>
17#include <fstream>
18#include <itksys/SystemTools.hxx>
19
20namespace mitk
21{
23 m_DistanceDataBuffer(nullptr), m_AmplitudeDataBuffer(nullptr), m_IntensityDataBuffer(nullptr), m_RGBDataBuffer(nullptr)
24{
25 m_Controller = ToFCameraMITKPlayerController::New();
26}
27
33
35{
36 bool ok = m_Controller->OpenCameraConnection();
37 if (ok)
38 {
39 this->m_CaptureWidth = m_Controller->GetCaptureWidth();
40 this->m_CaptureHeight = m_Controller->GetCaptureHeight();
41 this->m_RGBImageWidth = m_Controller->GetRGBCaptureWidth();
42 this->m_RGBImageHeight = m_Controller->GetRGBCaptureHeight();
43 this->m_PixelNumber = m_Controller->GetPixelNumber();
44 this->m_RGBPixelNumber = m_Controller->GetRGBPixelNumber();
45
47 this->SetBoolProperty("RGBImageHasDifferentResolution", true);
48
49
52
53 m_CameraConnected = true;
54 }
55 return ok;
56}
57
59{
60 bool ok = m_Controller->CloseCameraConnection();
61 if (ok)
62 {
63 m_CameraConnected = false;
64 m_PropertyList->Clear();
65 }
66 return ok;
67}
68
70{
72 {
73 // get the first image
74 this->m_Controller->UpdateCamera();
75 this->m_ImageMutex.lock();
76 this->m_Controller->GetDistances(this->m_DistanceDataBuffer[this->m_FreePos]);
77 this->m_Controller->GetAmplitudes(this->m_AmplitudeDataBuffer[this->m_FreePos]);
78 this->m_Controller->GetIntensities(this->m_IntensityDataBuffer[this->m_FreePos]);
79 this->m_Controller->GetRgb(this->m_RGBDataBuffer[this->m_FreePos]);
80 this->m_FreePos = (this->m_FreePos+1) % this->m_BufferSize;
81 this->m_CurrentPos = (this->m_CurrentPos+1) % this->m_BufferSize;
82 this->m_ImageSequence++;
83 this->m_ImageMutex.unlock();
84
85 this->m_CameraActiveMutex.lock();
86 this->m_CameraActive = true;
87 this->m_CameraActiveMutex.unlock();
88 m_Thread = std::thread(&ToFCameraMITKPlayerDevice::Acquire, this);
89 // wait a little to make sure that the thread is started
90 itksys::SystemTools::Delay(10);
91 }
92 else
93 {
94 MITK_INFO<<"Camera not connected";
95 }
96}
97
99{
100 m_Controller->UpdateCamera();
101}
102
104{
105 mitk::RealTimeClock::Pointer realTimeClock;
106 realTimeClock = mitk::RealTimeClock::New();
107 int n = 100;
108 double t1, t2;
109 t1 = realTimeClock->GetCurrentStamp();
110 bool overflow = false;
111 bool printStatus = false;
112 while (this->IsCameraActive())
113 {
114 // update the ToF camera
115 this->UpdateCamera();
116 // get image data from controller and write it to the according buffer
117 m_Controller->GetDistances(m_DistanceDataBuffer[m_FreePos]);
118 m_Controller->GetAmplitudes(m_AmplitudeDataBuffer[m_FreePos]);
119 m_Controller->GetIntensities(m_IntensityDataBuffer[m_FreePos]);
120 m_Controller->GetRgb(m_RGBDataBuffer[m_FreePos]);
121 this->Modified();
122 m_ImageMutex.lock();
126 if (m_FreePos == m_CurrentPos)
127 {
128 overflow = true;
129 }
130 if (m_ImageSequence % n == 0)
131 {
132 printStatus = true;
133 }
134 m_ImageMutex.unlock();
135 if (overflow)
136 {
137 overflow = false;
138 }
139 // print current framerate
140 if (printStatus)
141 {
142 t2 = realTimeClock->GetCurrentStamp() - t1;
143 MITK_INFO << " Framerate (fps): " << n / (t2/1000) << " Sequence: " << m_ImageSequence;
144 t1 = realTimeClock->GetCurrentStamp();
145 printStatus = false;
146 }
147 } // end of while loop
148}
149
150void ToFCameraMITKPlayerDevice::GetAmplitudes(float* amplitudeArray, int& imageSequence)
151{
152 m_ImageMutex.lock();
153 // write amplitude image data to float array
154 for (int i=0; i<this->m_PixelNumber; i++)
155 {
156 amplitudeArray[i] = this->m_AmplitudeDataBuffer[this->m_CurrentPos][i];
157 }
158 imageSequence = this->m_ImageSequence;
159 m_ImageMutex.unlock();
160}
161
162void ToFCameraMITKPlayerDevice::GetIntensities(float* intensityArray, int& imageSequence)
163{
164 m_ImageMutex.lock();
165 // write intensity image data to float array
166 for (int i=0; i<this->m_PixelNumber; i++)
167 {
168 intensityArray[i] = this->m_IntensityDataBuffer[this->m_CurrentPos][i];
169 }
170 imageSequence = this->m_ImageSequence;
171 m_ImageMutex.unlock();
172}
173
174void ToFCameraMITKPlayerDevice::GetDistances(float* distanceArray, int& imageSequence)
175{
176 m_ImageMutex.lock();
177 // write distance image data to float array
178 for (int i=0; i<this->m_PixelNumber; i++)
179 {
180 distanceArray[i] = this->m_DistanceDataBuffer[this->m_CurrentPos][i];
181 }
182 imageSequence = this->m_ImageSequence;
183 m_ImageMutex.unlock();
184}
185
186void ToFCameraMITKPlayerDevice::GetRgb(unsigned char* rgbArray, int& imageSequence)
187{
188 m_ImageMutex.lock();
189 // write intensity image data to unsigned char array
190 for (int i=0; i<this->m_RGBPixelNumber*3; i++)
191 {
192 rgbArray[i] = this->m_RGBDataBuffer[this->m_CurrentPos][i];
193 }
194 imageSequence = this->m_ImageSequence;
195 m_ImageMutex.unlock();
196}
197
198void ToFCameraMITKPlayerDevice::GetAllImages(float* distanceArray, float* amplitudeArray, float* intensityArray, char* /*sourceDataArray*/,
199 int requiredImageSequence, int& capturedImageSequence, unsigned char* rgbDataArray)
200{
201 m_ImageMutex.lock();
202
203 //check for empty buffer
204 if (this->m_ImageSequence < 0)
205 {
206 // buffer empty
207 MITK_INFO << "Buffer empty!! ";
208 capturedImageSequence = this->m_ImageSequence;
209 m_ImageMutex.unlock();
210 return;
211 }
212 // determine position of image in buffer
213 int pos = 0;
214 if ((requiredImageSequence < 0) || (requiredImageSequence > this->m_ImageSequence))
215 {
216 capturedImageSequence = this->m_ImageSequence;
217 pos = this->m_CurrentPos;
218 }
219 else if (requiredImageSequence <= this->m_ImageSequence - this->m_BufferSize)
220 {
221 capturedImageSequence = (this->m_ImageSequence - this->m_BufferSize) + 1;
222 pos = (this->m_CurrentPos + 1) % this->m_BufferSize;
223 }
224 else // (requiredImageSequence > this->m_ImageSequence - this->m_BufferSize) && (requiredImageSequence <= this->m_ImageSequence)
225 {
226 capturedImageSequence = requiredImageSequence;
227 pos = (this->m_CurrentPos + (10-(this->m_ImageSequence - requiredImageSequence))) % this->m_BufferSize;
228 }
229
230 if(this->m_DistanceDataBuffer&&this->m_AmplitudeDataBuffer&&this->m_IntensityDataBuffer&&this->m_RGBDataBuffer)
231 {
232 // write image data to float arrays
233 memcpy(distanceArray, this->m_DistanceDataBuffer[pos], this->m_PixelNumber * sizeof(float));
234 memcpy(amplitudeArray, this->m_AmplitudeDataBuffer[pos], this->m_PixelNumber * sizeof(float));
235 memcpy(intensityArray, this->m_IntensityDataBuffer[pos], this->m_PixelNumber * sizeof(float));
236 memcpy(rgbDataArray, this->m_RGBDataBuffer[pos], this->m_RGBPixelNumber * 3 * sizeof(unsigned char));
237 }
238 m_ImageMutex.unlock();
239}
240
241void ToFCameraMITKPlayerDevice::SetInputFileName(std::string inputFileName)
242{
243 this->m_InputFileName = inputFileName;
244 this->m_Controller->SetInputFileName(inputFileName);
245}
246
247void ToFCameraMITKPlayerDevice::SetProperty( const char *propertyKey, BaseProperty* propertyValue )
248{
249 this->m_PropertyList->SetProperty(propertyKey, propertyValue);
250
251 ToFCameraMITKPlayerController::Pointer myController = dynamic_cast<mitk::ToFCameraMITKPlayerController*>(this->m_Controller.GetPointer());
252
253 std::string strValue;
254 GetStringProperty(propertyKey, strValue);
255 if (strcmp(propertyKey, "DistanceImageFileName") == 0)
256 {
257 myController->SetDistanceImageFileName(strValue);
258 }
259 else if (strcmp(propertyKey, "AmplitudeImageFileName") == 0)
260 {
261 std::ifstream amplitudeImage(strValue.c_str());
262 if(amplitudeImage)
263 {
264 this->m_PropertyList->SetBoolProperty("HasAmplitudeImage", true);
265 myController->SetAmplitudeImageFileName(strValue);
266 }
267 else
268 {
269 MITK_WARN << "File " << strValue << " does not exist!";
270 }
271 }
272 else if (strcmp(propertyKey, "IntensityImageFileName") == 0)
273 {
274 std::ifstream intensityImage(strValue.c_str());
275 if(intensityImage)
276 {
277 this->m_PropertyList->SetBoolProperty("HasIntensityImage", true);
278 myController->SetIntensityImageFileName(strValue);
279 }
280 else
281 {
282 MITK_WARN << "File " << strValue << " does not exist!";
283 }
284 }
285 else if (strcmp(propertyKey, "RGBImageFileName") == 0)
286 {
287 std::ifstream intensityImage(strValue.c_str());
288 if(intensityImage)
289 {
290 this->m_PropertyList->SetBoolProperty("HasRGBImage", true);
291 myController->SetRGBImageFileName(strValue);
292 }
293 else
294 {
295 MITK_WARN << "File " << strValue << " does not exist!";
296 }
297 }
298}
299
301{
302 if (m_DistanceDataBuffer)
303 {
304 for(int i=0; i<this->m_MaxBufferSize; i++)
305 {
306 delete[] this->m_DistanceDataBuffer[i];
307 }
308 delete[] this->m_DistanceDataBuffer;
309 }
310 if (m_AmplitudeDataBuffer)
311 {
312 for(int i=0; i<this->m_MaxBufferSize; i++)
313 {
314 delete[] this->m_AmplitudeDataBuffer[i];
315 }
316 delete[] this->m_AmplitudeDataBuffer;
317 }
318 if (m_IntensityDataBuffer)
319 {
320 for(int i=0; i<this->m_MaxBufferSize; i++)
321 {
322 delete[] this->m_IntensityDataBuffer[i];
323 }
324 delete[] this->m_IntensityDataBuffer;
325 }
326 if (m_RGBDataBuffer)
327 {
328 for(int i=0; i<this->m_MaxBufferSize; i++)
329 {
330 delete[] this->m_RGBDataBuffer[i];
331 }
332 delete[] this->m_RGBDataBuffer;
333 }
334}
335
337{
338 // free memory if it was already allocated
339 this->CleanUpDataBuffers();
340 // allocate buffers
341 this->m_DistanceDataBuffer = new float*[this->m_MaxBufferSize];
342 for(int i=0; i<this->m_MaxBufferSize; i++)
343 {
344 this->m_DistanceDataBuffer[i] = new float[this->m_PixelNumber];
345 }
346 this->m_AmplitudeDataBuffer = new float*[this->m_MaxBufferSize];
347 for(int i=0; i<this->m_MaxBufferSize; i++)
348 {
349 this->m_AmplitudeDataBuffer[i] = new float[this->m_PixelNumber];
350 }
351 this->m_IntensityDataBuffer = new float*[this->m_MaxBufferSize];
352 for(int i=0; i<this->m_MaxBufferSize; i++)
353 {
354 this->m_IntensityDataBuffer[i] = new float[this->m_PixelNumber];
355 }
356 this->m_RGBDataBuffer = new unsigned char*[this->m_MaxBufferSize];
357 for(int i=0; i<this->m_MaxBufferSize; i++)
358 {
359 this->m_RGBDataBuffer[i] = new unsigned char[this->m_RGBPixelNumber*3];
360 }
361}
362}
static Pointer New(void)
instanciates a new, operating-system dependant, instance of mitk::RealTimeClock.
int m_ImageSequence
counter for acquired images
PropertyList::Pointer m_PropertyList
a list of the corresponding properties
int m_CaptureWidth
width of the range image (x dimension)
void SetBoolProperty(const char *propertyKey, bool boolValue)
set a bool property in the property list
int m_FreePos
current position in the buffer which will be filled with data acquired from the hardware
bool GetStringProperty(const char *propertyKey, std::string &string)
get a string from the property list
int m_RGBImageWidth
width of the RGB image (x dimension)
virtual bool IsCameraActive()
returns true if the camera is connected and started
virtual void AllocatePixelArrays()
method for allocating memory for pixel arrays m_IntensityArray, m_DistanceArray and m_AmplitudeArray
int m_CurrentPos
current position in the buffer which will be retrieved by the Get methods
int m_PixelNumber
number of pixels in the range image (m_CaptureWidth*m_CaptureHeight)
bool m_CameraConnected
flag indicating if the camera is successfully connected or not. Caution: thread safe access only!
int m_CaptureHeight
height of the range image (y dimension)
bool m_CameraActive
flag indicating if the camera is currently active or not. Caution: thread safe access only!
int m_BufferSize
buffer size of the image buffer needed for loss-less acquisition of range data
int m_RGBPixelNumber
number of pixels in the range image (m_RGBImageWidth*m_RGBImageHeight)
std::mutex m_CameraActiveMutex
mutex for the cameraActive flag
std::mutex m_ImageMutex
mutex for images provided by the range camera
int m_RGBImageHeight
height of the RGB image (y dimension)
int m_MaxBufferSize
maximal buffer size needed for initialization of data arrays. Default value is 100.
Controller for playing ToF images saved in NRRD format.
void SetProperty(const char *propertyKey, BaseProperty *propertyValue) override
set a BaseProperty
void Acquire()
Thread method continuously acquiring images from the specified input file.
virtual void SetInputFileName(std::string inputFileName)
Set file name where the data is recorded.
void GetIntensities(float *intensityArray, int &imageSequence) override
gets the intensity data from the ToF camera as a greyscale image. Caution! The user is responsible fo...
void CleanUpDataBuffers()
Clean up memory (pixel buffers)
ToFCameraMITKPlayerController::Pointer m_Controller
member holding the corresponding controller
void AllocateDataBuffers()
Allocate pixel buffers.
std::string m_InputFileName
member holding the file name of the current input file
virtual void GetRgb(unsigned char *rgbArray, int &imageSequence)
gets the rgb data from the ToF camera. Caution! The user is responsible for allocating and deleting t...
void GetAmplitudes(float *amplitudeArray, int &imageSequence) override
gets the amplitude data from the ToF camera as the strength of the active illumination of every pixel...
bool OnConnectCamera() override
opens a connection to the ToF camera
void GetDistances(float *distanceArray, int &imageSequence) override
gets the distance data from the ToF camera measuring the distance between the camera and the differen...
void StartCamera() override
starts the continuous updating of the camera. A separate thread updates the source data,...
void GetAllImages(float *distanceArray, float *amplitudeArray, float *intensityArray, char *sourceDataArray, int requiredImageSequence, int &capturedImageSequence, unsigned char *rgbDataArray=nullptr) override
gets the 3 images (distance, amplitude, intensity) from the ToF camera. Caution! The user is responsi...
void UpdateCamera() override
updates the camera for image acquisition
bool DisconnectCamera() override
closes the connection to the camera
IGT Exceptions.
int strcmp(const String &s1, const String &s2)
Definition relates.cpp:14