Dear experts,
I am running the dwibiascorrect with ANTs, but I get the errors below:

bio-imaging@bioimaging:~/Documents/MRtrix$ dwibiascorrect -ants -mask mask.nii scan.mif scan_corrected.nii -force dwibiascorrect: dwibiascorrect: Note that this script makes use of commands / algorithms that have relevant articles for citation; INCLUDING FROM EXTERNAL SOFTWARE PACKAGES. Please consult the help page (-help option) for more information. dwibiascorrect: dwibiascorrect: Generated temporary directory: /tmp/dwibiascorrect-tmp-KB9ZKG/ dwibiascorrect: [WARNING] Output file scan_corrected.nii already exists; will be overwritten at script completion Command: mrconvert /home/bio-imaging/Documents/MRtrix/scan.mif /tmp/dwibiascorrect-tmp-KB9ZKG/in.mif Command: mrconvert /home/bio-imaging/Documents/MRtrix/mask.nii /tmp/dwibiascorrect-tmp-KB9ZKG/mask.mif dwibiascorrect: Changing to temporary directory (/tmp/dwibiascorrect-tmp-KB9ZKG/) Command: dwiextract in.mif - -bzero | mrmath - mean mean_bzero.mif -axis 3 Command: mrconvert mean_bzero.mif mean_bzero.nii -stride +1,+2,+3 Command: mrconvert mask.mif mask.nii -stride +1,+2,+3 Command: N4BiasFieldCorrection -d 3 -i mean_bzero.nii -w mask.nii -o [corrected.nii,bias.nii] -s 2 -b [150] -c [200x200,0.0] dwibiascorrect: [ERROR] Command failed: N4BiasFieldCorrection -d 3 -i mean_bzero.nii -w mask.nii -o [corrected.nii,bias.nii] -s 2 -b [150] -c [200x200,0.0] dwibiascorrect: Changing back to original directory (/home/bio-imaging/Documents/MRtrix) dwibiascorrect: Deleting temporary directory /tmp/dwibiascorrect-tmp-KB9ZKG/
I guess the problem is that the default setting of N4BiasFieldCorrection. Should I change something by myself?
Any suggestions? Thanks for your help!



I found the same error, removing -d and leaving only the dimension (3) in the N4biasfieldcommand work for me.




Dear Manuel,
I think it works, but I get another error below:

Running N4 for 3-dimensional images.

Mask not read.  Creating Otsu mask.

Specified spline distance: 100mm
  original image size:  [96, 96, 28]
  padded image size:  [469, 468, 468]
  number of control points:  [4, 4, 4]

Exception caught: 
itk::ExceptionObject (0x25a2a10)
Location: "unknown" 
File: /usr/local/antsbin/ITKv4-install/include/ITK-4.9/itkImageToImageFilter.hxx
Line: 250
Description: itk::ERROR: N4BiasFieldCorrectionImageFilter(0x2590f90): Inputs do not occupy the same physical space! 
InputImage Origin: [1.0040771e+01, 1.0058000e+01, -2.7820001e+00], InputImage_2 Origin: [1.0040610e+01, 1.0058000e+01, -2.7820001e+00]
	Tolerance: 2.1363300e-07
InputImage Spacing: [2.1363300e-01, 2.1400000e-01, 2.1400000e-01], InputImage_2 Spacing: [2.1363001e-01, 2.1400000e-01, 2.1400000e-01]
	Tolerance: 2.1363300e-07

I checked the command and I think it is not correct setting for my dataset. Hope someone knows it.
Anyway, thanks for your help!


Dear all,
The problem is solved. Just make sure the origin of all images are same. :slight_smile:


I’ll have a go at adding a check to the dwibiascorrect script to catch this issue earlier and give a clearer error. I should also get it to provide you with the N4 error message rather than hiding it. Out of curiosity, do you know what introduced that tiny offset between the images?


It happened the same to me, I was passing the mask that was in a different folder (a folder of masks) and it was giving me the same error, everything works fine if the mask is in the same folder that the dw image.


Hi all,

I am getting this error as well. However, it happens with only some subjects.

I ran the preproc pipeline: dwidenoise, mrdegibbs, dwipreproc (with reverse encode pair), dwibiascorrect.

The mask is generated from the participants dwi data within the same folder (dwi2mask dwi.mif mask.mif).

This is the terminal output:

N4BiasFieldCorrection -v -d 3 -i mean_bzero.nii -w mask.nii -o [corrected.nii,init_bias.nii] -s 4 -b [100,3] -c [1000,0.0]

Running N4 for 3-dimensional images.

Mask not read.  Using the entire image as the mask.

Specified spline distance: 100mm
  original image size:  [88, 88, 60]
  padded image size:  [121, 121, 81]
  number of control points:  [6, 6, 5]

Exception caught: 
itk::ExceptionObject (0x3763f70)
Location: "unknown" 
File: /usr/local/src/ANTS/2.2.0/ants/ITKv4-install/include/ITK-4.11/itkImageToImageFilter.hxx
Line: 250
Description: itk::ERROR: N4BiasFieldCorrectionImageFilter(0x358c740): Inputs do not occupy the same physical space! 
InputImage Origin: [9.9925629e+01, 1.0352393e+02, -8.5241733e+01], InputImage_2 Origin: [9.9925638e+01, 1.0352385e+02, -8.5243865e+01]
	Tolerance: 1.0000000e-05
