KCL-BMEIS / niftyreg

This project contains command line tools to perform rigid, affine and non-linear registration of nifti or analyse images as well as utilities
BSD 3-Clause "New" or "Revised" License
141 stars 42 forks source link

reg_aladin interpolation #59

Open rijobro opened 5 years ago

rijobro commented 5 years ago

I've noticed that when I set interpolation order of reg_aladin to nearest neighbour or trilinear, the output image which was initially non-negative, may contain negatives. To me, this implied that the interpolation was likely cubic.

I've created a small bash test to illustrate my point. It does the following:

  1. Downloads an example nifti from Slicer's BRAINSTools module
  2. Applies a translation of 10.5mm, to create the moved image
  3. The moved image is registered with reg_aladin with NN interpolation
  4. The transformation matrix from the registration is used with reg_resample for different types of interpolation.

Here is the output:

source min: 0.0
moved min: 0.0
registered NN min: -21.0
resampled NN min: 0.0
resampled LIN min: 0.0
resampled CUB min: -21.0

Have I misunderstood something or is this a bug?

The script is as follows:

source="source.nii.gz"
moved="moved.nii.gz"
registered="registered.nii.gz"
resampled_NN="resampled_NN.nii.gz"
resampled_LIN="resampled_LIN.nii.gz"
resampled_CUB="resampled_CUB.nii.gz"

url="https://github.com/pieper/Slicer3-shallow/raw/master/Applications/CLI/BRAINSTools/BRAINSCommonLib/TestData/test.nii.gz"
wget -nc -O source.nii.gz "$url"

function get_min {
    python3 -c "import sys, nibabel; exit(nibabel.load(sys.argv[1]).get_fdata().min())" $1 2>&1
}

# get min of source
min_source=$(get_min ${source})

# Create transformation matrix
echo "1 0 0 10.5
0 1 0 0
0 0 1 0
0 0 0 1" > tm.txt

# Move
reg_resample -ref ${source} -flo ${source} -res ${moved} -inter 0 -trans tm.txt
min_moved=$(get_min ${moved})

# Register
reg_aladin -ref ${source} -flo ${moved} -rigOnly -interp 0 -res ${registered} -aff tm_aladin.txt
min_registered_NN=$(get_min ${registered})

# Resample using registered tm
reg_resample -ref ${source} -flo ${moved} -res ${resampled_NN} -inter 0 -trans tm_aladin.txt
min_resampled_NN=$(get_min ${resampled_NN})
reg_resample -ref ${source} -flo ${moved} -res ${resampled_LIN} -inter 1 -trans tm_aladin.txt
min_resampled_LIN=$(get_min ${resampled_LIN})
reg_resample -ref ${source} -flo ${moved} -res ${resampled_CUB} -inter 3 -trans tm_aladin.txt
min_resampled_CUB=$(get_min ${resampled_CUB})

echo "source min: ${min_source}"
echo "moved min: ${min_moved}"
echo "registered NN min: ${min_registered_NN}"
echo "resampled NN min: ${min_resampled_NN}"
echo "resampled LIN min: ${min_resampled_LIN}"
echo "resampled CUB min: ${min_resampled_CUB}"
fepegar commented 5 years ago

+1. I also call reg_resample after registering because of this bug.

rijobro commented 4 years ago

any word on this one? I'm ok to try and figure it out with a bit of help...

XwK-P commented 1 year ago

For anyone who has the same question, I believe this is actually the intended behavior as mentioned in the help "Interpolation order to use internally to warp the floating image". Interp will only be used to do internal interpolation not to get the final warped image. You can make changes in https://github.com/KCL-BMEIS/niftyreg/blob/master/reg-lib/_reg_aladin.cpp#L663 to do whatever you want.