I have combined two images on matlab (3D and binary). I imported both using niftiread and then after I combined both I write them using niftiwrite. However the orientation seems to be wrong for the newly created image. Has anyone encountered this beforehand?
I tried permute, rot, and flip but it did not seem to solve this problem.
The issue is that niftiread only loads the image itself and not the associated metadata that specifies the image orientation (among other things). If you then use niftiwrite without specifying this information, you get default header values.
If the original images on your disk are "3D.nii" and "mask.nii", you would want to do something like:
threeD_info = niftiinfo('3D.nii'); % load metadata for 3D image
threeD_data = niftiread(threeD_info); % load 3D image by specifying info
mask_data = niftiread('mask.nii'); % load mask image by specifying filename
output_data = threeD_data .* mask_data; % multiply images (or other operation of your choice)
niftiwrite(output_data,'3Dmasked.nii',threeD_info); % write output image to 3Dmasked.nii including metadata
Note: Depending on what type of "combination" you are performing, you might need to update some of the fields in threeD_info accordingly, such as the datatype.