点云技术相关产学研社区

 找回密码
 立即注册加入PCL中国点云技术相关产学研社区

扫一扫,访问微社区

查看: 18418|回复: 5

vector subscript out of range问题解决方案之一,最常见原因

[复制链接]
发表于 2013-12-6 09:03:48 | 显示全部楼层 |阅读模式
这个貌似太常见,今天遇到一种新的引起原因,估计PCL中常遇见。就和大家分享了。

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

          seg.setOptimizeCoefficients (true);
          seg.setModelType (pcl::SACMODEL_PLANE);
          seg.setMethodType (pcl::SAC_RANSAC);
          seg.setMaxIterations (maxitter);
          seg.setDistanceThreshold (distance);
          pcl::PointCloud<pcl::PointXYZRGB>::Ptr  cloud_f (new pcl::PointCloud<pcl::PointXYZRGB>);
          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<pcl::PointXYZRGB> 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;替换解决问题。
          }
///////////////////////////////////////////////
回复

使用道具 举报

发表于 2015-3-19 21:37:20 | 显示全部楼层
这个小错误不容易发现,楼主解释的很详细。谢谢。
回复 支持 反对

使用道具 举报

发表于 2015-4-16 09:36:07 | 显示全部楼层
:P,给力楼主!!!
回复 支持 反对

使用道具 举报

发表于 2015-4-17 10:24:11 | 显示全部楼层
学习了。。。。
回复 支持 反对

使用道具 举报

发表于 2015-4-19 12:17:58 | 显示全部楼层
给楼主,赞一个,一直困扰的问题,终于明白了
回复 支持 反对

使用道具 举报

发表于 2015-4-20 16:12:52 | 显示全部楼层
各位,能看下这个程序吗 模板匹配 例程http://pointclouds.org/documenta ... #template-alignment,,我在release下 可以运行,debug下出现上述问题,我查了下
(1)270行出 cloud = tempCloud;    前面都加*后,还出现上述问题诶,求破;
(2)36行 也有一处, xyz_ = xyz;    我都加*后 ,release下也出现了问题,难道这处不能改??;
(3) 367行处的 链接 说 不能原地计算,,但是我看点云那本书给的程序是按原地计算的,求破?
回复 支持 反对

使用道具 举报

本版积分规则

QQ|小黑屋|点云技术相关产学研社区 ( 陕ICP备13001629号 )

GMT+8, 2024-6-8 14:44 , Processed in 1.746607 second(s), 16 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表