c++computer-visionpoint-cloud-librarypoint-clouds

After cropping a Point Cloud using pcl::CropBox filter, can't visualize that cropped Point Cloud


I have been trying to understand pcl::CropBoX filter and that's why have been experimenting with that. However, while doing so, I came across this issue:

  1. I'm creating a Point Cloud having a cube shape
  2. Visualizing it
  3. Cropping the original Point Cloud using pcl::CropBox filter
  4. Trying to visualize cropped Point Cloud

But, in the visualizer, for step 4, I could see only a blank screen!

Here is my code, main.cpp:

#include <iostream>
#include <string>
#include <vector>
#include <pcl/ModelCoefficients.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/common/common.h>
#include <pcl/filters/crop_box.h>

int main()
{
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr
        main_cloud_ptr(new pcl::PointCloud<pcl::PointXYZRGB>),
        cropped_cloud_ptr(new pcl::PointCloud<pcl::PointXYZRGB>);
    
    // setting the dimension of a point cloud
    float x_start = 0.25, x_end = 1.25, x_resolution = 0.05;
    float y_start = 0.5, y_end = 2.0, y_resolution = 0.05;
    float z_start = 0.0, z_end = 1.25, z_resolution = 0.05;

    pcl::PointXYZRGB pt;

    // generating a point cloud
    for (float i = x_start; i < x_end; i += x_resolution)
    {
        for (float j = y_start; j < y_end; j += y_resolution)
        {
            for (float k = z_start; k < z_end; k += z_resolution)
            {
                pt.x = i;
                pt.y = j;
                pt.z = k;
                pt.r = i * 100;
                pt.g = 200;
                pt.b = j * 100;
                main_cloud_ptr->points.push_back(pt);
            }
        }
    }

    std::cout << main_cloud_ptr->points.size() << "\n";

    // visualizing a main point cloud
    pcl::visualization::CloudViewer viewer1("main_cloud_ptr viewer - cube");
    viewer1.showCloud(main_cloud_ptr);
    while (!viewer1.wasStopped())
    {
    }

    // getting minimum & maximum x, y, and z values of a point cloud
    pcl::PointXYZRGB min_pt, max_pt;
    pcl::getMinMax3D(*main_cloud_ptr, min_pt, max_pt);
    std::cout << "minimum " << min_pt.x << " " << min_pt.y << " " << min_pt.z << "\n";
    std::cout << "maximum " << max_pt.x << " " << max_pt.y << " " << max_pt.z << "\n";

    // setting pcl::CropBox filter
    pcl::CropBox<pcl::PointXYZRGB> box_filter;
    box_filter.setMin(Eigen::Vector4f(0.25, 0.5, 0.25, 1.0));
    box_filter.setMin(Eigen::Vector4f(1.25, 2.0, 0.75, 1.0));

    box_filter.setInputCloud(main_cloud_ptr);
    box_filter.filter(*cropped_cloud_ptr);

    // visualizing a cropped point cloud
    pcl::visualization::CloudViewer viewer2("cropped_cloud_ptr");
    viewer2.showCloud(cropped_cloud_ptr);
    while (!viewer2.wasStopped())
    {
    }

    return 0;
}

My CMakeLists.txt:

cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)

project(cropbox_experiment)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

find_package(PCL 1.3 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable (${PROJECT_NAME} main.cpp)

target_link_libraries (${PROJECT_NAME} ${PCL_LIBRARIES})

This is the output for main point cloud: enter image description here

This is the output for cropped point cloud (As you can see, it's blank!!): enter image description here

Could you tell me where am I making mistake and why I'm getting a blank output for the Cropped Point Cloud? As far as I know, minimum and maximum values for x, y, and z axis in the setMin and setMax methods of pcl::CropBox filter is correct.


Solution

  • It seems you have set Min twice:

    box_filter.setMin(Eigen::Vector4f(0.25, 0.5, 0.25, 1.0));
    box_filter.setMin(Eigen::Vector4f(1.25, 2.0, 0.75, 1.0));