# dwibiascorrect\_ANTs\_error

**URL:** https://community.mrtrix.org/t/dwibiascorrect-ants-error/305
**Category:** Uncategorized
**Created:** [June 17, 2016, 8:47am UTC](https://community.mrtrix.org/t/dwibiascorrect-ants-error/305 "2016-06-17T08:47:36Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Li\_Changhong](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/l/8e8cbc/32.png) [@Li\_Changhong](https://community.mrtrix.org/u/Li_Changhong)
#### Post date: [June 17, 2016, 8:47am UTC](https://community.mrtrix.org/t/dwibiascorrect-ants-error/305/1 "2016-06-17T08:47:36Z")

</div>

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!  
Best,  
Changhong

---

<div class="post-metadata">

### Author: ![mblesac](https://community.mrtrix.org/user_avatar/community.mrtrix.org/mblesac/32/2210_2.png) [@mblesac](https://community.mrtrix.org/u/mblesac)
#### Post date: [June 17, 2016, 9:03am UTC](https://community.mrtrix.org/t/dwibiascorrect-ants-error/305/2 "2016-06-17T09:03:38Z")

</div>

Hi,

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

Regards,

Manuel

---

<div class="post-metadata">

### Author: ![Li\_Changhong](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/l/8e8cbc/32.png) [@Li\_Changhong](https://community.mrtrix.org/u/Li_Changhong)
#### Post date: [June 17, 2016, 9:13am UTC](https://community.mrtrix.org/t/dwibiascorrect-ants-error/305/3 "2016-06-17T09:13:45Z")

</div>

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

```auto
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!  
Best,  
Changhong

---

<div class="post-metadata">

### Author: ![Li\_Changhong](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/l/8e8cbc/32.png) [@Li\_Changhong](https://community.mrtrix.org/u/Li_Changhong)
#### Post date: [June 17, 2016, 12:54pm UTC](https://community.mrtrix.org/t/dwibiascorrect-ants-error/305/4 "2016-06-17T12:54:42Z")

</div>

Dear all,  
The problem is solved. Just make sure the origin of all images are same. 🙂  
Best,  
Changhong

---

<div class="post-metadata">

### Author: ![rsmith](https://community.mrtrix.org/user_avatar/community.mrtrix.org/rsmith/32/2672_2.png) [@rsmith](https://community.mrtrix.org/u/rsmith)
#### Post date: [June 20, 2016, 3:33am UTC](https://community.mrtrix.org/t/dwibiascorrect-ants-error/305/5 "2016-06-20T03:33:04Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![judominguez](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@judominguez](https://community.mrtrix.org/u/judominguez)
#### Post date: [October 22, 2018, 5:08am UTC](https://community.mrtrix.org/t/dwibiascorrect-ants-error/305/7 "2018-10-22T05:08:09Z")

</div>

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:

```auto
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:

```auto
************************************************
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
                     ...
                     -0.5791908513,-0.1664954211,0.798008291,3000
                     0,0,0,0
  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
                     ...
                     -0.5780436025,-0.1707664348,0.797937603,2999.999767
                     0,0,0,0

```

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](https://community.mrtrix.org/t/dwibiascorrect-ants-error/305/4?u=judominguez) 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.

Juan

---

<div class="post-metadata">

### Author: ![jdtournier](https://community.mrtrix.org/user_avatar/community.mrtrix.org/jdtournier/32/2594_2.png) [@jdtournier](https://community.mrtrix.org/u/jdtournier)
#### Post date: [October 24, 2018, 2:35pm UTC](https://community.mrtrix.org/t/dwibiascorrect-ants-error/305/8 "2018-10-24T14:35:03Z")

</div>

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:

> [@judominguez](#):
>
> ```auto
> ...
> Description: itk::ERROR: N4BiasFieldCorrectionImageFilter(0x358c740): Inputs do not occupy the same physical space! 
> ...
> Tolerance: 1.0000000e-05
> 
> ```

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](https://github.com/MRtrix3/mrtrix3/issues/1318#issuecomment-394531560) and [here](https://github.com/MRtrix3/mrtrix3/issues/420#issuecomment-164687632)). 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…

---

<div class="post-metadata">

### Author: ![judominguez](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@judominguez](https://community.mrtrix.org/u/judominguez)
#### Post date: [October 25, 2018, 2:44am UTC](https://community.mrtrix.org/t/dwibiascorrect-ants-error/305/9 "2018-10-25T02:44:54Z")

</div>

Thanks heaps for the quick reply.

> [@jdtournier](#):
>
> 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` …

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](http://community.mrtrix.org/t/dwibiascorrect-doesnt-do-a-good-job/1811) and [this](http://community.mrtrix.org/t/dwibiascorrect-with-ants-high-intensity-in-cerebellum-brainstem/1338) 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?

> [@jdtournier](#):
>
> 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 `dwibiascorrec`

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

---

<div class="post-metadata">

### Author: ![jdtournier](https://community.mrtrix.org/user_avatar/community.mrtrix.org/jdtournier/32/2594_2.png) [@jdtournier](https://community.mrtrix.org/u/jdtournier)
#### Post date: [October 25, 2018, 1:36pm UTC](https://community.mrtrix.org/t/dwibiascorrect-ants-error/305/10 "2018-10-25T13:36:46Z")

</div>

> [@judominguez](#):
>
> 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).

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:

```auto
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.

---

<div class="post-metadata">

### Author: ![judominguez](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@judominguez](https://community.mrtrix.org/u/judominguez)
#### Post date: [October 25, 2018, 11:12pm UTC](https://community.mrtrix.org/t/dwibiascorrect-ants-error/305/11 "2018-10-25T23:12:30Z")

</div>

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