Question

I have a function which takes a voxel representation of a 3D landscape and can plot a X-Y section to show the middle of the landscape. The voxel representation is stored in a 3 dimensional matrix with a number that represents something important. Obviously the matrix is

1,1,1 
2,2,2

in terms of accessing the elements but the actual 3D locations are found in the following method:

(index-1)*resolution+0.5*resolution+minPos;

where resolution is the grid size :

resolution
<-->
 __ __ __
|__|__|__|
 <- Min pos

and minPos is where the grid starts.

Now in terms of the actual question, i would like to extract a single X-Y section of this voxel representation and display it as a surf. This can be done by just doing this:

surf(voxel(:, :, section)) 

however then you get this:

The obvious problem is that the grid will start at 0 because that is how the matrix representation is. How can i set the minimum and cell size for surf, ie so that the grid will start at the minimum (shown above) and will have the grid spacing of resolution (shown above).

Était-ce utile?

La solution

Read the documentation of surf, you can also provide x and y coordinates corresponding to your data points.

surf(X,Y,Z)

X and Y can be either vectors or matrices:

surf(X,Y,Z) uses Z for the color data and surface height. X and Y are vectors or matrices defining the x and y components of a surface. If X and Y are vectors, length(X) = n and length(Y) = m, where [m,n] = size(Z). In this case, the vertices of the surface faces are (X(j), Y(i), Z(i,j)) triples. To create X and Y matrices for arbitrary domains, use the meshgrid function

Example

 Z=[ 0 1 2  3;
     7 6 5  4;
     8 9 10 11];

 x=[-1 0 1 2];
 y=[-2 0 2];
 surf(x,y,Z);

enter image description here Of course you have to match Z, x and y matrices/vectors as clearly described in the doc^^ Just remember that elements in columns of Z are surf'ed as values along the y-axis, elements in rows of Z are surf'ed as values along the x-axis. This is clearly to be seen in the example picture.

Solution

I think you switched the x and y-axis around, which you can fix by just transposing z:

s = size(voxel);
xi = (minPosX:resolution:(minPosX+resolution*s(1)-1));
yi = (minPosY:resolution:(minPosY+resolution*s(2)-1));
z = (voxel(:,:,section));

surf(xi, yi, z');

or that you're picking the wrong numbers for constructing xi and yi and it should be this instead:

xi = (minPosX:resolution:(minPosX+resolution*s(2)-1));
yi = (minPosY:resolution:(minPosY+resolution*s(1)-1));
z = (voxel(:,:,section));
surf(xi, yi, z);

Autres conseils

So it was easy enough to do:

lets say we have a 3D matrix "voxel";

s = size(voxel);
xi = (minPosX:resolution:(minPosX+resolution*s(1)-1));
yi = (minPosY:resolution:(minPosY+resolution*s(2)-1));
z = (voxel(:,:,section));
[x y] = meshgrid(xi, yi);
x = x';
y = y';

surf(x, y, z);

Provides the following plot:

This is rotated which is annoying, I cant seem to get it to rotate back (I could just visualise around the other way but that's ok)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top