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.
Related
I have a set of data vectors z that has this 2d plot
How would I go about embed this set of data into a 3d plot like this in matlab? I'm asking for advice and suggestions. The theory I'm trying to employ is "for each data vector~zj, “copies” the data vector intothe first two entries of a 3D data vector~yjand then computes the squared length of~zj as the third entry of~yj. " or kernel trick.
Your 2d data will somehow be in the form, that you have x-coordinates and y-coordinates. Let's say you have a vector x and a vector y for simplification.
As you found out the plot3-function proivdes functionality to plot arbitrary points in 3d without the need of generating a mesh. What you need additionally is a third vector z with data for the 3rd dimension.
So what else can you do? The thing I am thinking about is rotating the plane you are plotting you "2d" data:
Rotational matrices can be seen here:
https://en.wikipedia.org/wiki/Rotation_matrix
I have a set of data coordinates in 3D with respect to an Origin, say O1, and another set of data that represents the same movements but in 2D with respect to an Origin, say O2. What can I do to calculate the transformations that are required to implement on the 3D set to be able to compare the data points(2D vs 3D) in a 2D frame?
Setup of data generation
If the measurements are at the same times, then the following might work. I have not actually tried it and make no claim that it is the best solution, but it seems doable. Basically, after adjusting the coordinates so that they have the same origin, you want to minimize the following 2-norm:
|| vec(A*x) - vec(P*B*y) ||
where x is the 2xN array of the 2-D locations, A is a 2x2 rotation matrix that rotates x by the unknown angle theta, P is a 2x3 projection matrix that projects a 3x1 vector onto its first two dimensions (viz., just throws out the third value), B is a 3x3 rotation matrix, and y is the 3xN array of the 3-D locations.
The unknowns in the above are theta in the 2-D rotation matrix, and the three rotation angles needed to define B. The four angles that minimize the above should (at least I think) give you the rotation matrices needed to align the coordinate systems.
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)
This question entails rotating an image given as a 3D matrix where the first dimension is width, 2nd dimension is height, and 3rd dimension contains x,y,z coordinates.
Currently I am plotting a surface using the following code
Fig.sub1im=surf(ToFparam.ROI.XYZ(:,:,1),ToFparam.ROI.XYZ(:,:,2),ToFparam.ROI.XYZ(:,:,3),zeros(ToFparam.ROI.height,ToFparam.ROI.width,3));
Now, I have a 3-D matrix where it's a 100x50x3. All x data is in the first...page or layer of the 3rd dimension, y is the second layer...z the third. Now I need to apply a 3x3 rotation matrix on the x y and z data. I know how to reshape a matrix to do this I think....just put it into a 3 row by...50000 column matrix then apply the matrix.
Next I need to update my plot in a loop. I was going to then do the following, where I have also included my new matrix calculation.
ToFparam.ROI.XYZ_Vector = ToFparam.ROI.XYZ;
ToFparam.ROI.XYZ_Vector = reshape(ToFparam.ROI.XYZ, [size(ToFparam.ROI.XYZ,1)*size(ToFparam.ROI.XYZ,2),3]);
ToFparam.ROI.XYZ_Vector = ToFparam.ROI.XYZ_Vector';
ToFparam.ROI.XYZ_DICOM = inv(DICOMparam.calib.navi2dicom(1:3,1:3))*inv(Naviparam.data.Endo_RefHomMat(1:3,1:3))*ToFparam.ROI.XYZ_Vector;
%refresh plot standard cuts
set(Fig.sub1im,'CData', Color);
set(Fig.sub1im, 'XData', ToFparam.ROI.XYZ_DICOM(1,:) + DICOMPos(1)/Fig.sub2samp);
set(Fig.sub1im, 'YData', ToFparam.ROI.XYZ_DICOM(2,:) + DICOMPos(2)/Fig.sub2samp);
set(Fig.sub1im, 'ZData', ToFparam.ROI.XYZ_DICOM(3,:) + DICOMPos(3)/Fig.sub2samp);
When I update my plot, I get no errors, but it doesn't look like it's plotting it correctly. It seems like it makes a huge offset on my data and positions it somewhere I don't want it to be positioned. I wouldn't expect that a rotation matrix effects the scaling, just the orientation. Let me know if there are any faster/better ways to accomplish this surf plot, thanks!
For anyone interested, I have found the solution.
If you ever want to rotate a 3-D matrix where the x,y,z data is located in the 3rd dimension, use the following for maximum speed and efficiency.
[m,n,z]=size(inMatrix);
outMatrix=reshape((A*(reshape(double(inMatrix),[m*n 3]))')',[m n 3]);
Where inMatrix is your initial 3d matrix, outMatrix is your output 3d matrix, and A is your rotation matrix. Can be extremely useful when wanting to code rotations in images, since images will have 2 dimensions along their width and height, and the 3rd dimension will be the x, y and z coordinates. This will allow you to easily plot an initial data set, rotate it, then replot it.
I am trying to plot a 3d view of a very large CT dataset. My data is in a 3d matrix of 2000x2000x1000 dimension. The object is surrounded by air, which is set to NaN in my matrix.
I would like to be able to see the greyscale value of the surface of the object (no isosurface) but I cannot quite work out how to do that in Matlab. Can anyone help me please?
Given that I a dealing with a huge matrix and I am only interested in the surface of the object, does anyone know a good trick how to reduce the size of my dataset?
The function surf(X,Y,Z) allows you to plot 3d data, where (X,Y) gives the coordinates in the x-y-plane while Z gives the z-coordinate and the surface color.
By default the function does not plot anything for the NaN entries, so you should be good to go with the surf function.
To set the surf-function to use a grayscale plotting use:
surf(matrix3d);
colormap(gray);
This plots the matrix in a surface plot and sets the colormap to grayscale.
In addition, as I understand your data, you might be able to eliminate entire plane-segments in your matrix. If for instance the plane A(1,1:2000,1:1000) is NaN in all entries you could eliminate all those entries (thus the entire Y,Z-plane in entry X=1). This will however require some heavy for loops, which might be over the top. This depends on how many data matrices you have compared to how many different plot you want for each matrix.
I will try to give you some ideas. I assume lack of a direct 3D "surface detector".
Since you have a 3D matrix where XY-planes are CT scan slices and each slice is an image, I would try to find edges of each slice say with edge. This would require some preprocessing like first thresholding each slice image. Then I can either use scatter3 to display the edge data as a 3D point cloud or delaunay3 to display the edge data as a surface.
I hope this will help you achieve what you are asking for.
I managed to get it working:
function [X,Y,Z,C] = extract_surface(file_name,slice_number,voxel_size)
LT = imread(file_name);%..READ THE 2D MAP
BW = im2bw(LT,1);%..THRESHOLD TO BINARY
B = bwboundaries(BW,8,'noholes');%..FIND THE OUTLINE OF THE IMAGE
X = B{1}(:,1);%..EXTRACT X AND Y COORDINATES
Y = B{1}(:,2);
indices = sub2ind(size(LT),X,Y);%..FIND THE CORRESPONDING LINEAR INDICES
C = LT(indices);%..NOW READ THE VALUES AT THE OUTLINE POSITION
Z = ones(size(X))*slice_number;
I can then plot this with
figure
scatter3(X,Y,Z,2,C)
Now the only thing I could improve is to have all these points in the scatter plot connected with a surface. #upperBound you suggested delaunay3 for this purpose - I cannot quite figure out how to do this. Do you have a tip?