Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkToFCameraMITKPlayerController.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 ===================================================================*/
17 #include <itksys/SystemTools.hxx>
18 
19 // mitk includes
20 #include "mitkIOUtil.h"
21 #include <mitkIpPic.h>
22 #include "mitkImageReadAccessor.h"
23 
24 namespace mitk
25 {
26 
28  m_PixelNumber(0),
29  m_RGBPixelNumber(0),
30  m_NumberOfBytes(0),
31  m_NumberOfRGBBytes(0),
32  m_CaptureWidth(0),
33  m_CaptureHeight(0),
34  m_RGBCaptureWidth(0),
35  m_RGBCaptureHeight(0),
36  m_ConnectionCheck(false),
37  m_InputFileName(""),
38  m_ToFImageType(ToFImageType3D),
39  m_DistanceImage(0),
40  m_AmplitudeImage(0),
41  m_IntensityImage(0),
42  m_RGBImage(0),
43  m_DistanceInfile(NULL),
44  m_AmplitudeInfile(NULL),
45  m_IntensityInfile(NULL),
46  m_RGBInfile(NULL),
47  m_IntensityArray(NULL),
48  m_DistanceArray(NULL),
49  m_AmplitudeArray(NULL),
50  m_RGBArray(NULL),
51  m_DistanceImageFileName(""),
52  m_AmplitudeImageFileName(""),
53  m_IntensityImageFileName(""),
54  m_RGBImageFileName(""),
55  m_PixelStartInFile(0),
56  m_CurrentFrame(-1),
57  m_NumOfFrames(0)
58 {
59  m_ImageStatus = std::vector<bool>(4,true);
60 }
61 
63 {
64  this->CleanUp();
65 }
66 
67 void ToFCameraMITKPlayerController::CleanUp()
68 {
69  if(m_DistanceImage.IsNotNull())
70  {
71  m_DistanceImage->ReleaseData();
72  m_DistanceImage = NULL;
73  }
74  if(m_AmplitudeImage.IsNotNull())
75  {
76  m_AmplitudeImage->ReleaseData();
77  m_AmplitudeImage = NULL;
78  }
79  if(m_IntensityImage.IsNotNull())
80  {
81  m_IntensityImage->ReleaseData();
82  m_IntensityImage = NULL;
83  }
84  if(m_RGBImage.IsNotNull())
85  {
86  m_RGBImage->ReleaseData();
87  m_RGBImage = NULL;
88  }
89 
90  delete[] this->m_DistanceArray;
91  this->m_DistanceArray = NULL;
92  delete[] this->m_AmplitudeArray;
93  this->m_AmplitudeArray = NULL;
94  delete[] this->m_IntensityArray;
95  this->m_IntensityArray = NULL;
96  delete[] this->m_RGBArray;
97  this->m_RGBArray = NULL;
98 
99  this->m_DistanceImageFileName = "";
100  this->m_AmplitudeImageFileName = "";
101  this->m_IntensityImageFileName = "";
102  this->m_RGBImageFileName = "";
103 }
104 
106 {
107  if(!this->m_ConnectionCheck)
108  {
109  // reset the image status before connection
110  m_ImageStatus = std::vector<bool>(4,true);
111  try
112  {
113  if (this->m_DistanceImageFileName.empty() &&
114  this->m_AmplitudeImageFileName.empty() &&
115  this->m_IntensityImageFileName.empty() &&
116  this->m_RGBImageFileName.empty())
117  {
118  throw std::logic_error("No image data file names set");
119  }
120 
121  if (!this->m_DistanceImageFileName.empty())
122  {
124  }
125  else
126  {
127  MITK_ERROR << "ToF distance image data file empty";
128  }
129  if (!this->m_AmplitudeImageFileName.empty())
130  {
132  }
133  else
134  {
135  MITK_WARN << "ToF amplitude image data file empty";
136  }
137  if (!this->m_IntensityImageFileName.empty())
138  {
140  }
141  else
142  {
143  MITK_WARN << "ToF intensity image data file empty";
144  }
145  if (!this->m_RGBImageFileName.empty())
146  {
148  }
149  else
150  {
151  MITK_WARN << "ToF RGB image data file empty";
152  }
153 
154  // check if the opened files contained data
155  if(m_DistanceImage.IsNull())
156  {
157  m_ImageStatus.at(0) = false;
158  }
159  if(m_AmplitudeImage.IsNull())
160  {
161  m_ImageStatus.at(1) = false;
162  }
163  if(m_IntensityImage.IsNull())
164  {
165  m_ImageStatus.at(2) = false;
166  }
167  if(m_RGBImage.IsNull())
168  {
169  m_ImageStatus.at(3) = false;
170  }
171 
172  // Check for dimension type
173  mitk::Image::Pointer infoImage = NULL;
174  if(m_ImageStatus.at(0))
175  {
176  infoImage = m_DistanceImage;
177  }
178  else if (m_ImageStatus.at(1))
179  {
180  infoImage = m_AmplitudeImage;
181  }
182  else if(m_ImageStatus.at(2))
183  {
184  infoImage = m_IntensityImage;
185  }
186  else if(m_ImageStatus.at(3))
187  {
188  infoImage = m_RGBImage;
189  }
190 
191  if (infoImage->GetDimension() == 2)
193 
194  else if (infoImage->GetDimension() == 3)
196 
197  else if (infoImage->GetDimension() == 4)
199 
200  else
201  throw std::logic_error("Error opening ToF data file: Invalid dimension.");
202 
203  this->m_CaptureWidth = infoImage->GetDimension(0);
204  this->m_CaptureHeight = infoImage->GetDimension(1);
205  this->m_PixelNumber = this->m_CaptureWidth*this->m_CaptureHeight;
206  this->m_NumberOfBytes = this->m_PixelNumber * sizeof(float);
207 
208  if(m_RGBImage)
209  {
210  m_RGBCaptureWidth = m_RGBImage->GetDimension(0);
211  m_RGBCaptureHeight = m_RGBImage->GetDimension(1);
214  }
215 
216  if (this->m_ToFImageType == ToFImageType2DPlusT)
217  {
218  this->m_NumOfFrames = infoImage->GetDimension(3);
219  }
220  else
221  {
222  this->m_NumOfFrames = infoImage->GetDimension(2);
223  }
224 
225  // allocate buffer
226  this->m_DistanceArray = new float[this->m_PixelNumber];
227  for(int i=0; i<this->m_PixelNumber; i++) {this->m_DistanceArray[i]=0.0;}
228  this->m_AmplitudeArray = new float[this->m_PixelNumber];
229  for(int i=0; i<this->m_PixelNumber; i++) {this->m_AmplitudeArray[i]=0.0;}
230  this->m_IntensityArray = new float[this->m_PixelNumber];
231  for(int i=0; i<this->m_PixelNumber; i++) {this->m_IntensityArray[i]=0.0;}
232  this->m_RGBArray = new unsigned char[m_NumberOfRGBBytes];
233  for(int i=0; i<m_NumberOfRGBBytes; i++) {this->m_RGBArray[i]=0.0;}
234 
235  MITK_INFO << "NumOfFrames: " << this->m_NumOfFrames;
236 
237  this->m_ConnectionCheck = true;
238  return this->m_ConnectionCheck;
239  }
240  catch(std::exception& e)
241  {
242  MITK_ERROR << "Error opening ToF data file " << this->m_InputFileName << " " << e.what();
243  throw std::logic_error("Error opening ToF data file");
244  return false;
245  }
246  }
247  else
248  return this->m_ConnectionCheck;
249 }
250 
252 {
253  if (this->m_ConnectionCheck)
254  {
255  this->CleanUp();
256  this->m_ConnectionCheck = false;
257  return true;
258  }
259  return false;
260 }
261 
263 {
264  this->m_CurrentFrame++;
265  if(this->m_CurrentFrame >= this->m_NumOfFrames)
266  {
267  this->m_CurrentFrame = 0;
268  }
269 
270  if(this->m_ImageStatus.at(0))
271  {
272  this->AccessData(this->m_CurrentFrame, this->m_DistanceImage, this->m_DistanceArray);
273  }
274  if(this->m_ImageStatus.at(1))
275  {
276  this->AccessData(this->m_CurrentFrame, this->m_AmplitudeImage, this->m_AmplitudeArray);
277  }
278  if(this->m_ImageStatus.at(2))
279  {
280  this->AccessData(this->m_CurrentFrame, this->m_IntensityImage, this->m_IntensityArray);
281  }
282  if(this->m_ImageStatus.at(3))
283  {
284  if(!this->m_ToFImageType)
285  {
286  ImageReadAccessor rgbAcc(m_RGBImage, m_RGBImage->GetSliceData(m_CurrentFrame));
287  memcpy(m_RGBArray, rgbAcc.GetData(), m_NumberOfRGBBytes );
288  }
289  else if(this->m_ToFImageType)
290  {
291  ImageReadAccessor rgbAcc(m_RGBImage, m_RGBImage->GetVolumeData(m_CurrentFrame));
292  memcpy(m_RGBArray, rgbAcc.GetData(), m_NumberOfRGBBytes);
293  }
294  }
295  itksys::SystemTools::Delay(50);
296 }
297 
298 void ToFCameraMITKPlayerController::AccessData(int frame, Image::Pointer image, float* &data)
299 {
300  if(!this->m_ToFImageType)
301  {
302  ImageReadAccessor imgAcc(image, image->GetSliceData(frame));
303  memcpy(data, imgAcc.GetData(), this->m_NumberOfBytes );
304  }
305  else if(this->m_ToFImageType)
306  {
307  ImageReadAccessor imgAcc(image, image->GetVolumeData(frame));
308  memcpy(data, imgAcc.GetData(), this->m_NumberOfBytes);
309  }
310 }
311 
313 {
314  memcpy(amplitudeArray, this->m_AmplitudeArray, this->m_NumberOfBytes);
315 }
316 
318 {
319  memcpy(intensityArray, this->m_IntensityArray, this->m_NumberOfBytes);
320 }
321 
323 {
324  memcpy(distanceArray, this->m_DistanceArray, this->m_NumberOfBytes);
325 }
326 
327 void ToFCameraMITKPlayerController::GetRgb(unsigned char* rgbArray)
328 {
329  memcpy(rgbArray, this->m_RGBArray, m_NumberOfRGBBytes);
330 }
331 
332 void ToFCameraMITKPlayerController::SetInputFileName(std::string inputFileName)
333 {
334  this->m_InputFileName = inputFileName;
335 }
336 
337 }
bool m_ConnectionCheck
flag showing whether the camera is connected (true) or not (false)
virtual void GetRgb(unsigned char *rgbArray)
gets the current RGB frame from the input if available
int m_RGBCaptureWidth
same for RGB image which can be different then depth etc.
#define MITK_INFO
Definition: mitkLogMacros.h:22
virtual bool CloseCameraConnection()
closes the connection to the camera
std::string m_RGBImageFileName
file name of the rgb image to be played
#define MITK_ERROR
Definition: mitkLogMacros.h:24
int m_PixelNumber
holds the number of pixels contained in the image
virtual void UpdateCamera()
updates the current image frames from input
DataCollection - Class to facilitate loading/accessing structured data.
std::string m_AmplitudeImageFileName
file name of the amplitude image to be played
float * m_AmplitudeArray
member holding the current amplitude frame
int m_CaptureHeight
holds the height of the image
ToFImageType m_ToFImageType
type of the ToF image to be played: 3D Volume (ToFImageType3D), temporal 2D image stack (ToFImageType...
virtual void GetAmplitudes(float *amplitudeArray)
gets the current amplitude frame from the input These values can be used to determine the quality of ...
virtual void SetInputFileName(std::string inputFileName)
#define MITK_WARN
Definition: mitkLogMacros.h:23
std::string m_DistanceImageFileName
file name of the distance image to be played
virtual void GetIntensities(float *intensityArray)
gets the current intensity frame from the input as a greyscale image
virtual void GetDistances(float *distanceArray)
gets the current distance frame from the inpug measuring the distance between the camera and the diff...
float * m_DistanceArray
member holding the current distance frame
virtual bool OpenCameraConnection()
opens a connection to the ToF camera
std::string m_IntensityImageFileName
file name of the intensity image to be played
int m_RGBCaptureHeight
same for RGB image which can be different then depth etc.
ImageReadAccessor class to get locked read access for a particular image part.
float * m_IntensityArray
member holding the current intensity frame
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
Definition: mitkIOUtil.cpp:597
int m_NumberOfBytes
holds the number of bytes contained in the image
unsigned char * m_RGBArray
member holding the current rgb frame