Assuming that it is a binary image, then you could do something like
the following:
Nii = nifti('ROI.img');
X = Nii.dat(:,:,:);
ind = find(X(:));
dm = size(X);
i = rem(ind-1,dm(1))+1;
j = rem(floor((ind-1)/dm(1)),dm(2))+1;
k = floor((ind-1)/(dm(1)*dm(2)))+1;
[i j k]
The i, j and k indices can then be converted to mm coordinates by:
xyz = (Nii.mat(1:3,:)*[i j k ones(size(i))]')'
Best regards,
-John
2011/3/17 黄沛钰 <[log in to unmask]>:
> Another question please...
> How to transform an ROI image into a range of coordinates?
>
> Thanks again for your kind help!
>
>
> Peiyu
>
>
>
> 在 2011年3月17日 下午10:38,黄沛钰 <[log in to unmask]>写道:
>>
>> Thank you very much!
>>
>>
>> 2011/3/17 John Ashburner <[log in to unmask]>
>>>
>>> You can convert from MNI coordinates to voxel indices by multiplying
>>> with the inverse of the voxel-to-world transform matrix of the image.
>>>
>>> Nii = nifti('filename.img');
>>>
>>> mni_coords = [10 -20 10]; % for example
>>> M = inv(Nii.mat);
>>>
>>> ind = M(1:3,1:3)*mni_coords' + M(1:3,4)
>>>
>>> Reading values from a NIfTI file can be done by:
>>>
>>> ind = round(ind); % round to closest voxel
>>> f = Nii.dat(ind(1), ind(2), ind(3)) % read value
>>>
>>> If you wanted, you could use ranges of values to read, or even read
>>> the entire volume by:
>>>
>>> F = Nii.dat(:,:,:);
>>>
>>> If you have a series of volumes (the same size) that you'd like to
>>> work with, you can concatenate the data by:
>>>
>>> P=spm_select(Inf,'nifti');
>>> Nii = nifti(P);
>>> X = cat(4,Nii.dat);
>>>
>>> Then you could read a time series by:
>>>
>>> f = squeeze(X(ind(1), ind(2), ind(3), :))
>>>
>>> If you have lots of memory, then you could read the entire dataset by:
>>>
>>> F = X(:,:,:,:);
>>>
>>> Best regards,
>>> -John
>>>
>>> 2011/3/17 黄沛钰 <[log in to unmask]>:
>>> > Hi everybody,
>>> > I'm trying to write a customized programme which would be used to
>>> > calculate
>>> > the specific signal changes between different designed blocks, for
>>> > example,
>>> > picking out those voxels whose BOLD signal change are between 1% & 2%.
>>> > I've done the preprocessing by SPM. The problem is, I don't quite
>>> > understand how images were stored in the NIfTI files. How do I get a
>>> > BOLD
>>> > value corresponding to a specific voxel interpreted by MNI coordinates?
>>> > Or could you recommend me some existing tools that could be used in my
>>> > purpose?
>>> >
>>> > Thanks!
>>> > Peiyu, Huang
>>
>
>
|