Search code examples
c++vtkpoint-cloud-librarypoint-clouds

Removing point clouds inside a given box


I would like to implement a code where I can remove certain points of the point clouds inside a given box. This is what I have so far:

void removePoints(){

    pcl::CropBox<pcl::PointXYZI> boxFilter;
    float x_min = -0.15, y_min = -0.5, z_min = -0.7;
    float x_max = +1, y_max = +2, z_max = +5;
    boxFilter.setMin(Eigen::Vector4f(x_min, y_min, z_min, 1.0));
    boxFilter.setMax(Eigen::Vector4f(x_max, y_max, z_max, 1.0));

    boxFilter.setInputCloud(cloud);
    boxFilter.filter(*newCloud);

    viewer->removeAllPointClouds();

    cloudLabel=new int[newCloud->size()];
    memset(cloudLabel, 0, newCloud->size()*sizeof(int));

    ui->label_filename->setText(QString::fromStdString(pointcloudFileName));
    colorHandler.setInputCloud(newCloud);
    colorHandler.setLabel(cloudLabel);
    viewer->addPointCloud<PointT>(newCloud,colorHandler,"cloud",0);


    viewer->resetCamera();
    viewer->updatePointCloud<PointT>(newCloud,"cloud");

    ui->qvtkWidget->update();


    std::cout<<"Removal done"<<std::endl;
    viewer->addCube(x_min, x_max, y_min, y_max, z_min, z_max, 1, 0, 0, "cube");
    viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR,
    0.7, 0.7, 0, "cube");
     viewer->setRepresentationToWireframeForAllActors();
}

This code remove the points outside the box rather than the inside. Is there a way to inverse pcl::CropBox process?


Solution

  • Almost all filters in PCL inherit pcl::FilterIndices, where the function setNegative is exposed.
    Use boxFilter.setNegative(true) to filter the points inside the box.