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