1 #include <bits/stdc++.h>
2 #include <boost/thread/thread.hpp>
3 #include <camera_info_manager/camera_info_manager.h>
5 #include <opencv2/core.hpp>
6 #include <opencv2/highgui.hpp>
15 #include <sensor_msgs/CompressedImage.h>
16 #include <compressed_depth_image_transport/rvl_codec.h>
50 bool comparator(std::pair<long, int>& first_element, std::pair<long, int>& second_element)
52 return first_element.first < second_element.first;
60 void sort_map(std::map<long, int>& mapped_array)
63 std::vector<std::pair<long, int>> vector_of_pairs;
67 vector_of_pairs.reserve(mapped_array.size());
69 for (
auto& it : mapped_array)
71 vector_of_pairs.push_back(it);
75 sort(vector_of_pairs.begin(), vector_of_pairs.end(),
comparator);
87 const std::string& str = std::move(file_name);
88 size_t null_char_pos = str.find_last_not_of(
'\0');
89 char* char_array =
new char[null_char_pos + 1];
90 std::strcpy(char_array, str.substr(0, null_char_pos + 1).c_str());
116 fin.open(file_name, std::ios::binary | std::ios::in);
118 uint8_t image_indicator;
119 long time_stamp_depth;
121 std::vector<long> list_depth_timestamps;
122 std::vector<long> list_ab_timestamps;
127 fin.read((
char*)&total_frames,
sizeof(uint32_t));
128 fin.read((
char*)&image_width,
sizeof(uint32_t));
129 fin.read((
char*)&image_height,
sizeof(uint32_t));
131 uint32_t header_size;
133 fin.read((
char*)&header_size,
sizeof(uint32_t));
134 char*
header =
new char[header_size];
136 fin.read(
header, header_size);
138 uint64_t startframe = header_size;
139 long lowest_timestamp_depth = 0;
142 while (j < num_depth_frames)
144 fin.seekg(startframe);
145 fin.read((
char*)&image_indicator,
sizeof(uint8_t));
146 if (image_indicator == 0)
148 fin.read((
char*)&lowest_timestamp_depth,
sizeof(
long));
149 list_depth_timestamps.push_back(lowest_timestamp_depth);
152 startframe = startframe + (image_width * image_height * 2) + 9;
155 std::sort(list_depth_timestamps.begin(), list_depth_timestamps.end());
157 startframe = header_size;
158 long lowest_timestamp_ab = 0;
161 while (k < num_ab_frames)
163 fin.seekg(startframe);
164 fin.read((
char*)&image_indicator,
sizeof(uint8_t));
165 if (image_indicator == 1)
167 fin.read((
char*)&lowest_timestamp_ab,
sizeof(
long));
168 list_ab_timestamps.push_back(lowest_timestamp_ab);
171 startframe = startframe + (image_width * image_height * 2) + 9;
174 std::sort(list_ab_timestamps.begin(), list_ab_timestamps.end());
176 std::cout <<
"\n\n" << file_name << std::endl;
177 std::cout <<
"Checking whether any Depth frame missing its AB pair....\n\n";
180 long depth_time_stamp;
182 for (
int i = 0; i < num_depth_frames; i++)
184 depth_time_stamp = list_depth_timestamps[i];
186 (std::find(list_ab_timestamps.begin(), list_ab_timestamps.end(), depth_time_stamp) != list_ab_timestamps.end());
193 std::cout <<
"depth frame with timestamp " << depth_time_stamp <<
" do not have AB frame" << std::endl;
197 std::cout <<
"\n\nChecking whether any AB frame missing its Depth pair....\n\n";
198 bool found_depth_match;
201 for (
int i = 0; i < num_ab_frames; i++)
203 ab_time_stamp = list_ab_timestamps[i];
204 found_depth_match = (std::find(list_depth_timestamps.begin(), list_depth_timestamps.end(), ab_time_stamp) !=
205 list_depth_timestamps.end());
206 if (found_depth_match)
212 std::cout <<
"ab frame with timestamp " << ab_time_stamp <<
" do not have depth frame" << std::endl;
227 fin.open(file_name, std::ios::binary | std::ios::in);
233 fin.read((
char*)&total_frames,
sizeof(uint32_t));
235 fin.read((
char*)&image_width,
sizeof(uint32_t));
237 fin.read((
char*)&image_height,
sizeof(uint32_t));
239 uint32_t header_size;
242 fin.read((
char*)&header_size,
sizeof(uint32_t));
243 char*
header =
new char[header_size];
246 fin.read(
header, header_size);
249 long prev_time_stamp;
250 fin.read((
char*)&time_stamp,
sizeof(
long));
251 prev_time_stamp = time_stamp;
253 frame_position = header_size;
257 while (k < total_frames)
259 fin.seekg(frame_position);
260 fin.read((
char*)&time_stamp,
sizeof(
long));
261 if ((time_stamp - prev_time_stamp) > 110000000)
263 std::cout << time_stamp <<
" - " << prev_time_stamp <<
" = " << time_stamp - prev_time_stamp <<
" ns"
265 std::cout <<
"Capture is less than 10FPS" << std::endl;
267 prev_time_stamp = time_stamp;
268 frame_position = frame_position + ((image_width * image_height * 2) + 8) * 2;
286 fin.open(file_name, std::ios::binary | std::ios::in);
288 std::string output_file;
290 output_file = file_name.substr(0, file_name.find_last_of(
'.')) +
"_" +
"out" +
".bin";
294 fout.open(output_file, std::ios::binary | std::ios::out);
297 uint8_t image_indicator;
298 long time_stamp_depth;
300 long lowest_timestamp_depth;
306 fin.read((
char*)&total_frames,
sizeof(uint32_t));
307 fin.read((
char*)&image_width,
sizeof(uint32_t));
308 fin.read((
char*)&image_height,
sizeof(uint32_t));
311 uint32_t header_size;
313 fin.read((
char*)&header_size,
sizeof(uint32_t));
314 char*
header =
new char[header_size];
317 fin.read(
header, header_size);
319 fout.write(
header, header_size);
321 char* frame_buffer_depth =
new char[image_width * image_height * 2];
322 char* frame_buffer_ab =
new char[image_width * image_height * 2];
325 std::map<long, int> depth_data;
328 uint64_t startframe = header_size;
330 while (j < num_depth_frames)
332 fin.seekg(startframe);
333 fin.read((
char*)&image_indicator,
sizeof(uint8_t));
334 if (image_indicator == 0)
336 fin.read((
char*)&lowest_timestamp_depth,
sizeof(
long));
338 depth_data.insert({ lowest_timestamp_depth, startframe });
341 startframe = startframe + (image_width * image_height * 2) + 9;
347 std::map<long, int> ab_data;
349 startframe = header_size;
351 while (k < num_ab_frames)
353 fin.seekg(startframe);
354 fin.read((
char*)&image_indicator,
sizeof(uint8_t));
355 if (image_indicator == 1)
357 fin.read((
char*)&lowest_timestamp_depth,
sizeof(
long));
359 ab_data.insert({ lowest_timestamp_depth, startframe });
362 startframe = startframe + (image_width * image_height * 2) + 9;
368 fin.open(file_name, std::ios::binary | std::ios::in);
370 long frame_time_stamp;
371 uint32_t total_frames_count = 0;
373 for (
auto & it : depth_data)
375 frame_time_stamp = it.first;
376 if (ab_data.find(frame_time_stamp) != ab_data.end())
378 int frame_ptr_ab = ab_data.find(frame_time_stamp)->second;
379 int frame_ptr_depth = it.second;
380 frame_ptr_depth = frame_ptr_depth + 9;
381 frame_ptr_ab = frame_ptr_ab + 9;
382 fin.seekg(frame_ptr_depth);
383 fin.read(frame_buffer_depth, image_width * image_height * 2);
384 fin.seekg(frame_ptr_ab);
385 fin.read(frame_buffer_ab, image_width * image_height * 2);
386 fout.write((
char*)&frame_time_stamp,
sizeof(
long));
387 fout.write(frame_buffer_depth, image_width * image_height * 2);
388 fout.write((
char*)&frame_time_stamp,
sizeof(
long));
389 fout.write(frame_buffer_ab, image_width * image_height * 2);
390 total_frames_count++;
395 fout.write((
char*)&total_frames_count,
sizeof(uint32_t));
412 std::cout <<
"Reading image topics from one or more cameras and writing to bin files.." << std::endl;
414 const std::string& input_bag_file = std::move(bag_file_name);
417 std::vector<std::string> cam_bin_file_name;
419 std::vector<std::string> cam_ab_image_topic_names;
421 std::vector<std::string> cam_compressed_ab_image_topic_names;
423 std::vector<std::string> cam_depth_image_topic_names;
425 std::vector<std::string> cam_compressed_depth_image_topic_names;
427 std::vector<std::string> cam_info_topic_names;
435 std::vector<HeaderOfBinFile> headers_of_bin_files;
438 std::vector<int> camera_total_ab_frames;
439 std::vector<int> camera_total_depth_frames;
440 std::vector<bool> camera_device_time_stamp_written;
441 std::vector<bool> camera_info_written;
444 std::vector<std::ofstream*> files;
445 files.resize(cam_name.size());
447 for (
int j = 0; j < cam_name.size(); j++)
450 std::string cam_bin_file;
451 cam_bin_file = input_bag_file.substr(0, input_bag_file.find_last_of(
'.')) +
"_" + cam_name[j] +
".bin";
452 cam_bin_file_name.push_back(cam_bin_file);
455 std::string cam_ab_image =
"/" + cam_name[j] +
"/ab_image";
456 cam_ab_image_topic_names.push_back(cam_ab_image);
458 std::string compressed_cam_ab_image =
"/" + cam_name[j] +
"/ab_image/compressedDepth";
459 cam_compressed_ab_image_topic_names.push_back(compressed_cam_ab_image);
462 std::string cam_depth_image =
"/" + cam_name[j] +
"/depth_image";
463 cam_depth_image_topic_names.push_back(cam_depth_image);
466 std::string compressed_cam_depth_image =
"/" + cam_name[j] +
"/depth_image/compressedDepth";
467 cam_compressed_depth_image_topic_names.push_back(compressed_cam_depth_image);
470 std::string cam_info =
"/" + cam_name[j] +
"/camera_info";
471 cam_info_topic_names.push_back(cam_info);
473 headers_of_bin_files.push_back(header_of_bin_file);
475 camera_total_ab_frames.push_back(0);
476 camera_total_depth_frames.push_back(0);
477 camera_device_time_stamp_written.push_back(
false);
478 camera_info_written.push_back(
false);
480 files[j] =
new std::ofstream();
481 files[j]->open(cam_bin_file_name[j], std::ios::out);
482 files[j]->write((
char*)&headers_of_bin_files[j],
sizeof(
HeaderOfBinFile));
487 bool all_camera_info_written =
false;
490 bag.
open(input_bag_file);
494 const std::string& img_topic = message.getTopic();
496 for (
int i = 0; i < cam_name.size(); i++)
499 if ((img_topic == cam_info_topic_names[i]) && (!camera_info_written[i]))
501 sensor_msgs::CameraInfoPtr camera_info_ptr = message.instantiate<sensor_msgs::CameraInfo>();
502 headers_of_bin_files[i].cam_image_width = camera_info_ptr->width;
503 headers_of_bin_files[i].cam_image_height = camera_info_ptr->height;
504 files[i]->write((
char*)&camera_info_ptr->K,
sizeof(double) * 9);
506 size_of_d = camera_info_ptr->D.size();
507 files[i]->write((
char*)&size_of_d,
sizeof(uint32_t));
508 files[i]->write((
char*)camera_info_ptr->D.data(),
sizeof(double) * size_of_d);
509 headers_of_bin_files[i].cam_first_frame_position =
510 (headers_of_bin_files[i].cam_first_frame_position - 16 *
sizeof(double)) + (size_of_d *
sizeof(double));
511 files[i]->write((
char*)&camera_info_ptr->R,
sizeof(double) * 9);
512 files[i]->write((
char*)&camera_info_ptr->P,
sizeof(double) * 12);
513 camera_info_written[i] =
true;
517 if (((img_topic == cam_ab_image_topic_names[i]) || (img_topic == cam_depth_image_topic_names[i])) &&
518 (camera_info_written[i]))
523 sensor_msgs::ImageConstPtr img_msg_ptr = message.instantiate<sensor_msgs::Image>();
525 timestamp = (long)img_msg_ptr->header.stamp.toNSec();
529 if (img_topic == cam_ab_image_topic_names[i])
531 files[i]->write((
char*)&ab,
sizeof(
bool));
532 camera_total_ab_frames[i]++;
534 else if (img_topic == cam_depth_image_topic_names[i])
536 files[i]->write((
char*)&depth,
sizeof(
bool));
537 camera_total_depth_frames[i]++;
539 if (!camera_device_time_stamp_written[i])
541 headers_of_bin_files[i].cam_device_timestamp = timestamp;
542 camera_device_time_stamp_written[i] =
true;
544 files[i]->write((
char*)×tamp,
sizeof(
long));
545 files[i]->write((
char*)image.data,
sizeof(
unsigned short) * (headers_of_bin_files[i].cam_image_width *
546 headers_of_bin_files[i].cam_image_height));
550 ROS_ERROR(
"Failed while reading raw images from bag file");
557 if (((img_topic == cam_compressed_ab_image_topic_names[i]) ||
558 (img_topic == cam_compressed_depth_image_topic_names[i])) &&
559 (camera_info_written[i]))
564 sensor_msgs::CompressedImageConstPtr compressed_message = message.instantiate<sensor_msgs::CompressedImage>();
567 unsigned char* compressed_image_buf =
569 int compressed_image_buf_size = compressed_message->data.size();
577 headers_of_bin_files[i].cam_image_width = *image_width;
578 headers_of_bin_files[i].cam_image_height = *image_height;
580 unsigned short* raw_image =
new unsigned short[headers_of_bin_files[i].cam_image_width *
581 headers_of_bin_files[i].cam_image_height * 2];
584 headers_of_bin_files[i].cam_image_width * headers_of_bin_files[i].cam_image_height);
587 timestamp = (long)compressed_message->header.stamp.toNSec();
589 if (img_topic == cam_compressed_ab_image_topic_names[i])
591 files[i]->write((
char*)&ab,
sizeof(bool));
592 camera_total_ab_frames[i]++;
594 else if (img_topic == cam_compressed_depth_image_topic_names[i])
596 files[i]->write((
char*)&depth,
sizeof(bool));
597 camera_total_depth_frames[i]++;
599 if (!camera_device_time_stamp_written[i])
601 headers_of_bin_files[i].cam_device_timestamp = timestamp;
602 camera_device_time_stamp_written[i] =
true;
604 files[i]->write((
char*)×tamp,
sizeof(long));
605 files[i]->write((
char*)raw_image,
sizeof(
unsigned short) * (headers_of_bin_files[i].cam_image_width *
606 headers_of_bin_files[i].cam_image_height));
611 ROS_ERROR(
"Failed while reading compressed images from bag file");
619 for (
int k = 0; k < cam_name.size(); k++)
623 headers_of_bin_files[k].cam_total_frames = std::min(camera_total_ab_frames[k], camera_total_depth_frames[k]);
624 files[k]->write((
char*)&headers_of_bin_files[k].cam_total_frames,
sizeof(uint32_t));
625 files[k]->write((
char*)&headers_of_bin_files[k].cam_image_width,
sizeof(uint32_t));
626 files[k]->write((
char*)&headers_of_bin_files[k].cam_image_height,
sizeof(uint32_t));
628 files[k]->write((
char*)&headers_of_bin_files[k].cam_first_frame_position,
sizeof(uint32_t));
630 files[k]->write((
char*)&headers_of_bin_files[k].cam_device_timestamp,
sizeof(
long));
642 cam_bin_file_name.clear();
643 cam_bin_file_name.shrink_to_fit();
644 cam_ab_image_topic_names.clear();
645 cam_ab_image_topic_names.shrink_to_fit();
646 cam_compressed_ab_image_topic_names.clear();
647 cam_compressed_ab_image_topic_names.shrink_to_fit();
648 cam_depth_image_topic_names.clear();
649 cam_depth_image_topic_names.shrink_to_fit();
650 cam_compressed_depth_image_topic_names.clear();
651 cam_compressed_depth_image_topic_names.shrink_to_fit();
652 cam_info_topic_names.clear();
653 cam_info_topic_names.shrink_to_fit();
654 headers_of_bin_files.clear();
655 headers_of_bin_files.shrink_to_fit();
656 camera_total_ab_frames.clear();
657 camera_total_ab_frames.shrink_to_fit();
658 camera_total_depth_frames.clear();
659 camera_total_depth_frames.shrink_to_fit();
660 camera_device_time_stamp_written.clear();
661 camera_device_time_stamp_written.shrink_to_fit();
662 camera_info_written.clear();
663 camera_device_time_stamp_written.shrink_to_fit();
665 files.shrink_to_fit();
675 int main(
int argc,
char** argv)
677 ros::init(argc, argv,
"adi_read_rosbags");
680 std::string input_file_name;
681 nh.
param<std::string>(
"param_input_file_name", input_file_name,
"no name");
684 std::vector<std::string> cam_prefix;
686 nh.
param(
"param_camera_prefixes", cam_prefix_arr, cam_prefix_arr);
687 for (
int i = 0; i < cam_prefix_arr.
size(); i++)
689 cam_prefix.push_back(cam_prefix_arr[i]);
690 std::cerr <<
"camera_prefixes: " << cam_prefix[i] << std::endl;
695 std::cout <<
"Completed." << std::endl;