Rosanswers logo

Hey everyone, I'm using the "standard" way of creating bounding boxes for a point cloud cluster, which most resources online are using. My code posted below. However it seems there is a flaw here, because as seen in the screenshot, the algorithm seems to shift the bounding box a certain amount too far forward. I'm wondering where the offset is coming from?

image description

visualization_msgs::Marker mark_cluster(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster, int ide)
  Eigen::Vector4f centroid;
  Eigen::Vector4f min;
  Eigen::Vector4f max;

  pcl::compute3DCentroid (*cloud_cluster, centroid);
  pcl::getMinMax3D (*cloud_cluster, min, max);

  uint32_t shape = visualization_msgs::Marker::CUBE;
  visualization_msgs::Marker marker;
  marker.header.frame_id = "laser";
  marker.header.stamp = ros::Time::now();

  marker.ns = "objects";
  marker.id = ide;
  marker.type = shape;
  marker.action = visualization_msgs::Marker::ADD;

  marker.pose.position.x = centroid[0];
  marker.pose.position.y = centroid[1];
  marker.pose.position.z = centroid[2];
  marker.pose.orientation.x = 0.0;
  marker.pose.orientation.y = 0.0;
  marker.pose.orientation.z = 0.0;
  marker.pose.orientation.w = 1.0;

  marker.scale.x = (max[0]-min[0]);
  marker.scale.y = (max[1]-min[1]);
  marker.scale.z = (max[2]-min[2]);

Originally posted by th6262 on ROS Answers with karma: 50 on 2018-11-26

Post score: 0

Original comments

Comment by hichriayoub on 2022-04-08:
hello can i get the full version of your code ?

Thank you in advance


1 Answer 1


Rosanswers logo

The problem is that the box you're creating to visualise this cluster assumes that the centroid of the cluster is exactly half way between the min and max values in each dimension. This is an invalid assumption, the centroid is the mean position of every point in the cloud.

If you want the box visual to simply show the extents of the cluster then you should set it's position to (min+max)/2 however this will not display the centroid.

I'd recommend using a line array marker instead so you can visualise the extents of the box as well as the centroid do describe the cluster more completely.

Originally posted by PeteBlackerThe3rd with karma: 9529 on 2018-11-26

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by th6262 on 2018-11-26:
thanks a bunch

Comment by th6262 on 2018-11-26:
will move my pcl specific question to the pcl forum in future! still massive thanks for all the help


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.