更改时间戳的代码
import rospy
import rosbag
import sys
if sys.getdefaultencoding() != 'utf-8':
reload(sys)
sys.setdefaultencoding('utf-8')
bag_name = '2021-06-09-15-16-30.bag'
out_bag_name = 'change_2021-06-09-15-16-30.bag'
dst_dir = '/home/stone/projects/ws/config/'
with rosbag.Bag(dst_dir+out_bag_name, 'w') as outbag:
stamp = None
for topic, msg, t in rosbag.Bag(dst_dir+bag_name).read_messages():
if topic == '/livox/imu':
stamp = msg.header.stamp
msg.header.stamp=stamp
outbag.write(topic, msg, stamp)
elif topic == '/livox/lidar' and stamp is not None:
stamp = msg.header.stamp
msg.header.stamp=stamp
outbag.write(topic, msg, stamp)
elif topic == '/a_image' and stamp is not None:
stamp = msg.header.stamp-rospy.Duration(1623123042.253242879)-rospy.Duration(0.80)
msg.header.stamp=stamp
print(msg.header.stamp)
print(stamp)
outbag.write(topic, msg, stamp)
print("finished")
bag2depth.cpp
#include "bag2depth/getparam.hpp"
std::deque<cv::Mat> img_buffer;
std::deque<pcl::PointCloud<pcl::PointXYZ>> cloud_buffer;
void ImageHandler(const sensor_msgs::Image::ConstPtr& img_msg) {
cv::Mat img = cv_bridge::toCvCopy(img_msg)->image;
if (!img.empty()) {
img_buffer.push_back(img);
if (img_buffer.size() > 10) {
img_buffer.pop_front();
}
}
}
void LiDARHandler(const sensor_msgs::PointCloud2::ConstPtr& lidar_msg) {
pcl::PointCloud<pcl::PointXYZ> cloud;
pcl::fromROSMsg(*lidar_msg, cloud);
cloud_buffer.push_back(cloud);
if (cloud_buffer.size() > 10) {
cloud_buffer.pop_front();
}
}
int num=0;
void processData( pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, cv::Mat image)
{
num+=1;
cv::Mat img_undistort;
cv::resize(image, image, cv::Size( m_image_size.width,m_image_size.height));
cv::undistort(image, img_undistort, camK, distort_param, camK);
createpath("/home/stone/projects/ws/output/image/");
cv::imwrite("/home/stone/projects/ws/output/image/"+std::to_string(num)+".png", img_undistort);
cv::Mat depthimage=cv::Mat::zeros( m_image_size.height,m_image_size.width,CV_16UC1);
pcl::PointXYZ ptmp;
double max_depth_value=0;
double min_depth_value=260;
for(unsigned int index=0; index<cloud->size(); index++)
{
ptmp = cloud->points[index];
Eigen::Vector3d Pw(ptmp.x, ptmp.y, ptmp.z);
Eigen::Vector2d Pcam;
if ( spaceToPlane(Pw, Pcam, distance_valid) ) {
int x = Pcam[0];
int y = Pcam[1];
unsigned char r, g, b;
double depth_value;
depth_value=sqrt(ptmp.x*ptmp.x+ptmp.y*ptmp.y+ptmp.z*ptmp.z);
if(depth_value>max_depth_value)
{max_depth_value=depth_value;}
if(depth_value<min_depth_value)
{min_depth_value=depth_value;}
double h = (depth_value-zmin)/(zmax -zmin)*65535;
unsigned short *p = depthimage.ptr<unsigned short>(y);
p[x] = h;
HSVtoRGB(h/256, 100, 100, &r, &g, &b);
cv::circle(img_undistort, cv::Point2d(x,y), 0.6, cv::Scalar(r,g,b), 2);
}
}
std::cout<<"max_depth_value="<<max_depth_value<<std::endl;
std::cout<<"min_depth_value="<<min_depth_value<<std::endl;
createpath("/home/stone/projects/ws/output/lidar_image/");
cv::imwrite("/home/stone/projects/ws/output/lidar_image/"+std::to_string(num)+".png", img_undistort);
std::vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(0);
createpath("/home/stone/projects/ws/output/depthmap/");
cv::imwrite("/home/stone/projects/ws/output/depthmap/"+std::to_string(num)+".png",depthimage,compression_params);
}
int main (int argc, char** argv)
{
ros::init(argc,argv,"bag2depth");
ros::NodeHandle nh;
double T;
std::string yaml_path, image_topic, lidar_topic;
std::string extrinsic_file;
ros::NodeHandle nh_private("~");
nh_private.param<std::string>("extrinsic_file", extrinsic_file, "/home/stone/projects/ws/config/pointgrey1.bin");
nh_private.param<std::string>("yaml_path", yaml_path, "/home/stone/projects/ws/config/cam_matrix_b.yaml");
nh_private.param<std::string>("image_topic", image_topic, "/a_image");
nh_private.param<std::string>("lidar_topic", lidar_topic, "/livox/lidar");
nh_private.param<double>("integral_time",T, 1);
nh_private.param<double>("distance_valid", distance_valid,260);
nh_private.param<double>("zmin", zmin,0);
nh_private.param<double>("zmax", zmax,260);
std::cout << "load T_lidar2cam from"<< extrinsic_file<< std::endl;
Load_extrinsic(extrinsic_file);
Load_intrinsic(yaml_path);
ros::Subscriber sub_livox =nh.subscribe<sensor_msgs::PointCloud2>(lidar_topic, 20, LiDARHandler);
ros::Subscriber sub_image = nh.subscribe<sensor_msgs::Image>(image_topic, 20, ImageHandler);
ros::Rate loop_rate(1/T);
int num_cloud=0;
while (ros::ok()) {
if(img_buffer.size() > 0 && cloud_buffer.size() > 0)
{
pcl::PointCloud<pcl::PointXYZ> integralcloud;
for(int i=0; i<cloud_buffer.size();i++)
{
pcl::PointCloud<pcl::PointXYZ> tmp_cloud = cloud_buffer.back();
cloud_buffer.pop_back();
integralcloud+= tmp_cloud;
}
if(integralcloud.width!=0)
{ num_cloud+=1;
std::cout<<num_cloud<<std::endl;
std::string folderPath="/home/stone/projects/ws/output/integral_lidar"+std::to_string(T)+"/";
createpath(folderPath);
pcl::io::savePCDFileASCII (folderPath+"lidar"+std::to_string(num_cloud)+".pcd", integralcloud);
}
cv::Mat tmp_image = img_buffer.back();
cloud_buffer.clear();
img_buffer.clear();
processData(integralcloud.makeShared(),tmp_image);
}
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
message_filter同步
#include "bag2depth/getparam.hpp"
std::deque<cv::Mat> img_buffer;
std::deque<pcl::PointCloud<pcl::PointXYZ>> cloud_buffer;
void ImageHandler(const sensor_msgs::Image::ConstPtr& img_msg) {
cv::Mat img = cv_bridge::toCvCopy(img_msg)->image;
if (!img.empty()) {
img_buffer.push_back(img);
if (img_buffer.size() > 10) {
img_buffer.pop_front();
}
}
}
void LiDARHandler(const sensor_msgs::PointCloud2::ConstPtr& lidar_msg) {
pcl::PointCloud<pcl::PointXYZ> cloud;
pcl::fromROSMsg(*lidar_msg, cloud);
cloud_buffer.push_back(cloud);
if (cloud_buffer.size() > 10) {
cloud_buffer.pop_front();
}
}
int num=0;
void processData( pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, cv::Mat image)
{
num+=1;
cv::Mat img_undistort;
cv::resize(image, image, cv::Size( m_image_size.width,m_image_size.height));
cv::undistort(image, img_undistort, camK, distort_param, camK);
createpath("/home/stone/projects/ws/output/image/");
cv::imwrite("/home/stone/projects/ws/output/image/"+std::to_string(num)+".png", img_undistort);
cv::Mat depthimage=cv::Mat::zeros( m_image_size.height,m_image_size.width,CV_16UC1);
pcl::PointXYZ ptmp;
double max_depth_value=0;
double min_depth_value=260;
for(unsigned int index=0; index<cloud->size(); index++)
{
ptmp = cloud->points[index];
Eigen::Vector3d Pw(ptmp.x, ptmp.y, ptmp.z);
Eigen::Vector2d Pcam;
if ( spaceToPlane(Pw, Pcam, distance_valid) ) {
int x = Pcam[0];
int y = Pcam[1];
unsigned char r, g, b;
double depth_value;
depth_value=sqrt(ptmp.x*ptmp.x+ptmp.y*ptmp.y+ptmp.z*ptmp.z);
if(depth_value>max_depth_value)
{max_depth_value=depth_value;}
if(depth_value<min_depth_value)
{min_depth_value=depth_value;}
double h = (depth_value-zmin)/(zmax -zmin)*65535;
unsigned short *p = depthimage.ptr<unsigned short>(y);
p[x] = h;
HSVtoRGB(h/256, 100, 100, &r, &g, &b);
cv::circle(img_undistort, cv::Point2d(x,y), 0.6, cv::Scalar(r,g,b), 2);
}
}
std::cout<<"max_depth_value="<<max_depth_value<<std::endl;
std::cout<<"min_depth_value="<<min_depth_value<<std::endl;
createpath("/home/stone/projects/ws/output/lidar_image/");
cv::imwrite("/home/stone/projects/ws/output/lidar_image/"+std::to_string(num)+".png", img_undistort);
std::vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(0);
createpath("/home/stone/projects/ws/output/depthmap/");
cv::imwrite("/home/stone/projects/ws/output/depthmap/"+std::to_string(num)+".png",depthimage,compression_params);
}
void callback(const sensor_msgs::PointCloud2::ConstPtr& lidar_msg,const sensor_msgs::Image::ConstPtr& img_msg)
{
cv::Mat img = cv_bridge::toCvCopy(img_msg)->image;
pcl::PointCloud<pcl::PointXYZ> cloud;
pcl::fromROSMsg(*lidar_msg, cloud);
processData(cloud.makeShared(),img);
}
int main (int argc, char** argv)
{
ros::init(argc,argv,"bag2depth");
ros::NodeHandle nh;
double T;
std::string yaml_path, image_topic, lidar_topic;
std::string extrinsic_file;
ros::NodeHandle nh_private("~");
nh_private.param<std::string>("extrinsic_file", extrinsic_file, "/home/stone/projects/ws/config/pointgrey1.bin");
nh_private.param<std::string>("yaml_path", yaml_path, "/home/stone/projects/ws/config/cam_matrix_b.yaml");
nh_private.param<std::string>("image_topic", image_topic, "/a_image");
nh_private.param<std::string>("lidar_topic", lidar_topic, "/livox/lidar");
nh_private.param<double>("integral_time",T, 1);
nh_private.param<double>("distance_valid", distance_valid,260);
nh_private.param<double>("zmin", zmin,0);
nh_private.param<double>("zmax", zmax,260);
std::cout << "load T_lidar2cam from"<< extrinsic_file<< std::endl;
Load_extrinsic(extrinsic_file);
Load_intrinsic(yaml_path);
message_filters::Subscriber<sensor_msgs::PointCloud2> cloud_sub(nh, lidar_topic, 1);
message_filters::Subscriber<sensor_msgs::Image> image_sub(nh, image_topic, 1);
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::PointCloud2, sensor_msgs::Image> MySyncPolicy;
message_filters::Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), cloud_sub, image_sub);
sync.registerCallback(boost::bind(&callback, _1, _2));
ROS_INFO("waiting for lidar image topic %s %s", lidar_topic.c_str(), image_topic.c_str());
ros::spin();
}