Search code examples
matlab3dmatlab-cvstpoint-clouds

How to extract xyz coordinates from 3D point cloud in MATLAB


I am using a Kinect for Windows to import 3D images into MATLAB. I want to be able to find the 3D co-ordinates of objects within the 3D scene.

One simple way to do this is to use the clickA3DPoint function found here, and then click the point I want to know the co-ordinates of.

The problem is clickA3DPoint expects the arguments in a 3 by N matrix, which is the x y and z coordinates of N samples. When I use the Kinect to get a point cloud with depthToPointCloud it returns a 480 * 640 * 3 matrix.

How can I extract the x, y, and z coordinates from this matrix so I can plot it with clickA3DPoint? (or scatter3?)

My attempt so far:

depthDevice = imaq.VideoDevice('kinect',2)  %this is the kinect depth sensor

depthImage = step(depthDevice);  %this takes a depth image. (A 480 * 640 uint16 array)

xyzPoints = depthToPointCloud(depthImage,depthDevice); %convert the depth image to a point cloud

clickA3DPoint(reshape(xyzPoints,[3,307200])) %I'm guessing each of the 480 * 640 points (307200 points) has an x,y and z coordinate, so this concates the coordinates of all these points.

But this just plots the points along a diagonal line in the 3D space. How do I actually extract the x, y and z coordinates from a point cloud in Matlab?


Solution

  • You can use the pcshow function to plot your points, and it will take the M-by-N-by-3 array directly. You can then turn on data tips and click on the points in the plot to see their coordinates.

    If you still want to create a 3-by-N matrix, then the easiest way to do that is the following:

    x = xyzPoints(:,:,1);
    y = xyzPoints(:,:,2);
    z = zyzPoints(:,:,3);
    points3D = [x(:)'; y(:)', z(:)'];