How to extract xyz coordinates from 3D point cloud in MATLAB - 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?

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(:)'];

you can use the Property 'Location' of pointCloud to get the x,y,z.
for example:
moving = movingReg.Location;%movingReg is a pointCloud
scatter(moving(:,1),moving(:,2));

Related

plotting a point (4x1) matrix in 3d Plot on matlab

How can i plot the New_P in 3D
i'm using robotic toolbox peter corke
i should use trplot() function but it's not working
it shows that it takes 3x3 or 4x4
any way to plot it on 3d
P = transpose([2,3,4,0]);
New_P = trotx(45) * P
trplot(New_P)
Please read on how to ask questions properly. If you read on trplot, it obviously says that:
trplot(T, options) draws a 3D coordinate frame represented by the
homogeneous transform T (4x4).
and
trplot(R, options) as above but the coordinate frame is rotated
about the origin according to the orthonormal rotation matrix R
(3x3).
I do not know how trtox is defined and what the class of trtox(45) is, but P is a 4x1 vector and therefor New_P most likely does not seem to be a proper input for trplot, hence the complaint on the input size.

Plotting one point in 3D matrix for all slices in 2D plane

I have 3D matrix (100*50*10) and I want to plot one specific point in all slices. Let us say point (10*6*:). The plot should be in 2D plane
Example (I have this coordinate for point that I want to plot)
x (10*6*1)
x (10*6*2)
x (10*6*3)
x (10*6*4)
x (10*6*5)
x (10*6*6)
x (10*6*7)
x (10*6*8)
x (10*6*9)
x (10*6*10)
I tried plot (x(10,6,:)) but I got error
plot(squeeze(x(10,6,:)))
see: https://www.mathworks.com/help/matlab/ref/squeeze.html
x(10,6,:) is still a 3D matrix, and needs to be reduced to a 1D form before plotting it. This is where the squeeze function comes in.

Create depth map from 3d points

I have given 3d points of a scene or a subset of these points comprising one object of the scene. I would like to create a depth image from these points, that is the pixel value in the image encodes the distance of the corresponding 3d point to the camera.
I have found the following similar question
http://www.mathworks.in/matlabcentral/newsreader/view_thread/319097
however the answers there do not help me, since I want to use MATLAB. To get the image values is not difficult (e.g. simply compute the distance of each 3d point to the camera's origin), however I do not know how to figure out the corresponding locations in the 2d image.
I could only imagine that you project all 3d points on a plane and bin their positions on the plane in discrete, well, rectangles on the plane. Then you could average the depth value for each bin.
I could however imagine that the result of such a procedure would be a very pixelated image, not being very smooth.
How would you go about this problem?
Assuming you've corrected for camera tilt (a simple matrix multiplication if you know the angle), you can probably just follow this example
X = data(:,1);
Y = data(:,1);
Z = data(:,1);
%// This bit requires you to make some choices like the start X and Z, end X and Z and resolution (X and Z) of your desired depth map
[Xi, Zi] = meshgrid(X_start:X_res:X_end, Z_start:Z_res:Z_end);
depth_map = griddata(X,Z,Y,Xi,Zi)

Plot distribution inside a region

I have a distribution of points inside a circle.
So, I draw a circular grid inside that circle. I want to find the number of points inside each cell of the circular gird.
Is there a way to implement that easily. or maybe drawing a grid is not necessary?
My goal is plotting the distribution.
Any help is highly appreciated.
Thanks in advance.
If X,Y are the coordinates of the points in your circle, the distances from the center can be obtained with
(edit: T/H #horchler)
d = sqrt(sum([X(:)-X0 Y(:)-Y0].^2,2));
where X0, Y0 are the coordinates of the center of the circle.
You can then compute a radial distribution using hist:
figure, hist(d)
or if you just want the distribution and bins
[distr bins] = hist(d);
By "circular Grid" I understand a grid in azimuth and modulus. I suggest you convert to polar coordinates:
z = x + j*y; % x, y are vectors woth x, y coordinates of the points
az = angle(z); % note that this gives azimuth in radians
mod = abs(z);
and then apply some kind ot 2D histogram to az and mod, for example using this function. (Please note it is a user-contributed file. I haven't tested it myself).

Plotting 3xN matrix(N number of 3D points) on same graph using matlab

I've a 3xN matrix W where N is 50
W(1,1) is x coordinate of a point
W(2,1) is y coordinate of same point
W(3,1) is z coordinate of same point
Similarly:
W(1,2) is x coordinate of another point
W(2,2) is y coordinate of same point
W(3,2) is z coordinate of same point
....
Now I want to 3d plot all these 3d points on same figure using matlab. How can I plot all these
points on same figure?
Is it possible to plot this matrix using a single function call(in matlab)?
I know that plot3 can be used but it can be used for one graph at a time.
So plot3(v(1,1),v(1,2),v(1,3)); is just a single point. But how do I plot all N points?
Is there an easier and better method?
I guess you can use plot3(w(1,:),w(2,:),w(3,:)).