Medical Imaging Interaction Toolkit  2018.4.99-b585543d
Medical Imaging Interaction Toolkit
mitkToFCameraPMDRawDataCamBoardDevice.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 ============================================================================*/
14 
15 // vtk includes
16 #include "vtkSmartPointer.h"
17 
18 namespace mitk
19 {
21  {
23  }
24 
26  {
27  }
28 
30  {
31  bool ok = Superclass::ConnectCamera();
32  if(ok)
33  {
37  this->AllocatePixelArrays();
38  this->AllocateSourceData();
39  }
40  return ok;
41  }
42 
43  void ToFCameraPMDRawDataCamBoardDevice::GetChannelSourceData(short* sourceData, vtkShortArray* vtkChannelArray )
44  {
45  int i = 0;
46  //unsigned int channelDataPosition = 0;
47  unsigned int channelSize = (this->m_OriginControllerHeight*this->m_OriginControllerWidth*2);
48  this->SetChannelSize(channelSize);
49  signed short* channel1;
50  signed short* channel2;
51  signed short* channel3;
52  signed short* channel4;
53 
54  vtkSmartPointer<vtkShortArray> cvtkChannelArray = vtkSmartPointer<vtkShortArray>::New();
55  cvtkChannelArray->SetNumberOfComponents(channelSize);
56  cvtkChannelArray->SetNumberOfTuples(4);
57  cvtkChannelArray->Allocate(1);
58 
59  channel1 = sourceData;
60  cvtkChannelArray->InsertTupleValue(0,channel1);
61  sourceData += channelSize;
62  channel2 = sourceData;
63  cvtkChannelArray->InsertTupleValue(1,channel2);
64  sourceData += channelSize;
65  channel3 = sourceData;
66  cvtkChannelArray->InsertTupleValue(2,channel3);
67  sourceData += channelSize;
68  channel4 = sourceData;
69  cvtkChannelArray->InsertTupleValue(3,channel4);
70  vtkChannelArray->DeepCopy(cvtkChannelArray);
71  }
72 
73  void ToFCameraPMDRawDataCamBoardDevice::SetProperty( const char *propertyKey, BaseProperty* propertyValue )
74  {
75  ToFCameraPMDRawDataDevice::SetProperty(propertyKey,propertyValue);
76  this->m_PropertyList->SetProperty(propertyKey, propertyValue);
77  }
78 
79  void ToFCameraPMDRawDataCamBoardDevice::GetAmplitudes(float* amplitudeArray, int& imageSequence)
80  {
81  if (m_CameraActive)
82  {
83  this->ResizeOutputImage(m_AmplitudeArray,amplitudeArray);
84  imageSequence = this->m_ImageSequence;
85  }
86  else
87  {
88  MITK_WARN("ToF") << "Warning: Data can only be acquired if camera is active.";
89  }
90  }
91 
92  void ToFCameraPMDRawDataCamBoardDevice::GetIntensities(float* intensityArray, int& imageSequence)
93  {
94  if (m_CameraActive)
95  {
96  this->ResizeOutputImage(m_IntensityArray, intensityArray);
97  imageSequence = this->m_ImageSequence;
98  }
99  else
100  {
101  MITK_WARN("ToF") << "Warning: Data can only be acquired if camera is active.";
102  }
103  }
104 
105  void ToFCameraPMDRawDataCamBoardDevice::GetDistances(float* distanceArray, int& imageSequence)
106  {
107  if (m_CameraActive)
108  {
109  this->ResizeOutputImage(m_DistanceArray, distanceArray);
110  imageSequence = this->m_ImageSequence;
111  }
112  else
113  {
114  MITK_WARN("ToF") << "Warning: Data can only be acquired if camera is active.";
115  }
116  }
117 
118  void ToFCameraPMDRawDataCamBoardDevice::GetAllImages(float* distanceArray, float* amplitudeArray, float* intensityArray, char* sourceDataArray,
119  int requiredImageSequence, int& capturedImageSequence, unsigned char* rgbDataArray)
120  {
121  if (m_CameraActive)
122  {
123  // check for empty buffer
124  if (this->m_ImageSequence < 0)
125  {
126  // buffer empty
127  MITK_INFO << "Buffer empty!! ";
128  capturedImageSequence = this->m_ImageSequence;
129  return;
130  }
131 
132  // determine position of image in buffer
133  int pos = 0;
134  if ((requiredImageSequence < 0) || (requiredImageSequence > this->m_ImageSequence))
135  {
136  capturedImageSequence = this->m_ImageSequence;
137  pos = this->m_CurrentPos;
138  //MITK_INFO << "Required image not found! Required: " << requiredImageSequence << " delivered/current: " << this->m_ImageSequence;
139  }
140  else if (requiredImageSequence <= this->m_ImageSequence - this->m_BufferSize)
141  {
142  capturedImageSequence = (this->m_ImageSequence - this->m_BufferSize) + 1;
143  pos = (this->m_CurrentPos + 1) % this->m_BufferSize;
144  //MITK_INFO << "Out of buffer! Required: " << requiredImageSequence << " delivered: " << capturedImageSequence << " current: " << this->m_ImageSequence;
145  }
146  else // (requiredImageSequence > this->m_ImageSequence - this->m_BufferSize) && (requiredImageSequence <= this->m_ImageSequence)
147 
148  {
149  capturedImageSequence = requiredImageSequence;
150  pos = (this->m_CurrentPos + (10-(this->m_ImageSequence - requiredImageSequence))) % this->m_BufferSize;
151  }
152 
153  this->ResizeOutputImage(m_DistanceArray, distanceArray);
154  this->ResizeOutputImage(m_AmplitudeArray, amplitudeArray);
155  this->ResizeOutputImage(m_IntensityArray, intensityArray);
156  memcpy(sourceDataArray, this->m_SourceDataBuffer[this->m_CurrentPos], this->m_SourceDataSize);
157  }
158  else
159  {
160  MITK_WARN("ToF") << "Warning: Data can only be acquired if camera is active.";
161  }
162  }
163 
164  void ToFCameraPMDRawDataCamBoardDevice::ResizeOutputImage(float* in, float* out)
165  {
166  vnl_matrix<float> inMat = vnl_matrix<float>(m_OriginControllerHeight, m_OriginControllerWidth);
167  inMat.copy_in(in);
168  vnl_matrix<float> outMat = vnl_matrix<float>(m_CaptureHeight,m_CaptureWidth);
169  vnl_matrix<float> temp = vnl_matrix<float>(m_CaptureHeight,m_CaptureWidth);
170  temp = inMat.extract(m_CaptureHeight,m_CaptureWidth, 0, 4);
171  outMat = temp.transpose(); // rotate the image data
172  outMat.flipud(); // flip image upside down
173  outMat.copy_out(out);
174  inMat.clear(); // clean data
175  outMat.clear();
176  temp.clear();
177  }
178 }
void GetDistances(float *distanceArray, int &imageSequence)
Returns distance data.
virtual void GetChannelSourceData(short *sourceData, vtkShortArray *vtkChannelArray)
Transforms the sourceData into an array with four tuples holding the channels for raw data reconstruc...
virtual void SetProperty(const char *propertyKey, BaseProperty *propertyValue)
set a BaseProperty
virtual int GetCaptureHeight()
get the currently set capture height
#define MITK_INFO
Definition: mitkLogMacros.h:18
char ** m_SourceDataBuffer
buffer holding the last acquired images
int m_SourceDataSize
size of the PMD source data
float * m_IntensityArray
float array holding the intensity image
unsigned int m_OriginControllerHeight
holds the original controller height
int m_PixelNumber
number of pixels in the range image (m_CaptureWidth*m_CaptureHeight)
DataCollection - Class to facilitate loading/accessing structured data.
PropertyList::Pointer m_PropertyList
a list of the corresponding properties
float * m_DistanceArray
float array holding the distance image
Interface to the Time-of-Flight (ToF) camera PMD CamBoard.
void GetAmplitudes(float *amplitudeArray, int &imageSequence)
Returns amplitude data.
bool ConnectCamera()
Establishes camera connection and sets the class variables.
Abstract base class for properties.
#define MITK_WARN
Definition: mitkLogMacros.h:19
bool m_CameraActive
flag indicating if the camera is currently active or not. Caution: thread safe access only! ...
int m_CurrentPos
current position in the buffer which will be retrieved by the Get methods
void GetIntensities(float *intensityArray, int &imageSequence)
Returns intensity data.
virtual void AllocateSourceData()
method for allocating m_SourceDataArray and m_SourceDataBuffer
int m_CaptureWidth
width of the range image (x dimension)
int m_BufferSize
buffer size of the image buffer needed for loss-less acquisition of range data
virtual void SetProperty(const char *propertyKey, BaseProperty *propertyValue)
set a BaseProperty
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
Definition: jsoncpp.cpp:244
ToFCameraPMDController::Pointer m_Controller
corresponding CameraController
int m_CaptureHeight
height of the range image (y dimension)
virtual void AllocatePixelArrays()
method for allocating memory for pixel arrays m_IntensityArray, m_DistanceArray and m_AmplitudeArray ...
float * m_AmplitudeArray
float array holding the amplitude image
virtual void SetChannelSize(int _arg)
unsigned int m_OriginControllerWidth
holds the original controller width
virtual int GetCaptureWidth()
get the currently set capture width
void GetAllImages(float *distanceArray, float *amplitudeArray, float *intensityArray, char *sourceDataArray, int requiredImageSequence, int &capturedImageSequence, unsigned char *rgbDataArray=nullptr)
Returns all image data at once.
int m_ImageSequence
counter for acquired images