Detecting planar regions in a point cloud with RANSAC and coloring each detected plane.
Overview
This interactive tutorial loads the polyhedron point cloud and, on Ctrl+E, extracts its planar faces automatically. Each detected plane is assigned a distinct color so the segmentation is visible. Like Tutorial_702, it requires per-point normals (see Tutorial_701 for estimating them).
The technique
RANSAC (RANdom SAmple Consensus; Fischler and Bolles, 1981) fits a model robustly in the presence of outliers: repeatedly draw a minimal random sample (three points define a plane), fit the candidate plane, and count how many points lie within a distance threshold (the "inliers"). The candidate with the most support wins; its inliers are removed and the search repeats for the next primitive. Easy3D implements the efficient variant of Schnabel et al. ("Efficient RANSAC for
Point-Cloud Shape Detection", Computer Graphics Forum 2007), which accelerates inlier testing with a spatial bitmap and rejects candidates whose normals disagree with the samples. The detect() arguments here are: min_support = 200 points, distance threshold = 0.005, bitmap resolution = 0.02, normal compatibility threshold = 0.8, and overlook probability = 0.001.
Easy3D API
- PrimitivesRansac: the detector. add_primitive_type(PrimitivesRansac::PLANE) selects planes; detect(cloud, ...) returns the number of primitives found and stores a "v:primitive_index" per-point label.
- Renderer::color_from_segmentation(cloud, segments, coloring): maps integer segment labels to distinct colors in a vertex property.
- PointsDrawable::set_property_coloring(State::VERTEX, name): colors each point from that property.
Running it
Press Ctrl+E to extract planes. The console reports how many primitives were found, and each planar region is drawn in its own color (points not on any detected plane stay black).
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/point_cloud.h>
29#include <easy3d/core/model.h>
30#include <easy3d/renderer/drawable_points.h>
31#include <easy3d/renderer/renderer.h>
32#include <easy3d/algo/point_cloud_ransac.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
82 if (!viewer || !model)
85 auto cloud =
dynamic_cast<PointCloud *
>(model);
89 std::cerr <<
"Plane extraction using RANSAC requires normal information but it is not available" << std::endl;
99 const int num = algo.
detect(cloud, 200, 0.005f, 0.02f, 0.8f, 0.001f);
101 std::cout << num <<
" primitives extracted" << std::endl;
105 auto segments = cloud->vertex_property<
int>(
"v:primitive_index");
106 const std::string color_name =
"v:color-segments";
107 auto coloring = cloud->vertex_property<
vec3>(color_name,
vec3(0, 0, 0));
108 Renderer::color_from_segmentation(cloud, segments, coloring);
111 auto drawable = cloud->renderer()->get_points_drawable(
"vertices");
112 drawable->set_property_coloring(State::VERTEX, color_name);
120int main(
int argc,
char **argv) {
125 const std::string file = resource::directory() +
"/data/polyhedron.bin";
128 Viewer viewer(EXAMPLE_TITLE);
130 Model *model = viewer.add_model(file,
true);
132 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
137 auto drawable = model->renderer()->get_points_drawable(
"vertices");
138 drawable->set_uniform_coloring(
vec4(0.6f, 0.6f, 1.0f, 1.0f));
139 drawable->set_point_size(3.0f);
142 viewer.bind_shortcut(extract_plane, model, Viewer::KEY_E, Viewer::MODIF_CTRL);
144 viewer.set_usage(
"default",
"Ctrl + e: extract planes");
Base class for spatial models with optional rendering and manipulation state.
Definition model.h:53
A data structure for point clouds.
Definition point_cloud.h:46
VertexProperty< T > get_vertex_property(const std::string &name) const
Get the vertex property named name of type T.
Definition point_cloud.h:514
Extract primitives from point clouds using RANSAC.
Definition point_cloud_ransac.h:51
int detect(PointCloud *cloud, unsigned int min_support=1000, float dist_threshold=0.005f, float bitmap_resolution=0.02f, float normal_threshold=0.8f, float overlook_probability=0.001f)
Extract primitives from a point cloud.
Definition point_cloud_ransac.cpp:242
void add_primitive_type(PrimType t)
Set up the primitive types to be extracted.
Definition point_cloud_ransac.cpp:232
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
- See also
- PrimitivesRansac, Renderer, Tutorial_701_Cloud_NormalEstimation