简体   繁体   中英

how to extract a cube from a 3D volume having a centre point and lengths of X, Y, Z in MATLAB?

In MATLAB, if we assume the starting point of a cube is up-left corner,

3D_vol(Start_x : Start_x + SizeX-1, Start_y : Start_y + SizeY-1, Start_z : Start_z + SizeZ-1);

gets a cube having starting points of X, Y, Z and size of each. Now, if the starting point is the center of the cube, how can I get the cube?

I tried the following, but it is not correct when the size of X/Y/Z is even.

3D_vol(start_x - (SizeX/2) - 1 : Start_x + (SizeX/2) - 1, ......

3D_vol is a 3d matrix.

I am assuming the start points are integers.
If SizeX is odd it is easier. You have you centre point and (SizeX-1)/2 in each side:

start_x + (-(SizeX-1)/2:(SizeX-1)/2)

That would give you a total of 1+2*(SizeX-1)/2 = SizeX points.
In case SizeX is even, your start_x is not actually the centre but one of the closest points to it. You should decide if its left or right of centre. Let's assume left. Then, on the left side you need the start_x point and SizeX/2-1 more points. the rest would be on the right:

start_x + (-(SizeX/2-1):SizeX/2)

That would give you a total of 1+(SizeX-1)/2-1+SizeX/2 = SizeX points.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM