title: Sphere Detection in Point Cloud using PCL
body: <p>I have captured a point cloud of a scene in which a ball is mounted on a robotic arm. The Ball is rigid and having the diameter approximately 150 mm. Below is the screenshot of Point Cloud-</p>
  
  <p><a href="https://i.stack.imgur.com/r7f89.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r7f89.png" alt="enter image description here"></a></p>
  
  <p>In order to track the ball (orange color in above screenshot), I tried to use Sample Consensus Initial Alignment (SAC-IA) algorithm, hence I first made a template, which is the point cloud of ball using following code-</p>
  
  <pre><code>typedef pcl::PointXYZRGB Point;
  typedef pcl::PointCloud&lt;Point&gt; PointCloud;
  
  PointCloud sphere;
  
  float r = 0.075; // in meters
  for(float phi = 0;  phi &lt; 2* M_PI; phi+=0.01)
  {
    float z = r * cos(phi);
    for(float th = 0;  th &lt; M_PI; th+=0.01)
    {
      float x = r * sin(phi) * cos(th);
      float y = r * sin(phi) * sin(th);
  
      Point point;
      point.x = x; point.y = y; point.z = z;  // position
      point.b = 51; point.g = 87; point.r = 255; // color
  
      sphere.push_back(point);
    }
  }
  sphere.is_dense = true;
  sphere.height = 1;
  sphere.width = sphere.points.size();
  </code></pre>
  
  <p>The template is then matched with point cloud using the code provided in <a href="http://pointclouds.org/documentation/tutorials/template_alignment.php" rel="nofollow noreferrer">official PCL website here</a>. I am not putting the code here since it is too long (approximately 300 lines).</p>
  
  <p>Below is the screenshot of the detection, in which I painted a green sphere in detected location-</p>
  
  <p><a href="https://i.stack.imgur.com/xhNC0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xhNC0.png" alt="enter image description here"></a></p>
  
  <p>Clearly, the <strong>ball detection has failed</strong>. Below are my queries-</p>
  
  <ol>
  <li>How to make it working? Any fine-tuning of parameters?</li>
  <li>Is there any better approach for sphere detection in PCL?</li>
  </ol>