vector subscript out of range问题解决方案之一

原文地址:http://www.pclcn.org/bbs/forum.php?mod=viewthread&tid=589


今天debug模式下调试下面代码,总提示vector subscript out of range,但在release 下面没问题。后面查了查,是因为这句赋值语句造成的  cloud_filtered = cloud_f; ,这两个都是点云对象指针变量,这样一赋值,他们就都指向一个对象了,而在  pcl::ExtractIndices extract;里面进行filter的时候,第一轮while的时候没问题,第二轮就出现vector越界问题,好好看看不一样的地方就是,这两个指针第一轮指向的点云对象是不同的。而后面就都指向一个了,在ExtractIndices这个类里面函数pcl::ExtractIndices::applyFilter (PointCloud &output)中有一句 copyPointCloud (*input_, indices, output);,在这句的时候,里面对应的in和out就是同一个对象了,看看IO的pcl::copyPointCloud (const pcl::PointCloud &cloud_in,                      const std::vector &indices,

                     pcl::PointCloud &cloud_out)  函数,其中, cloud_out.points.resize (indices.size ());,把大小给改了,这段程序刚好是剔除外点的,所以就是cloud_in的大小同时改小了,再通过  cloud_out.points[i] = cloud_in.points[indices[i]];赋值那肯定就引用超了。

大概就是本来两个对象,就因为指针赋值语句,后面变成一个对象了,正确的解决方式自然就是,保持两个对象存在,把指针赋值改为对象赋值。问题就解决了。

代码////////////////////////////////////////////////////////////////

cloud_filtered=back_cloud;
      pcl::SACSegmentation seg;
          pcl::PointIndices::Ptr tmpinliers (new pcl::PointIndices);
          pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
          pcl::PointCloud::Ptr cloud_plane (new pcl::PointCloud ());

          seg.setOptimizeCoefficients (true);
          seg.setModelType (pcl::SACMODEL_PLANE);
          seg.setMethodType (pcl::SAC_RANSAC);
          seg.setMaxIterations (maxitter);
          seg.setDistanceThreshold (distance);
          pcl::PointCloud::Ptr  cloud_f (new pcl::PointCloud);
          int nr_points = (int) cloud_filtered->points.size ();
          while (cloud_filtered->points.size () > ratio * nr_points)
          {
                  // Segment the largest planar component from the remaining cloud
                  seg.setInputCloud (cloud_filtered);
                  seg.segment (*tmpinliers, *coefficients);
                  std::cout<<"plane coefficients:" << *coefficients << std::endl;//打印平面的四个参数


                  if (tmpinliers->indices.size () == 0)
                  {
                          std::cout << "Could not estimate a planar model for the given dataset." << std::endl;
                          break;
                  }

                  // Extract the planar inliers from the input cloud
                  pcl::ExtractIndices extract;
                  extract.setInputCloud (cloud_filtered);
                  extract.setIndices (tmpinliers);
                  extract.setNegative (false);

                  // Write the planar inliers to disk
                  extract.filter (*cloud_plane);
                  std::cout << "PointCloud representing the planar component: " << cloud_plane->points.size () << " data points." << std::endl;

                  // Remove the planar inliers, extract the rest
                  extract.setNegative (true);
                  extract.filter (*cloud_f);
                  cloud_filtered = cloud_f;   //用*cloud_filtered = *cloud_f;替换解决问题。
          }
///////////////////////////////////////////////

你可能感兴趣的:(vector subscript out of range问题解决方案之一)