input_sensor_file_rosbagbin.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright (c), 2023 - Analog Devices Inc. All Rights Reserved.
3 This software is PROPRIETARY & CONFIDENTIAL to Analog Devices, Inc.
4 and its licensors.
5 ******************************************************************************/
6 
8 #include <fstream>
9 #include <ros/ros.h>
10 #include <boost/thread/thread.hpp>
11 
21 void InputSensorFileRosbagBin::openSensor(std::string sensor_name, int /*input_image_width*/, int /*input_image_height*/,
22  std::string /*config_file_name*/, std::string /*input_sensor_ip*/)
23 {
24  in_file_name_ = sensor_name;
25  frame_counter_ = 0;
26  sensor_open_flag_ = false;
27 
28  // Open file for streaming.
29  in_file_.open(sensor_name, std::ifstream::binary);
30  if (in_file_.is_open())
31  {
32  // Update flag.
33  sensor_open_flag_ = true;
34  }
35 }
36 
43 {
44  total_frames_ = 0;
45  if (in_file_.is_open())
46  {
47  in_file_.seekg(0, std::ios::end);
48  std::fstream::pos_type file_length = in_file_.tellg();
49  in_file_.seekg(0, std::ios::beg);
50  in_file_.read((char*)header_buffer_, 36); // 36 bytes header
51  // Populate header data
52  uint8_t* header_ptr = header_buffer_;
53  uint32_t first_frame_pos;
54  memcpy(&total_frames_, header_ptr, sizeof(uint32_t));
55  header_ptr += 4; // skipping head byte
56  memcpy(&input_frame_width_, header_ptr, sizeof(uint32_t));
57  header_ptr += 4;
58  printf("input_frame_width= %d \n", input_frame_width_);
59  memcpy(&input_frame_height_, header_ptr, sizeof(uint32_t));
60  header_ptr += 4;
61  printf("input_frame_height= %d \n", input_frame_height_);
62  memcpy(&bytes_per_pixel_, header_ptr, sizeof(uint32_t));
63  header_ptr += 4;
64  memcpy(&header_version_, header_ptr, sizeof(uint32_t));
65  header_ptr += 4;
66  memcpy(&first_frame_pos, header_ptr, sizeof(uint32_t));
67  header_ptr += 4;
68  memcpy(&frame_pitch_, header_ptr, sizeof(uint32_t));
69  header_ptr += 4;
70  memcpy(&device_timestamp_, header_ptr, sizeof(uint64_t));
71  header_ptr += 8;
72 
73  in_file_.read((char*)cam_info_buffer_, first_frame_pos - 36); // 36 bytes header excluded
74  uint8_t* cam_info_ptr = cam_info_buffer_;
75  if ((input_frame_width_ == 1024 && input_frame_height_ == 1024) ||
76  (input_frame_width_ == 512 && input_frame_height_ == 640))
77  {
80  }
81  else
82  {
85  }
86 
87  // Scale Intrinsics
92 
95 
96  // Reading camera info
97 
98  uint32_t size_of_d;
99  double k_cam[9] = { 0 };
100  double r_cam[9] = { 0 };
101  double p_cam[12] = { 0 };
102 
103  memcpy(&k_cam, cam_info_ptr, sizeof(k_cam));
104  cam_info_ptr += sizeof(k_cam);
105 
106  memcpy(&size_of_d, cam_info_ptr, sizeof(size_of_d));
107  cam_info_ptr += sizeof(size_of_d);
108 
109  double* d_cam = (double*)malloc(sizeof(double) * size_of_d);
110 
111  memcpy(d_cam, cam_info_ptr, sizeof(double) * size_of_d);
112  cam_info_ptr += (size_of_d * sizeof(double));
113 
114  memcpy(&r_cam, cam_info_ptr, sizeof(r_cam));
115  cam_info_ptr += sizeof(r_cam);
116 
117  memcpy(&p_cam, cam_info_ptr, sizeof(p_cam));
118 
119  camera_intrinsics_.camera_matrix[0] = (float)k_cam[0];
121  camera_intrinsics_.camera_matrix[2] = (float)k_cam[2];
123  camera_intrinsics_.camera_matrix[4] = (float)k_cam[4];
124  camera_intrinsics_.camera_matrix[5] = (float)k_cam[5];
128 
129  /*when the header version is 1, the size of distortion coefficients is 16.
130  they are represented as [0 0 0 0 0 0 0 0 k1 k2 p1 p2 k3 k4 k5 k6] hence offset of 8 is needed to read distortion
131  coefficients When the header version is 2, the size of distortion coefficients is 8.
132  they are represented as [k1 k2 p1 p2 k3 k4 k5 k6] hence no offset is needed to read the distortion coefficients*/
133 
134  if (header_version_ == 1)
135  {
136  for (int i = 0; i < 8; i++)
137  {
138  camera_intrinsics_.distortion_coeffs[i] = (float)d_cam[i + 8];
139  }
140  }
141  else if (header_version_ >= 2)
142  {
143  for (int i = 0; i < 8; i++)
144  {
145  camera_intrinsics_.distortion_coeffs[i] = (float)d_cam[i];
146  }
147  }
148 
149  free(d_cam);
150  }
151  return;
152 }
153 
160 {
161  memcpy(camera_intrinsics_data, &camera_intrinsics_, sizeof(camera_intrinsics_));
162  return;
163 }
164 
171 {
172  memcpy(camera_extrinsics_data, &camera_extrinsics_, sizeof(camera_extrinsics_));
173  return;
174 }
175 
185 bool InputSensorFileRosbagBin::readNextFrame(unsigned short* out_depth_frame, unsigned short* out_ab_frame,
186  unsigned short* out_conf_frame, short* out_xyz_frame)
187 {
188  // Read next frame
189 
190  // Sleep for 100ms - To simulate 10FPS
191  boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
192 
193  int num_samples_in_frame = input_frame_width_ * input_frame_height_;
194  int num_bytes_in_combo_frame = ((num_samples_in_frame * bytes_per_pixel_) + 8) * 2; // 8 header bytes per frame
195  char* frame_buffer = new char[num_bytes_in_combo_frame];
196  bool error_reading_frame = false;
197 
198  // Allocate memory
199  unsigned short* depth_frame = new unsigned short[num_samples_in_frame];
200  unsigned short* ab_frame = new unsigned short[num_samples_in_frame];
201 
202  ROS_ASSERT(out_depth_frame != nullptr);
203  ROS_ASSERT(out_ab_frame != nullptr);
204  if (header_version_ >= 3)
205  {
206  // Version 3 and above would have confidence image as well
207  num_bytes_in_combo_frame += ((num_samples_in_frame * bytes_per_pixel_) + 8);
208  ROS_ASSERT(out_conf_frame != nullptr);
209  }
210 
211  if (header_version_ == 4)
212  {
213  // Version 4 will have xyz image as well
214  num_bytes_in_combo_frame += (num_samples_in_frame * bytes_per_pixel_ * 3);
215  ROS_ASSERT(out_xyz_frame != nullptr);
216  }
217 
218  if (in_file_.is_open() && (frame_counter_ < total_frames_))
219  {
220  in_file_.read(frame_buffer, num_bytes_in_combo_frame);
221  if (in_file_.gcount() != num_bytes_in_combo_frame)
222  {
223  // Error in reading frame.
224  error_reading_frame = true;
225  }
226 
227  ++frame_counter_;
228  }
229  else
230  {
231  // EOF is reached or file is not openned
232  error_reading_frame = true;
233  }
234 
235  uint8_t* pframe_buffer_ptr = reinterpret_cast<uint8_t*>(frame_buffer);
236  uint8_t* ptemp_frame_buffer_ptr;
237  bool read_pass = false;
238  uint64_t timestamp_ab, timestamp_depth;
239  if (!error_reading_frame)
240  {
241  while (!read_pass)
242  {
243  ptemp_frame_buffer_ptr = pframe_buffer_ptr;
244  memcpy(&timestamp_depth, ptemp_frame_buffer_ptr, sizeof(uint64_t));
245  ptemp_frame_buffer_ptr += 8; // skipping frame header
246  // Copy depth data and ab data
247  // First comes depth frame,
248  memcpy(depth_frame, ptemp_frame_buffer_ptr, num_samples_in_frame * bytes_per_pixel_);
249  pframe_buffer_ptr += 8 + (num_samples_in_frame * bytes_per_pixel_); // proceeding to ab data
250  ptemp_frame_buffer_ptr = pframe_buffer_ptr;
251  memcpy(&timestamp_ab, ptemp_frame_buffer_ptr, sizeof(uint64_t));
252  if (timestamp_ab == timestamp_depth)
253  {
254  ptemp_frame_buffer_ptr += 8; // skipping frame header
255  // followed by ab frame.
256  memcpy(ab_frame, ptemp_frame_buffer_ptr, num_samples_in_frame * bytes_per_pixel_);
257  pframe_buffer_ptr += 8 + (num_samples_in_frame * bytes_per_pixel_);
258  read_pass = true;
259  frame_timestamp_ = timestamp_ab;
260  }
261  }
262  }
263 
264  if (error_reading_frame)
265  {
266  // close file
267  closeSensor();
268 
269  // delete memory
270  delete[] frame_buffer;
271  delete[] depth_frame;
272  delete[] ab_frame;
273  return false;
274  }
275 
276  unsigned short* temp_depth_frame = depth_frame;
277  unsigned short* temp_ab_frame = ab_frame;
278 
279  for (unsigned int i = 0; i < input_frame_height_; i++)
280  {
281  for (unsigned int j = 0; j < input_frame_width_; j++)
282  {
283  *out_depth_frame++ = temp_depth_frame[j];
284  *out_ab_frame++ = temp_ab_frame[j];
285  }
286  // Skip rows.
287  temp_depth_frame += (input_frame_width_);
288  temp_ab_frame += (input_frame_width_);
289  }
290 
291  // Compute XYZ
292 
293  delete[] frame_buffer;
294  delete[] depth_frame;
295  delete[] ab_frame;
296 
297  return true;
298 }
299 
305 {
306  if (frame_timestamp_ > 0)
307  {
308  ros::Time temp_timestamp((uint32_t)(frame_timestamp_ / 1000000000), (uint32_t)frame_timestamp_);
309  *timestamp = temp_timestamp;
310  }
311  else
312  *timestamp = ros::Time::now();
313  return true;
314 }
315 
321 {
322  if (in_file_.is_open())
323  {
324  in_file_.close();
325  }
326 }
InputSensorFileRosbagBin::readNextFrame
bool readNextFrame(unsigned short *out_depth_frame, unsigned short *out_ab_frame, unsigned short *out_conf_frame, short *out_xyz_frame)
reads next frame
Definition: input_sensor_file_rosbagbin.cpp:185
InputSensorFileRosbagBin::getExtrinsics
void getExtrinsics(CameraExtrinsics *camera_extrinsics_data)
gets camera extrinsics
Definition: input_sensor_file_rosbagbin.cpp:170
InputSensorFileRosbagBin::input_frame_height_
uint32_t input_frame_height_
Definition: input_sensor_file_rosbagbin.h:145
IInputSensor::setFrameWidth
void setFrameWidth(int frm_width)
Set the Frame Width object Only integer scaling is supported.
Definition: input_sensor.h:84
ros.h
InputSensorFileRosbagBin::in_file_
std::ifstream in_file_
Definition: input_sensor_file_rosbagbin.h:143
InputSensorFileRosbagBin::camera_intrinsics_
CameraIntrinsics camera_intrinsics_
Definition: input_sensor_file_rosbagbin.h:155
CameraIntrinsics::camera_matrix
float camera_matrix[9]
Camera matrix = [fx, 0, cx, 0, fy, cy, 0, 0, 1] fx, fy : Camera focal lengths (pixels) cx,...
Definition: adi_camera.h:27
IInputSensor::frame_counter_
unsigned int frame_counter_
Definition: input_sensor.h:30
InputSensorFileRosbagBin::header_version_
uint32_t header_version_
Definition: input_sensor_file_rosbagbin.h:147
InputSensorFileRosbagBin::device_timestamp_
uint64_t device_timestamp_
Definition: input_sensor_file_rosbagbin.h:149
IInputSensor::input_scale_factor_
int input_scale_factor_
Definition: input_sensor.h:29
InputSensorFileRosbagBin::camera_extrinsics_
CameraExtrinsics camera_extrinsics_
Definition: input_sensor_file_rosbagbin.h:156
InputSensorFileRosbagBin::frame_timestamp_
uint64_t frame_timestamp_
Definition: input_sensor_file_rosbagbin.h:150
IInputSensor::setFrameHeight
void setFrameHeight(int frm_height)
Sett the Frame Height object Only integer scaling is supported.
Definition: input_sensor.h:93
IInputSensor::setProcessingScale
void setProcessingScale(int scale_factor)
Set the Processing Scale object.
Definition: input_sensor.h:103
ros::Time
CameraExtrinsics
Extrinsic parameters of the camera.
Definition: adi_camera.h:38
InputSensorFileRosbagBin::openSensor
void openSensor(std::string sensor_name, int input_image_width, int input_image_height, std::string config_file_name, std::string input_sensor_ip)
opens the bag file.
Definition: input_sensor_file_rosbagbin.cpp:21
InputSensorFileRosbagBin::input_frame_width_
uint32_t input_frame_width_
Definition: input_sensor_file_rosbagbin.h:144
input_sensor_file_rosbagbin.h
CameraIntrinsics
Intrinsic parameters of the camera.
Definition: adi_camera.h:15
InputSensorFileRosbagBin::in_file_name_
std::string in_file_name_
Definition: input_sensor_file_rosbagbin.h:142
InputSensorFileRosbagBin::frame_pitch_
uint32_t frame_pitch_
Definition: input_sensor_file_rosbagbin.h:148
InputSensorFileRosbagBin::processing_scale_
int processing_scale_
Definition: input_sensor_file_rosbagbin.h:146
InputSensorFileRosbagBin::header_buffer_
uint8_t header_buffer_[36]
Definition: input_sensor_file_rosbagbin.h:153
InputSensorFileRosbagBin::configureSensor
void configureSensor(int camera_mode)
Configures the sensor.
Definition: input_sensor_file_rosbagbin.cpp:42
InputSensorFileRosbagBin::bytes_per_pixel_
uint32_t bytes_per_pixel_
Definition: input_sensor_file_rosbagbin.h:151
IInputSensor::sensor_open_flag_
bool sensor_open_flag_
Definition: input_sensor.h:25
InputSensorFileRosbagBin::getFrameTimestamp
bool getFrameTimestamp(ros::Time *timestamp)
returns frame timestamp
Definition: input_sensor_file_rosbagbin.cpp:304
InputSensorFileRosbagBin::getIntrinsics
void getIntrinsics(CameraIntrinsics *camera_intrinsics_data)
Gets the camera intrinsics.
Definition: input_sensor_file_rosbagbin.cpp:159
ROS_ASSERT
#define ROS_ASSERT(cond)
CameraIntrinsics::distortion_coeffs
float distortion_coeffs[8]
Distortion vector = [k1, k2, p1, p2, k3, k4, k5, k6] k1, k2, k3, k4, k5, k6 : Camera radial distortio...
Definition: adi_camera.h:35
InputSensorFileRosbagBin::closeSensor
void closeSensor()
closes the input file
Definition: input_sensor_file_rosbagbin.cpp:320
ros::Time::now
static Time now()
InputSensorFileRosbagBin::cam_info_buffer_
uint8_t cam_info_buffer_[400]
Definition: input_sensor_file_rosbagbin.h:154
InputSensorFileRosbagBin::total_frames_
uint32_t total_frames_
Definition: input_sensor_file_rosbagbin.h:152


adi_3dtof_adtf31xx
Author(s):
autogenerated on Sat May 17 2025 02:12:30