Create a cylindrical mask in a 3D matrix

6 views (last 30 days)
Kenneth
Kenneth on 14 Aug 2023
Edited: Matt J on 14 Aug 2023
Hi, I would like to create a mask in a 3D matrix in the shape of a cylinder. The reason why I want to do this is to analyse the properties of the MRI data within the mask only.
The following parameters are given:
  • Coordinates of the center of the two faces of the cylinder.
  • The radius of the cylinder.
  • The size of the matrix.

Answers (1)

Matt J
Matt J on 14 Aug 2023
Edited: Matt J on 14 Aug 2023
  • Coordinates of the center of the two faces of the cylinder. Call this c1,c2
  • The radius of the cylinder. Call this r
  • The size of the matrix. Call this [sx,sy,sz]
o=(c1+c2)/2; %origin
h=norm(c1(:)-c2(:)); %cylinder height
n=normalize(c1(:)-c2(:),'n')';
R=[n; null(n)']'; %rotation matrix
[I,J,K]=ndgrid((1:sx)-o(1),(1:sy)-o(2),(1:sz)-o(3));
C=[I(:),J(:),K(:)]*R; %rotated coordinates
mask=reshape( vecnorm(C,2,2)<=r & abs(C(:,1))<=h/2 ,sx,sy,sz);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!