How to extract xyz coordinates from a 3D point cloud in MATLAB
I am using Kinect for Windows to import 3D images into MATLAB. I want to find the 3D coordinates of objects in a 3D scene.
One easy way to do this is to use the clickA3DPoint
function I found here and then click on the point I want to know - constraints.
The task clickA3DPoint
expects arguments in a matrix 3 by N
, which is coordinates x
y
and z
patterns N
. When I use Kinect to get a point cloud with depthToPointCloud
, it returns a matrix 480 * 640 * 3
.
How can I extract the x, y and z coordinates from this matrix so that I can plot it using clickA3DPoint
? (or scatter3
?)
My attempt:
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 draws points along a diagonal line in 3D space. How do I actually extract the x, y and z coordinates from a point cloud in Matlab?
source to share
You can use a function pcshow
to plot your points and it will take an M-by-N-by-3 array directly. You can then turn on data tips and click on the points on the graph to see their coordinates.
If you still want to create a 3-by-N matrix, then the easiest way to do it:
x = xyzPoints(:,:,1);
y = xyzPoints(:,:,2);
z = zyzPoints(:,:,3);
points3D = [x(:)'; y(:)', z(:)'];
source to share