MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkToFImageRecorder.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 <mitkRealTimeClock.h>
14#include <itksys/SystemTools.hxx>
15#pragma GCC visibility push(default)
16#include <itkEventObject.h>
17#pragma GCC visibility pop
18
19namespace mitk
20{
22{
23 this->m_ToFCameraDevice = nullptr;
24 this->m_NumOfFrames = 1; //lets record one frame per default
25 this->m_ToFImageWriter = nullptr;
26 this->m_DistanceImageSelected = true; //lets assume a device only has depth data by default
27 this->m_AmplitudeImageSelected = false;
28 this->m_IntensityImageSelected = false;
29 this->m_RGBImageSelected = false;
30 this->m_Abort = false;
31 this->m_ToFCaptureWidth = 0;
32 this->m_ToFCaptureHeight = 0;
33 this->m_RGBCaptureWidth = 0;
34 this->m_RGBCaptureHeight = 0;
35 this->m_FileFormat = ".nrrd"; //lets make nrrd the default
36 this->m_ToFPixelNumber = 0;
37 this->m_RGBPixelNumber = 0;
38 this->m_SourceDataSize = 0;
41 this->m_DistanceImageFileName = "";
42 this->m_AmplitudeImageFileName = "";
43 this->m_IntensityImageFileName = "";
44 this->m_RGBImageFileName = "";
45 this->m_ImageSequence = 0;
46 this->m_DistanceArray = nullptr;
47 this->m_AmplitudeArray = nullptr;
48 this->m_IntensityArray = nullptr;
49 this->m_RGBArray = nullptr;
50 this->m_SourceDataArray = nullptr;
51}
52
54{
55 delete[] m_DistanceArray;
56 delete[] m_AmplitudeArray;
57 delete[] m_IntensityArray;
58 delete[] m_RGBArray;
59 delete[] m_SourceDataArray;
60}
61
63{
64
65 this->m_AbortMutex.lock();
66 this->m_Abort = true;
67 this->m_AbortMutex.unlock();
68
69}
70
72{
73 if (this->m_ToFCameraDevice.IsNull())
74 {
75 throw std::invalid_argument("ToFCameraDevice is nullptr.");
76 return;
77 }
78 if (this->m_FileFormat.compare(".csv") == 0)
79 {
80 this->m_ToFImageWriter = ToFImageCsvWriter::New();
81 }
82 else if(this->m_FileFormat.compare(".nrrd") == 0)
83 {
84 this->m_ToFImageWriter = ToFNrrdImageWriter::New();
85 this->m_ToFImageWriter->SetExtension(m_FileFormat);
86 }
87 else
88 {
89 throw std::logic_error("No file format specified!");
90 }
91
92 this->m_RGBCaptureWidth = this->m_ToFCameraDevice->GetRGBCaptureWidth();
93 this->m_RGBCaptureHeight = this->m_ToFCameraDevice->GetRGBCaptureHeight();
95
96 this->m_ToFCaptureWidth = this->m_ToFCameraDevice->GetCaptureWidth();
97 this->m_ToFCaptureHeight = this->m_ToFCameraDevice->GetCaptureHeight();
99
100 this->m_SourceDataSize = this->m_ToFCameraDevice->GetSourceDataSize();
101
102 // allocate buffer
103 if(m_IntensityArray == nullptr)
104 {
105 this->m_IntensityArray = new float[m_ToFPixelNumber];
106 }
107 if(this->m_DistanceArray == nullptr)
108 {
109 this->m_DistanceArray = new float[m_ToFPixelNumber];
110 }
111 if(this->m_AmplitudeArray == nullptr)
112 {
113 this->m_AmplitudeArray = new float[m_ToFPixelNumber];
114 }
115 if(this->m_RGBArray == nullptr)
116 {
117 this->m_RGBArray = new unsigned char[m_RGBPixelNumber*3];
118 }
119 if(this->m_SourceDataArray == nullptr)
120 {
121 this->m_SourceDataArray = new char[m_SourceDataSize];
122 }
123
124 this->m_ToFImageWriter->SetDistanceImageFileName(this->m_DistanceImageFileName);
125 this->m_ToFImageWriter->SetAmplitudeImageFileName(this->m_AmplitudeImageFileName);
126 this->m_ToFImageWriter->SetIntensityImageFileName(this->m_IntensityImageFileName);
127 this->m_ToFImageWriter->SetRGBImageFileName(this->m_RGBImageFileName);
128 this->m_ToFImageWriter->SetRGBCaptureWidth(this->m_RGBCaptureWidth);
129 this->m_ToFImageWriter->SetRGBCaptureHeight(this->m_RGBCaptureHeight);
130 this->m_ToFImageWriter->SetToFCaptureWidth(this->m_ToFCaptureWidth);
131 this->m_ToFImageWriter->SetToFCaptureHeight(this->m_ToFCaptureHeight);
132 this->m_ToFImageWriter->SetToFImageType(this->m_ToFImageType);
133 this->m_ToFImageWriter->SetDistanceImageSelected(this->m_DistanceImageSelected);
134 this->m_ToFImageWriter->SetAmplitudeImageSelected(this->m_AmplitudeImageSelected);
135 this->m_ToFImageWriter->SetIntensityImageSelected(this->m_IntensityImageSelected);
136 this->m_ToFImageWriter->SetRGBImageSelected(this->m_RGBImageSelected);
137 this->m_ToFImageWriter->Open();
138
139 this->m_AbortMutex.lock();
140 this->m_Abort = false;
141 this->m_AbortMutex.unlock();
142 m_Thread = std::thread(&ToFImageRecorder::RecordData, this);
143}
144
146{
147 if (m_Thread.joinable())
148 m_Thread.join();
149}
150
152{
153 ToFCameraDevice::Pointer toFCameraDevice = this->GetCameraDevice();
154
155 mitk::RealTimeClock::Pointer realTimeClock;
156 realTimeClock = mitk::RealTimeClock::New();
157 int n = 100;
158 double t1 = 0;
159 double t2 = 0;
160 t1 = realTimeClock->GetCurrentStamp();
161 bool printStatus = false;
162 int requiredImageSequence = 0;
163 int numOfFramesRecorded = 0;
164
165 bool abort = false;
166 m_AbortMutex.lock();
167 abort = m_Abort;
168 m_AbortMutex.unlock();
169 while ( !abort )
170 {
171 if ( ((m_RecordMode == ToFImageRecorder::PerFrames) && (numOfFramesRecorded < m_NumOfFrames)) ||
173 {
174
175 toFCameraDevice->GetAllImages(m_DistanceArray, m_AmplitudeArray,
177
178 if (m_ImageSequence >= requiredImageSequence)
179 {
180 if (m_ImageSequence > requiredImageSequence)
181 {
182 MITK_INFO << "Problem! required: " << requiredImageSequence << " captured: " << m_ImageSequence;
183 }
184 requiredImageSequence = m_ImageSequence + 1;
186 numOfFramesRecorded++;
187 if (numOfFramesRecorded % n == 0)
188 {
189 printStatus = true;
190 }
191 if (printStatus)
192 {
193 t2 = realTimeClock->GetCurrentStamp() - t1;
194 MITK_INFO << " Framerate (fps): " << n / (t2/1000) << " Sequence: " << m_ImageSequence;
195 t1 = realTimeClock->GetCurrentStamp();
196 printStatus = false;
197 }
198 }
199 m_AbortMutex.lock();
200 abort = m_Abort;
201 m_AbortMutex.unlock();
202 }
203 else
204 {
205 abort = true;
206 }
207 } // end of while loop
208
209 this->InvokeEvent(itk::AbortEvent());
210
211 m_ToFImageWriter->Close();
212}
213
215{
216 this->m_ToFCameraDevice = aToFCameraDevice;
217}
218
223
228
230{
231 this->m_ToFImageType = toFImageType;
232}
233
238
240{
241 this->m_RecordMode = recordMode;
242}
243}
static Pointer New(void)
instanciates a new, operating-system dependant, instance of mitk::RealTimeClock.
Virtual interface and base class for all Time-of-Flight devices.
char * m_SourceDataArray
array holding the source data
bool m_AmplitudeImageSelected
flag indicating if amplitude image should be recorded
int m_SourceDataSize
size of the source data provided by the device
void StopRecording()
Stops the recording by setting the m_Abort flag to false.
void SetCameraDevice(ToFCameraDevice *aToFCameraDevice)
Set the device used for acquiring ToF images.
void WaitForThreadBeingTerminated()
Wait until thread is terinated.
int m_RGBCaptureHeight
height (y-dimension) of the images to record.
int m_ImageSequence
number of images currently acquired
ToFImageWriter::ToFImageType m_ToFImageType
type of image to be recorded: ToFImageType3D (0) or ToFImageType2DPlusT (1)
void SetRecordMode(ToFImageRecorder::RecordMode recordMode)
Set the RecordMode.
float * m_IntensityArray
array holding the intensity data
float * m_DistanceArray
array holding the distance data
bool m_DistanceImageSelected
flag indicating if distance image should be recorded
std::string m_DistanceImageFileName
file name for saving the distance image
int m_ToFCaptureHeight
height (y-dimension) of the images to record.
bool m_Abort
flag controlling the abort mechanism of the recording procedure. For thread-safety only use in combin...
std::string m_FileFormat
file format for saving images. If .csv is chosen, ToFImageCsvWriter is used
std::string m_RGBImageFileName
file name for saving the rgb image
std::mutex m_AbortMutex
mutex for thread-safe data access of abort flag
float * m_AmplitudeArray
array holding the amplitude data
ToFImageRecorder::RecordMode m_RecordMode
mode of recording the images: specified number of frames (PerFrames) or infinite (Infinite)
ToFImageWriter::Pointer m_ToFImageWriter
image writer writing the acquired images to a file
std::string m_IntensityImageFileName
file name for saving the intensity image
ToFCameraDevice * GetCameraDevice()
Get the device used for acquiring ToF images.
int m_RGBCaptureWidth
width (x-dimension) of the images to record.
void SetToFImageType(ToFImageWriter::ToFImageType toFImageType)
Set the type of image to be recorded.
bool m_RGBImageSelected
flag indicating if rgb image should be recorded
int m_ToFPixelNumber
number of pixels (widht*height) of the images to record
void StartRecording()
Starts the recording by spawning a Thread which streams the data to a file. Aborting of the record pr...
std::string m_AmplitudeImageFileName
file name for saving the amplitude image
ToFImageRecorder::RecordMode GetRecordMode()
Returns the currently set RecordMode.
ToFCameraDevice::Pointer m_ToFCameraDevice
ToFCameraDevice used for acquiring the images.
unsigned char * m_RGBArray
array holding the RGB data if available (e.g. for Kinect)
bool m_IntensityImageSelected
flag indicating if intensity image should be recorded
int m_NumOfFrames
number of frames to be recorded by this recorder
int m_RGBPixelNumber
number of pixels (widht*height) of the images to record
int m_ToFCaptureWidth
width (x-dimension) of the images to record.
ToFImageWriter::ToFImageType GetToFImageType()
Get the type of image to be recorded.
void RecordData()
Thread method acquiring data via the ToFCameraDevice and recording it to file via the ToFImageWriter.
IGT Exceptions.