brain images: Change data type of brain image

1 min read

Last update: 2012/09/04

An image file can be saved in different formats such as uint8 or int16, etc, based on different number of bytes used for each voxel. How to convert between them? Here is one solution using SPM functions (no need SPM’s interface). Assume img1.img is the original image with datatype int16, img2.img is the file you want to create with datatype uint8.

V=spm_vol('img1.img');
disp(V.dim); %you will find the last element is 4 in SPM2
V2=V;
V2.fname='img2.img';
V2.dim(4)=2;
M=spm_read_vols(V);
spm_write_vol(V2, M);

Explanation:
Variable V.dim is a 1×4 vector. The first three are number of voxels (or dimensions) along x,y and z direction. The last element (4th) is the number of bytes used for each voxel and it is this variable we want to change.

Note: If you are using SPM5 or SPM8, then the data type information is in V.dt

V=spm_vol('img1.img');
disp(V.dt); %for SPM5 and SPM8
V2=V;
V2.fname='img2.img';
V2.dt=[2 0];
M=spm_read_vols(V);
spm_write_vol(V2, M);

I myself find the following 3 SPM functions are extremely useful and use them a lot:

V=spm_vol('img1.img'); % read header information of an image file
M=spm_read_vols(V); % read the data (voxel intensity) from an image. The result is a 3D matrix with each point corresponding to a point in brain.
spm_write_vol(V2, M); % write data to an image file.

Check out here to see how to convert images with different formats (e.g. ANALYZE, Nifti, DICOM etc)



写作助手,把中式英语变成专业英文


Want to receive new post notification? 有新文章通知我

第五十一期fNIRS Journal Club通知2024/05/11, 10am 雷心博士

机器人通常以合作者的身份出现,以礼貌、鼓励、友好的方式与人类互动,但如果有一个竞争导向的机器人出现会如何?人们更喜欢与合作导向的机器人互动,还是更愿意通过竞争来激发自身动力呢?当我们与机器人合作或竞争
Wanling Zhu
13 sec read

第五十期fNIRS Journal Club视频 王一晖

Youtube: https://youtu.be/a2QlCFZUytA优酷:https://v.youku.com/v_show/id_XNjM3MjMyNjUxMg==.html 早期的 STE
Wanling Zhu
13 sec read

第五十期fNIRS Journal Club通知2024/03/30, 10am 王一晖

早期的 STEM 教育对于以后的学习至关重要。现有研究尚未就STEM教学法达成共识,学生先验知识对基于故事的STEM教学法的影响还有待探讨。来自澳门大学张娟教授团队的王一晖将会分享基于fNIRS超扫描
Wanling Zhu
9 sec read

2 Replies to “brain images: Change data type of brain image”

  1. If I’m not mistaken that’s only true for SPM2. For SPM5 and SPM8 the field ‘dim’ has only three entries and the first entry of the field ‘dt’ carries the information about the data type.

Leave a Reply to Xu Cui Cancel reply

Your email address will not be published. Required fields are marked *