For each of the two subjects A and B, I have MRI image registered in the MNI space. If I have a gray voxel at the location (50,50,50) in subject A, and the same voxel location in subject B is a white matter, how can I find the nearest gray matter voxels to that location? I am thinking about a searching algorithm that starts from (50,50,50) location and goes in different direction but that would be computationally expensive. I am hoping that someone may aware of a smarter algorithm?
Since MNI space registers the brain according to the brain properties. So since the functional region of the voxel (50,50,50) in subject A is almost the same functional region of subject B’s voxel at the same location, I guess I can assume that the nearest voxel to the location (50,50,50) in subject B is also in the same region of the voxel (50,50,50) in subject A. Do you agree?
You might want to specify your question a bit more (are the gray matter tissue maps binary or do they encode partial volume, how do you wish to resolve ties, etc.).
While not a direct answer to your question, the Euclidean Distance Transform may provide a nice metric for you. I assume your gray matter map
GM.nii.gz is a binary image (otherwise, apply a threshold) where the gray matter is one and all other tissues are zero. Using niimath the function
niimath GM -edt dxInGM
will generate a map where each voxel reports the distance from the black voxels (e.g. all black voxels have a value 0).
niimath GM -binv -edt dxFromGM
will generate a map that encodes the distance from the nearest gray matter (e.g. all bright voxels have a value of 0).
Therefore, if you have a gray voxel at the location (50,50,50) in subject A, then querying location 50,50,50 in subject B’s image
dxFromGM.nii.gz will return the distance (in mm) to the nearest gray matter (with 0 indicating this is a gray matter voxel, and 10 suggesting the nearest gray matter is 10mm away).
You could also create an EDT with FSL’s
distancemap (though you may need to be very patient or AFNI’s