我正在尝试订阅来自 kinect2 相机的主题并保存这些图像。我将 iai_kinect2 包与 libfreenect2 一起使用。我的问题是除了 .bmp 格式之外,所有其他格式都会在时间戳之间产生一些跳跃。我不想使用 .bmp 格式,因为显然它没有给出正确的深度图像。当我尝试在 matlab 中打开它时,我只有零和 255 个深度图像。这是 .png 的时间戳示例:
1533061840.176977104
1533061840.210227483
1533061840.243339200
1533061840.376478700 //see the jump
1533061840.476526118
1533061840.510053089
1533061840.576983109
除 .bmp 外,所有文件扩展名都有相同类型的跳转。我不能使用 .bmp ,因为它只为深度图像提供 0 或 255 这里是一个例子: 000001.bmp 给出不正确的深度图像只是零和 255 另一方面 .png 例如给出正确的深度图像,如下所示: 000005.png给出正确的深度图像
我的问题是如何用 .png 或任何其他文件扩展名解决这个时间戳问题?或如何使用 .bmp 图像生成正确的深度图像?当我将此 .bmp 图像添加到 matlab 时,它说它同时具有图像和颜色图。
感谢您的帮助。
你可以在下面找到我的源代码。
#include "kinect2_recorder/kinect2_recorder.h"
namespace kinect2_recorder {
// Constructor definition
RecordData::RecordData(const ros::NodeHandle & nh1, const std::string &save_dir_, const std::string &object_name_) : global_image_counter_(1), kinect_node_(nh1), it_(kinect_node_), sub_color_(it_, "color", 5), sub_depth_(it_,"depth", 5) , sync( MySyncPolicy_( 10 ), sub_depth_, sub_color_ ) , local_image_index_(1), padding_digit_number_(6)
{
/* Some code*/
// Exact time policy
sync.registerCallback(boost::bind(&RecordData::masterkinectCallback, this, _1, _2 ) );
void RecordData::masterkinectCallback(const sensor_msgs::ImageConstPtr& depth_msg, const sensor_msgs::ImageConstPtr& color_msg) {
ros::Time last_time1 = ros::Time::now();
cv_bridge::CvImageConstPtr cv_depthptr;
try{
cv_depthptr = cv_bridge::toCvShare(depth_msg, sensor_msgs::image_encodings::TYPE_16UC1);
//cv_depthptr->image.convertTo(cv_depthptr->image, CV_16U);
//cv::normalize(cv_depthptr->image, cv_depthptr->image, 1, 0, cv::NORM_MINMAX);
}
catch (cv_bridge::Exception& e2){
ROS_ERROR("Could not convert from '%s' to '16UC1'.", e2.what());
return;
cv_bridge::CvImageConstPtr cv_colorptr;
try{
cv_colorptr = cv_bridge::toCvShare(color_msg, sensor_msgs::image_encodings::BGR8);
}
catch (cv_bridge::Exception& e1){
ROS_ERROR("Could not convert from '%s' to 'BGR8'.", e1.what());
return;
}
ros::Time last_time2 = ros::Time::now();
// Save images with increasing index names
std::ostringstream str_localindex;
// Ignore First 10 Frames
if (global_image_counter_ <= 10) {} //do nothing
else {
// Save depth images
std::cout << " Before depth: " << ros::Time::now();
str_localindex << std::setw(padding_digit_number_) << std::setfill('0') << local_image_index_;
cv::imwrite(depth_dir_ + str_localindex.str() + ".bmp", cv_depthptr->image);
std::cout << " After depth: " << ros::Time::now();
std::cout << "saved_depth_index: " << local_image_index_ << std::endl;
// Save color images
cv::imwrite(color_dir_ + str_localindex.str() + ".bmp", cv_colorptr->image);
std::cout << " After color: " << ros::Time::now();
// Save timestamps
textfile_ << cv_depthptr->header.stamp << std::endl;
local_image_index_++;
}
global_image_counter_++;
ros::Time last_time3 = ros::Time::now();
std::cout << "First time difference: " << last_time2-last_time1<< std::endl;
std::cout << "Second time difference: " << last_time3-last_time2<< std::endl;
std::cout << "Third time difference: " << last_time3-last_time1<< std::endl;
}
} //end of namespace
编辑:我理解这个问题。保存过程花费的时间比我从 kinect2 接收信息的时间要长。我能做些什么来解决这个问题?这是最后的输出:
First time difference: 0.000049324
Second time difference: 0.000002948
Third time difference: 0.000052272
saved_depth_index: 1
First time difference: 0.000040741
Second time difference: 0.051224336
Third time difference: 0.051265077
saved_depth_index: 2
First time difference: 0.000033076
Second time difference: 0.042704145
Third time difference: 0.042737221
saved_depth_index: 3
First time difference: 0.000033785
Second time difference: 0.042851813
Third time difference: 0.042885598
这是 .png 打印时间的输出:
Before depth: 1533067649.437008647 After depth: 1533067649.449930403saved_depth_index: 1
After color: 1533067649.479346684 Before depth: 1533067649.479786642 After depth: 1533067649.490577579saved_depth_index: 2
After color: 1533067649.520330356 Before depth: 1533067649.520770286 After depth: 1533067649.531645103saved_depth_index: 3
After color: 1533067649.561450753 Before depth: 1533067649.570491592 After depth: 1533067649.583154752saved_depth_index: 4
After color: 1533067649.612989915 Before depth: 1533067649.614441724 After depth: 1533067649.625398696saved_depth_index: 5
After color: 1533067649.654874365 Before depth: 1533067649.655352960 After depth: 1533067649.666162400saved_depth_index: 6
After color: 1533067649.695561608 Before depth: 1533067649.696115115 After depth: 1533067649.708304851saved_depth_index: 7
After color: 1533067649.736989649 Before depth: 1533067649.738013842 After depth: 1533067649.751335296saved_depth_index: 8
After color: 1533067649.780984546 Before depth: 1533067649.781488371 After depth: 1533067649.792345625saved_depth_index: 9
After color: 1533067649.822243383 Before depth: 1533067649.822725751 After depth: 1533067649.833530992saved_depth_index: 10
After color: 1533067649.862818010 Before depth: 1533067649.863264236 After depth: 1533067649.875531476saved_depth_index: 11
After color: 1533067649.904058584 Before depth: 1533067649.904558682 After depth: 1533067649.917082444saved_depth_index: 12
After color: 1533067649.946167277 Before depth: 1533067649.946650618 After depth: 1533067649.957500888saved_depth_index: 13
After color: 1533067649.987059465 Before depth: 1533067649.989056085 After depth: 1533067649.999987811saved_depth_index: 14
After color: 1533067650.029481919 Before depth: 1533067650.070665790 After depth: 1533067650.083509494saved_depth_index: 15
After color: 1533067650.113966288 Before depth: 1533067650.115590415 After depth: 1533067650.126528055saved_depth_index: 16
After color: 1533067650.156366989 Before depth: 1533067650.237325222 After depth: 1533067650.250013465saved_depth_index: 17
After color: 1533067650.280332343 Before depth: 1533067650.404256322 After depth: 1533067650.416994490saved_depth_index: 18
这是 .png 对应的 timestamp.txt 行:
1533067649.405156137
1533067649.438584768
1533067649.471184334
1533067649.538291826
1533067649.571496466
1533067649.604620511
1533067649.637874297
1533067649.671323063
1533067649.704726902
1533067649.738140404
1533067649.771370780
1533067649.804495997
1533067649.837931434
1533067649.938107491
1533067650.037869503
1533067650.071517274
1533067650.204548131
这是 .bmp 的输出:
Before depth: 1533068550.007569541 After depth: 1533068550.039293417saved_depth_index: 1
After color: 1533068550.040323334 Before depth: 1533068550.041525631 After depth: 1533068550.041995796saved_depth_index: 2
After color: 1533068550.043473161 Before depth: 1533068550.074098761 After depth: 1533068550.074672501saved_depth_index: 3
After color: 1533068550.075766884 Before depth: 1533068550.106968347 After depth: 1533068550.107476638saved_depth_index: 4
After color: 1533068550.108505096 Before depth: 1533068550.141064749 After depth: 1533068550.141878365saved_depth_index: 5
After color: 1533068550.144248827 Before depth: 1533068550.174576245 After depth: 1533068550.175276900saved_depth_index: 6
After color: 1533068550.176800271 Before depth: 1533068550.207376338 After depth: 1533068550.207835761saved_depth_index: 7
After color: 1533068550.209161683 Before depth: 1533068550.242071242 After depth: 1533068550.242618620saved_depth_index: 8
After color: 1533068550.243642589 Before depth: 1533068550.275326665 After depth: 1533068550.275932215saved_depth_index: 9
After color: 1533068550.278269832 Before depth: 1533068550.307122129 After depth: 1533068550.307663711saved_depth_index: 10
After color: 1533068550.309189049 Before depth: 1533068550.341019501 After depth: 1533068550.341644754saved_depth_index: 11
After color: 1533068550.343060063 Before depth: 1533068550.374125217 After depth: 1533068550.374740065saved_depth_index: 12
After color: 1533068550.376692289 Before depth: 1533068550.407086067 After depth: 1533068550.407683886saved_depth_index: 13
After color: 1533068550.409053003 Before depth: 1533068550.441498083 After depth: 1533068550.442042331saved_depth_index: 14
After color: 1533068550.444182590 Before depth: 1533068550.507368335 After depth: 1533068550.507842169saved_depth_index: 15
After color: 1533068550.509398491 Before depth: 1533068550.541008375 After depth: 1533068550.541669296saved_depth_index: 16
After color: 1533068550.543557788 Before depth: 1533068550.575678313 After depth: 1533068550.576168080saved_depth_index: 17
After color: 1533068550.577603736 Before depth: 1533068550.608481247 After depth: 1533068550.608980560saved_depth_index: 18
After color: 1533068550.610480415 Before depth: 1533068550.674148550 After depth: 1533068550.674846522saved_depth_index: 19
After color: 1533068550.676181557 Before depth: 1533068550.707998985 After depth: 1533068550.708643165saved_depth_index: 20