#include #include #include #include #include #include #include #include #include //for accessing camera #include "o3d3xx_camera.h" #include "o3d3xx_framegrabber.h" #include "o3d3xx_image.h" pcl::PointCloud convertCloud(pcl::PointCloud::Ptr inputCloud){ pcl::PointCloud::Ptr cloud = inputCloud; pcl::PointCloud newcloud; newcloud.width = cloud->width; newcloud.height = cloud->height; newcloud.points.resize(cloud->points.size()); for(std::size_t i = 0; i < cloud->points.size(); i++) { newcloud.points[i].x = cloud->points[i].x; newcloud.points[i].y = cloud->points[i].y; newcloud.points[i].z = cloud->points[i].z; } return newcloud; } int main (int argc, char** argv) { //logging method o3d3xx::Logging::Init(); //initialise camera constructor expects IP address, using default one o3d3xx::Camera::Ptr cam = std::make_shared("192.168.1.69"); //create buffer to fetch image o3d3xx::ImageBuffer::Ptr img = std::make_shared(); //framegrabber o3d3xx::FrameGrabber::Ptr fg = std::make_shared( cam, o3d3xx::IMG_AMP|o3d3xx::IMG_RDIS|o3d3xx::IMG_CART); //get frame from camera (could be looped to create an actual live feed) if (! fg->WaitForFrame(img.get(), 2000)) { std::cerr << "Timeout waiting for camera!" << std::endl; return -1; } pcl::PointCloud cloud(convertCloud(img->Cloud())); pcl::io::savePCDFileASCII("temp.pcd", cloud); std::cout << "Saved PCD" << std::endl; pcl::PCLPointCloud2::Ptr cloud_blob (new pcl::PCLPointCloud2), cloud_filtered_blob (new pcl::PCLPointCloud2); pcl::PointCloud::Ptr cloud_filtered (new pcl::PointCloud), cloud_p (new pcl::PointCloud), cloud_f (new pcl::PointCloud); // Fill in the cloud data pcl::PCDReader reader; reader.read ("temp.pcd", *cloud_blob); std::cerr << "PointCloud before filtering: " << cloud_blob->width * cloud_blob->height << " data points." << std::endl; // Create the filtering object: downsample the dataset using a leaf size of 1cm pcl::VoxelGrid sor; sor.setInputCloud (cloud_blob); sor.setLeafSize (0.01f, 0.01f, 0.01f); sor.filter (*cloud_filtered_blob); // Convert to the templated PointCloud pcl::fromPCLPointCloud2 (*cloud_filtered_blob, *cloud_filtered); std::cerr << "PointCloud after filtering: " << cloud_filtered->width * cloud_filtered->height << " data points." << std::endl; // Write the downsampled version to disk pcl::PCDWriter writer; writer.write ("table_scene_lms400_downsampled.pcd", *cloud_filtered, false); pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients ()); pcl::PointIndices::Ptr inliers (new pcl::PointIndices ()); // Create the segmentation object pcl::SACSegmentation seg; // Optional seg.setOptimizeCoefficients (true); // Mandatory seg.setModelType (pcl::SACMODEL_PLANE); seg.setMethodType (pcl::SAC_RANSAC); seg.setMaxIterations (1000); seg.setDistanceThreshold (0.01); // Create the filtering object pcl::ExtractIndices extract; int i = 0, nr_points = (int) cloud_filtered->points.size (); // While 30% of the original cloud is still there while (cloud_filtered->points.size () > 0.3 * nr_points) { // Segment the largest planar component from the remaining cloud seg.setInputCloud (cloud_filtered); seg.segment (*inliers, *coefficients); if (inliers->indices.size () == 0) { std::cerr << "Could not estimate a planar model for the given dataset." << std::endl; break; } // Extract the inliers extract.setInputCloud (cloud_filtered); extract.setIndices (inliers); extract.setNegative (false); extract.filter (*cloud_p); std::cerr << "PointCloud representing the planar component: " << cloud_p->width * cloud_p->height << " data points." << std::endl; std::stringstream ss; ss << "table_scene_lms400_plane_" << i << ".pcd"; writer.write (ss.str (), *cloud_p, false); // Create the filtering object extract.setNegative (true); extract.filter (*cloud_f); cloud_filtered.swap (cloud_f); i++; } return (0); }