Extract arbitrarily rotated plane of data from 3D

2020-01-29 15:49发布

问题:

I have a 3D matrix of data in matlab, but I want to extract an arbitrarily rotated slice of data from that matrix and store it as a 2D matrix, which I can access. Similar to how the slice() function displays data sliced at any angle, except I would also like to be able to view and modify the data as if it were an array.

I have the coordinates of the pivot-point of the plane as well as the angles of rotation (in x, y and z axis), I have also calculated the equation of the plane in the form:

Ax + By + Cz = D

and can extract a 3D matrix containing only the data that fall on that plane, but I don't know how to then convert that into a simple 2D array.

Another way of doing it would be to somehow rotate the source matrix in the opposite direction of the angle of the plane, so as to line up the plane of data with the XY axis, and simply extract that portion of the matrix, but I do not know if rotating a matrix like that is possible.

I hope this hasn't been answered elsewhere, I've been googling it all day, but none of the problems seem to exactly match mine.

Thanks

回答1:

You can take a look at the code here. I think the function is similar to what you are trying to solve.

The function extracts an arbitrary plane from a volume given the size of the plane, the center point of the plane, and the plane normal, i.e. [A,B,C]. It also outputs the volumetric index and coordinate of each pixel on the plane.



回答2:

Aha! May have just solved it myself.

To produce the plane equation I rotate a normal vector of (0,0,1) using rotation matrices and then find D. If I also rotate the following vectors:

(1,0,0) //step in the x direction of our 2D array

and

(0,1,0) //step in the y direction of our 2D array

I'll have the gradients that denote how much my coordinates in x,y,z have to change before I step to the next column in my array, or to the next row.

I'll mock this up ASAP and mark it as the answer if it works

EDIT: Ok slight alteration, when I'm rotating my vectors I should also rotate the point in 3D space that represents the xyz coordinates of x=0,y=0,z=0 (although I'm rotating around the centre of the structure, so it's actually -sizex/2,-sizey/2,-sizez/2, where size is the size of the data, and then I simply add size/2 to each coordinate after the rotations to translate it back to where it should be).

Now that I have the gradient change in 3D as I increase the x coordinate of my 2D array and the gradient change as I increase the y coordinate, I can simply loop through all possible x and y coordinates (the resulting array will be 50x50 for a 50x50x50 array, I'm not sure what it will be for irregular sizes, which I'll need to work out eventually) in my 2D array and calculate the resulting 3D coordinates on my plane in the data. My rotated corner value serves as the starting point. Hooray!

Just got to work out a good test for this encompassing all angles and then I'll approve this as an answer