Clear Filters
Clear Filters

Saving 3D volume and mask on a same file

2 views (last 30 days)
Jacob Bunyamin
Jacob Bunyamin on 10 Feb 2023
Answered: Rishav on 7 Apr 2023
Hi,
I am trying to combine 3D T1 image and binary mask on one file. I used
niftiread to import the image (volume-> uint16 file, mask -> uint8 file). I add +1 on the mask matrix otherwise it will result in 0 after I multiply. After that I transformed both files into double files and then .* both to create a new file which I used niftwrite to save as.
I can open the image in FSLeyes (it looked good) but it missed the orientation and also the mask is not coloured (it becomes grayscale).
Is there anything to solve this problem (re: orientation and mask colour)?
Thank you
  1 Comment
prasanth s
prasanth s on 10 Feb 2023
convert both images to same format 'uint8' then add value 255 to mask region.

Sign in to comment.

Answers (1)

Rishav
Rishav on 7 Apr 2023
Hi Jacob,
It sounds like you are trying to create a new nifti file that combines a T1-weighted image and a binary mask in one file. However, you are encountering issues with the orientation and the mask color. Here are some suggestions that may help you:
  1. Check the orientation of the original T1-weighted image and the mask. You can use the "niftiinfo" function to obtain the orientation information. If the orientations of the two images are different, you can use the "niftireorient" function to reorient the mask so that it aligns with the T1-weighted image.
  2. To add color to the mask, you can convert the binary mask to a color image by setting the RGB channels to different values based on the intensity of the mask. For example, you can use the "gray2ind" function to convert the binary mask to an indexed image, and then use the "ind2rgb" function to convert the indexed image to a true-color image.
Here is the code:
% Read in the T1-weighted image and the binary mask
t1 = niftiread('t1.nii.gz');
mask = niftiread('mask.nii.gz') + 1;
% Reorient the mask to match the orientation of the T1-weighted image
mask = niftireorient(mask, niftiinfo('t1.nii.gz'));
% Convert the binary mask to a color image
mask_rgb = ind2rgb(gray2ind(mask), jet(256));
% Scale the T1-weighted image and convert it to double
t1_scaled = double(t1) / double(intmax(class(t1)));
% Combine the T1-weighted image and the mask
t1_masked = repmat(t1_scaled, [1 1 1 3]) .* repmat(mask_rgb, [1 1 1 3]);
% Save the combined image as a nifti file
niftiwrite(t1_masked, 't1_masked.nii.gz', niftiinfo('t1.nii.gz'));
This code should create a new nifti file that combines the T1-weighted image and the binary mask, with the mask shown in color. The resulting file should also have the same orientation as the original T1-weighted image. Note that the mask is scaled by a colormap (jet(256)), so you can adjust the colormap to your preference.
Hope this helps!

Tags

Community Treasure Hunt

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

Start Hunting!