Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (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 ============================================================================*/
13 #include <itksys/SystemTools.hxx>
14 
15 // mitk includes
16 #include "mitkIOUtil.h"
17 #include <mitkIpPic.h>
18 #include "mitkImageReadAccessor.h"
19 
20 namespace mitk
21 {
22 
24  m_PixelNumber(0),
25  m_RGBPixelNumber(0),
26  m_NumberOfBytes(0),
27  m_NumberOfRGBBytes(0),
28  m_CaptureWidth(0),
29  m_CaptureHeight(0),
30  m_RGBCaptureWidth(0),
31  m_RGBCaptureHeight(0),
32  m_ConnectionCheck(false),
33  m_InputFileName(""),
34  m_ToFImageType(ToFImageType3D),
35  m_DistanceImage(nullptr),
36  m_AmplitudeImage(nullptr),
37  m_IntensityImage(nullptr),
38  m_RGBImage(nullptr),
39  m_DistanceInfile(nullptr),
40  m_AmplitudeInfile(nullptr),
41  m_IntensityInfile(nullptr),
42  m_RGBInfile(nullptr),
43  m_IntensityArray(nullptr),
44  m_DistanceArray(nullptr),
45  m_AmplitudeArray(nullptr),
46  m_RGBArray(nullptr),
47  m_DistanceImageFileName(""),
48  m_AmplitudeImageFileName(""),
49  m_IntensityImageFileName(""),
50  m_RGBImageFileName(""),
51  m_PixelStartInFile(0),
52  m_CurrentFrame(-1),
53  m_NumOfFrames(0)
54 {
55  m_ImageStatus = std::vector<bool>(4,true);
56 }
57 
59 {
60  this->CleanUp();
61 }
62 
63 void ToFCameraMITKPlayerController::CleanUp()
64 {
65  if(m_DistanceImage.IsNotNull())
66  {
67  m_DistanceImage->ReleaseData();
68  m_DistanceImage = nullptr;
69  }
70  if(m_AmplitudeImage.IsNotNull())
71  {
72  m_AmplitudeImage->ReleaseData();
73  m_AmplitudeImage = nullptr;
74  }
75  if(m_IntensityImage.IsNotNull())
76  {
77  m_IntensityImage->ReleaseData();
78  m_IntensityImage = nullptr;
79  }
80  if(m_RGBImage.IsNotNull())
81  {
82  m_RGBImage->ReleaseData();
83  m_RGBImage = nullptr;
84  }
85 
86  delete[] this->m_DistanceArray;
87  this->m_DistanceArray = nullptr;
88  delete[] this->m_AmplitudeArray;
89  this->m_AmplitudeArray = nullptr;
90  delete[] this->m_IntensityArray;
91  this->m_IntensityArray = nullptr;
92  delete[] this->m_RGBArray;
93  this->m_RGBArray = nullptr;
94 
95  this->m_DistanceImageFileName = "";
96  this->m_AmplitudeImageFileName = "";
97  this->m_IntensityImageFileName = "";
98  this->m_RGBImageFileName = "";
99 }
100 
102 {
103  if(!this->m_ConnectionCheck)
104  {
105  // reset the image status before connection
106  m_ImageStatus = std::vector<bool>(4,true);
107  try
108  {
109  if (this->m_DistanceImageFileName.empty() &&
110  this->m_AmplitudeImageFileName.empty() &&
111  this->m_IntensityImageFileName.empty() &&
112  this->m_RGBImageFileName.empty())
113  {
114  throw std::logic_error("No image data file names set");
115  }
116 
117  if (!this->m_DistanceImageFileName.empty())
118  {
119  m_DistanceImage = mitk::IOUtil::Load<mitk::Image>(this->m_DistanceImageFileName);
120  }
121  else
122  {
123  MITK_ERROR << "ToF distance image data file empty";
124  }
125  if (!this->m_AmplitudeImageFileName.empty())
126  {
127  m_AmplitudeImage = mitk::IOUtil::Load<mitk::Image>(this->m_AmplitudeImageFileName);
128  }
129  else
130  {
131  MITK_WARN << "ToF amplitude image data file empty";
132  }
133  if (!this->m_IntensityImageFileName.empty())
134  {
135  m_IntensityImage = mitk::IOUtil::Load<mitk::Image>(this->m_IntensityImageFileName);
136  }
137  else
138  {
139  MITK_WARN << "ToF intensity image data file empty";
140  }
141  if (!this->m_RGBImageFileName.empty())
142  {
143  m_RGBImage = mitk::IOUtil::Load<mitk::Image>(this->m_RGBImageFileName);
144  }
145  else
146  {
147  MITK_WARN << "ToF RGB image data file empty";
148  }
149 
150  // check if the opened files contained data
151  if(m_DistanceImage.IsNull())
152  {
153  m_ImageStatus.at(0) = false;
154  }
155  if(m_AmplitudeImage.IsNull())
156  {
157  m_ImageStatus.at(1) = false;
158  }
159  if(m_IntensityImage.IsNull())
160  {
161  m_ImageStatus.at(2) = false;
162  }
163  if(m_RGBImage.IsNull())
164  {
165  m_ImageStatus.at(3) = false;
166  }
167 
168  // Check for dimension type
169  mitk::Image::Pointer infoImage = nullptr;
170  if(m_ImageStatus.at(0))
171  {
172  infoImage = m_DistanceImage;
173  }
174  else if (m_ImageStatus.at(1))
175  {
176  infoImage = m_AmplitudeImage;
177  }
178  else if(m_ImageStatus.at(2))
179  {
180  infoImage = m_IntensityImage;
181  }
182  else if(m_ImageStatus.at(3))
183  {
184  infoImage = m_RGBImage;
185  }
186 
187  if (infoImage->GetDimension() == 2)
189 
190  else if (infoImage->GetDimension() == 3)
192 
193  else if (infoImage->GetDimension() == 4)
195 
196  else
197  throw std::logic_error("Error opening ToF data file: Invalid dimension.");
198 
199  this->m_CaptureWidth = infoImage->GetDimension(0);
200  this->m_CaptureHeight = infoImage->GetDimension(1);
201  this->m_PixelNumber = this->m_CaptureWidth*this->m_CaptureHeight;
202  this->m_NumberOfBytes = this->m_PixelNumber * sizeof(float);
203 
204  if(m_RGBImage)
205  {
206  m_RGBCaptureWidth = m_RGBImage->GetDimension(0);
207  m_RGBCaptureHeight = m_RGBImage->GetDimension(1);
210  }
211 
212  if (this->m_ToFImageType == ToFImageType2DPlusT)
213  {
214  this->m_NumOfFrames = infoImage->GetDimension(3);
215  }
216  else
217  {
218  this->m_NumOfFrames = infoImage->GetDimension(2);
219  }
220 
221  // allocate buffer
222  this->m_DistanceArray = new float[this->m_PixelNumber];
223  for(int i=0; i<this->m_PixelNumber; i++) {this->m_DistanceArray[i]=0.0;}
224  this->m_AmplitudeArray = new float[this->m_PixelNumber];
225  for(int i=0; i<this->m_PixelNumber; i++) {this->m_AmplitudeArray[i]=0.0;}
226  this->m_IntensityArray = new float[this->m_PixelNumber];
227  for(int i=0; i<this->m_PixelNumber; i++) {this->m_IntensityArray[i]=0.0;}
228  this->m_RGBArray = new unsigned char[m_NumberOfRGBBytes];
229  for(int i=0; i<m_NumberOfRGBBytes; i++) {this->m_RGBArray[i]=0.0;}
230 
231  MITK_INFO << "NumOfFrames: " << this->m_NumOfFrames;
232 
233  this->m_ConnectionCheck = true;
234  return this->m_ConnectionCheck;
235  }
236  catch(std::exception& e)
237  {
238  MITK_ERROR << "Error opening ToF data file " << this->m_InputFileName << " " << e.what();
239  throw std::logic_error("Error opening ToF data file");
240  return false;
241  }
242  }
243  else
244  return this->m_ConnectionCheck;
245 }
246 
248 {
249  if (this->m_ConnectionCheck)
250  {
251  this->CleanUp();
252  this->m_ConnectionCheck = false;
253  return true;
254  }
255  return false;
256 }
257 
259 {
260  this->m_CurrentFrame++;
261  if(this->m_CurrentFrame >= this->m_NumOfFrames)
262  {
263  this->m_CurrentFrame = 0;
264  }
265 
266  if(this->m_ImageStatus.at(0))
267  {
268  this->AccessData(this->m_CurrentFrame, this->m_DistanceImage, this->m_DistanceArray);
269  }
270  if(this->m_ImageStatus.at(1))
271  {
272  this->AccessData(this->m_CurrentFrame, this->m_AmplitudeImage, this->m_AmplitudeArray);
273  }
274  if(this->m_ImageStatus.at(2))
275  {
276  this->AccessData(this->m_CurrentFrame, this->m_IntensityImage, this->m_IntensityArray);
277  }
278  if(this->m_ImageStatus.at(3))
279  {
280  if(!this->m_ToFImageType)
281  {
282  ImageReadAccessor rgbAcc(m_RGBImage, m_RGBImage->GetSliceData(m_CurrentFrame));
283  memcpy(m_RGBArray, rgbAcc.GetData(), m_NumberOfRGBBytes );
284  }
285  else if(this->m_ToFImageType)
286  {
287  ImageReadAccessor rgbAcc(m_RGBImage, m_RGBImage->GetVolumeData(m_CurrentFrame));
288  memcpy(m_RGBArray, rgbAcc.GetData(), m_NumberOfRGBBytes);
289  }
290  }
291  itksys::SystemTools::Delay(50);
292 }
293 
294 void ToFCameraMITKPlayerController::AccessData(int frame, Image::Pointer image, float* &data)
295 {
296  if(!this->m_ToFImageType)
297  {
298  ImageReadAccessor imgAcc(image, image->GetSliceData(frame));
299  memcpy(data, imgAcc.GetData(), this->m_NumberOfBytes );
300  }
301  else if(this->m_ToFImageType)
302  {
303  ImageReadAccessor imgAcc(image, image->GetVolumeData(frame));
304  memcpy(data, imgAcc.GetData(), this->m_NumberOfBytes);
305  }
306 }
307 
309 {
310  memcpy(amplitudeArray, this->m_AmplitudeArray, this->m_NumberOfBytes);
311 }
312 
314 {
315  memcpy(intensityArray, this->m_IntensityArray, this->m_NumberOfBytes);
316 }
317 
319 {
320  memcpy(distanceArray, this->m_DistanceArray, this->m_NumberOfBytes);
321 }
322 
323 void ToFCameraMITKPlayerController::GetRgb(unsigned char* rgbArray)
324 {
325  memcpy(rgbArray, this->m_RGBArray, m_NumberOfRGBBytes);
326 }
327 
328 void ToFCameraMITKPlayerController::SetInputFileName(std::string inputFileName)
329 {
330  this->m_InputFileName = inputFileName;
331 }
332 
333 }
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:18
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:20
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:19
std::string m_DistanceImageFileName
file name of the distance image to be played
mitk::Image::Pointer image
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
int m_NumberOfBytes
holds the number of bytes contained in the image
unsigned char * m_RGBArray
member holding the current rgb frame