InputImage Direction: -9.9998129e-01 -3.5016375e-07 -6.1173676e-03
-4.2742471e-04 -9.9755206e-01 6.9926487e-02
-6.1024173e-03 6.9927797e-02 9.9753339e-01
, InputImage_2 Direction: -9.9998135e-01 -1.3054409e-11 -6.1073659e-03
-4.2707461e-04 -9.9755206e-01 6.9926491e-02
-6.0924153e-03 6.9927797e-02 9.9753345e-01

	Tolerance: 1.0000000e-06

These are the headers for the images:

Image:               "in.mif"
  Dimensions:        88 x 88 x 60 x 67
  Voxel size:        2.5 x 2.5 x 2.5 x 1
  Data strides:      [ -2 -3 4 1 ]
  Format:            MRtrix
  Data type:         32 bit float (little endian)
  Intensity scaling: offset = 0, multiplier = 1
  Transform:                    1   3.502e-07    0.006117      -104.9
                        0.0004274      0.9976    -0.06993      -108.5
                        -0.006102     0.06993      0.9975      -85.56
  comments:          5.0.11
  dw_scheme:         0,0,0,0
  [67 entries]       0.6897376084,0.5755765135,-0.4392877287,3000
  mrtrix_version:    unknown
Image:               "mask.mif"
  Dimensions:        88 x 88 x 60
  Voxel size:        2.5 x 2.5 x 2.5
  Data strides:      [ -1 -2 3 ]
  Format:            MRtrix
  Data type:         bitwise
  Intensity scaling: offset = 0, multiplier = 1
  Transform:                    1   1.632e-11    0.006107      -104.9
                        0.0004271      0.9976    -0.06993      -108.5
                        -0.006092     0.06993      0.9975      -85.56
  FlipAngle:         90
  RepetitionTime:    10.1
  mrtrix_version:    unknown
  prior_dw_scheme:   0,0,0,0
  [67 entries]       0.6887398877,0.5758745104,-0.440461026,3000.00059

As you can see, there is a tiny offset between the images. I was able to track down where the offset was introduced and it was by/during dwibiascorrect.

I’d appreciate your help with this issue. In particular, @Li_Changhong above mentioned solving the problem by making sure the origin of all images is the same. Can one safely correct the offset introduced during dwibiascorrect (if so how)?

Many thanks for your help with this.



OK, there’s a couple of potential issues here. First, can I ask how your input images were generated? Were these produced using MRtrix3 commands? If so, I’d have expected to see an actual version in the mrtrix_version field, not unknown

The reason this matter is because there is already a minor discrepancy in the transforms, which is probably just big enough to trigger the error you’re seeing in ANTs:

There are indeed elements of the transform that seem to differ by about 10-5

I’ve not come across this issue before, and wasn’t aware ANTs had such stringent requirements when checking for matching voxel grids. But if you had the same transform in both input .mif images, it should result in identical transforms in the NIfTI files passed to ANTs – though not necessarily, see below.

The other potential culprit is that the way this information is stored in NIfTI is actually not all that stable – see our extensive discussions about this problem in various issues (notably here and here). There can be loss of precision when converting to NIfTI and back again, which could introduce discrepancies of this magnitude. In your case, it is possible that the input produced by the dwipreproc call would differ slightly in that respect due to the internal conversion to NIfTI and back that takes place when feeding the data to FSL’s topup and eddy. This may have introduced the discrepancy, and led to the subsequent failure in dwibiascorrect – but that’s just a hunch…


Thanks heaps for the quick reply.

I used the mrtrix pipeline from mrconvert to dwibiascorrect starting directly from the dicoms. I am using 3.0_RC3. I have noticed the version being missing from the header. Not sure if it’s because prior versions of MRtrix are also available (I am running MRtrix in a cluster, but 3.0_RC3 is the default).

The simple solution is to allow dwibiascorrect to generate its own mask but then again, this does not work in some cases where ANTs fails the same as in this and this posts. So this issue is also happening with 3.0_RC3, but I know the parameters can be adjusted with the -ants options in dwibiascorrect as a way to address this. Unless it is advisable to change the header of the mask passed to dwibiascorrect so that is has the same origin as the input?

I’d say this was the case as the header changes from the input to dwipreproc to its output.


OK, that’s a bit odd. But one potential explanation for this might be that the installation was done using a direct download of the ZIP archive of the code, rather than through a git clone. The script updates the version string using the git describe command, but that relies on this being a git clone, and will substitute unknown if not found. Not sure how to fix that…

Otherwise, one potential way to fix your issue might be simply to use mrtransform to regrid your input mask onto the exact same grid as the DWI – this assumes that they really do match, just not quite to the precision required by ANTs. Something like this ought to do the job:

mrtransform mask.mif -template in.mif -interp nearest mask_fixed.mif

Then try running dwibiascorrect using mask_fixed.mif. You could also verify that the transform now match exactly using mrinfo as you’ve done previously.


Never sure if I should reply to thank so as to not clog people’s inboxes but thanks a lot!