can't save point cloud in bag file complitly
Hy, i am trying to save the point cloud with size(480,640) in bag file by their coordinate values and then read them in another node so that machine determines which part of the point cloud has been changed in next frames and what are their coordinates so that they can be searched in coming frames and that surface with those points can be detected in next point cloud. My problem is when i save point cloud of size (479,50) range it saves and can be visualized, but as the size change to above 50, i get segmented error, what should i do? My codes are: To read the point cloud of size(50,50), determined by raw = 479, col = 50:
#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <iostream>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <visualization_msgs/Marker.h>
#define pi 3.1453
#define pu 3779.527559055
#include "std_msgs/String.h"
#include "std_msgs/Float32.h"
#include "std_msgs/Int32.h"
#include "rosbag/bag.h"
#include <rosbag/view.h>
#include <vector>
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
ros::Publisher pub;
ros::Publisher marker_pub;
void
cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
{
pcl::PointCloud<pcl::PointXYZ> output;
pcl::fromROSMsg(*input,output);
pcl::PointXYZ P[479][639];
for(int raw = 0;raw<479;raw++)
{
for(int col = 0;col<639;col++)
{
P[raw][col] = output.at(col,raw);
}
}
int raw = 479, col = 50;
rosbag::Bag bag("test.bag", rosbag::bagmode::Write);
std_msgs::Float32 x[raw][col], y[raw][col], z[raw][col]; // y[480][640], z[480][640];
for(int k=0;k<=raw;k++)
{
for(int l=0;l<=col;l++) {
x[k][l].data = P[k][l].x;
y[k][l].data = P[k][l].y;
z[k][l].data = P[k][l].z;
} }
for(int j=0;j<=raw;j++) {
for(int k=0;k<=col;k++) {
bag.write("numbers", ros::Time::now(), x[j][k]);
bag.write("numbers", ros::Time::now(), y[j][k]);
bag.write("numbers", ros::Time::now(), z[j][k]);
// std::cout<<j<<"\t"<<k<<"\t"<<i[j][k]<<"\t"<<y[j][k]<<"\t"<<z[j][k]<<std::endl;
}
}
bag.close();
std::cout<<"saved"<<std::endl;
sensor_msgs::PointCloud2 cloud;
pcl::toROSMsg(output,cloud);
pub.publish(cloud);
}
int
main(int argc, char** argv)
{
ROS_INFO("main");
ros::init(argc, argv,"example");
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe ("input", 1, cloud_cb);
pub = nh.advertise<sensor_msgs::PointCloud2>("output",1);
marker_pub = nh.advertise<visualization_msgs::Marker> ("out",1);
ros::spin();
}
And the code for reading them is:(so that if some object moves in depth frame it could be deteted and saved, tracked, since every point in rigid object are always at same position with respect to each other with small error):
void
cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
{
pcl::PointCloud<pcl::PointXYZ> output;
pcl::fromROSMsg(*input,output);
visualization_msgs::Marker points, line_strip, line_list;
points.header.frame_id = line_strip.header.frame_id = line_list.header.frame_id = "/camera_depth_frame";
points ...