Create mask based on coordinates obtained with regionprops3 and write output to nifti file

8 views (last 30 days)
Dear community,
I have extracted the coordinates (x,y,z) of the two largest clusters in my 3D matrix via regionprops3 (i.e. VoxelList, or the linear indices via VoxelIdxList). The size of my 3D matrix is 256 x 256 x 256, and the two extracted clusters of connected components are 29 and 15 voxels in size. I would like to create a mask that consists of these clusters only and output this mask to a nifti file for further processing, and I am hoping that someone can help me with this. I am wondering if i have to iterate over the z-dimension with poly2mask, or if there is another more direct way of doing this.
The 3D matrix reflects a brain scan and contains either zeros (reflecting no signal above a certain threshold at that voxel) or a value.
I obtained the voxel coordinates like this:
%first I selected those voxels that contain a value over a certain
%threshold
binaryMatrix = my3Dmatrix > threshold;
%calculate the connected components
CC = bwconncomp(binaryMatrix); % this yields two connected component clusters of 29 and 15 voxels
stats=regionprops3(CC,"ConvexVolume","VoxelIdxList","VoxelList","Image");
%Now I would like to use the coordinates contained in stats.VoxelList{1,1}
%to create a mask to apply to the 3D matrix, and output this mask as a nifti
%file
Thank you for your help!
Best wishes,
Elouise
  1 Comment
Kevin Holly
Kevin Holly on 16 Aug 2022
Edited: Kevin Holly on 16 Aug 2022
Elouise,
You can use niftiwrite to save the volumetric data into a NIfTI file. If you want to visualize your data beforehand to make sure your binary matrix was thresholded correcly. You can use the isosurface function. Please let me know if this answers your question.
For clarification, did you want the mask with the dimension of the regionprop bounding box or of the entire 256 x 256 x 256 3D matrix?

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 16 Aug 2022
You can use bwareafilt to get the largest 2 blobs
binaryImage = my3Dmatrix > threshold; % A 3-D logical vector
binaryImage = bwareafilt(binaryImage, 2); % Retain 2 largest blobs only.
You don't need regionprops3 unless you want to measure something like volume, but to just get the locations (binary array) you don't need it. If you want coordinates of it you could use regionprops3 though.
I don't know how to use nifti.

Community Treasure Hunt

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

Start Hunting!