3

I try to crete a Oriented Bounding Box with PCA. In the image you can see my results:

enter image description here

  • Red points: point cloud
  • Blue vectors: PCA components

I tried to project the points on the vectors, in order to get the min, max and mean values.

But how can I define my box now? Any ideas?

I would like to get a Box like: centroid, and min max in two directions.

NKN
  • 6,482
  • 6
  • 36
  • 55
user2576458
  • 161
  • 3
  • 9

1 Answers1

0

Your question is not so much about PCA, as about how to handle points and lines in the plane: shifting points, and projecting points to a line. (For basics of this, look through SO questions/tagged/2d+geometry, or ask a new question with those tags.) Without those basics, and a little Python or Matlab, this little Python program will make no sense, but here it is anyway:

from __future__ import division
import numpy as np  # http://www.numpy.org/

def pcabox( Pointcloud, Pca1, Pca2 ):
    """ Lo1, Hi1, Lo2, Hi2 = pcabox( Pointcloud, Pca1, Pca2 )
        In: Pointcloud: an N x 2 array of points
        In: Pca1, Pca2: unit vectors at right angles, from PCA
        Out: Lo1, Hi1, Lo2, Hi2: midpoints of the sides of a bounding box
    """
        # convert inputs to numpy arrays (if they aren't already) --
    Pointcloud = np.asarray(Pointcloud)
    Pca1 = np.asarray(pca1)
    Pca2 = np.asarray(pca2)
        # check N x 2 --
    assert Pointcloud.ndim == 2  and  Pointcloud.shape[1] == 2, Pointcloud.shape

    C = np.mean( Pointcloud, axis=0 )  # the centre of all the points
    Pointcloud = Pointcloud - C  # shift the cloud to be centred at [0 0]

        # distances along the long axis t * Pca1 --
    Dist1 = np.dot( Pointcloud, Pca1 )
    Lo1 = Dist1.min() * Pca1
    Hi1 = Dist1.max() * Pca1
        # and along the short axis t * Pca2 --
    Dist2 = np.dot( Pointcloud, Pca2 )
    Lo2 = Dist2.min() * Pca2
    Hi2 = Dist2.max() * Pca2

    return [Lo1, Hi1, Lo2, Hi2] + C  # 4 points
Community
  • 1
  • 1
denis
  • 21,378
  • 10
  • 65
  • 88
  • 1
    I guess he is using PCL (pointcloud library), and the answer needs to use pcl classes/functions. – NKN Jan 22 '14 at 14:26