Home > database >  Saving Detected Objects Individually from Point Cloud with RANSAC and DBSCAN
Saving Detected Objects Individually from Point Cloud with RANSAC and DBSCAN

Time:04-30

Hello, I am trying to detect objects from point cloud data using RANSAC and DBSCAN algorithms. I need to save these detected objects as separate files and then produce their solid models. But I was not able to save the objects. I'm new to python and I don't know much about what to do, if you can help I'd appreciate it.

import open3d as o3d
import numpy as np

import matplotlib.pyplot as plt
import time 
import pandas as pd
start = time.time()

 
pcd = o3d.io.read_point_cloud("D:\\Bitirme_Veri\\mini.pcd")
plane_model, inliers = pcd.segment_plane(distance_threshold=0.05, ransac_n=3, num_iterations=1000)
inlier_cloud = pcd.select_by_index(inliers)
outlier_cloud = pcd.select_by_index(inliers, invert=True)
inlier_cloud.paint_uniform_color([1, 0, 0])
o3d.visualization.draw_geometries([inlier_cloud, outlier_cloud])
o3d.io.write_point_cloud("D:\\bitirme2\\Sonuçlar\\sonuc1.pcd", outlier_cloud, write_ascii=True, compressed=True, print_progress=False)
#DBSCAN
labels = np.array(outlier_cloud.cluster_dbscan(eps=0.05, min_points=5))
max_label = labels.max()
colors = plt.get_cmap("tab20")(labels / (max_label 
if max_label > 0 else 1))
colors[labels < 0] = 0

inlier_cloud.colors = o3d.utility.Vector3dVector(colors[:, :3])
colors = plt.get_cmap("tab10")(labels / (max_label if max_label > 0 else 1))
colors[labels < 0] = 0
o3d.visualization.draw_geometries([outlier_cloud])
end = time.time()
print(f"süre: { end-start:.3f}")
o3d.io.write_point_cloud("D:\\bitirme2\\Sonuçlar\\Bolge2v3.pcd", outlier_cloud, write_ascii=True, compressed=True, print_progress=False)

CodePudding user response:

I ran your code with a PCD file I have. I was able to save the files and I can see that the saved files contain points.

I would suggest you check the followings:

  1. See if the input point cloud has valid points (if the coordinates are nan or if there is any point at all). This can be checked by print(np.asarray(pcd.points)). You can use the same statement before saving point clouds also to see if the saved point cloud has any points.
  2. Depending on your input, you might need to change the hyperparameters (the arguments of segment_plane and cluster_dbscan). For example, when I set distance_threshold to 5, segment_plane is not able to detect any plane from my input point cloud.
  • Related