sudo apt-get install ros-melodic-navigation
IPM-ROS
git clone https://github.com/Irvingao/IPM-mapping-ros.git
编译
cd ~/your_ws
catkin_make -DCATKIN_WHITELIST_PACKAGES="IPM-mapping-ros"
打开Realsense发布点云数据
roslaunch realsense2_camera rs_camera.launch filters:=pointcloud
rviz
PCL点云处理
接收点云信息并用pcl显示
pcl_view.cpp
#include <iostream>
#include <algorithm>
#include <vector>
#include <Eigen/Dense>
#include <Eigen/Core>
#include <boost/thread/thread.hpp>
#include <fstream>
#include <string>
#include <ros/ros.h>
#include <std_msgs/Header.h>
#include <std_msgs/Float64MultiArray.h>
#include <std_msgs/UInt16.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl/common/common.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/radius_outlier_removal.h>
#include <pcl/search/kdtree.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>
#include <pcl/io/pcd_io.h>
#include <pcl/ros/conversions.h>
#include <pcl/sample_consensus/ransac.h>
#include <pcl/sample_consensus/sac_model_normal_sphere.h>
#include <pcl/sample_consensus/sac_model_cylinder.h>
#include <pcl/features/normal_3d.h>
#include <pcl/features/principal_curvatures.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/region_growing.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/features/moment_of_inertia_estimation.h>
using namespace std;
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer1(new pcl::visualization::PCLVisualizer("realtime pcl"));
ros::Publisher pub;
typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;
void cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
{
pcl::PCLPointCloud2* cloud = new pcl::PCLPointCloud2;
pcl::PCLPointCloud2ConstPtr cloudPtr(cloud);
pcl_conversions::toPCL(*input, *cloud);
pub.publish (*cloud);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1;
cloud1.reset (new pcl::PointCloud<pcl::PointXYZ>);
pcl::fromROSMsg (*input, *cloud1);
viewer1->removeAllPointClouds();
viewer1->addPointCloud(cloud1, "realtime pcl");
viewer1->updatePointCloud(cloud1, "realtime pcl");
viewer1->spinOnce(0.001);
}
int main (int argc, char** argv)
{
ros::init (argc, argv, "pcl");
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe ("/camera/depth/color/points", 10, cloud_cb);
pub = nh.advertise<pcl::PCLPointCloud2> ("output", 10);
ros::spin ();
}
CMakeLists.txt
find_package(PCL REQUIRED)
catkin_package()
include_directories(
include
${catkin_INCLUDE_DIRS}
${PCL_INCLUDE_DIRS}
)
add_executable(pcl_view src/pcl_view.cpp)
target_link_libraries(pcl_view ${catkin_LIBRARIES} ${PCL_LIBRARIES})
接收到点云并进行色彩处理并发布
pcl_process.cpp
#include <iostream>
#include <algorithm>
#include <vector>
#include <Eigen/Dense>
#include <Eigen/Core>
#include <boost/thread/thread.hpp>
#include <fstream>
#include <string>
#include <ros/ros.h>
#include <std_msgs/Header.h>
#include <std_msgs/Float64MultiArray.h>
#include <std_msgs/UInt16.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl/common/common.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/radius_outlier_removal.h>
#include <pcl/search/kdtree.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>
#include <pcl/io/pcd_io.h>
#include <pcl/ros/conversions.h>
#include <pcl/sample_consensus/ransac.h>
#include <pcl/sample_consensus/sac_model_normal_sphere.h>
#include <pcl/sample_consensus/sac_model_cylinder.h>
#include <pcl/features/normal_3d.h>
#include <pcl/features/principal_curvatures.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/region_growing.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/features/moment_of_inertia_estimation.h>
class pcl_sub
{
private:
ros::NodeHandle n;
ros::Subscriber subCloud;
ros::Publisher pubCloud;
sensor_msgs::PointCloud2 msg;
sensor_msgs::PointCloud2 adjust_msg;
pcl::PointCloud<pcl::PointXYZRGB> adjust_pcl;
public:
pcl_sub():
n("~"){
subCloud = n.subscribe<sensor_msgs::PointCloud2>("/camera/depth/color/points", 1, &pcl_sub::getcloud, this);
pubCloud = n.advertise<sensor_msgs::PointCloud2>("/adjustd_cloud", 1000);
}
void getcloud(const sensor_msgs::PointCloud2ConstPtr& laserCloudMsg){
pcl::PointCloud<pcl::PointXYZRGB>::Ptr adjust_pcl_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::fromROSMsg(*laserCloudMsg, *adjust_pcl_ptr);
adjust_pcl = *adjust_pcl_ptr;
for (int i = 0; i < adjust_pcl.points.size(); i++)
{
adjust_pcl.points[i].r = 0;
adjust_pcl.points[i].g = 255;
adjust_pcl.points[i].b = 0;
}
pcl::toROSMsg(adjust_pcl, adjust_msg);
pubCloud.publish(adjust_msg);
}
~pcl_sub(){}
};
int main(int argc, char** argv) {
ros::init(argc, argv, "colored");
pcl_sub ps;
ros::spin();
return 0;
}
接收到图片进行处理并转换为彩色点云发布
#include <iostream>
#include <algorithm>
#include <vector>
#include <Eigen/Dense>
#include <Eigen/Core>
#include <boost/thread/thread.hpp>
#include <fstream>
#include <string>
#include <ros/ros.h>
#include <std_msgs/Header.h>
#include <std_msgs/Float64MultiArray.h>
#include <std_msgs/UInt16.h>
#include <sensor_msgs/PointCloud2.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <pcl/common/common.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/radius_outlier_removal.h>
#include <pcl/search/kdtree.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>
#include <pcl/io/pcd_io.h>
#include <pcl/ros/conversions.h>
#include <pcl/sample_consensus/ransac.h>
#include <pcl/sample_consensus/sac_model_normal_sphere.h>
#include <pcl/sample_consensus/sac_model_cylinder.h>
#include <pcl/features/normal_3d.h>
#include <pcl/features/principal_curvatures.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/region_growing.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/features/moment_of_inertia_estimation.h>
ros::Publisher pubCloud;
int R, G, B;
cv::Mat image;
pcl::PointCloud<pcl::PointXYZRGB> cloud;
sensor_msgs::PointCloud2 output_msg;
void imagepointcloudCallback(const sensor_msgs::ImageConstPtr& msg)
{
try
{
image = cv_bridge::toCvShare(msg, "bgr8") -> image;
pcl::PointCloud<pcl::PointXYZRGB> cloud;
cloud.width = image.rows;
cloud.height =image.cols;
cloud.points.resize(cloud.width * cloud.height);
sensor_msgs::PointCloud2 output_msg;
output_msg.header.stamp = ros::Time::now();
int n = 0;
for (int i = 0; i < image.rows; i++)
{
for(int j=0;j<image.cols;j++)
{
R = image.at<cv::Vec3b>(i, j)[2];
G = image.at<cv::Vec3b>(i, j)[1];
B = image.at<cv::Vec3b>(i, j)[0];
if (R==0 && G==0 && B==0){
continue;
} else {
cloud.points[n].x = 5 * j / (double)cloud.height;
cloud.points[n].y = 5 * i / (double)cloud.width;
cloud.points[n].z = 0;
cloud.points[n].r = R;
cloud.points[n].g = G;
cloud.points[n].b = B;
}
n = n + 1;
}
}
pcl::toROSMsg(cloud, output_msg);
output_msg.header.frame_id = "map";
pubCloud.publish(output_msg);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
}
cv::waitKey(1);
}
int main(int argc, char** argv) {
ros::init(argc, argv, "pcl_process_node");
ros::NodeHandle n;
ros::Subscriber subCloud;
image_transport::ImageTransport it(n);
image_transport::Subscriber sub = it.subscribe("/rgb_camera/image_ipm", 1, imagepointcloudCallback);
pubCloud = n.advertise<sensor_msgs::PointCloud2>("/map/pointcloud_ipm", 1000);
ros::spin();
return 0;
}
读取图像转成点云并合并已有点云
#include <iostream>
#include <algorithm>
#include <vector>
#include <Eigen/Dense>
#include <Eigen/Core>
#include <boost/thread/thread.hpp>
#include <fstream>
#include <string>
#include <ros/ros.h>
#include <std_msgs/Header.h>
#include <std_msgs/Float64MultiArray.h>
#include <std_msgs/UInt16.h>
#include <sensor_msgs/PointCloud2.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <pcl/common/common.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/radius_outlier_removal.h>
#include <pcl/search/kdtree.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>
#include <pcl/io/pcd_io.h>
#include <pcl/ros/conversions.h>
#include <pcl/sample_consensus/ransac.h>
#include <pcl/sample_consensus/sac_model_normal_sphere.h>
#include <pcl/sample_consensus/sac_model_cylinder.h>
#include <pcl/features/normal_3d.h>
#include <pcl/features/principal_curvatures.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/segmentation/region_growing.h>
#include <pcl/segmentation/extract_clusters.h>
#include <pcl/features/moment_of_inertia_estimation.h>
ros::Publisher pubCloud;
cv::Mat image;
int R, G, B;
pcl::PointCloud<pcl::PointXYZRGB> final_rec_cloud;
void getcloud(const sensor_msgs::PointCloud2ConstPtr& input_msg){
pcl::PointCloud<pcl::PointXYZRGB> recive_cloud;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr recive_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::fromROSMsg(*input_msg, *recive_cloud_ptr);
recive_cloud = *recive_cloud_ptr;
final_rec_cloud = recive_cloud;
}
void imagepointcloudCallback(const sensor_msgs::ImageConstPtr& msg) {
pcl::PointCloud<pcl::PointXYZRGB> cloud;
pcl::PointCloud<pcl::PointXYZRGB> output_cloud;
sensor_msgs::PointCloud2 output_msg;
try {
image = cv_bridge::toCvShare(msg, "bgr8") -> image;
cloud.width = image.rows;
cloud.height =image.cols;
cloud.points.resize(cloud.width * cloud.height);
output_msg.header.stamp = ros::Time::now();
int n = 0;
for (int i = 0; i < image.rows; i++) {
for(int j=0;j<image.cols;j++) {
R = image.at<cv::Vec3b>(i, j)[2];
G = image.at<cv::Vec3b>(i, j)[1];
B = image.at<cv::Vec3b>(i, j)[0];
if (R==0 && G==0 && B==0){
continue;
} else {
cloud.points[n].x = 5 * j / (double)cloud.height;
cloud.points[n].y = 5 * i / (double)cloud.width;
cloud.points[n].z = 0;
cloud.points[n].r = R;
cloud.points[n].g = G;
cloud.points[n].b = B;
}
n = n + 1;
}
}
if (final_rec_cloud.points.size() != 0) {
ROS_INFO_STREAM("merge two cloud");
output_cloud.points.resize(final_rec_cloud.points.size() + image.rows*image.cols);
output_cloud = cloud + final_rec_cloud;
} else {
output_cloud = cloud;
}
pcl::toROSMsg(output_cloud, output_msg);
output_msg.header.frame_id = "camera_link";
pubCloud.publish(output_msg);
}
catch (cv_bridge::Exception& e) {
ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
}
cv::waitKey(1);
}
int main(int argc, char** argv) {
ros::init(argc, argv, "pcl_process_node");
ros::NodeHandle n;
ros::Subscriber subCloud;
image_transport::ImageTransport it(n);
image_transport::Subscriber sub = it.subscribe("/rgb_camera/image_ipm", 1, imagepointcloudCallback);
subCloud = n.subscribe<sensor_msgs::PointCloud2>("/camera/depth/color/points", 1, getcloud);
pubCloud = n.advertise<sensor_msgs::PointCloud2>("/merge_cloud", 1000);
ros::spin();
return 0;
}
|