Do we have the function to create ROIs from coordinates?

I have a short question.
when I’m using SPM I know it could create ROIs form coordinates, and it’s useful.
Do we Mrtrix also have any similar function?

Best regards,
Ziqian Wang

Unfortunately not. I’ve not heard of this before, how does this work exactly? Do you have to provide a list of all the voxels you want in the mask…?

It depends on your option. In my mind, there are at least three options: voxel, sphere, box. After you input the center coordinates and the diameter or something it will give you the ROI.

OK, that’s not something that is immediately obvious. But there are tools in MRtrix3 to approximate some of these – although they’re admittedly not the most easy to use. These rely on generating an image of coordinates, which can be produced using warpinit. Once you have this image, you can use mrcalc to compute the ROI – examples below. These aren’t pretty commands, but they’d be trivial to script if needed.

Note that warpinit produces an image of coordinates in real/scanner space, so all operations are then relative to that. If you need them to be relative to voxel space, this is trickier since there is no command to produce an image of voxel indices currently (although it would obviously be a trivial addition). But I think there’s better ways to deal with this going forward, which I’ve now documented on the issue tracker – we’ll see if we can do something about this in the not too distant future.

To create a spherical ROI:

  1. Create images of coordinates (one image for x, y, z – using multi-file syntax):

    warpinit teamplate.mif pos-[].mif
  2. Set location and radius of sphere (using BASH variables):

    x=0 y=10 z=-5 r=20
  3. Compute ROI using mrcalc (note stack-based syntax – not pretty, but very effective):

    mrcalc pos-0.mif $x -sub 2 -pow pos-1.mif $y -sub 2 -pow pos-2.mif $z -sub 2 -pow -add -add $r 2 -pow -lt roi.mif
    mrcalc: [100%] computing: (((pos-0.mif - 0)^2 + ((pos-1.mif - 10)^2 + (pos-2.mif - -5)^2)) < 400)

To create a box ROI:

  1. Create images of coordinates as above:

    warpinit teamplate.mif pos-[].mif
  2. Set bounds of box (using BASH variables):

    x1=-10 x2=10 y1=20 y2=40 z1=-3 z2=30
  3. Compute ROI using mrcalc:

    mrcalc pos-0.mif $x1 -gt pos-0.mif $x2 -lt -mult pos-1.mif $y1 -gt pos-1.mif $y2 -lt -mult pos-2.mif $z1 -gt pos-2.mif $z2 -lt -mult -mult -mult roi.mif
    mrcalc: [100%] computing: (((pos-0.mif > -10) * (pos-0.mif < 10)) * (((pos-1.mif > 20) * (pos-1.mif < 40)) * ((pos-2.mif > -3) * (pos-2.mif < 30))))

Hi Ziqian,

You might want to give mredit a go; that should cover the voxel and sphere cases (though not the box, yet), as well as whole planes; all for voxel as well as scanner coordinates.


Thank you very much for your reply. I’ve tried these two ways and both working well.
Especially thanks for mentioning warpinit. Not only solve this problem and make me learn something from it.
Maybe you’ve noticed the way Thijs mentioned.
I also tried and feel it is also convenient. First I use fslmaths -mul 0 to clean up the original image and then use the mredit to create the ROI. At least works well on sphere ROI.


1 Like

Hi Thijs,

thanks. I’ve tried mredit and feel it is convenient.


1 Like

Guess you learn something new every day… :joy:

Yes, that sounds a lot simpler…

1 Like

Thanks for offering a solution for spherical ROIs.

As mentioned here, Mac OS comes now with zsh shell as default. Because of that, for example these[ ] need to be quoted:

warpinit ch2bet.nii.gz pos-'[]'.mif

@Darius researched this link for further explanation regarding special characters & shells, thx.
Might help the next zsh user…


Hi all,
I am also intersted in creating a spherical ROI and when I tired this method:

an error appeared and I could not find a solution for it. Here is the error:

Snowths:DTI salah$ warpinit fa_C07.mif pos-fa_C07.mif

warpinit: [100%] generating identity warp

Snowths:DTI salah$ x=46 y=67 z=25 r=10

Snowths:DTI salah$ mrcal pos-0.mif $x -sub 2 -pow pos-1.mif $y -sub 2 -pow pos-2.mif $z -sub 2 -pow -add $r 2 -pow -lt roi_x46_y67_z25

-bash: mrcal: command not found

Snowths:DTI salah$ mrcalc pos-0.mif $x -sub 2 -pow pos-1.mif $y -sub 2 -pow pos-2.mif $z -sub 2 -pow -add $r 2 -pow -lt roi_x46_y67_z25

mrcalc: [ERROR] Could not interpret string “pos-0.mif” as either an image path or a numerical value

Could you please help identifying this issue?


For this step, it’s important to use the square brackets for the output image, as originally suggested: pos-[].mif. This splits the output over multiple files, one per volume, which would then give the pos-0.mif, pos-1.mif, and pos-2.mif images used in the subsequent mrcalc call.

But more to the point: use mredit – it’s much much more convenient…

1 Like